Java Code Examples for javax.servlet.http.HttpServletResponseWrapper

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project gxa, under directory /atlas-web/src/main/java/uk/ac/ebi/gxa/web/filter/.

Source file: NoEtagHeaderFilter.java

  26 
vote

public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException, ServletException {
  chain.doFilter(request,new HttpServletResponseWrapper((HttpServletResponse)response){
    public void setHeader(    String name,    String value){
      if (!"etag".equalsIgnoreCase(name)) {
        super.setHeader(name,value);
      }
 else {
        log.debug("Ignoring ETag header: {} {}",name,value);
      }
    }
  }
);
}
 

Example 2

From project jboss-as-quickstart, under directory /kitchensink-html5-mobile/src/main/java/org/jboss/as/quickstarts/html5_mobile/util/.

Source file: JSONPRequestFilter.java

  22 
vote

@Override public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException, ServletException {
  if (!(request instanceof HttpServletRequest)) {
    throw new ServletException("Only HttpServletRequest requests are supported");
  }
  final HttpServletRequest httpRequest=(HttpServletRequest)request;
  final HttpServletResponse httpResponse=(HttpServletResponse)response;
  String callback=getCallbackMethod(httpRequest);
  if (!isJSONPRequest(callback)) {
    chain.doFilter(request,response);
  }
 else {
    if (!SAFE_PRN.matcher(callback).matches()) {
      throw new ServletException("JSONP Callback method '" + CALLBACK_METHOD + "' parameter not valid function");
    }
    final ByteArrayOutputStream byteStream=new ByteArrayOutputStream();
    HttpServletResponseWrapper responseWrapper=new HttpServletResponseWrapper(httpResponse){
      @Override public ServletOutputStream getOutputStream() throws IOException {
        return new ServletOutputStream(){
          @Override public void write(          int b) throws IOException {
            byteStream.write(b);
          }
        }
;
      }
      @Override public PrintWriter getWriter() throws IOException {
        return new PrintWriter(byteStream);
      }
    }
;
    chain.doFilter(request,responseWrapper);
    response.setContentType(CONTENT_TYPE);
    response.setCharacterEncoding("UTF-8");
    response.getOutputStream().write((callback + "(").getBytes());
    response.getOutputStream().write(byteStream.toByteArray());
    response.getOutputStream().write(");".getBytes());
  }
}
 

Example 3

From project jspwiki, under directory /src/org/apache/wiki/ui/.

Source file: WikiJSPFilter.java

  21 
vote

public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws ServletException, IOException {
  WatchDog w=m_engine.getCurrentWatchDog();
  try {
    NDC.push(m_engine.getApplicationName() + ":" + ((HttpServletRequest)request).getRequestURI());
    w.enterState("Filtering for URL " + ((HttpServletRequest)request).getRequestURI(),90);
    HttpServletResponseWrapper responseWrapper;
    if (m_useOutputStream) {
      log.debug("Using ByteArrayResponseWrapper");
      responseWrapper=new ByteArrayResponseWrapper((HttpServletResponse)response);
    }
 else {
      log.debug("Using MyServletResponseWrapper");
      responseWrapper=new MyServletResponseWrapper((HttpServletResponse)response);
    }
    String pagename=DefaultURLConstructor.parsePageFromURL((HttpServletRequest)request,response.getCharacterEncoding());
    fireEvent(WikiPageEvent.PAGE_REQUESTED,pagename);
    super.doFilter(request,responseWrapper,chain);
    try {
      w.enterState("Delivering response",30);
      WikiContext wikiContext=getWikiContext(request);
      String r=filter(wikiContext,responseWrapper);
      response.getWriter().write(r);
      if (wikiContext != null) {
        wikiContext.getWikiSession().clearMessages();
      }
      fireEvent(WikiPageEvent.PAGE_DELIVERED,pagename);
    }
  finally {
      w.exitState();
    }
  }
  finally {
    w.exitState();
    NDC.pop();
    NDC.remove();
  }
}
 

Example 4

From project scooter, under directory /source/src/com/scooterframework/web/controller/.

Source file: ScooterRequestFilter.java

  21 
vote

