Search in sources :

Example 11 with ServletResponse

use of javax.servlet.ServletResponse in project sonarqube by SonarSource.

the class MasterServletFilterTest method filters_should_be_optional.

@Test
public void filters_should_be_optional() throws Exception {
    FilterConfig config = mock(FilterConfig.class);
    MasterServletFilter filters = new MasterServletFilter();
    filters.init(config, Collections.emptyList());
    ServletRequest request = mock(HttpServletRequest.class);
    ServletResponse response = mock(HttpServletResponse.class);
    FilterChain chain = mock(FilterChain.class);
    filters.doFilter(request, response, chain);
    verify(chain).doFilter(request, response);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) FilterConfig(javax.servlet.FilterConfig) Test(org.junit.Test)

Example 12 with ServletResponse

use of javax.servlet.ServletResponse in project head by mifos.

the class CharacterEncodingFilter method doFilter.

/* NOT OncePerRequestFilter !!! */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    request.setCharacterEncoding(CharEncoding.UTF_8);
    response.setCharacterEncoding(CharEncoding.UTF_8);
    if (!CharEncoding.UTF_8.equals(response.getCharacterEncoding())) {
        // MIFOS-5435 - the character encoding was not set because the connection is in including state
        try {
            Method getResponse = response.getClass().getMethod("getResponse");
            ServletResponse servletResponse = (ServletResponse) getResponse.invoke(response);
            getResponse = servletResponse.getClass().getMethod("getResponse");
            ServletResponse jettyResponse = (ServletResponse) getResponse.invoke(servletResponse);
            Field _characterEncoding = jettyResponse.getClass().getDeclaredField("_characterEncoding");
            _characterEncoding.setAccessible(true);
            _characterEncoding.set(jettyResponse, CharEncoding.UTF_8);
        } catch (NoSuchFieldException e) {
            logger.debug(e);
        } catch (IllegalAccessException e) {
            logger.debug(e);
        } catch (NoSuchMethodException e) {
            logger.debug(e);
        } catch (InvocationTargetException e) {
            logger.debug(e);
        }
    }
    chain.doFilter(request, response);
}
Also used : ServletResponse(javax.servlet.ServletResponse) Field(java.lang.reflect.Field) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 13 with ServletResponse

use of javax.servlet.ServletResponse in project spark by perwendel.

the class MatcherFilter method doFilter.

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
    HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
    // handle static resources
    boolean consumedByStaticFile = staticFiles.consume(httpRequest, httpResponse);
    if (consumedByStaticFile) {
        return;
    }
    String method = getHttpMethodFrom(httpRequest);
    String httpMethodStr = method.toLowerCase();
    String uri = httpRequest.getRequestURI();
    String acceptType = httpRequest.getHeader(ACCEPT_TYPE_REQUEST_MIME_HEADER);
    Body body = Body.create();
    RequestWrapper requestWrapper = RequestWrapper.create();
    ResponseWrapper responseWrapper = ResponseWrapper.create();
    Response response = RequestResponseFactory.create(httpResponse);
    HttpMethod httpMethod = HttpMethod.get(httpMethodStr);
    RouteContext context = RouteContext.create().withMatcher(routeMatcher).withHttpRequest(httpRequest).withUri(uri).withAcceptType(acceptType).withBody(body).withRequestWrapper(requestWrapper).withResponseWrapper(responseWrapper).withResponse(response).withHttpMethod(httpMethod);
    try {
        try {
            BeforeFilters.execute(context);
            Routes.execute(context);
            AfterFilters.execute(context);
        } catch (HaltException halt) {
            Halt.modify(httpResponse, body, halt);
        } catch (Exception generalException) {
            GeneralError.modify(httpRequest, httpResponse, body, requestWrapper, responseWrapper, generalException);
        }
        // If redirected and content is null set to empty string to not throw NotConsumedException
        if (body.notSet() && responseWrapper.isRedirected()) {
            body.set("");
        }
        if (body.notSet() && hasOtherHandlers) {
            if (servletRequest instanceof HttpRequestWrapper) {
                ((HttpRequestWrapper) servletRequest).notConsumed(true);
                return;
            }
        }
        if (body.notSet() && !externalContainer) {
            LOG.info("The requested route [{}] has not been mapped in Spark for {}: [{}]", uri, ACCEPT_TYPE_REQUEST_MIME_HEADER, acceptType);
            httpResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
            if (CustomErrorPages.existsFor(404)) {
                requestWrapper.setDelegate(RequestResponseFactory.create(httpRequest));
                responseWrapper.setDelegate(RequestResponseFactory.create(httpResponse));
                body.set(CustomErrorPages.getFor(404, requestWrapper, responseWrapper));
            } else {
                body.set(String.format(CustomErrorPages.NOT_FOUND));
            }
        }
    } finally {
        try {
            AfterAfterFilters.execute(context);
        } catch (Exception generalException) {
            GeneralError.modify(httpRequest, httpResponse, body, requestWrapper, responseWrapper, generalException);
        }
    }
    if (body.isSet()) {
        body.serializeTo(httpResponse, serializerChain, httpRequest);
    } else if (chain != null) {
        chain.doFilter(httpRequest, httpResponse);
    }
}
Also used : HaltException(spark.HaltException) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HaltException(spark.HaltException) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) Response(spark.Response) HttpRequestWrapper(spark.embeddedserver.jetty.HttpRequestWrapper) HttpRequestWrapper(spark.embeddedserver.jetty.HttpRequestWrapper) HttpMethod(spark.route.HttpMethod)

