Search in sources :

Example 21 with Resource

use of org.glassfish.jersey.server.model.Resource in project jersey by jersey.

the class App method create.

/**
     * Create example application resource configuration.
     *
     * @return initialized resource configuration of the example application.
     */
public static ResourceConfig create() {
    final Resource.Builder resourceBuilder = Resource.builder(ROOT_PATH);
    resourceBuilder.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext data) {
            getMethodCalled = true;
            return Response.ok("Hello World!").build();
        }
    });
    Inflector<ContainerRequestContext, Response> noContentResponder = new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext data) {
            headMethodCalled = true;
            return Response.noContent().build();
        }
    };
    resourceBuilder.addMethod("HEAD").handledBy(noContentResponder);
    resourceBuilder.addMethod("OPTIONS").handledBy(noContentResponder);
    return new ResourceConfig().registerResources(resourceBuilder.build());
}
Also used : Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Inflector(org.glassfish.jersey.process.Inflector) Resource(org.glassfish.jersey.server.model.Resource) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 22 with Resource

use of org.glassfish.jersey.server.model.Resource in project graylog2-server by Graylog2.

the class RestTools method getPathFromResource.

public static String getPathFromResource(Resource resource) {
    String path = resource.getPath();
    Resource parent = resource.getParent();
    while (parent != null) {
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        path = parent.getPath() + path;
        parent = parent.getParent();
    }
    return path;
}
Also used : Resource(org.glassfish.jersey.server.model.Resource)

Example 23 with Resource

use of org.glassfish.jersey.server.model.Resource in project graylog2-server by Graylog2.

the class PrefixAddingModelProcessorTest method processResourceModelAddsPrefixToResourceClassInCorrectSubPackage.

@Test
public void processResourceModelAddsPrefixToResourceClassInCorrectSubPackage() throws Exception {
    final ImmutableMap<String, String> packagePrefixes = ImmutableMap.of("org", "/generic", "org.graylog2", "/test/prefix", "org.graylog2.wrong", "/wrong");
    final PrefixAddingModelProcessor modelProcessor = new PrefixAddingModelProcessor(packagePrefixes);
    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("/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 24 with Resource

use of org.glassfish.jersey.server.model.Resource in project graylog2-server by Graylog2.

the class PrintModelProcessor method processResourceModel.

@Override
public ResourceModel processResourceModel(ResourceModel resourceModel, Configuration configuration) {
    LOG.debug("Map for resource model <" + resourceModel + ">:");
    final List<Resource> resources = new ArrayList<>();
    for (Resource resource : resourceModel.getResources()) {
        resources.add(resource);
        resources.addAll(findChildResources(resource));
    }
    logResources(resources);
    return resourceModel;
}
Also used : ArrayList(java.util.ArrayList) Resource(org.glassfish.jersey.server.model.Resource)

Example 25 with Resource

use of org.glassfish.jersey.server.model.Resource in project graylog2-server by Graylog2.

the class PrintModelProcessor method logResources.

private void logResources(List<Resource> resources) {
    final List<ResourceDescription> resourceDescriptions = new ArrayList<>();
    for (Resource resource : resources) {
        for (ResourceMethod resourceMethod : resource.getAllMethods()) {
            final String path = RestTools.getPathFromResource(resource);
            resourceDescriptions.add(new ResourceDescription(resourceMethod.getHttpMethod(), path, resource.getHandlerClasses()));
        }
    }
    Collections.sort(resourceDescriptions);
    for (ResourceDescription resource : resourceDescriptions) {
        LOG.debug(resource.toString());
    }
}
Also used : ArrayList(java.util.ArrayList) Resource(org.glassfish.jersey.server.model.Resource) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Aggregations

Resource (org.glassfish.jersey.server.model.Resource)44 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)12 ResourceModel (org.glassfish.jersey.server.model.ResourceModel)12 Test (org.junit.Test)10 ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)9 ArrayList (java.util.ArrayList)7 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)7 Response (javax.ws.rs.core.Response)6 HashSet (java.util.HashSet)4 RuntimeResource (org.glassfish.jersey.server.model.RuntimeResource)4 Inflector (org.glassfish.jersey.process.Inflector)3 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)3 Type (java.lang.reflect.Type)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 Configuration (javax.ws.rs.core.Configuration)2 ComponentBag (org.glassfish.jersey.model.internal.ComponentBag)2 ModelProcessor (org.glassfish.jersey.server.model.ModelProcessor)2 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)1