Search in sources :

Example 11 with ResourceModel

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}");
}
Also used : Resource(org.glassfish.jersey.server.model.Resource) ResourceModel(org.glassfish.jersey.server.model.ResourceModel) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 12 with ResourceModel

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}");
}
Also used : Resource(org.glassfish.jersey.server.model.Resource) ResourceModel(org.glassfish.jersey.server.model.ResourceModel) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 13 with ResourceModel

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();
}
Also used : HideOnCloud(org.graylog2.shared.rest.HideOnCloud) Provider(javax.ws.rs.ext.Provider) ImmutableMap(com.google.common.collect.ImmutableMap) Configuration(javax.ws.rs.core.Configuration) Map(java.util.Map) ResourceModel(org.glassfish.jersey.server.model.ResourceModel) Optional(java.util.Optional) ModelProcessor(org.glassfish.jersey.server.model.ModelProcessor) Resource(org.glassfish.jersey.server.model.Resource) Resource(org.glassfish.jersey.server.model.Resource) ResourceModel(org.glassfish.jersey.server.model.ResourceModel) HideOnCloud(org.graylog2.shared.rest.HideOnCloud) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Aggregations

ResourceModel (org.glassfish.jersey.server.model.ResourceModel)13 Resource (org.glassfish.jersey.server.model.Resource)12 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)5 Test (org.junit.Test)5 RuntimeResource (org.glassfish.jersey.server.model.RuntimeResource)3 ImmutableMap (com.google.common.collect.ImmutableMap)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Configuration (javax.ws.rs.core.Configuration)1 GenericType (javax.ws.rs.core.GenericType)1 Provider (javax.ws.rs.ext.Provider)1 CacheBuilder (org.glassfish.jersey.internal.guava.CacheBuilder)1 Ref (org.glassfish.jersey.internal.util.collection.Ref)1 MessageBodyWorkers (org.glassfish.jersey.message.MessageBodyWorkers)1 ComponentBag (org.glassfish.jersey.model.internal.ComponentBag)1 Inflector (org.glassfish.jersey.process.Inflector)1