Search in sources :

Example 36 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class UrlRewriteRequest method getTargetUrl.

// Note: Source url was added to the request attributes by the GatewayFilter doFilter method.
private Template getTargetUrl() {
    boolean rewriteRequestUrl = true;
    Template targetUrl;
    if (rewriteRequestUrl) {
        targetUrl = (Template) getAttribute(AbstractGatewayFilter.TARGET_REQUEST_URL_ATTRIBUTE_NAME);
        if (targetUrl == null) {
            Template sourceUrl = getSourceUrl();
            targetUrl = rewriter.rewrite(this, sourceUrl, UrlRewriter.Direction.IN, urlRuleName);
            setAttribute(AbstractGatewayFilter.TARGET_REQUEST_URL_ATTRIBUTE_NAME, targetUrl);
        }
    } else {
        targetUrl = (Template) getAttribute(AbstractGatewayFilter.SOURCE_REQUEST_URL_ATTRIBUTE_NAME);
    }
    return targetUrl;
}
Also used : Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 37 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class FormUrlRewriteFilterReader method filterValue.

// TODO: Need to limit which values are attempted to be filtered by the name.
@Override
protected String filterValue(String name, String value, String rule) {
    try {
        Template input = Parser.parseLiteral(value);
        Template output = rewriter.rewrite(resolver, input, direction, rule);
        value = output.getPattern();
    } catch (URISyntaxException e) {
        LOG.failedToParseValueForUrlRewrite(value);
    }
    return value;
}
Also used : URISyntaxException(java.net.URISyntaxException) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 38 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class HtmlUrlRewriteFilterReader method filterValueString.

// TODO: Need to limit which values are attempted to be filtered by the name.
@Override
public String filterValueString(String name, String value, String rule) {
    try {
        Template input = Parser.parseLiteral(value);
        Template output = rewriter.rewrite(resolver, input, direction, rule);
        if (output != null) {
            value = output.getPattern();
        }
    } catch (URISyntaxException e) {
        LOG.failedToParseValueForUrlRewrite(value);
    }
    return value;
}
Also used : URISyntaxException(java.net.URISyntaxException) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 39 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class SecureQueryDecodeProcessorTest method testSimpleQueryDecode.

@Test
public void testSimpleQueryDecode() throws Exception {
    UrlRewriteEnvironment environment = new UrlRewriteEnvironment() {

        @Override
        public URL getResource(String name) throws IOException {
            return null;
        }

        @Override
        public <T> T getAttribute(String name) {
            return null;
        }

        @Override
        public List<String> resolve(String name) {
            return null;
        }
    };
    BASE64Encoder encoder = new BASE64Encoder();
    String encQuery = encoder.encode("test-query".getBytes("utf-8"));
    encQuery = encQuery.replaceAll("\\=", "");
    String inString = "http://host:0/root/path?_=" + encQuery;
    Template inTemplate = Parser.parseLiteral(inString);
    UrlRewriteContext context = EasyMock.createNiceMock(UrlRewriteContext.class);
    EasyMock.expect(context.getCurrentUrl()).andReturn(inTemplate);
    Capture<Template> outTemplate = new Capture<Template>();
    context.setCurrentUrl(EasyMock.capture(outTemplate));
    EasyMock.replay(context);
    SecureQueryDecodeDescriptor descriptor = new SecureQueryDecodeDescriptor();
    SecureQueryDecodeProcessor processor = new SecureQueryDecodeProcessor();
    processor.initialize(environment, descriptor);
    processor.process(context);
    String outActual = outTemplate.getValue().toString();
    assertThat(outActual, is("http://host:0/root/path?test-query"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) BASE64Encoder(sun.misc.BASE64Encoder) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) UrlRewriteContext(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext) Capture(org.easymock.Capture) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 40 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class SecureQueryDecodeProcessorTest method testDecodeQueryWithNonEncodedParams.

@Test
public void testDecodeQueryWithNonEncodedParams() throws Exception {
    UrlRewriteEnvironment environment = new UrlRewriteEnvironment() {

        @Override
        public URL getResource(String name) throws IOException {
            return null;
        }

        @Override
        public <T> T getAttribute(String name) {
            return null;
        }

        @Override
        public List<String> resolve(String name) {
            return null;
        }
    };
    BASE64Encoder encoder = new BASE64Encoder();
    String inQuery = "test-query=test-value";
    String encQuery = encoder.encode(inQuery.getBytes("utf-8"));
    encQuery = encQuery.replaceAll("\\=", "");
    String inString = "http://host:0/root/path?_=" + encQuery + "&clear-param=clear-value";
    Template inTemplate = Parser.parseLiteral(inString);
    UrlRewriteContext context = EasyMock.createNiceMock(UrlRewriteContext.class);
    EasyMock.expect(context.getCurrentUrl()).andReturn(inTemplate);
    Capture<Template> outTemplate = new Capture<Template>();
    context.setCurrentUrl(EasyMock.capture(outTemplate));
    EasyMock.replay(context);
    SecureQueryDecodeDescriptor descriptor = new SecureQueryDecodeDescriptor();
    SecureQueryDecodeProcessor processor = new SecureQueryDecodeProcessor();
    processor.initialize(environment, descriptor);
    processor.process(context);
    String outActual = outTemplate.getValue().toString();
    assertThat(outActual, containsString("http://host:0/root/path?"));
    assertThat(outActual, containsString("test-query=test-value"));
    assertThat(outActual, containsString("clear-param=clear-value"));
    assertThat(outActual, not(containsString(encQuery)));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) BASE64Encoder(sun.misc.BASE64Encoder) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) UrlRewriteContext(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext) Capture(org.easymock.Capture) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Aggregations

Template (org.apache.knox.gateway.util.urltemplate.Template)50 Test (org.junit.Test)23 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)15 Resolver (org.apache.knox.gateway.util.urltemplate.Resolver)10 URISyntaxException (java.net.URISyntaxException)9 URL (java.net.URL)8 UrlRewriteProcessor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor)8 UrlRewriteRuleDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor)8 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)8 UrlRewriteActionRewriteDescriptorExt (org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 UrlRewriteContext (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext)6 GatewayServices (org.apache.knox.gateway.services.GatewayServices)5 Capture (org.easymock.Capture)5 Host (org.apache.knox.gateway.util.urltemplate.Host)4 Matcher (org.apache.knox.gateway.util.urltemplate.Matcher)4 Query (org.apache.knox.gateway.util.urltemplate.Query)4 URI (java.net.URI)3 UrlRewriteStepStatus (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)3