Search in sources :

Example 36 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project dubbo-faker by moyada.

the class SwaggerFilter method doFilter.

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest hrequest = (HttpServletRequest) servletRequest;
    String requestURI = hrequest.getRequestURI();
    if ("/api-docs".equals(requestURI)) {
        // RequestDispatcher dispatcher = servletRequest.getRequestDispatcher("/swagger-ui.html");
        // dispatcher.forward(servletRequest, servletResponse);
        HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) servletResponse);
        wrapper.sendRedirect("/swagger-ui.html");
        return;
    }
    filterChain.doFilter(servletRequest, servletResponse);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper)

Example 37 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project quickstart by wildfly.

the class JSONPRequestFilter method doFilter.

@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;
    // extract the callback method from the request query parameters
    String callback = getCallbackMethod(httpRequest);
    if (!isJSONPRequest(callback)) {
        // Request is not a JSONP request move on
        chain.doFilter(request, response);
    } else {
        // Need to check if the callback method is safe
        if (!SAFE_PRN.matcher(callback).matches()) {
            throw new ServletException("JSONP Callback method '" + CALLBACK_METHOD + "' parameter not valid function");
        }
        // Will stream updated response
        final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        // Create a custom response wrapper to adding in the padding
        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 boolean isReady() {
                        // The stream is always ready
                        return true;
                    }

                    @Override
                    public void setWriteListener(WriteListener writeListener) {
                    // Nothing to do
                    }
                };
            }

            @Override
            public PrintWriter getWriter() throws IOException {
                return new PrintWriter(byteStream);
            }
        };
        // Process the rest of the filter chain, including the JAX-RS request
        chain.doFilter(request, responseWrapper);
        // Override response content and encoding
        response.setContentType(CONTENT_TYPE);
        response.setCharacterEncoding("UTF-8");
        // Write the padded updates to the output stream.
        response.getOutputStream().write((callback + "(").getBytes());
        response.getOutputStream().write(byteStream.toByteArray());
        response.getOutputStream().write(");".getBytes());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WriteListener(javax.servlet.WriteListener) PrintWriter(java.io.PrintWriter)

Aggregations

HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)37 HttpServletResponse (javax.servlet.http.HttpServletResponse)31 ServletResponse (javax.servlet.ServletResponse)23 IOException (java.io.IOException)22 ServletException (javax.servlet.ServletException)20 ServletRequest (javax.servlet.ServletRequest)20 Test (org.junit.Test)20 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)15 MockFilterChain (org.springframework.mock.web.MockFilterChain)14 NestedServletException (org.springframework.web.util.NestedServletException)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 ErrorPage (org.springframework.boot.web.server.ErrorPage)12 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)8 PrintWriter (java.io.PrintWriter)7 ServletOutputStream (javax.servlet.ServletOutputStream)6 Provider (com.google.inject.Provider)4 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)4 ServletTestUtils.newFakeHttpServletResponse (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse)4 Filter (javax.servlet.Filter)4