@Override
  public void handleResourceRequest(FacesContext context) throws IOException {
    Resource resource = createResource(context);

    if (resource == null) {
      super.handleResourceRequest(context);
      return;
    }

    ExternalContext externalContext = context.getExternalContext();

    if (!resource.userAgentNeedsUpdate(context)) {
      externalContext.setResponseStatus(SC_NOT_MODIFIED);
      return;
    }

    InputStream inputStream = resource.getInputStream();

    if (inputStream == null) {
      externalContext.setResponseStatus(SC_NOT_FOUND);
      return;
    }

    externalContext.setResponseContentType(resource.getContentType());

    for (Entry<String, String> header : resource.getResponseHeaders().entrySet()) {
      externalContext.setResponseHeader(header.getKey(), header.getValue());
    }

    stream(inputStream, externalContext.getResponseOutputStream());
  }
Пример #2
0
 /**
  * Crea un archivo para que pueda ser descargado en el navegador, debe previamente existir el path
  * de archivo
  *
  * @param path
  */
 public void crearArchivo(String path) {
   FacesContext facesContext = FacesContext.getCurrentInstance();
   StreamedContent content;
   InputStream stream = null;
   try {
     if (path.startsWith("/")) {
       stream =
           ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext())
               .getResourceAsStream(path);
     } else {
       stream = new FileInputStream(path);
     }
   } catch (Exception e) {
     crearError("No se puede generar el archivo path: " + path, "crearArchivo()", e);
   }
   if (stream == null) {
     return;
   }
   content = new DefaultStreamedContent(stream);
   if (content == null) {
     return;
   }
   ExternalContext externalContext = facesContext.getExternalContext();
   String contentDispositionValue = "attachment";
   try {
     externalContext.setResponseContentType(content.getContentType());
     externalContext.setResponseHeader(
         "Content-Disposition",
         contentDispositionValue
             + ";filename=\""
             + path.substring(path.lastIndexOf("/") + 1)
             + "\"");
     externalContext.addResponseCookie(
         Constants.DOWNLOAD_COOKIE, "true", new HashMap<String, Object>());
     byte[] buffer = new byte[2048];
     int length;
     InputStream inputStream = content.getStream();
     OutputStream outputStream = externalContext.getResponseOutputStream();
     while ((length = (inputStream.read(buffer))) != -1) {
       outputStream.write(buffer, 0, length);
     }
     externalContext.setResponseStatus(200);
     externalContext.responseFlushBuffer();
     content.getStream().close();
     facesContext.getApplication().getStateManager().saveView(facesContext);
     facesContext.responseComplete();
   } catch (Exception e) {
     crearError("No se puede descargar :  path: " + path, "crearArchivo()", e);
   }
 }
  public void download(String fileName) {
    for (ServerInfo info : servers) {
      try (Socket server = info.getSocket();
          OutputStream os = server.getOutputStream();
          PrintWriter pw = new PrintWriter(os);
          InputStream is = server.getInputStream()) {

        /*Protocol*/
        os.write(Protocol.DOWNLOAD);
        pw.println(fileName);
        pw.flush();

        /*Check if file exists, 1 if present, 0 if otherwise*/
        int reply = is.read();
        if (reply == 1) {

          /*Response header*/
          FacesContext fc = FacesContext.getCurrentInstance();
          ExternalContext ec = fc.getExternalContext();
          ec.responseReset();
          ec.setResponseContentType(ec.getMimeType(fileName));
          ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

          /*Write bytes*/
          transferBytes(is, ec.getResponseOutputStream());
          fc.responseComplete();
          System.out.println(
              "File " + fileName + " was successfully downloaded with size " + file.getSize());
          break;
        }
      } catch (ConnectException e) {
        // Socket is closed
        System.out.println(e + " download at " + info);
      } catch (IOException ex) {
        Logger.getLogger(ClientController.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
  }
Пример #4
0
  @Override
  public void export(
      FacesContext context,
      DataTable table,
      String filename,
      boolean pageOnly,
      boolean selectionOnly,
      String encodingType,
      MethodExpression preProcessor,
      MethodExpression postProcessor)
      throws IOException {
    ExternalContext externalContext = context.getExternalContext();
    configureResponse(externalContext, filename);
    OutputStream os = externalContext.getResponseOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(os, encodingType);
    PrintWriter writer = new PrintWriter(osw);

    writer.write("<?xml version=\"1.0\"?>\n");
    writer.write("<" + table.getId() + ">\n");

    if (pageOnly) {
      exportPageOnly(table, writer);
    } else if (selectionOnly) {
      exportSelectionOnly(context, table, writer);
    } else {
      exportAll(table, writer);
    }

    writer.write("</" + table.getId() + ">");

    table.setRowIndex(-1);

    writer.flush();
    writer.close();

    externalContext.responseFlushBuffer();
  }