Example #1
0
 @Override
 public void doFilter(
     final ServletRequest request, ServletResponse response, final FilterChain chain)
     throws IOException, ServletException {
   TraceResponseWrapper wrappedResponse = null;
   if (((HttpServletRequest) request).getHeader(X_NO_FILTER_HEADER) == null) {
     response = wrappedResponse = new TraceResponseWrapper((HttpServletResponse) response);
   }
   String status = "n/a";
   final long startTime = System.nanoTime();
   try {
     chain.doFilter(request, response);
     status = "OK";
   } catch (final Throwable th) {
     status = "FAIL";
   } finally {
     final long duration = System.nanoTime() - startTime;
     ((HttpServletResponse) response)
         .addHeader(X_SERVER_DURATION_HEADER, String.valueOf(duration));
     ((HttpServletResponse) response).addHeader(X_STATUS_HEADER, status);
     if (wrappedResponse != null) {
       ((HttpServletResponse) response)
           .setHeader(HttpHeaders.CONTENT_LENGTH, wrappedResponse.getContentLength());
       wrappedResponse.writeBodyAndClose(response.getCharacterEncoding());
     }
   }
 }
Example #2
0
  @Override
  public void start() {
    if (_os != null || _writer != null) throw new IllegalStateException();

    ServletResponse next = _response.getResponse();
    if (next == null) throw new NullPointerException();

    if (next instanceof CauchoResponse) {
      CauchoResponse cNext = (CauchoResponse) next;

      if (cNext.isCauchoResponseStream()) _stream = cNext.getResponseStream();
    }

    _isCommitted = false;
    _headerKeys.clear();
    _headerValues.clear();

    super.start();

    // server/053n
    try {
      setEncoding(next.getCharacterEncoding());
    } catch (Exception e) {
      log.log(Level.WARNING, e.toString(), e);
    }
  }
Example #3
0
  /** Sets up the charset for the request and response based on
   * {@link #getPreferredLocale(HttpSession,ServletRequest)}. After setting up, you shall invoke
   * {@link #cleanup} before exiting.
   *
   * <pre><code> final Object old = setup(request, response, null);
   * try {
   *	  ....
   * } finally {
   *    cleanup(request, old);
   * }
   *
   * <p>It is OK to call this method multiple time, since it is smart
   * enough to ignore redundant calls.
   *
   * <p>{@link CharsetFilter} actually use this method to setup
   * the proper charset and locale. By mapping {@link CharsetFilter} to
   * all servlets, the encoding charset could be prepared correctly.
   * However, if you are writing libraries to be as independent of
   * web.xml as possible, you might choose to invoke this method directly.
   *
   * @param sess the session to look for the preferred locale. Ignored if null.
   * @param charset the response's charset. If null or empty,
   * response.setCharacterEncoding won't be called, i.e., the container's
   * default is used.
   * @return an object that must be passed to {@link #cleanup}
   */
  public static final Object setup(
      HttpSession sess, ServletRequest request, ServletResponse response, String charset) {
    if (hasSetup(request)) // processed before?
    return Objects.UNKNOWN;

    final Locale locale = getPreferredLocale(sess, request);
    response.setLocale(locale);
    if (charset != null && charset.length() > 0) {
      try {
        if (Servlets.isServlet24()) {
          response.setCharacterEncoding(charset);
        } else {
          // don't access 2.4 API: setCharacterEncoding, getContentType
          response.setContentType(";charset=" + charset);
        }
      } catch (Throwable ex) {
        try {
          final String v = response.getCharacterEncoding();
          if (!Objects.equals(v, charset))
            log.warn("Unable to set response's charset: " + charset + " (current=" + v + ')', ex);
        } catch (Throwable t) { // just in case
        }
      }
    }

    if (request.getCharacterEncoding() == null) {
      charset = response.getCharacterEncoding();
      try {
        request.setCharacterEncoding(charset);
      } catch (Throwable ex) {
        final String v = request.getCharacterEncoding();
        if (!Objects.equals(v, charset))
          log.warn(
              "Unable to set request's charset: "
                  + charset
                  + " (current="
                  + v
                  + "): "
                  + Exceptions.getMessage(ex));
      }
    }

    markSetup(request, true);
    return Locales.setThreadLocal(locale);
  }
Example #4
0
  public void encodePartially(FacesContext facesContext, UIComponent component) throws IOException {
    DataGrid grid = (DataGrid) component;

    ServletResponse response = (ServletResponse) facesContext.getExternalContext().getResponse();
    response.setContentType("text/xml");

    ResponseWriter writer = facesContext.getResponseWriter();
    try {
      writer.write("<?xml version=\"1.0\" encoding=\"" + response.getCharacterEncoding() + "\"?>");
      writer.write("<partial-response>");

      // Tab content
      writer.write("<table>");
      RendererUtils.startCDATA(facesContext);

      encodeTable(facesContext, grid, (grid.isPaginator() && grid.isEffect()));

      RendererUtils.endCDATA(facesContext);
      writer.write("</table>");

      // State
      writer.write("<state>");
      RendererUtils.startCDATA(facesContext);

      StateManager stateManager = facesContext.getApplication().getStateManager();
      stateManager.writeState(facesContext, stateManager.saveView(facesContext));

      RendererUtils.endCDATA(facesContext);
      writer.write("</state>");

      writer.write("</partial-response>");
    } catch (IOException exception) {
      exception.printStackTrace();
    }

    facesContext.responseComplete();
  }
Example #5
0
 public String getCharacterEncoding() {
   return response.getCharacterEncoding();
 }
Example #6
0
 /** @see javax.faces.context.ExternalContext#getResponseCharacterEncoding() */
 @Override
 public String getResponseCharacterEncoding() {
   return (response.getCharacterEncoding());
 }