Search in sources :

Example 31 with HttpServletRequest

use of javax.servlet.http.HttpServletRequest in project groovy by apache.

the class GroovyServletTest method testRequestGetCommandOK.

public void testRequestGetCommandOK() {
    Mock requestMock = mock(HttpServletRequest.class);
    requestMock.expects(once()).method("getParameter").with(eq("command")).will(returnValue("SELECT..."));
    HttpServletRequest request = (HttpServletRequest) requestMock.proxy();
    String command = request.getParameter("command");
    assertEquals("SELECT...", command);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Mock(org.jmock.Mock)

Example 32 with HttpServletRequest

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

the class AuthenticationWithProxyUserFilter method doFilter.

/**
   * This method provide the ability to do pre/post tasks
   * in filter chain. Override this method to authorize
   * proxy user between AuthenticationFilter and next filter.
   * @param filterChain the filter chain object.
   * @param request the request object.
   * @param response the response object.
   *
   * @throws IOException
   * @throws ServletException
   */
@Override
protected void doFilter(FilterChain filterChain, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    final String proxyUser = getDoAs(request);
    if (proxyUser != null) {
        // Change the remote user after proxy user is authorized.
        final HttpServletRequest finalReq = request;
        request = new HttpServletRequestWrapper(finalReq) {

            private String getRemoteOrProxyUser() throws AuthorizationException {
                UserGroupInformation realUser = UserGroupInformation.createRemoteUser(finalReq.getRemoteUser());
                UserGroupInformation proxyUserInfo = UserGroupInformation.createProxyUser(proxyUser, realUser);
                ProxyUsers.authorize(proxyUserInfo, finalReq.getRemoteAddr());
                return proxyUserInfo.getUserName();
            }

            @Override
            public String getRemoteUser() {
                try {
                    return getRemoteOrProxyUser();
                } catch (AuthorizationException ex) {
                    LOG.error("Unable to verify proxy user: " + ex.getMessage(), ex);
                }
                return null;
            }
        };
    }
    filterChain.doFilter(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) AuthorizationException(org.apache.hadoop.security.authorize.AuthorizationException)

Example 33 with HttpServletRequest

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

the class TestMDCFilter method mdc.

@Test
public void mdc() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Mockito.when(request.getUserPrincipal()).thenReturn(null);
    Mockito.when(request.getMethod()).thenReturn("METHOD");
    Mockito.when(request.getPathInfo()).thenReturn("/pathinfo");
    ServletResponse response = Mockito.mock(ServletResponse.class);
    final AtomicBoolean invoked = new AtomicBoolean();
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertEquals(MDC.get("hostname"), null);
            assertEquals(MDC.get("user"), null);
            assertEquals(MDC.get("method"), "METHOD");
            assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
        }
    };
    MDC.clear();
    Filter filter = new MDCFilter();
    filter.init(null);
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    assertNull(MDC.get("hostname"));
    assertNull(MDC.get("user"));
    assertNull(MDC.get("method"));
    assertNull(MDC.get("path"));
    Mockito.when(request.getUserPrincipal()).thenReturn(new Principal() {

        @Override
        public String getName() {
            return "name";
        }
    });
    invoked.set(false);
    chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertEquals(MDC.get("hostname"), null);
            assertEquals(MDC.get("user"), "name");
            assertEquals(MDC.get("method"), "METHOD");
            assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
        }
    };
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    HostnameFilter.HOSTNAME_TL.set("HOST");
    invoked.set(false);
    chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertEquals(MDC.get("hostname"), "HOST");
            assertEquals(MDC.get("user"), "name");
            assertEquals(MDC.get("method"), "METHOD");
            assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
        }
    };
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    HostnameFilter.HOSTNAME_TL.remove();
    filter.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletResponse(javax.servlet.ServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) IOException(java.io.IOException) Principal(java.security.Principal) Test(org.junit.Test)

Example 34 with HttpServletRequest

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

the class MDCFilter method doFilter.

/**
   * Sets the slf4j <code>MDC</code> and delegates the request to the chain.
   *
   * @param request servlet request.
   * @param response servlet response.
   * @param chain filter chain.
   *
   * @throws IOException thrown if an IO error occurs.
   * @throws ServletException thrown if a servlet error occurs.
   */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    try {
        MDC.clear();
        String hostname = HostnameFilter.get();
        if (hostname != null) {
            MDC.put("hostname", HostnameFilter.get());
        }
        Principal principal = ((HttpServletRequest) request).getUserPrincipal();
        String user = (principal != null) ? principal.getName() : null;
        if (user != null) {
            MDC.put("user", user);
        }
        MDC.put("method", ((HttpServletRequest) request).getMethod());
        if (((HttpServletRequest) request).getPathInfo() != null) {
            MDC.put("path", ((HttpServletRequest) request).getPathInfo());
        }
        chain.doFilter(request, response);
    } finally {
        MDC.clear();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Principal(java.security.Principal)

Example 35 with HttpServletRequest

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

the class CheckUploadContentTypeFilter method doFilter.

/**
   * Enforces the content-type to be application/octet-stream for
   * POST and PUT requests.
   *
   * @param request servlet request.
   * @param response servlet response.
   * @param chain filter chain.
   *
   * @throws IOException thrown if an IO error occurs.
   * @throws ServletException thrown if a servlet error occurs.
   */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    boolean contentTypeOK = true;
    HttpServletRequest httpReq = (HttpServletRequest) request;
    HttpServletResponse httpRes = (HttpServletResponse) response;
    String method = httpReq.getMethod();
    if (method.equals("PUT") || method.equals("POST")) {
        String op = httpReq.getParameter(HttpFSFileSystem.OP_PARAM);
        if (op != null && UPLOAD_OPERATIONS.contains(StringUtils.toUpperCase(op))) {
            if ("true".equalsIgnoreCase(httpReq.getParameter(HttpFSParametersProvider.DataParam.NAME))) {
                String contentType = httpReq.getContentType();
                contentTypeOK = HttpFSFileSystem.UPLOAD_CONTENT_TYPE.equalsIgnoreCase(contentType);
            }
        }
    }
    if (contentTypeOK) {
        chain.doFilter(httpReq, httpRes);
    } else {
        httpRes.sendError(HttpServletResponse.SC_BAD_REQUEST, "Data upload requests must have content-type set to '" + HttpFSFileSystem.UPLOAD_CONTENT_TYPE + "'");
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Aggregations

HttpServletRequest (javax.servlet.http.HttpServletRequest)2488 HttpServletResponse (javax.servlet.http.HttpServletResponse)1308 Test (org.junit.Test)987 IOException (java.io.IOException)595 ServletException (javax.servlet.ServletException)498 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)223 FilterChain (javax.servlet.FilterChain)200 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)196 Test (org.testng.annotations.Test)168 Request (org.eclipse.jetty.server.Request)164 CountDownLatch (java.util.concurrent.CountDownLatch)160 HttpServlet (javax.servlet.http.HttpServlet)156 HttpSession (javax.servlet.http.HttpSession)150 HashMap (java.util.HashMap)130 PrintWriter (java.io.PrintWriter)121 Map (java.util.Map)100 InterruptedIOException (java.io.InterruptedIOException)97 ServletRequest (javax.servlet.ServletRequest)95 ServletContext (javax.servlet.ServletContext)91 ServletOutputStream (javax.servlet.ServletOutputStream)90