Search in sources :

Example 6 with Resource

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

the class RuntimeLocatorModelBuilder method buildRouting.

private LocatorRouting buildRouting(final Resource subResource) {
    final ResourceModel model = new ResourceModel.Builder(true).addResource(subResource).build();
    final ResourceModel enhancedModel = enhance(model);
    if (!disableValidation) {
        validateResource(enhancedModel);
    }
    final Resource enhancedLocator = enhancedModel.getResources().get(0);
    for (final Class<?> handlerClass : enhancedLocator.getHandlerClasses()) {
        resourceContext.bindResource(handlerClass);
    }
    return new LocatorRouting(enhancedModel, runtimeModelBuilder.buildModel(enhancedModel.getRuntimeResourceModel(), true));
}
Also used : CacheBuilder(org.glassfish.jersey.internal.guava.CacheBuilder) Resource(org.glassfish.jersey.server.model.Resource) ResourceModel(org.glassfish.jersey.server.model.ResourceModel)

Example 7 with Resource

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

the class RuntimeModelBuilder method createResourceMethodRouters.

private List<MethodRouting> createResourceMethodRouters(final RuntimeResource runtimeResource, final boolean subResourceMode) {
    final List<MethodRouting> methodRoutings = new ArrayList<>();
    int i = 0;
    for (final Resource resource : runtimeResource.getResources()) {
        final Resource parentResource = runtimeResource.getParent() == null ? null : runtimeResource.getParentResources().get(i++);
        final UriTemplate template = resource.getPathPattern().getTemplate();
        final PushMatchedTemplateRouter templateRouter = parentResource == null ? getTemplateRouter(subResourceMode, template, null) : getTemplateRouter(subResourceMode, parentResource.getPathPattern().getTemplate(), template);
        for (final ResourceMethod resourceMethod : resource.getResourceMethods()) {
            methodRoutings.add(new MethodRouting(resourceMethod, templateRouter, new PushMatchedMethodRouter(resourceMethod), createMethodRouter(resourceMethod)));
        }
    }
    return methodRoutings.isEmpty() ? Collections.<MethodRouting>emptyList() : methodRoutings;
}
Also used : ArrayList(java.util.ArrayList) Resource(org.glassfish.jersey.server.model.Resource) RuntimeResource(org.glassfish.jersey.server.model.RuntimeResource) UriTemplate(org.glassfish.jersey.uri.UriTemplate) Endpoint(org.glassfish.jersey.server.internal.process.Endpoint) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 8 with Resource

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

the class ExtendedWadlWebappTest method configureDeployment.

@Override
protected DeploymentContext configureDeployment() {
    final ResourceConfig resourceConfig = new ResourceConfig(new MyApplication().getClasses());
    resourceConfig.property(ServerProperties.WADL_GENERATOR_CONFIG, "org.glassfish.jersey.examples.extendedwadl" + ".SampleWadlGeneratorConfig");
    final Resource.Builder resourceBuilder = Resource.builder();
    resourceBuilder.name("resource-programmatic").path("programmatic").addMethod("GET").handledBy(new ProgrammaticResource());
    resourceConfig.registerResources(resourceBuilder.build());
    return DeploymentContext.builder(resourceConfig).contextPath("extended-wadl-webapp").build();
}
Also used : Resource(org.glassfish.jersey.server.model.Resource) MyApplication(org.glassfish.jersey.examples.extendedwadl.resources.MyApplication) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 9 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 10 with Resource

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

the class ModelProcessorUtil method enhanceResource.

/**
     * Enhance the runtime resource referenced by {@code resource} parameter with a list of additional methods.
     *
     * The new {@code methods} are added to the runtime resource. In case of method conflicts, the existing resource methods
     * will be preserved and will not be 'overridden' by any new method from the {@code methods} list.
     * Overriding check takes into account media types of methods so that new resource methods with same HTTP method
     * can be defined only for a more more specific media type.
     *
     * @param resource Runtime resource to be enhanced.
     * @param enhancedModelBuilder Builder for the enhanced resource model to be used.
     * @param methods List of enhancing methods.
     * @param extended This flag will initialize the property
     *                  {@link org.glassfish.jersey.server.model.ResourceMethod#isExtended()}.
     */
public static void enhanceResource(RuntimeResource resource, ResourceModel.Builder enhancedModelBuilder, List<Method> methods, boolean extended) {
    final Resource firstResource = resource.getResources().get(0);
    if (methodsSuitableForResource(firstResource, methods)) {
        for (Method method : methods) {
            final Set<MediaType> produces = new HashSet<>(method.produces);
            for (ResourceMethod resourceMethod : resource.getResourceMethods()) {
                for (final MediaType produce : method.produces) {
                    if (ModelProcessorUtil.isMethodOverridden(resourceMethod, method.httpMethod, method.consumes.get(0), produce)) {
                        produces.remove(produce);
                    }
                }
            }
            if (!produces.isEmpty()) {
                final Resource parentResource = resource.getParentResources().get(0);
                if (parentResource != null && method.path != null) {
                    continue;
                }
                final Resource.Builder resourceBuilder = Resource.builder(firstResource.getPath());
                final Resource.Builder builder = method.path != null ? resourceBuilder.addChildResource(method.path) : resourceBuilder;
                final ResourceMethod.Builder methodBuilder = builder.addMethod(method.httpMethod).consumes(method.consumes).produces(produces);
                if (method.inflector != null) {
                    methodBuilder.handledBy(method.inflector);
                } else {
                    methodBuilder.handledBy(method.inflectorClass);
                }
                methodBuilder.extended(extended);
                final Resource newResource = resourceBuilder.build();
                if (parentResource != null) {
                    final Resource.Builder parentBuilder = Resource.builder(parentResource.getPath());
                    parentBuilder.addChildResource(newResource);
                    enhancedModelBuilder.addResource(parentBuilder.build());
                } else {
                    enhancedModelBuilder.addResource(newResource);
                }
            }
        }
    }
    for (RuntimeResource child : resource.getChildRuntimeResources()) {
        enhanceResource(child, enhancedModelBuilder, methods, extended);
    }
}
Also used : Resource(org.glassfish.jersey.server.model.Resource) RuntimeResource(org.glassfish.jersey.server.model.RuntimeResource) MediaType(javax.ws.rs.core.MediaType) HttpMethod(javax.ws.rs.HttpMethod) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) HashSet(java.util.HashSet) RuntimeResource(org.glassfish.jersey.server.model.RuntimeResource)

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