Search in sources :

Example 36 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper in project nutz by nutzam.

the class WhaleFilter method handleUpload.

@SuppressWarnings("unchecked")
public HttpServletRequest handleUpload(HttpServletRequest req) throws ServletException {
    try {
        FastUploading fup = new FastUploading();
        final Map<String, Object> params = fup.parse(req, (UploadingContext) uc);
        final List<TempFile> files = new ArrayList<TempFile>();
        Iterator<Entry<String, Object>> it = params.entrySet().iterator();
        while (it.hasNext()) {
            Object obj = it.next().getValue();
            final boolean[] re = new boolean[1];
            Lang.each(obj, new Each<Object>() {

                public void invoke(int index, Object ele, int length) {
                    if (ele != null && ele instanceof TempFile) {
                        files.add((TempFile) ele);
                        re[0] = true;
                    }
                }
            });
            if (re[0])
                it.remove();
        }
        req.setAttribute("_files", files);
        params.putAll(req.getParameterMap());
        return new HttpServletRequestWrapper(req) {

            public String getParameter(String name) {
                return (String) params.get(name);
            }

            @SuppressWarnings("rawtypes")
            public Map getParameterMap() {
                return params;
            }

            @SuppressWarnings("rawtypes")
            public Enumeration getParameterNames() {
                return Collections.enumeration(params.keySet());
            }

            public String[] getParameterValues(String name) {
                if (params.containsKey(name))
                    return new String[] { (String) params.get(name) };
                return null;
            }
        };
    } catch (UploadException e) {
        throw new ServletException("upload fail", e);
    }
}
Also used : UploadException(org.nutz.mvc.upload.UploadException) ArrayList(java.util.ArrayList) ServletException(javax.servlet.ServletException) FastUploading(org.nutz.mvc.upload.FastUploading) Entry(java.util.Map.Entry) TempFile(org.nutz.mvc.upload.TempFile) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper)

Example 37 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper in project spring-security by spring-projects.

the class HttpSessionSecurityContextRepositoryTests method traverseWrappedRequests.

// SEC-2578
@Test
public void traverseWrappedRequests() {
    HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, response);
    SecurityContext context = repo.loadContext(holder);
    assertThat(request.getSession(false)).isNull();
    // Simulate authentication during the request
    context.setAuthentication(testToken);
    repo.saveContext(context, new HttpServletRequestWrapper(holder.getRequest()), new HttpServletResponseWrapper(holder.getResponse()));
    assertThat(request.getSession(false)).isNotNull();
    assertThat(request.getSession().getAttribute(SPRING_SECURITY_CONTEXT_KEY)).isEqualTo(context);
}
Also used : HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SecurityContext(org.springframework.security.core.context.SecurityContext) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 38 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper in project spring-security by spring-projects.

the class DebugFilterTest method doFilterDoesNotWrapWithDebugRequestWrapperAgain.

