Search in sources :

Example 1 with RuntimeResource

use of org.glassfish.jersey.server.model.RuntimeResource 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 2 with RuntimeResource

use of org.glassfish.jersey.server.model.RuntimeResource 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)

Example 3 with RuntimeResource

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

the class ExtendedUriInfoTest method convertToString.

private static String convertToString(final List<RuntimeResource> matchedResources) {
    final StringBuilder sb = new StringBuilder();
    for (final RuntimeResource resource : matchedResources) {
        final String path = resource.getRegex();
        sb.append(path == null ? "<no-path>" : path);
        sb.append(";");
    }
    sb.setLength(sb.length() - 1);
    return sb.toString();
}
Also used : RuntimeResource(org.glassfish.jersey.server.model.RuntimeResource)

Example 4 with RuntimeResource

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

the class NaiveResourceMappingContext method buildMappings.

private void buildMappings() {
    if (mappings != null) {
        return;
    }
    mappings = new HashMap<>();
    erc.getResourceModel().accept(new ResourceModelVisitor() {

        Deque<PathPattern> stack = new LinkedList<>();

        private void processComponents(final ResourceModelComponent component) {
            final List<? extends ResourceModelComponent> components = component.getComponents();
            if (components != null) {
                for (final ResourceModelComponent rc : components) {
                    rc.accept(this);
                }
            }
        }

        @Override
        public void visitInvocable(final Invocable invocable) {
            processComponents(invocable);
        }

        @Override
        public void visitRuntimeResource(final RuntimeResource runtimeResource) {
            processComponents(runtimeResource);
        }

        @Override
        public void visitResourceModel(final ResourceModel resourceModel) {
            processComponents(resourceModel);
        }

        @Override
        public void visitResourceHandlerConstructor(final HandlerConstructor handlerConstructor) {
            processComponents(handlerConstructor);
        }

        @Override
        public void visitMethodHandler(final MethodHandler methodHandler) {
            processComponents(methodHandler);
        }

        @Override
        public void visitChildResource(final Resource resource) {
            visitResourceIntl(resource, false);
        }

        @Override
        public void visitResource(final Resource resource) {
            visitResourceIntl(resource, true);
        }

        private void visitResourceIntl(final Resource resource, final boolean isRoot) {
            try {
                stack.addLast(resource.getPathPattern());
                processComponents(resource);
                if (isRoot) {
                    Class likelyToBeRoot = null;
                    for (final Class next : resource.getHandlerClasses()) {
                        if (!(Inflector.class.isAssignableFrom(next))) {
                            likelyToBeRoot = next;
                        }
                    }
                    if (likelyToBeRoot != null) {
                        mappings.put(likelyToBeRoot, getMapping(getTemplate()));
                    }
                }
            } finally {
                stack.removeLast();
            }
        }

        @Override
        public void visitResourceMethod(final ResourceMethod resourceMethod) {
            if (resourceMethod.isExtended()) {
                return;
            }
            if (ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR.equals(resourceMethod.getType())) {
                if (resourceMethod.getInvocable() != null) {
                    final Invocable i = resourceMethod.getInvocable();
                    final Type type = i.getResponseType();
                    final StringBuilder template = getTemplate();
                    mappings.put((Class) type, getMapping(template));
                    // Process sub resources ?
                    Resource.Builder builder = Resource.builder(i.getRawResponseType());
                    if (builder == null) {
                        // for example in the case the return type of the sub resource locator is Object
                        builder = Resource.builder().path(resourceMethod.getParent().getPath());
                    }
                    final Resource subResource = builder.build();
                    visitChildResource(subResource);
                }
            }
            processComponents(resourceMethod);
        }

        private StringBuilder getTemplate() {
            final StringBuilder template = new StringBuilder();
            for (final PathPattern pp : stack) {
                final String ppTemplate = pp.getTemplate().getTemplate();
                final int tlength = template.length();
                if (tlength > 0) {
                    if (template.charAt(tlength - 1) == '/') {
                        if (ppTemplate.startsWith("/")) {
                            template.append(ppTemplate, 1, ppTemplate.length());
                        } else {
                            template.append(ppTemplate);
                        }
                    } else {
                        if (ppTemplate.startsWith("/")) {
                            template.append(ppTemplate);
                        } else {
                            template.append("/");
                            template.append(ppTemplate);
                        }
                    }
                } else {
                    template.append(ppTemplate);
                }
            }
            return template;
        }
    });
}
Also used : Inflector(org.glassfish.jersey.process.Inflector) Resource(org.glassfish.jersey.server.model.Resource) RuntimeResource(org.glassfish.jersey.server.model.RuntimeResource) ResourceModelComponent(org.glassfish.jersey.server.model.ResourceModelComponent) LinkedList(java.util.LinkedList) RuntimeResource(org.glassfish.jersey.server.model.RuntimeResource) Invocable(org.glassfish.jersey.server.model.Invocable) PathPattern(org.glassfish.jersey.uri.PathPattern) HandlerConstructor(org.glassfish.jersey.server.model.HandlerConstructor) MethodHandler(org.glassfish.jersey.server.model.MethodHandler) Type(java.lang.reflect.Type) ResourceModelVisitor(org.glassfish.jersey.server.model.ResourceModelVisitor) ResourceModel(org.glassfish.jersey.server.model.ResourceModel) List(java.util.List) LinkedList(java.util.LinkedList) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 5 with RuntimeResource

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

the class RuntimeModelBuilder method buildModel.

