Search in sources :

Example 16 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 ResourceConfig resourceConfig = new ResourceConfig(TracingResource.class);
    final Resource.Builder resourceBuilder = Resource.builder(ROOT_PATH_PROGRAMMATIC);
    resourceBuilder.addMethod(TRACE.NAME).handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext request) {
            if (request == null) {
                return Response.noContent().build();
            } else {
                return Response.ok(Stringifier.stringify((ContainerRequest) request), MediaType.TEXT_PLAIN).build();
            }
        }
    });
    return resourceConfig.registerResources(resourceBuilder.build());
}
Also used : Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Resource(org.glassfish.jersey.server.model.Resource) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 17 with Resource

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

the class WadlGeneratorResourceDocSupportTest method wadlIsGeneratedWithUnknownCustomParameterAnnotation.

@Test
public void wadlIsGeneratedWithUnknownCustomParameterAnnotation() throws JAXBException {
    /* Set up a ClassDocType that has something for a custom-annotated parameter */
    ClassDocType cdt = new ClassDocType();
    cdt.setClassName(TestResource.class.getName());
    MethodDocType mdt = new MethodDocType();
    mdt.setMethodName("method");
    cdt.getMethodDocs().add(mdt);
    ParamDocType pdt = new ParamDocType("x", "comment about x");
    mdt.getParamDocs().add(pdt);
    AnnotationDocType adt = new AnnotationDocType();
    adt.setAnnotationTypeName(CustomParam.class.getName());
    adt.getAttributeDocs().add(new NamedValueType("value", "x"));
    pdt.getAnnotationDocs().add(adt);
    ResourceDocType rdt = new ResourceDocType();
    rdt.getDocs().add(cdt);
    /* Generate WADL for that class */
    WadlGenerator wg = new WadlGeneratorResourceDocSupport(new WadlGeneratorImpl(), rdt);
    WadlBuilder wb = new WadlBuilder(wg, false, null);
    Resource resource = Resource.from(TestResource.class);
    ApplicationDescription app = wb.generate(Collections.singletonList(resource));
    /* Confirm that it can be marshalled without error */
    StringWriter sw = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(Application.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(app.getApplication(), sw);
}
Also used : WadlGeneratorImpl(org.glassfish.jersey.server.wadl.internal.WadlGeneratorImpl) Marshaller(javax.xml.bind.Marshaller) ClassDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ClassDocType) NamedValueType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.NamedValueType) Resource(org.glassfish.jersey.server.model.Resource) AnnotationDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.AnnotationDocType) WadlBuilder(org.glassfish.jersey.server.wadl.internal.WadlBuilder) JAXBContext(javax.xml.bind.JAXBContext) ApplicationDescription(org.glassfish.jersey.server.wadl.internal.ApplicationDescription) WadlGenerator(org.glassfish.jersey.server.wadl.WadlGenerator) StringWriter(java.io.StringWriter) MethodDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.MethodDocType) ResourceDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ResourceDocType) WadlGeneratorResourceDocSupport(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.WadlGeneratorResourceDocSupport) ParamDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ParamDocType) Test(org.junit.Test)

Example 18 with Resource

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

the class ChildResourceTest method createApplication.

private ApplicationHandler createApplication() {
    final Resource.Builder rootBuilder = Resource.builder("root");
    rootBuilder.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, String>() {

        @Override
        public String apply(ContainerRequestContext requestContext) {
            return "root-get";
        }
    });
    rootBuilder.addChildResource("child").addMethod("GET").handledBy(new Inflector<ContainerRequestContext, String>() {

        @Override
        public String apply(ContainerRequestContext requestContext) {
            return "sub-get";
        }
    }).build();
    Resource.Builder anotherChildBuilder = Resource.builder("another-child");
    anotherChildBuilder.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, String>() {

        @Override
        public String apply(ContainerRequestContext requestContext) {
            return "another-child-get";
        }
    });
    rootBuilder.addChildResource(anotherChildBuilder.build());
    Resource resource = rootBuilder.build();
    ResourceConfig resourceConfig = new ResourceConfig().registerResources(resource);
    return new ApplicationHandler(resourceConfig);
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Inflector(org.glassfish.jersey.process.Inflector) Resource(org.glassfish.jersey.server.model.Resource) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 19 with Resource

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

the class App method createProgrammaticClipboardApp.

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

        @Override
        public Response apply(ContainerRequestContext data) {
            final String content = clipboard.content();
            if (content.isEmpty()) {
                return Response.noContent().build();
            }
            return Response.ok(content).build();
        }
    });
    resourceBuilder.addMethod("PUT").handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext data) {
            if (data != null) {
                clipboard.setContent(((ContainerRequest) data).readEntity(String.class));
            }
            return Response.noContent().build();
        }
    });
    resourceBuilder.addMethod("POST").handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext data) {
            String newContent = (data != null) ? clipboard.append(((ContainerRequest) data).readEntity(String.class)) : "";
            return Response.ok(newContent).build();
        }
    });
    resourceBuilder.addMethod("DELETE").handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext data) {
            clipboard.clear();
            return Response.noContent().build();
        }
    });
    return new ResourceConfig().registerResources(resourceBuilder.build());
}
Also used : Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Resource(org.glassfish.jersey.server.model.Resource) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 20 with Resource

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

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