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"));
}
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, "/*");
}
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));
}
}
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);
}
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]);
}
Aggregations