Beispiel #1
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);
   }
 }
Beispiel #2
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();
  }
Beispiel #3
0
  /**
   * @see javax.faces.view.ViewDeclarationLanguage#renderView(javax.faces.context.FacesContext,
   *     javax.faces.component.UIViewRoot)
   */
  public void renderView(FacesContext context, UIViewRoot view) throws IOException {

    // suppress rendering if "rendered" property on the component is
    // false
    if (!view.isRendered() || context.getResponseComplete()) {
      return;
    }

    ExternalContext extContext = context.getExternalContext();

    if (!Util.isViewPopulated(context, view)) {
      buildView(context, view);
    }

    // set up the ResponseWriter

    RenderKitFactory renderFactory =
        (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
    RenderKit renderKit = renderFactory.getRenderKit(context, view.getRenderKitId());

    ResponseWriter oldWriter = context.getResponseWriter();

    WriteBehindStateWriter stateWriter =
        new WriteBehindStateWriter(
            extContext.getResponseOutputWriter(), context, responseBufferSize);
    ResponseWriter newWriter;
    if (null != oldWriter) {
      newWriter = oldWriter.cloneWithWriter(stateWriter);
    } else {
      newWriter =
          renderKit.createResponseWriter(
              stateWriter, null, extContext.getRequestCharacterEncoding());
    }
    context.setResponseWriter(newWriter);

    //  Don't call startDoc and endDoc on a partial response
    if (context.getPartialViewContext().isPartialRequest()) {
      doRenderView(context, view);
      try {
        extContext.getFlash().doPostPhaseActions(context);
      } catch (UnsupportedOperationException uoe) {
        if (LOGGER.isLoggable(Level.FINE)) {
          LOGGER.fine(
              "ExternalContext.getFlash() throw UnsupportedOperationException -> Flash unavailable");
        }
      }
    } else {
      // render the view to the response
      newWriter.startDocument();
      doRenderView(context, view);
      try {
        extContext.getFlash().doPostPhaseActions(context);
      } catch (UnsupportedOperationException uoe) {
        if (LOGGER.isLoggable(Level.FINE)) {
          LOGGER.fine(
              "ExternalContext.getFlash() throw UnsupportedOperationException -> Flash unavailable");
        }
      }
      newWriter.endDocument();
    }

    // replace markers in the body content and write it to response.

    // flush directly to the response
    if (stateWriter.stateWritten()) {
      stateWriter.flushToWriter();
    }

    // clear the ThreadLocal reference.
    stateWriter.release();

    if (null != oldWriter) {
      context.setResponseWriter(oldWriter);
    }

    // write any AFTER_VIEW_CONTENT to the response
    // side effect: AFTER_VIEW_CONTENT removed
    ViewHandlerResponseWrapper wrapper =
        (ViewHandlerResponseWrapper)
            RequestStateManager.remove(context, RequestStateManager.AFTER_VIEW_CONTENT);
    if (null != wrapper) {
      wrapper.flushToWriter(
          extContext.getResponseOutputWriter(), extContext.getResponseCharacterEncoding());
    }

    extContext.responseFlushBuffer();
  }