/**
     * Build a runtime model of routers based on the {@code resourceModel}.
     *
     * @param resourceModel   Resource model from which the runtime model should be built.
     * @param subResourceMode True if the {@code resourceModel} is a sub resource model returned from sub resource locator.
     * @return Root router of the router structure representing the resource model.
     */
public Router buildModel(final RuntimeResourceModel resourceModel, final boolean subResourceMode) {
    final List<RuntimeResource> runtimeResources = resourceModel.getRuntimeResources();
    final PushMatchedUriRouter uriPushingRouter = new PushMatchedUriRouter();
    PathMatchingRouterBuilder currentRouterBuilder = null;
    // route methods
    for (final RuntimeResource resource : runtimeResources) {
        final PushMatchedRuntimeResourceRouter resourcePushingRouter = new PushMatchedRuntimeResourceRouter(resource);
        // resource methods
        if (!resource.getResourceMethods().isEmpty()) {
            final List<MethodRouting> methodRoutings = createResourceMethodRouters(resource, subResourceMode);
            final Router methodSelectingRouter = new MethodSelectingRouter(workers, methodRoutings);
            if (subResourceMode) {
                currentRouterBuilder = startNextRoute(currentRouterBuilder, PathPattern.END_OF_PATH_PATTERN).to(resourcePushingRouter).to(methodSelectingRouter);
            } else {
                currentRouterBuilder = startNextRoute(currentRouterBuilder, PathPattern.asClosed(resource.getPathPattern())).to(uriPushingRouter).to(resourcePushingRouter).to(methodSelectingRouter);
            }
        }
        PathMatchingRouterBuilder srRoutedBuilder = null;
        if (!resource.getChildRuntimeResources().isEmpty()) {
            for (final RuntimeResource childResource : resource.getChildRuntimeResources()) {
                final PathPattern childOpenPattern = childResource.getPathPattern();
                final PathPattern childClosedPattern = PathPattern.asClosed(childOpenPattern);
                final PushMatchedRuntimeResourceRouter childResourcePushingRouter = new PushMatchedRuntimeResourceRouter(childResource);
                // sub resource methods
                if (!childResource.getResourceMethods().isEmpty()) {
                    final List<MethodRouting> childMethodRoutings = createResourceMethodRouters(childResource, subResourceMode);
                    srRoutedBuilder = startNextRoute(srRoutedBuilder, childClosedPattern).to(uriPushingRouter).to(childResourcePushingRouter).to(new MethodSelectingRouter(workers, childMethodRoutings));
                }
                // sub resource locator
                if (childResource.getResourceLocator() != null) {
                    final PushMatchedTemplateRouter locTemplateRouter = getTemplateRouterForChildLocator(subResourceMode, childResource);
                    srRoutedBuilder = startNextRoute(srRoutedBuilder, childOpenPattern).to(uriPushingRouter).to(locTemplateRouter).to(childResourcePushingRouter).to(new PushMatchedMethodRouter(childResource.getResourceLocator())).to(createMethodRouter(childResource.getResourceLocator()));
                }
            }
        }
        // resource locator with empty path
        if (resource.getResourceLocator() != null) {
            final PushMatchedTemplateRouter resourceTemplateRouter = getTemplateRouter(subResourceMode, getLocatorResource(resource).getPathPattern().getTemplate(), PathPattern.OPEN_ROOT_PATH_PATTERN.getTemplate());
            srRoutedBuilder = startNextRoute(srRoutedBuilder, PathPattern.OPEN_ROOT_PATH_PATTERN).to(uriPushingRouter).to(resourceTemplateRouter).to(new PushMatchedMethodRouter(resource.getResourceLocator())).to(createMethodRouter(resource.getResourceLocator()));
        }
        if (srRoutedBuilder != null) {
            final Router methodRouter = srRoutedBuilder.build();
            if (subResourceMode) {
                currentRouterBuilder = startNextRoute(currentRouterBuilder, PathPattern.OPEN_ROOT_PATH_PATTERN).to(resourcePushingRouter).to(methodRouter);
            } else {
                currentRouterBuilder = startNextRoute(currentRouterBuilder, resource.getPathPattern()).to(uriPushingRouter).to(resourcePushingRouter).to(methodRouter);
            }
        }
    }
    return createRootRouter(currentRouterBuilder, subResourceMode);
}
Also used : PathPattern(org.glassfish.jersey.uri.PathPattern) RuntimeResource(org.glassfish.jersey.server.model.RuntimeResource)

Aggregations

RuntimeResource (org.glassfish.jersey.server.model.RuntimeResource)5 Resource (org.glassfish.jersey.server.model.Resource)3 ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)3 PathPattern (org.glassfish.jersey.uri.PathPattern)2 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 HttpMethod (javax.ws.rs.HttpMethod)1 MediaType (javax.ws.rs.core.MediaType)1 Inflector (org.glassfish.jersey.process.Inflector)1 Endpoint (org.glassfish.jersey.server.internal.process.Endpoint)1 HandlerConstructor (org.glassfish.jersey.server.model.HandlerConstructor)1 Invocable (org.glassfish.jersey.server.model.Invocable)1 MethodHandler (org.glassfish.jersey.server.model.MethodHandler)1 ResourceModel (org.glassfish.jersey.server.model.ResourceModel)1 ResourceModelComponent (org.glassfish.jersey.server.model.ResourceModelComponent)1 ResourceModelVisitor (org.glassfish.jersey.server.model.ResourceModelVisitor)1 UriTemplate (org.glassfish.jersey.uri.UriTemplate)1