Search in sources :

Example 11 with ServletRequest

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

the class PKIAuthenticationIntegrationTest method testPkiAuth.

@Test
public void testPkiAuth() throws Exception {
    CollectionAdminRequest.createCollection("collection", "conf", 2, 1).process(cluster.getSolrClient());
    // TODO make a SolrJ helper class for this
    byte[] bytes = Utils.toJSON(makeMap("authorization", singletonMap("class", MockAuthorizationPlugin.class.getName()), "authentication", singletonMap("class", MockAuthenticationPlugin.class.getName())));
    zkClient().setData(ZkStateReader.SOLR_SECURITY_CONF_PATH, bytes, true);
    HttpClient httpClient = cluster.getSolrClient().getHttpClient();
    for (JettySolrRunner jetty : cluster.getJettySolrRunners()) {
        String baseUrl = jetty.getBaseUrl().toString();
        verifySecurityStatus(httpClient, baseUrl + "/admin/authorization", "authorization/class", MockAuthorizationPlugin.class.getName(), 20);
        verifySecurityStatus(httpClient, baseUrl + "/admin/authentication", "authentication.enabled", "true", 20);
    }
    log.info("Starting test");
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add("q", "*:*");
    params.add("__user", "solr");
    params.add("__pwd", "SolrRocks");
    // This should work fine.
    final AtomicInteger count = new AtomicInteger();
    MockAuthorizationPlugin.predicate = new Predicate<AuthorizationContext>() {

        @Override
        public boolean test(AuthorizationContext context) {
            if ("/select".equals(context.getResource())) {
                Principal principal = context.getUserPrincipal();
                log.info("principalIs : {}", principal);
                if (principal != null && principal.getName().equals("solr")) {
                    count.incrementAndGet();
                }
            }
            return true;
        }
    };
    MockAuthenticationPlugin.predicate = new Predicate<ServletRequest>() {

        @Override
        public boolean test(ServletRequest servletRequest) {
            String s = ((HttpServletRequest) servletRequest).getQueryString();
            if (s != null && s.contains("__user=solr") && s.contains("__pwd=SolrRocks")) {
                servletRequest.setAttribute(Principal.class.getName(), "solr");
            }
            return true;
        }
    };
    QueryRequest query = new QueryRequest(params);
    query.process(cluster.getSolrClient(), "collection");
    assertTrue("all nodes must get the user solr , no:of nodes got solr : " + count.get(), count.get() > 2);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClient(org.apache.http.client.HttpClient) Principal(java.security.Principal) Test(org.junit.Test)

Example 12 with ServletRequest

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

the class MockAuthenticationPlugin method doAuthenticate.

@Override
public boolean doAuthenticate(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
    String user = null;
    if (predicate != null) {
        if (predicate.test(request)) {
            user = (String) request.getAttribute(Principal.class.getName());
            request.removeAttribute(Principal.class.getName());
        }
    }
    final FilterChain ffc = filterChain;
    final AtomicBoolean requestContinues = new AtomicBoolean(false);
    forward(user, request, response, new FilterChain() {

        @Override
        public void doFilter(ServletRequest req, ServletResponse res) throws IOException, ServletException {
            ffc.doFilter(req, res);
            requestContinues.set(true);
        }
    });
    return requestContinues.get();
}
Also used : ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) IOException(java.io.IOException) Principal(java.security.Principal) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal)

Example 13 with ServletRequest

use of javax.servlet.ServletRequest in project sling by apache.

the class ExternalServletContextWrapperTest method testUnwrappingWrappedSlingRequest.

/**
     * Unwrapping a wrapped sling request should return the first-level request
     * wrapped by the sling request.
     */
@Test
public void testUnwrappingWrappedSlingRequest() {
    final HttpServletRequest req = context.mock(HttpServletRequest.class);
    context.checking(new Expectations() {

        {
            allowing(req).getServletPath();
            will(returnValue("/"));
            allowing(req).getPathInfo();
            will(returnValue("/test"));
        }
    });
    final HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(req);
    final HttpServletRequestWrapper wrapper2 = new HttpServletRequestWrapper(wrapper);
    final SlingHttpServletRequestImpl slingRequest = new SlingHttpServletRequestImpl(null, wrapper2);
    final HttpServletRequestWrapper slingWrapper = new HttpServletRequestWrapper(slingRequest);
    ServletRequest unwrapped = ExternalServletContextWrapper.RequestDispatcherWrapper.unwrapServletRequest(slingWrapper);
    assertEquals(wrapper2, unwrapped);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Expectations(org.jmock.Expectations) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) SlingHttpServletRequestImpl(org.apache.sling.engine.impl.SlingHttpServletRequestImpl) Test(org.junit.Test)

Example 14 with ServletRequest

use of javax.servlet.ServletRequest in project sling by apache.

the class ExternalServletContextWrapperTest method testUnwrappingWrappedRequest.

/**
     * Unwrapping a wrapper request should return in the request.
     */
@Test
public void testUnwrappingWrappedRequest() {
    final ServletRequest req = context.mock(ServletRequest.class);
    final ServletRequestWrapper wrapper = new ServletRequestWrapper(req);
    ServletRequest unwrapped = ExternalServletContextWrapper.RequestDispatcherWrapper.unwrapServletRequest(wrapper);
    assertEquals(req, unwrapped);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) Test(org.junit.Test)

Example 15 with ServletRequest

use of javax.servlet.ServletRequest in project sling by apache.

the class ExternalServletContextWrapperTest method testUnwrappingDoubleWrappedRequest.

@Test
public void testUnwrappingDoubleWrappedRequest() {
    final ServletRequest req = context.mock(ServletRequest.class);
    final ServletRequestWrapper wrapper = new ServletRequestWrapper(req);
    final ServletRequestWrapper wrapper2 = new ServletRequestWrapper(wrapper);
    ServletRequest unwrapped = ExternalServletContextWrapper.RequestDispatcherWrapper.unwrapServletRequest(wrapper2);
    assertEquals(req, unwrapped);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) Test(org.junit.Test)

Aggregations

ServletRequest (javax.servlet.ServletRequest)314 HttpServletRequest (javax.servlet.http.HttpServletRequest)188 ServletResponse (javax.servlet.ServletResponse)183 HttpServletResponse (javax.servlet.http.HttpServletResponse)118 FilterChain (javax.servlet.FilterChain)113 Test (org.junit.Test)82 IOException (java.io.IOException)65 ServletException (javax.servlet.ServletException)64 Filter (javax.servlet.Filter)41 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)28 Injector (com.google.inject.Injector)26 JspException (javax.servlet.jsp.JspException)26 RequestContext (com.agiletec.aps.system.RequestContext)25 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)25 FilterConfig (javax.servlet.FilterConfig)24 MockFilterChain (org.springframework.mock.web.MockFilterChain)24 ServletContext (javax.servlet.ServletContext)23 HttpSession (javax.servlet.http.HttpSession)21 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)18 ServletTestUtils.newFakeHttpServletResponse (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse)18