Search in sources :

Example 1 with UrlRewriteFunctionProcessor

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor in project knox by apache.

the class InboundUrlFunctionProcessorTest method testServiceResolve.

@Test
public void testServiceResolve() throws Exception {
    ServiceLoader loader = ServiceLoader.load(UrlRewriteFunctionProcessor.class);
    Iterator iterator = loader.iterator();
    assertThat("Service iterator empty.", iterator.hasNext());
    InboundUrlFunctionProcessor proc = null;
    while (iterator.hasNext()) {
        Object object = iterator.next();
        if (object instanceof InboundUrlFunctionProcessor) {
            proc = (InboundUrlFunctionProcessor) object;
        }
    }
    if (proc == null)
        fail("Failed to find " + InboundUrlFunctionProcessor.class.getName() + " via service loader.");
    Map<String, UrlRewriteFunctionProcessor> functions = new HashMap<>();
    UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    UrlRewriter.Direction direction = UrlRewriter.Direction.OUT;
    List<String> parameters = Collections.singletonList("host");
    Template template = Parser.parseLiteral("https://localhost:8443/gateway/default/datanode/?host=http://foo:50075");
    UrlRewriteContextImpl ctx = new UrlRewriteContextImpl(environment, this.getRewriteResponse(), functions, direction, template);
    List<String> result = proc.resolve(ctx, parameters);
    assertThat(result.get(0), is("http://foo:50075"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) UrlRewriteFunctionProcessor(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor) Template(org.apache.knox.gateway.util.urltemplate.Template) UrlRewriter(org.apache.knox.gateway.filter.rewrite.api.UrlRewriter) UrlRewriteContextImpl(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteContextImpl) Test(org.junit.Test)

Example 2 with UrlRewriteFunctionProcessor

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor in project knox by apache.

the class UrlRewriteFunctionProcessorFactory method loadProcessors.

private static Map<Class<? extends UrlRewriteFunctionDescriptor>, Map<String, Class<? extends UrlRewriteFunctionProcessor>>> loadProcessors() {
    Map<Class<? extends UrlRewriteFunctionDescriptor>, Map<String, Class<? extends UrlRewriteFunctionProcessor>>> descriptorMap = new HashMap<>();
    ServiceLoader<UrlRewriteFunctionProcessor> processors = ServiceLoader.load(UrlRewriteFunctionProcessor.class);
    for (UrlRewriteFunctionProcessor processor : processors) {
        Class<? extends UrlRewriteFunctionDescriptor> descriptorInterface = getDescriptorInterface(processor);
        Map<String, Class<? extends UrlRewriteFunctionProcessor>> typeMap = descriptorMap.get(descriptorInterface);
        if (typeMap == null) {
            typeMap = new HashMap<>();
            descriptorMap.put(descriptorInterface, typeMap);
        }
        String functionName = processor.name();
        typeMap.put(functionName, processor.getClass());
    }
    return descriptorMap;
}
Also used : UrlRewriteFunctionProcessor(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor) HashMap(java.util.HashMap) UrlRewriteFunctionDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with UrlRewriteFunctionProcessor

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor in project knox by apache.

the class UrlRewriteFunctionProcessorFactory method create.

public static UrlRewriteFunctionProcessor create(String name, UrlRewriteFunctionDescriptor descriptor) throws IllegalAccessException, InstantiationException {
    UrlRewriteFunctionProcessor processor;
    if (descriptor == null) {
        descriptor = UrlRewriteFunctionDescriptorFactory.create(name);
    }
    Map<String, Class<? extends UrlRewriteFunctionProcessor>> typeMap;
    typeMap = MAP.get(descriptor.getClass());
    if (typeMap == null) {
        Class<? extends UrlRewriteFunctionDescriptor> descriptorInterface = getDescriptorInterface(descriptor);
        typeMap = MAP.get(descriptorInterface);
    }
    if (typeMap == null) {
        throw new IllegalArgumentException(descriptor.getClass().getName());
    } else {
        Class<? extends UrlRewriteFunctionProcessor> processorClass = typeMap.get(name);
        if (processorClass == null) {
            throw new IllegalArgumentException(name);
        } else {
            processor = processorClass.newInstance();
        }
    }
    return processor;
}
Also used : UrlRewriteFunctionProcessor(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor)

Example 4 with UrlRewriteFunctionProcessor

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor in project knox by apache.

the class UrlRewriteProcessor method initializeFunctions.

@SuppressWarnings("unchecked")
private void initializeFunctions(UrlRewriteRulesDescriptor rules) {
    for (String name : UrlRewriteFunctionDescriptorFactory.getNames()) {
        try {
            UrlRewriteFunctionDescriptor descriptor = rules.getFunction(name);
            UrlRewriteFunctionProcessor processor = UrlRewriteFunctionProcessorFactory.create(name, descriptor);
            processor.initialize(environment, descriptor);
            functions.put(name, processor);
        } catch (Exception e) {
            // Ignore it and it won't be available as a function.
            LOG.failedToInitializeRewriteFunctions(e);
        }
    }
}
Also used : UrlRewriteFunctionProcessor(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor)

Example 5 with UrlRewriteFunctionProcessor

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor in project knox by apache.

the class UrlRewriteContextImplTest method testResolve.

@Test
public void testResolve() throws Exception {
    UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(environment.resolve("test-env-param-name")).andReturn(Arrays.asList("test-env-param-value")).anyTimes();
    Resolver resolver = EasyMock.createNiceMock(Resolver.class);
    EasyMock.expect(resolver.resolve("test-ctx-param-name")).andReturn(Arrays.asList("test-ctx-param-value"));
    EasyMock.replay(environment, resolver);
    Map<String, UrlRewriteFunctionProcessor> functions = new HashMap<>();
    UrlRewriter.Direction direction = UrlRewriter.Direction.OUT;
    Template template = Parser.parseLiteral("scheme://host:port/dir/file");
    UrlRewriteContextImpl context = new UrlRewriteContextImpl(environment, resolver, functions, direction, template);
    Params params = context.getParameters();
    List<String> values = params.resolve("test-env-param-name");
    assertThat(values, notNullValue());
    assertThat(values.size(), is(1));
    assertThat(values.get(0), is("test-env-param-value"));
    values = params.resolve("test-ctx-param-name");
    assertThat(values, notNullValue());
    assertThat(values.size(), is(1));
    assertThat(values.get(0), is("test-ctx-param-value"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) UrlRewriteFunctionProcessor(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) HashMap(java.util.HashMap) UrlRewriter(org.apache.knox.gateway.filter.rewrite.api.UrlRewriter) Params(org.apache.knox.gateway.util.urltemplate.Params) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Aggregations

UrlRewriteFunctionProcessor (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor)5 HashMap (java.util.HashMap)2 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)2 UrlRewriter (org.apache.knox.gateway.filter.rewrite.api.UrlRewriter)2 Template (org.apache.knox.gateway.util.urltemplate.Template)2 Test (org.junit.Test)2 Map (java.util.Map)1 UrlRewriteFunctionDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor)1 UrlRewriteContextImpl (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteContextImpl)1 Params (org.apache.knox.gateway.util.urltemplate.Params)1 Resolver (org.apache.knox.gateway.util.urltemplate.Resolver)1