Search in sources :

Example 96 with ServletResponse

use of javax.servlet.ServletResponse in project hadoop by apache.

the class TestNMWebFilter method testRedirection.

@Test(timeout = 5000)
public void testRedirection() throws Exception {
    ApplicationId appId = ApplicationId.newInstance(System.currentTimeMillis(), 1);
    ApplicationAttemptId attemptId = ApplicationAttemptId.newInstance(appId, 1);
    ContainerId containerId = ContainerId.newContainerId(attemptId, 1);
    NMContext mockNMContext = mock(NMContext.class);
    ConcurrentMap<ApplicationId, Application> applications = new ConcurrentHashMap<>();
    when(mockNMContext.getApplications()).thenReturn(applications);
    LocalDirsHandlerService mockLocalDirsHandlerService = mock(LocalDirsHandlerService.class);
    Configuration conf = new Configuration();
    conf.setBoolean(YarnConfiguration.LOG_AGGREGATION_ENABLED, true);
    conf.set(YarnConfiguration.YARN_LOG_SERVER_URL, "http://" + LOG_SERVER_URI);
    when(mockLocalDirsHandlerService.getConfig()).thenReturn(conf);
    when(mockNMContext.getLocalDirsHandler()).thenReturn(mockLocalDirsHandlerService);
    NodeId nodeId = NodeId.newInstance("testNM", 9999);
    when(mockNMContext.getNodeId()).thenReturn(nodeId);
    Injector mockInjector = mock(Injector.class);
    NMWebAppFilter testFilter = new NMWebAppFilter(mockInjector, mockNMContext);
    HttpServletResponseForTest response = new HttpServletResponseForTest();
    // dummy filter
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
        // Do Nothing
        }
    };
    String uri = "testNM:8042/node/containerlogs/" + containerId.toString() + "/" + USER;
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getRequestURI()).thenReturn(uri);
    testFilter.doFilter(request, response, chain);
    assertEquals(HttpServletResponse.SC_TEMPORARY_REDIRECT, response.status);
    String redirect = response.getHeader("Location");
    assertTrue(redirect.contains(LOG_SERVER_URI));
    assertTrue(redirect.contains(nodeId.toString()));
    assertTrue(redirect.contains(containerId.toString()));
    assertTrue(redirect.contains(USER));
    String logType = "syslog";
    uri = "testNM:8042/node/containerlogs/" + containerId.toString() + "/" + USER + "/" + logType + "/?start=10";
    HttpServletRequest request2 = mock(HttpServletRequest.class);
    when(request2.getRequestURI()).thenReturn(uri);
    when(request2.getQueryString()).thenReturn("start=10");
    testFilter.doFilter(request2, response, chain);
    assertEquals(HttpServletResponse.SC_TEMPORARY_REDIRECT, response.status);
    redirect = response.getHeader("Location");
    assertTrue(redirect.contains(LOG_SERVER_URI));
    assertTrue(redirect.contains(nodeId.toString()));
    assertTrue(redirect.contains(containerId.toString()));
    assertTrue(redirect.contains(USER));
    assertTrue(redirect.contains(logType));
    assertTrue(redirect.contains("start=10"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) Configuration(org.apache.hadoop.conf.Configuration) FilterChain(javax.servlet.FilterChain) ApplicationAttemptId(org.apache.hadoop.yarn.api.records.ApplicationAttemptId) LocalDirsHandlerService(org.apache.hadoop.yarn.server.nodemanager.LocalDirsHandlerService) HttpServletRequest(javax.servlet.http.HttpServletRequest) NMContext(org.apache.hadoop.yarn.server.nodemanager.NodeManager.NMContext) ContainerId(org.apache.hadoop.yarn.api.records.ContainerId) Injector(com.google.inject.Injector) NodeId(org.apache.hadoop.yarn.api.records.NodeId) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Application(org.apache.hadoop.yarn.server.nodemanager.containermanager.application.Application) Test(org.junit.Test)

Example 97 with ServletResponse

use of javax.servlet.ServletResponse in project newts by OpenNMS.

the class NewtsService method configureUIRedirect.

private void configureUIRedirect(Environment environment) {
    environment.servlets().addFilter("TrailingSlashRedirect", new Filter() {

        @Override
        public void init(FilterConfig cfg) throws ServletException {
            LOG.info("Initializing redirect filter");
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            StringBuffer url = ((HttpServletRequest) request).getRequestURL();
            String path = new URL(url.toString()).getPath();
            if (path.endsWith(UI_URL_PATH)) {
                LOG.debug("Caught request to malformed URL {}, redirecting...", UI_URL_PATH);
                ((HttpServletResponse) response).sendRedirect(String.format("%s/", url.toString()));
            } else {
                chain.doFilter(request, response);
            }
        }

        @Override
        public void destroy() {
        }
    }).addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) Filter(javax.servlet.Filter) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterConfig(javax.servlet.FilterConfig) URL(java.net.URL)

Example 98 with ServletResponse

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

the class RedirectFilter method doFilter.

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    String path = extractPath(request);
    Predicate<Redirect> match = redirect -> redirect.test(path);
    List<Redirect> redirects = REDIRECTS.stream().filter(match).collect(Collectors.toList());
    switch(redirects.size()) {
        case 0:
            chain.doFilter(request, response);
            break;
        case 1:
            response.sendRedirect(redirects.get(0).apply(request));
            break;
        default:
            throw new IllegalStateException(format("Multiple redirects have been found for '%s'", path));
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterChain(javax.servlet.FilterChain) ServletRequest(javax.servlet.ServletRequest) ServletException(javax.servlet.ServletException) Predicate(java.util.function.Predicate) HttpServletResponse(javax.servlet.http.HttpServletResponse) Collectors(org.sonar.core.util.stream.Collectors) IOException(java.io.IOException) Function(java.util.function.Function) String.format(java.lang.String.format) List(java.util.List) HttpServletRequest(javax.servlet.http.HttpServletRequest) ImmutableList(com.google.common.collect.ImmutableList) ServletResponse(javax.servlet.ServletResponse) FilterConfig(javax.servlet.FilterConfig) Filter(javax.servlet.Filter) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 99 with ServletResponse

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

the class MasterServletFilterTest method should_keep_filter_ordering.

@Test
public void should_keep_filter_ordering() throws Exception {
    TrueFilter filter1 = new TrueFilter();
    TrueFilter filter2 = new TrueFilter();
    MasterServletFilter filters = new MasterServletFilter();
    filters.init(mock(FilterConfig.class), asList(filter1, filter2));
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getRequestURI()).thenReturn("/foo/bar");
    when(request.getContextPath()).thenReturn("");
    ServletResponse response = mock(HttpServletResponse.class);
    FilterChain chain = mock(FilterChain.class);
    filters.doFilter(request, response, chain);
    assertThat(filter1.count).isEqualTo(1);
    assertThat(filter2.count).isEqualTo(2);
}
Also used : 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 100 with ServletResponse

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

the class ServletTest method testRequestAndResponseBindings.

public void testRequestAndResponseBindings() throws Exception {
    final Injector injector = createInjector();
    final HttpServletRequest request = newFakeHttpServletRequest();
    final HttpServletResponse response = newFakeHttpServletResponse();
    final boolean[] invoked = new boolean[1];
    GuiceFilter filter = new GuiceFilter();
    FilterChain filterChain = new FilterChain() {

        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            invoked[0] = true;
            assertSame(request, servletRequest);
            assertSame(request, injector.getInstance(ServletRequest.class));
            assertSame(request, injector.getInstance(HTTP_REQ_KEY));
            assertSame(response, servletResponse);
            assertSame(response, injector.getInstance(ServletResponse.class));
            assertSame(response, injector.getInstance(HTTP_RESP_KEY));
            assertSame(servletRequest.getParameterMap(), injector.getInstance(REQ_PARAMS_KEY));
        }
    };
    filter.doFilter(request, response, 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) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse)

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