@Test public void testEmptyHostmapUseCase() throws Exception { URL configUrl = TestUtils.getResourceUrl(this.getClass(), "empty-hostmap.txt"); UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class); EasyMock.expect(environment.getResource("/WEB-INF/hostmap.txt")) .andReturn(configUrl) .anyTimes(); Resolver resolver = EasyMock.createNiceMock(Resolver.class); EasyMock.expect(resolver.resolve("host")) .andReturn(Arrays.asList("test-inbound-host")) .anyTimes(); EasyMock.replay(environment, resolver); UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create(); UrlRewriteRuleDescriptor rule = descriptor.addRule("test-rule"); rule.pattern("{*}://{host}:{*}/{**}?{**}"); UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite"); rewrite.template("{*}://{$hostmap(host)}:{*}/{**}?{**}"); UrlRewriteProcessor rewriter = new UrlRewriteProcessor(); rewriter.initialize(environment, descriptor); Template input = Parser.parseLiteral( "test-scheme://test-inbound-host:42/test-path/test-file?test-name=test-value"); Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.IN, null); // System.out.println( output ); assertThat(output, notNullValue()); assertThat(output.getHost().getFirstValue().getPattern(), is("test-inbound-host")); }
@Override public void doFilter( final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = ((HttpServletRequest) request); StringBuffer sourceUrl = httpRequest.getRequestURL(); String queryString = httpRequest.getQueryString(); if (queryString != null) { sourceUrl.append("?"); sourceUrl.append(queryString); } try { request.setAttribute( AbstractGatewayFilter.SOURCE_REQUEST_URL_ATTRIBUTE_NAME, Parser.parse(sourceUrl.toString())); } catch (URISyntaxException e) { throw new ServletException(e); } try { Subject.doAs( subject, new PrivilegedExceptionAction<Void>() { @Override public Void run() throws Exception { chain.doFilter(request, response); return null; } }); } catch (PrivilegedActionException e) { throw new ServletException(e); } }
private String rewriteValue(UrlRewriter rewriter, String value, String rule) { try { Template input = Parser.parse(value); Template output = rewriter.rewrite(this, input, UrlRewriter.Direction.IN, rule); value = output.toString(); } catch (URISyntaxException e) { LOG.failedToParseValueForUrlRewrite(value); } return value; }
@Test public void testBasicUseCase() throws Exception { URL configUrl = TestUtils.getResourceUrl(this.getClass(), "hostmap.txt"); HostMapper hm = EasyMock.createNiceMock(HostMapper.class); EasyMock.expect(hm.resolveInboundHostName("test-inbound-host")) .andReturn("test-inbound-rewritten-host") .anyTimes(); HostMapperService hms = EasyMock.createNiceMock(HostMapperService.class); GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class); EasyMock.expect(gatewayServices.getService(GatewayServices.HOST_MAPPING_SERVICE)) .andReturn(hms) .anyTimes(); UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class); EasyMock.expect(environment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)) .andReturn(gatewayServices) .anyTimes(); EasyMock.expect(environment.resolve("cluster.name")) .andReturn(Arrays.asList("test-cluster-name")) .anyTimes(); EasyMock.expect(environment.getResource("/WEB-INF/hostmap.txt")) .andReturn(configUrl) .anyTimes(); Resolver resolver = EasyMock.createNiceMock(Resolver.class); EasyMock.expect(resolver.resolve("host")) .andReturn(Arrays.asList("test-inbound-host")) .anyTimes(); EasyMock.replay(gatewayServices, hm, hms, environment, resolver); UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create(); UrlRewriteRuleDescriptor rule = descriptor.addRule("test-rule"); rule.pattern("{*}://{host}:{*}/{**}?{**}"); UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite"); rewrite.template("{*}://{$hostmap(host)}:{*}/{**}?{**}"); UrlRewriteProcessor rewriter = new UrlRewriteProcessor(); rewriter.initialize(environment, descriptor); Template input = Parser.parseLiteral( "test-scheme://test-inbound-host:42/test-path/test-file?test-name=test-value"); Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.IN, null); // System.out.println( output ); assertThat(output, notNullValue()); assertThat(output.getHost().getFirstValue().getPattern(), is("test-inbound-rewritten-host")); }
private Template getSourceUrl() { Template urlTemplate = null; StringBuffer urlString = super.getRequestURL(); String queryString = super.getQueryString(); if (queryString != null) { urlString.append('?'); urlString.append(queryString); } try { urlTemplate = Parser.parse(urlString.toString()); } catch (URISyntaxException e) { LOG.failedToParseValueForUrlRewrite(urlString.toString()); // Shouldn't be possible given that the URL is constructed from parts of an existing URL. urlTemplate = null; } return urlTemplate; }
@Test public void testQueryToPathRewriteWithFunction() throws Exception { URL configUrl = TestUtils.getResourceUrl(this.getClass(), "hdfs-hostmap.txt"); UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class); EasyMock.expect(environment.getResource("/WEB-INF/hostmap.txt")) .andReturn(configUrl) .anyTimes(); Resolver resolver = EasyMock.createNiceMock(Resolver.class); EasyMock.expect(resolver.resolve("host")) .andReturn(Arrays.asList("test-internal-host")) .anyTimes(); EasyMock.replay(environment, resolver); UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create(); UrlRewriteRuleDescriptor rule = descriptor.addRule("test-rule"); rule.pattern("{*}://{host}:{*}/{**}?{qp1}&{qp2}&{**}"); UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite"); rewrite.template("{*}://test-static-host:{*}/{qp1}/{qp2}/{**}?server={$hostmap(host)}&{**}"); UrlRewriteProcessor rewriter = new UrlRewriteProcessor(); rewriter.initialize(environment, descriptor); Template input = Parser.parseLiteral( "test-scheme://test-external-host:42/test-path/test-file?qp1=qp1-val&qp2=qp2-val&test-name-1=test-value-1&test-name-2=test-value-2"); Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.OUT, "test-rule"); // System.out.println( output ); assertThat(output, notNullValue()); assertThat(output.getHost().getFirstValue().getPattern(), is("test-static-host")); assertThat( output.getQuery().get("server").getFirstValue().getPattern(), is("test-external-host")); assertThat(output.getQuery().get("server").getValues().size(), is(1)); assertThat( output.getQuery().get("test-name-1").getFirstValue().getPattern(), is("test-value-1")); assertThat(output.getQuery().get("test-name-1").getValues().size(), is(1)); assertThat( output.getQuery().get("test-name-2").getFirstValue().getPattern(), is("test-value-2")); assertThat(output.getQuery().get("test-name-2").getValues().size(), is(1)); assertThat(output.getQuery().size(), is(3)); }
public void doGet(URI url, HttpServletRequest request, HttpServletResponse response) throws IOException, URISyntaxException { String sourcePathInfo = request.getPathInfo(); String sourcePattern = getConfig().getInitParameter("pattern"); String targetPattern = getConfig().getInitParameter("target"); // TODO: Some of the compilation should be done at servlet init for performance reasons. Template sourceTemplate = Parser.parseTemplate(sourcePattern); Template targetTemplate = Parser.parseTemplate(targetPattern); Resolver resolver = new DispatchParamResolver(getConfig(), request); URI sourceUri = new URI(sourcePathInfo); URI targetUri = Rewriter.rewrite(sourceUri, sourceTemplate, targetTemplate, resolver, null); // //TODO: This should be more at filter init. // Pattern sourceRegex = UrlRewriter.compileUrlRegex( sourcePattern ); // Matcher matcher = sourceRegex.matcher( sourcePathInfo ); // String targetUrl = MessageFormat.format( targetPattern, Regex.toGroupArray( matcher ) ); // System.out.println( "Source URI: " + expect.getRequestURI() ); // System.out.println( "Source URL: " + expect.getRequestURL() ); // System.out.println( "Source Query: " + expect.getQueryString() ); // System.out.println( "Source pathInfo: " + sourcePathInfo ); // System.out.println( "Source pattern: " + sourcePattern ); // System.out.println( "Target pattern: " + targetPattern ); // System.out.println( "Resolved target: " + targetUrl ); StringBuilder paramStr = new StringBuilder(); Enumeration paramNames = request.getParameterNames(); if (paramNames.hasMoreElements()) { paramStr.append("?"); } while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); String paramValue = request.getParameter(paramName); paramStr.append(paramName); paramStr.append("="); paramStr.append(URLEncoder.encode(paramValue, "UTF-8")); if (paramNames.hasMoreElements()) { paramStr.append("&"); } } String urlStr = targetUri.toString() + paramStr.toString(); try { URL clientUrl = new URL(urlStr); // System.out.println( "Resolved query: " + clientUrl ); AuthenticatedURL.Token token = new AuthenticatedURL.Token(); KerberosAuthenticator authenticator = new KerberosAuthenticator(); auditor.audit(Action.DISPATCH, urlStr, ResourceType.URI, ActionOutcome.UNAVAILABLE); HttpURLConnection conn = new AuthenticatedURL(authenticator).openConnection(clientUrl, token); // System.out.println( "STATUS=" + conn.getResponseCode() ); InputStream input = conn.getInputStream(); if (input != null) { OutputStream output = response.getOutputStream(); try { IOUtils.copy(input, output); } finally { output.flush(); input.close(); } } auditor.audit(Action.DISPATCH, urlStr, ResourceType.URI, ActionOutcome.SUCCESS); } catch (AuthenticationException e) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED); LOG.failedToEstablishConnectionToUrl(urlStr, e); auditor.audit( Action.DISPATCH, urlStr, ResourceType.URI, ActionOutcome.FAILURE, RES.responseStatus(HttpServletResponse.SC_UNAUTHORIZED)); } catch (FileNotFoundException e) { response.sendError(HttpServletResponse.SC_NOT_FOUND); LOG.failedToEstablishConnectionToUrl(urlStr, e); auditor.audit( Action.DISPATCH, urlStr, ResourceType.URI, ActionOutcome.FAILURE, RES.responseStatus(HttpServletResponse.SC_NOT_FOUND)); } }