Search in sources :

Example 76 with ServletRequest

use of javax.servlet.ServletRequest in project gerrit by GerritCodeReview.

the class HttpPluginServlet method service.

@Override
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    List<String> parts = Lists.newArrayList(Splitter.on('/').limit(3).omitEmptyStrings().split(Strings.nullToEmpty(RequestUtil.getEncodedPathInfo(req))));
    if (isApiCall(req, parts)) {
        managerApi.service(req, res);
        return;
    }
    String name = parts.get(0);
    final PluginHolder holder = plugins.get(name);
    if (holder == null) {
        CacheHeaders.setNotCacheable(res);
        res.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    HttpServletRequest wr = wrapper.create(req, name);
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest req, ServletResponse res) throws IOException {
            onDefault(holder, (HttpServletRequest) req, (HttpServletResponse) res);
        }
    };
    if (holder.filter != null) {
        holder.filter.doFilter(wr, res, chain);
    } else {
        chain.doFilter(wr, res);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterChain(javax.servlet.FilterChain)

Example 77 with ServletRequest

use of javax.servlet.ServletRequest in project indy by Commonjava.

the class RequestScopeListener method requestInitialized.

@Override
public void requestInitialized(final ServletRequestEvent evt) {
    final BoundRequestContext ctx = CDI.current().select(BoundRequestContext.class).get();
    final Map<String, Object> instanceMap = new HashMap<>();
    final ServletRequest req = evt.getServletRequest();
    req.setAttribute(CDI_CONTEXT, ctx);
    req.setAttribute(CDI_INSTANCE_MAP, instanceMap);
    ctx.associate(instanceMap);
    ctx.activate();
}
Also used : BoundRequestContext(org.jboss.weld.context.bound.BoundRequestContext) ServletRequest(javax.servlet.ServletRequest) HashMap(java.util.HashMap)

Example 78 with ServletRequest

use of javax.servlet.ServletRequest in project indy by Commonjava.

the class RequestScopeListener method requestDestroyed.

@Override
public void requestDestroyed(final ServletRequestEvent evt) {
    final ServletRequest req = evt.getServletRequest();
    @SuppressWarnings("unchecked") final Map<String, Object> instanceMap = (Map<String, Object>) req.getAttribute(CDI_INSTANCE_MAP);
    final BoundRequestContext ctx = (BoundRequestContext) req.getAttribute(CDI_CONTEXT);
    ctx.invalidate();
    ctx.deactivate();
    ctx.dissociate(instanceMap);
    req.removeAttribute(CDI_CONTEXT);
    req.removeAttribute(CDI_INSTANCE_MAP);
}
Also used : ServletRequest(javax.servlet.ServletRequest) BoundRequestContext(org.jboss.weld.context.bound.BoundRequestContext) Map(java.util.Map) HashMap(java.util.HashMap)

Example 79 with ServletRequest

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

the class LogTracerTest method checkTracing.

@Test
public void checkTracing() throws Exception {
    HttpServletRequest request = mock(SlingHttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getParameter(LogTracer.PARAM_TRACER_CONFIG)).thenReturn("a.b.c;level=trace,a.b;level=debug");
    activateTracer();
    Level oldLevel = rootLogger().getLevel();
    rootLogger().setLevel(Level.INFO);
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            TurboFilter turboFilter = context.getService(TurboFilter.class);
            assertNotNull(turboFilter);
            getLogContext().addTurboFilter(turboFilter);
            getLogger("a").info("a-info");
            getLogger("a").debug("a-debug");
            getLogger("a.b").info("a.b-info");
            getLogger("a.b").debug("a.b-debug");
            getLogger("a.b").trace("a.b-trace");
            getLogger("a.b.c").debug("a.b.c-debug");
            getLogger("a.b.c").trace("a.b.c-trace");
            getLogger("a.b.c.d").trace("a.b.c.d-trace");
            if (getLogger("a.b.c").isTraceEnabled()) {
                getLogger("a.b.c").trace("a.b.c-trace2");
            }
        }
    };
    Filter filter = getFilter(true);
    filter.doFilter(request, response, chain);
    assertNull(context.getService(TurboFilter.class));
    List<String> logs = logCollector.getLogs();
    assertThat(logs, hasItem("a-info"));
    assertThat(logs, not(hasItem("a-debug")));
    assertThat(logs, hasItem("a.b-info"));
    assertThat(logs, hasItem("a.b-debug"));
    assertThat(logs, not(hasItem("a.b-trace")));
    assertThat(logs, hasItem("a.b.c-debug"));
    assertThat(logs, hasItem("a.b.c-trace"));
    assertThat(logs, hasItem("a.b.c-trace2"));
    assertThat(logs, hasItem("a.b.c.d-trace"));
    rootLogger().setLevel(oldLevel);
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) Level(ch.qos.logback.classic.Level) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Test(org.junit.Test)

Example 80 with ServletRequest

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

the class LogTracerTest method turboFilterRegisteredWhenTracingRequested.

@Test
public void turboFilterRegisteredWhenTracingRequested() throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getHeader(LogTracer.HEADER_TRACER_CONFIG)).thenReturn("foo.bar");
    activateTracer();
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            assertNotNull(context.getService(TurboFilter.class));
        }
    };
    Filter filter = getFilter(false);
    filter.doFilter(request, response, chain);
    assertNull("TurboFilter should get removed once request is done", context.getService(TurboFilter.class));
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Test(org.junit.Test)

Aggregations

ServletRequest (javax.servlet.ServletRequest)185 ServletResponse (javax.servlet.ServletResponse)129 HttpServletRequest (javax.servlet.http.HttpServletRequest)117 HttpServletResponse (javax.servlet.http.HttpServletResponse)95 FilterChain (javax.servlet.FilterChain)79 Test (org.junit.Test)75 ServletException (javax.servlet.ServletException)59 IOException (java.io.IOException)57 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)35 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)35 MockFilterChain (org.springframework.mock.web.MockFilterChain)32 Filter (javax.servlet.Filter)28 Injector (com.google.inject.Injector)25 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)21 NestedServletException (org.springframework.web.util.NestedServletException)19 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)18 ServletTestUtils.newFakeHttpServletResponse (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse)18 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)15 ErrorPage (org.springframework.boot.web.server.ErrorPage)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14