Search in sources :

Example 11 with Resource

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

the class ResourceMergeTest method testMergingTemplateResources.

private void testMergingTemplateResources(List<Resource> resources) {
    assertEquals(2, resources.size());
    final Resource resB = ResourceTestUtils.getResource(resources, "root/{b}");
    ResourceTestUtils.containsExactMethods(resB, false, "GET");
    final Resource resA = ResourceTestUtils.getResource(resources, "root/{a}");
    assertTrue(resA.getResourceMethods().isEmpty());
    final List<Resource> childResources = resA.getChildResources();
    assertEquals(2, childResources.size());
    final Resource childQ = ResourceTestUtils.getResource(childResources, "{q}");
    ResourceTestUtils.containsExactMethods(childQ, false, "GET", "PUT");
    final Resource childPost = ResourceTestUtils.getResource(childResources, "{post}");
    ResourceTestUtils.containsExactMethods(childPost, false, "POST");
}
Also used : Resource(org.glassfish.jersey.server.model.Resource)

Example 12 with Resource

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

the class ResourceMergeTest method createResources.

private List<Resource> createResources(Class<?>... resourceClass) {
    final ResourceBag.Builder builder = new ResourceBag.Builder();
    for (Class<?> clazz : resourceClass) {
        final Resource resource = Resource.from(clazz);
        builder.registerProgrammaticResource(resource);
    }
    final ResourceBag bag = builder.build();
    return bag.getRootResources();
}
Also used : Resource(org.glassfish.jersey.server.model.Resource)

Example 13 with Resource

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

the class ResourceMergeTest method testResourceMerge.

@Test
public void testResourceMerge() {
    final List<Resource> rootResources = createRootResources();
    assertEquals(2, rootResources.size());
    final Resource resourceC = ResourceTestUtils.getResource(rootResources, "different-path");
    ResourceTestUtils.containsExactMethods(resourceC, false, "POST");
    final Resource resourceAB = ResourceTestUtils.getResource(rootResources, "a");
    ResourceTestUtils.containsExactMethods(resourceAB, false, "POST", "GET");
}
Also used : Resource(org.glassfish.jersey.server.model.Resource) Test(org.junit.Test)

Example 14 with Resource

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

the class WadlModelProcessor method processResourceModel.

@Override
public ResourceModel processResourceModel(final ResourceModel resourceModel, final Configuration configuration) {
    final boolean disabled = PropertiesHelper.isProperty(configuration.getProperty(ServerProperties.WADL_FEATURE_DISABLE));
    if (disabled) {
        return resourceModel;
    }
    final ResourceModel.Builder builder = ModelProcessorUtil.enhanceResourceModel(resourceModel, false, methodList, true);
    // Do not add WadlResource if already present in the classes (i.e. added during scanning).
    if (!configuration.getClasses().contains(WadlResource.class)) {
        final Resource wadlResource = Resource.builder(WadlResource.class).build();
        builder.addResource(wadlResource);
    }
    return builder.build();
}
Also used : WadlResource(org.glassfish.jersey.server.wadl.internal.WadlResource) Resource(org.glassfish.jersey.server.model.Resource) RuntimeResource(org.glassfish.jersey.server.model.RuntimeResource) ResourceModel(org.glassfish.jersey.server.model.ResourceModel) WadlResource(org.glassfish.jersey.server.wadl.internal.WadlResource)

Example 15 with Resource

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

the class AbstractJsonTest method configureJaxrsApplication.

/**
     * Creates and configures a JAX-RS {@link Application} for given {@link JsonTestSetup}. The {@link Application} will
     * contain one resource that can be accessed via {@code POST} method at {@code <jsonProviderName>/<entityClassName>} path.
     * The resource also checks whether is the incoming JSON same as the one stored in a appropriate file.
     *
     * @param jsonTestSetup configuration to create a JAX-RS {@link Application} for.
     * @return an {@link Application} instance.
     */
private static Application configureJaxrsApplication(final JsonTestSetup jsonTestSetup) {
    final Resource.Builder resourceBuilder = Resource.builder();
    final String providerName = getProviderPathPart(jsonTestSetup);
    final String testName = getEntityPathPart(jsonTestSetup);
    resourceBuilder.path("/" + providerName + "/" + testName).addMethod("POST").consumes(MediaType.APPLICATION_JSON_TYPE).produces(MediaType.APPLICATION_JSON_TYPE).handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(final ContainerRequestContext containerRequestContext) {
            final ContainerRequest containerRequest = (ContainerRequest) containerRequestContext;
            // Check if the JSON is the same as in the previous version.
            containerRequest.bufferEntity();
            try {
                String json = JsonTestHelper.getResourceAsString(PKG_NAME, providerName + "_" + testName + (moxyJaxbProvider() || runningOnJdk7AndLater() ? "_MOXy" : "") + ".json").trim();
                final InputStream entityStream = containerRequest.getEntityStream();
                String retrievedJson = JsonTestHelper.getEntityAsString(entityStream).trim();
                entityStream.reset();
                // JAXB-RI and MOXy generate namespace prefixes differently - unify them (ns1/ns2 into ns0)
                if (jsonTestSetup.getJsonProvider() instanceof JsonTestProvider.JettisonBadgerfishJsonTestProvider) {
                    if (retrievedJson.contains("\"ns1\"")) {
                        json = json.replace("ns1", "ns0");
                        retrievedJson = retrievedJson.replace("ns1", "ns0");
                    } else if (retrievedJson.contains("\"ns2\"")) {
                        json = json.replace("ns2", "ns0");
                        retrievedJson = retrievedJson.replace("ns2", "ns0");
                    }
                }
                if (!json.equals(retrievedJson)) {
                    LOGGER.log(Level.SEVERE, "Expected: " + json);
                    LOGGER.log(Level.SEVERE, "Actual:   " + retrievedJson);
                    return Response.ok("{\"error\":\"JSON values doesn't match.\"}").build();
                }
            } catch (final IOException e) {
                return Response.ok("{\"error\":\"Cannot find original JSON file.\"}").build();
            }
            final Object testBean = containerRequest.readEntity(jsonTestSetup.getEntityClass());
            return Response.ok(testBean).build();
        }
    });
    final ResourceConfig resourceConfig = new ResourceConfig().registerResources(resourceBuilder.build()).register(jsonTestSetup.getJsonProvider().getFeature());
    resourceConfig.registerInstances(getJaxbContextResolver(jsonTestSetup));
    if (jsonTestSetup.getProviders() != null) {
        resourceConfig.registerInstances(jsonTestSetup.getProviders());
    }
    return resourceConfig;
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) InputStream(java.io.InputStream) Resource(org.glassfish.jersey.server.model.Resource) IOException(java.io.IOException) Response(javax.ws.rs.core.Response) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

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