@Test
public void doFilterDoesNotWrapWithDebugRequestWrapperAgain() throws Exception {
    when(request.getAttribute(requestAttr)).thenReturn(Boolean.TRUE);
    HttpServletRequest fireWalledRequest = new HttpServletRequestWrapper(new DebugRequestWrapper(this.request));
    filter.doFilter(fireWalledRequest, response, filterChain);
    verify(fcp).doFilter(fireWalledRequest, response, filterChain);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Example 39 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper in project spring-framework by spring-projects.

the class ServletWebRequestTests method decoratedNativeRequest.

@Test
public void decoratedNativeRequest() {
    HttpServletRequest decoratedRequest = new HttpServletRequestWrapper(servletRequest);
    HttpServletResponse decoratedResponse = new HttpServletResponseWrapper(servletResponse);
    ServletWebRequest request = new ServletWebRequest(decoratedRequest, decoratedResponse);
    assertSame(decoratedRequest, request.getNativeRequest());
    assertSame(decoratedRequest, request.getNativeRequest(ServletRequest.class));
    assertSame(decoratedRequest, request.getNativeRequest(HttpServletRequest.class));
    assertSame(servletRequest, request.getNativeRequest(MockHttpServletRequest.class));
    assertNull(request.getNativeRequest(MultipartRequest.class));
    assertSame(decoratedResponse, request.getNativeResponse());
    assertSame(decoratedResponse, request.getNativeResponse(ServletResponse.class));
    assertSame(decoratedResponse, request.getNativeResponse(HttpServletResponse.class));
    assertSame(servletResponse, request.getNativeResponse(MockHttpServletResponse.class));
    assertNull(request.getNativeResponse(MultipartRequest.class));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MultipartRequest(org.springframework.web.multipart.MultipartRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 40 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper in project hadoop by apache.

the class DelegationTokenAuthenticationFilter method doFilter.

@Override
protected void doFilter(FilterChain filterChain, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    boolean requestCompleted = false;
    UserGroupInformation ugi = null;
    AuthenticationToken authToken = (AuthenticationToken) request.getUserPrincipal();
    if (authToken != null && authToken != AuthenticationToken.ANONYMOUS) {
        // if the request was authenticated because of a delegation token,
        // then we ignore proxyuser (this is the same as the RPC behavior).
        ugi = (UserGroupInformation) request.getAttribute(DelegationTokenAuthenticationHandler.DELEGATION_TOKEN_UGI_ATTRIBUTE);
        if (ugi == null) {
            String realUser = request.getUserPrincipal().getName();
            ugi = UserGroupInformation.createRemoteUser(realUser, handlerAuthMethod);
            String doAsUser = getDoAs(request);
            if (doAsUser != null) {
                ugi = UserGroupInformation.createProxyUser(doAsUser, ugi);
                try {
                    ProxyUsers.authorize(ugi, request.getRemoteAddr());
                } catch (AuthorizationException ex) {
                    HttpExceptionUtils.createServletExceptionResponse(response, HttpServletResponse.SC_FORBIDDEN, ex);
                    requestCompleted = true;
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Authentication exception: " + ex.getMessage(), ex);
                    } else {
                        LOG.warn("Authentication exception: " + ex.getMessage());
                    }
                }
            }
        }
        UGI_TL.set(ugi);
    }
    if (!requestCompleted) {
        final UserGroupInformation ugiF = ugi;
        try {
            request = new HttpServletRequestWrapper(request) {

                @Override
                public String getAuthType() {
                    return (ugiF != null) ? handlerAuthMethod.toString() : null;
                }

                @Override
                public String getRemoteUser() {
                    return (ugiF != null) ? ugiF.getShortUserName() : null;
                }

                @Override
                public Principal getUserPrincipal() {
                    return (ugiF != null) ? new Principal() {

                        @Override
                        public String getName() {
                            return ugiF.getUserName();
                        }
                    } : null;
                }
            };
            super.doFilter(filterChain, request, response);
        } finally {
            UGI_TL.remove();
        }
    }
}
Also used : AuthenticationToken(org.apache.hadoop.security.authentication.server.AuthenticationToken) AuthorizationException(org.apache.hadoop.security.authorize.AuthorizationException) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) Principal(java.security.Principal) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Aggregations

HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)54 HttpServletRequest (javax.servlet.http.HttpServletRequest)42 HttpServletResponse (javax.servlet.http.HttpServletResponse)17 ServletRequest (javax.servlet.ServletRequest)13 ServletResponse (javax.servlet.ServletResponse)11 IOException (java.io.IOException)10 FilterChain (javax.servlet.FilterChain)10 ServletException (javax.servlet.ServletException)9 Test (org.junit.Test)9 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)8 FilterConfig (javax.servlet.FilterConfig)7 Principal (java.security.Principal)6 Filter (javax.servlet.Filter)6 ArrayList (java.util.ArrayList)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 OutputStream (java.io.OutputStream)4 Cookie (javax.servlet.http.Cookie)4 Injector (com.google.inject.Injector)2