use of org.glassfish.jersey.server.model.ResourceModel in project graylog2-server by Graylog2.
the class PrefixAddingModelProcessorTest method processResourceModelWithHideOnCloudDoesNothing.
@Test
public void processResourceModelWithHideOnCloudDoesNothing() throws Exception {
final ImmutableMap<String, String> packagePrefixes = ImmutableMap.of(PACKAGE_NAME, "/test/prefix");
when(configuration.isCloud()).thenReturn(true);
final PrefixAddingModelProcessor modelProcessor = new PrefixAddingModelProcessor(packagePrefixes, configuration);
final ResourceModel originalResourceModel = new ResourceModel.Builder(false).addResource(Resource.from(TestResource.class)).addResource(Resource.from(HiddenTestResource.class)).build();
final ResourceModel resourceModel = modelProcessor.processResourceModel(originalResourceModel, new ResourceConfig());
assertThat(resourceModel.getResources()).hasSize(1);
final Resource resource = resourceModel.getResources().get(0);
assertThat(resource.getPath()).isEqualTo("/test/prefix/foobar/{test}");
}
use of org.glassfish.jersey.server.model.ResourceModel in project graylog2-server by Graylog2.
the class PrefixAddingModelProcessorTest method processResourceModelDoesNotAddPrefixToResourceClassInOtherPackage.
@Test
public void processResourceModelDoesNotAddPrefixToResourceClassInOtherPackage() throws Exception {
final ImmutableMap<String, String> packagePrefixes = ImmutableMap.of("org.example", "/test/prefix");
when(configuration.isCloud()).thenReturn(false);
final PrefixAddingModelProcessor modelProcessor = new PrefixAddingModelProcessor(packagePrefixes, configuration);
final ResourceModel originalResourceModel = new ResourceModel.Builder(false).addResource(Resource.from(TestResource.class)).build();
final ResourceModel resourceModel = modelProcessor.processResourceModel(originalResourceModel, new ResourceConfig());
assertThat(resourceModel.getResources()).hasSize(1);
final Resource resource = resourceModel.getResources().get(0);
assertThat(resource.getPath()).isEqualTo("/foobar/{test}");
}
use of org.glassfish.jersey.server.model.ResourceModel in project graylog2-server by Graylog2.
the class PrefixAddingModelProcessor method processResourceModel.
@Override
public ResourceModel processResourceModel(ResourceModel model, Configuration config) {
// Create new resource model.
final ResourceModel.Builder resourceModelBuilder = new ResourceModel.Builder(false);
for (final Resource resource : model.getResources()) {
for (Class<?> handlerClass : resource.getHandlerClasses()) {
final HideOnCloud hideOnCloud = handlerClass.getAnnotation(HideOnCloud.class);
if (hideOnCloud != null && configuration.isCloud()) {
break;
}
final String packageName = handlerClass.getPackage().getName();
final Optional<String> packagePrefix = packagePrefixes.entrySet().stream().filter(entry -> packageName.startsWith(entry.getKey())).sorted((o1, o2) -> -o1.getKey().compareTo(o2.getKey())).map(Map.Entry::getValue).findFirst();
if (packagePrefix.isPresent()) {
final String prefixedPath = prefixPath(packagePrefix.get(), resource.getPath());
final Resource newResource = Resource.builder(resource).path(prefixedPath).build();
resourceModelBuilder.addResource(newResource);
} else {
resourceModelBuilder.addResource(resource);
}
}
}
return resourceModelBuilder.build();
}
Aggregations