Esempio n. 1
0
 protected void configureResponse(ExternalContext externalContext, String filename) {
   externalContext.setResponseContentType("text/xml");
   externalContext.setResponseHeader("Expires", "0");
   externalContext.setResponseHeader(
       "Cache-Control", "must-revalidate, post-check=0, pre-check=0");
   externalContext.setResponseHeader("Pragma", "public");
   externalContext.setResponseHeader(
       "Content-disposition", "attachment;filename=" + filename + ".xml");
   externalContext.addResponseCookie(
       Constants.DOWNLOAD_COOKIE, "true", new HashMap<String, Object>());
 }
  @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());
  }
Esempio n. 3
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);
      }
    }
  }