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"));
}
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;
}
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;
}
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);
}
}
}
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"));
}
Aggregations