Example 14 with ServletResponse

use of javax.servlet.ServletResponse in project roboguice by roboguice.

the class ServletTest method testExistingSessionObject.

public void testExistingSessionObject() throws CreationException, IOException, ServletException {
    final Injector injector = createInjector();
    final HttpServletRequest request = newFakeHttpServletRequest();
    GuiceFilter filter = new GuiceFilter();
    final boolean[] invoked = new boolean[1];
    FilterChain filterChain = new FilterChain() {

        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            invoked[0] = true;
            InSession inSession = injector.getInstance(InSession.class);
            assertSame(inSession, injector.getInstance(InSession.class));
            assertNull(injector.getInstance(IN_SESSION_NULL_KEY));
            assertNull(injector.getInstance(IN_SESSION_NULL_KEY));
        }
    };
    filter.doFilter(request, null, filterChain);
    assertTrue(invoked[0]);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) Injector(com.google.inject.Injector) FilterChain(javax.servlet.FilterChain)

Example 15 with ServletResponse

use of javax.servlet.ServletResponse in project roboguice by roboguice.

the class ServletTest method testNewSessionObject.

public void testNewSessionObject() throws CreationException, IOException, ServletException {
    final Injector injector = createInjector();
    final HttpServletRequest request = newFakeHttpServletRequest();
    GuiceFilter filter = new GuiceFilter();
    final boolean[] invoked = new boolean[1];
    FilterChain filterChain = new FilterChain() {

        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            invoked[0] = true;
            assertNotNull(injector.getInstance(InSession.class));
            assertNull(injector.getInstance(IN_SESSION_NULL_KEY));
        }
    };
    filter.doFilter(request, null, filterChain);
    assertTrue(invoked[0]);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) Injector(com.google.inject.Injector) FilterChain(javax.servlet.FilterChain)

Aggregations

ServletResponse (javax.servlet.ServletResponse)155 ServletRequest (javax.servlet.ServletRequest)129 HttpServletResponse (javax.servlet.http.HttpServletResponse)104 HttpServletRequest (javax.servlet.http.HttpServletRequest)90 FilterChain (javax.servlet.FilterChain)81 Test (org.junit.Test)76 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 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)26 Injector (com.google.inject.Injector)25 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 FilterConfig (javax.servlet.FilterConfig)15 ErrorPage (org.springframework.boot.web.server.ErrorPage)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14