/** 
 * Time the processing that is performed by all subsequent filters in the current filter stack, including the ultimately invoked servlet.
 * @param request The servlet request we are processing
 * @param chain The filter chain we are processing
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet error occurs
 */
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException, ServletException {
  boolean staticContent=isStaticContentRequest((HttpServletRequest)request);
  if (encoding != null) {
    request.setCharacterEncoding(encoding);
    if (!staticContent) {
      response.setCharacterEncoding(encoding);
    }
  }
  boolean skip=staticContent;
  long before=System.currentTimeMillis();
  if (!skip) {
    initializeActionContext((HttpServletRequest)request,(HttpServletResponse)response);
  }
 else {
    request.setAttribute(Constants.SKIP_PATH,"Y");
  }
  if (isAjaxRequest((HttpServletRequest)request)) {
    request.setAttribute(Constants.SITEMESH_FILTERAPPLIED,Boolean.TRUE);
  }
  String requestPathKeyWithQueryString=requestInfo(skip,(HttpServletRequest)request);
  log.debug("============>>\"" + requestPathKeyWithQueryString + "\"");
  try {
    chain.doFilter(request,response);
  }
 catch (  Throwable ex) {
    log.error("Error from chain.doFilter: " + ex.getMessage(),ex);
  }
  long after=System.currentTimeMillis();
  if (EnvConfig.getInstance().allowRecordBenchmark()) {
    log.info("\"" + requestPathKeyWithQueryString + "\" takes: "+ (after - before)+ " ms");
    if (EnvConfig.getInstance().allowRecordBenchmarkInHeader()) {
      HttpServletResponseWrapper resw=new HttpServletResponseWrapper((HttpServletResponse)response);
      resw.addHeader("Exec-Time",(after - before) + " ms");
    }
  }
  clearCachedRequestData();
}
 

Example 5

From project spring-test-mvc, under directory /src/test/java/org/springframework/test/web/server/samples/standalone/.

Source file: FilterTests.java

  21 
vote

@Override protected void doFilterInternal(HttpServletRequest request,HttpServletResponse response,FilterChain filterChain) throws ServletException, IOException {
  filterChain.doFilter(new HttpServletRequestWrapper(request){
    @Override public Principal getUserPrincipal(){
      return new Principal(){
        public String getName(){
          return PRINCIPAL_NAME;
        }
      }
;
    }
  }
,new HttpServletResponseWrapper(response));
}
 

Example 6

From project pluto, under directory /pluto-container/src/main/java/org/apache/pluto/container/impl/.

Source file: PortletRequestDispatcherImpl.java

  19 
vote

private static HttpServletPortletResponseWrapper getWrappedResponse(ServletResponse response){
  HttpServletPortletResponseWrapper res=null;
  do {
    if (response instanceof HttpServletPortletResponseWrapper) {
      res=(HttpServletPortletResponseWrapper)response;
    }
 else     if (response instanceof HttpServletResponseWrapper) {
      response=((HttpServletResponseWrapper)response).getResponse();
    }
 else {
      response=null;
    }
  }
 while (response != null && res == null);
  return res;
}
 

Example 7

From project seamless, under directory /mock/src/main/java/org/seamless/mock/http/.

Source file: MockRequestDispatcher.java

  19 
vote

/** 
 * Obtain the underlying EnhancedMockHttpServletResponse, unwrapping  {@link javax.servlet.http.HttpServletResponseWrapper} decorators if necessary.
 */
protected MockHttpServletResponse getMockHttpServletResponse(ServletResponse response){
  if (response instanceof MockHttpServletResponse) {
    return (MockHttpServletResponse)response;
  }
  if (response instanceof HttpServletResponseWrapper) {
    return getMockHttpServletResponse(((HttpServletResponseWrapper)response).getResponse());
  }
  throw new IllegalArgumentException("MockRequestDispatcher requires MockHttpServletResponse");
}
 

Example 8

From project undertow, under directory /servlet/src/main/java/io/undertow/servlet/spec/.

Source file: HttpServletResponseImpl.java

  19 
vote

public static HttpServletResponseImpl getResponseImpl(final ServletResponse response){
  final HttpServletResponseImpl requestImpl;
  if (response instanceof HttpServletResponseImpl) {
    requestImpl=(HttpServletResponseImpl)response;
  }
 else   if (response instanceof HttpServletResponseWrapper) {
    requestImpl=getResponseImpl(((HttpServletResponseWrapper)response).getResponse());
  }
 else {
    throw UndertowServletMessages.MESSAGES.responseWasNotOriginalOrWrapper(response);
  }
  return requestImpl;
}