Search in sources :

Example 31 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project lucene-solr by apache.

the class KerberosPlugin method doAuthenticate.

@Override
public boolean doAuthenticate(ServletRequest req, ServletResponse rsp, FilterChain chain) throws Exception {
    log.debug("Request to authenticate using kerberos: " + req);
    final HttpServletResponse frsp = (HttpServletResponse) rsp;
    // kerberosFilter may close the stream and write to closed streams,
    // see HADOOP-13346.  To work around, pass a PrintWriter that ignores
    // closes
    HttpServletResponse rspCloseShield = new HttpServletResponseWrapper(frsp) {

        @SuppressForbidden(reason = "Hadoop DelegationTokenAuthenticationFilter uses response writer, this" + "is providing a CloseShield on top of that")
        @Override
        public PrintWriter getWriter() throws IOException {
            final PrintWriter pw = new PrintWriterWrapper(frsp.getWriter()) {

                @Override
                public void close() {
                }

                ;
            };
            return pw;
        }
    };
    kerberosFilter.doFilter(req, rspCloseShield, chain);
    String requestContinuesAttr = (String) req.getAttribute(RequestContinuesRecorderAuthenticationHandler.REQUEST_CONTINUES_ATTR);
    if (requestContinuesAttr == null) {
        log.warn("Could not find " + RequestContinuesRecorderAuthenticationHandler.REQUEST_CONTINUES_ATTR);
        return false;
    } else {
        return Boolean.parseBoolean(requestContinuesAttr);
    }
}
Also used : HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter)

Example 32 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project sling by apache.

the class ExternalServletContextWrapperTest method testUnwrappingSlingResponse.

/**
     * Unwrapping a sling response should return the first-level response wrapped
     * by the sling response.
     */
@Test
public void testUnwrappingSlingResponse() {
    final HttpServletResponse resp = context.mock(HttpServletResponse.class);
    final HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper(resp);
    final HttpServletResponseWrapper wrapper2 = new HttpServletResponseWrapper(wrapper);
    final SlingHttpServletResponseImpl slingResponse = new SlingHttpServletResponseImpl(null, wrapper2);
    ServletResponse unwrapped = ExternalServletContextWrapper.RequestDispatcherWrapper.unwrapServletResponse(slingResponse);
    assertEquals(wrapper2, unwrapped);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) SlingHttpServletResponseImpl(org.apache.sling.engine.impl.SlingHttpServletResponseImpl) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 33 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project sling by apache.

the class ExternalServletContextWrapperTest method testUnwrappingWrappedSlingResponse.

/**
     * Unwrapping a wrapped sling response should return the first-level response
     * wrapped by the sling response.
     */
@Test
public void testUnwrappingWrappedSlingResponse() {
    final HttpServletResponse resp = context.mock(HttpServletResponse.class);
    final HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper(resp);
    final HttpServletResponseWrapper wrapper2 = new HttpServletResponseWrapper(wrapper);
    final SlingHttpServletResponseImpl slingResponse = new SlingHttpServletResponseImpl(null, wrapper2);
    final HttpServletResponseWrapper slingWrapper = new HttpServletResponseWrapper(slingResponse);
    ServletResponse unwrapped = ExternalServletContextWrapper.RequestDispatcherWrapper.unwrapServletResponse(slingWrapper);
    assertEquals(wrapper2, unwrapped);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) SlingHttpServletResponseImpl(org.apache.sling.engine.impl.SlingHttpServletResponseImpl) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 34 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project stanbol by apache.

the class AuthenticatingFilter method doFilter.

@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain chain) throws IOException, ServletException {
    logger.debug("filtering request");
    final HttpServletRequest request = (HttpServletRequest) servletRequest;
    final HttpServletResponse response = (HttpServletResponse) servletResponse;
    final Subject subject = getSubject();
    {
        AuthenticationMethod authenticationMethod = null;
        try {
            for (Iterator<WeightedAuthenticationMethod> it = methodList.iterator(); it.hasNext(); ) {
                authenticationMethod = it.next();
                if (authenticationMethod.authenticate(request, subject)) {
                    break;
                }
            }
        } catch (LoginException ex) {
            if (!authenticationMethod.writeLoginResponse(request, response, ex)) {
                writeLoginResponse(request, response, ex);
            }
            return;
        }
    }
    Set<Principal> principals = subject.getPrincipals();
    if (principals.size() == 0) {
        principals.add(UserUtil.ANONYMOUS);
    }
    //the response wrapping is because of JERSEY-1926
    final ServletOutputStream[] out = new ServletOutputStream[1];
    final boolean[] closed = new boolean[1];
    final String[] sentErrorMsg = new String[1];
    final int[] sentErrorCode = new int[1];
    sentErrorCode[0] = -1;
    try {
        Subject.doAsPrivileged(subject, new PrivilegedExceptionAction<Object>() {

            @Override
            public Object run() throws Exception {
                HttpServletResponse wrapped = new HttpServletResponseWrapper(response) {

                    @Override
                    public ServletOutputStream getOutputStream() throws IOException {
                        final ServletOutputStream orig = response.getOutputStream();
                        out[0] = orig;
                        return new ServletOutputStream() {

                            @Override
                            public void write(int i) throws IOException {
                                orig.write(i);
                            }

                            @Override
                            public void close() throws IOException {
                                closed[0] = true;
                            }
                        };
                    }

                    @Override
                    public void sendError(int sc, String msg) throws IOException {
                        sentErrorCode[0] = sc;
                        sentErrorMsg[0] = msg;
                    }
                };
                chain.doFilter(request, wrapped);
                return null;
            }
        }, null);
    } catch (PrivilegedActionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof ServletException) {
            if (cause.getCause() instanceof SecurityException) {
                //working around JERSEY-1926
                writeLoginResponse(request, response, cause.getCause());
            } else {
                throw (ServletException) cause;
            }
        }
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        }
        throw new RuntimeException(e);
    } catch (SecurityException e) {
        writeLoginResponse(request, response, e);
    }
    if (!response.isCommitted()) {
        if (closed[0]) {
            try {
                out[0].close();
            } catch (IOException e) {
            //do nothing
            }
        }
        if (sentErrorCode[0] > -1) {
            response.sendError(sentErrorCode[0], sentErrorMsg[0]);
        }
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Iterator(java.util.Iterator) PrivilegedActionException(java.security.PrivilegedActionException) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Subject(javax.security.auth.Subject) ServletException(javax.servlet.ServletException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) Principal(java.security.Principal)

Aggregations

HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)34 HttpServletResponse (javax.servlet.http.HttpServletResponse)29 ServletResponse (javax.servlet.ServletResponse)23 IOException (java.io.IOException)22 ServletRequest (javax.servlet.ServletRequest)20 Test (org.junit.Test)20 ServletException (javax.servlet.ServletException)19 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 ErrorPage (org.springframework.boot.web.server.ErrorPage)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)8 PrintWriter (java.io.PrintWriter)6 ServletOutputStream (javax.servlet.ServletOutputStream)5 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