Search in sources :

Example 36 with Resource

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

the class IntrospectionModellerTest method testCreateResource.

@Test
public /**
     * Test of createResource method, of class IntrospectionModeller.
     */
void testCreateResource() {
    Class<?> resourceClass;
    Resource result;
    List<ResourceMethod> resourceMethods;
    ResourceMethod resourceMethod;
    // HelloWorldResource
    resourceClass = HelloWorldResource.class;
    result = Resource.builder(resourceClass).build();
    resourceMethods = result.getResourceMethods();
    assertEquals("Unexpected number of resource methods in the resource model.", 2, resourceMethods.size());
    resourceMethod = find(resourceMethods, "postA");
    assertEquals("Unexpected number of produced media types in the resource method model", 3, resourceMethod.getProducedTypes().size());
    assertEquals("Unexpected number of consumed media types in the resource method model", 2, resourceMethod.getConsumedTypes().size());
    assertEquals("Unexpected number of handler parameters", 0, resourceMethod.getInvocable().getHandler().getParameters().size());
    resourceMethod = find(resourceMethods, "postB");
    assertEquals("Unexpected number of inherited produced media types in the resource method model", 2, resourceMethod.getProducedTypes().size());
    assertEquals("Unexpected number of inherited consumed media types in the resource method model", 3, resourceMethod.getConsumedTypes().size());
    assertEquals("Unexpected number of handler parameters", 0, resourceMethod.getInvocable().getHandler().getParameters().size());
    // OtherResource
    resourceClass = OtherResource.class;
    result = Resource.builder(resourceClass).build();
    resourceMethods = result.getResourceMethods();
    assertEquals("Unexpected number of resource methods in the resource model.", 2, resourceMethods.size());
    resourceMethod = find(resourceMethods, "get");
    assertEquals("Unexpected number of produced media types in the resource method model", 0, resourceMethod.getProducedTypes().size());
    assertEquals("Unexpected number of consumed media types in the resource method model", 0, resourceMethod.getConsumedTypes().size());
    assertEquals("Unexpected number of handler parameters", 5, resourceMethod.getInvocable().getHandler().getParameters().size());
    assertSources(resourceMethod.getInvocable().getHandler().getParameters(), Parameter.Source.CONTEXT, Parameter.Source.PATH, Parameter.Source.QUERY, // @Inject on field
    Parameter.Source.UNKNOWN, // @Inject on setter
    Parameter.Source.UNKNOWN);
    resourceMethod = find(resourceMethods, "post");
    assertEquals("Unexpected number of inherited produced media types in the resource method model", 0, resourceMethod.getProducedTypes().size());
    assertEquals("Unexpected number of inherited consumed media types in the resource method model", 0, resourceMethod.getConsumedTypes().size());
    assertEquals("Unexpected number of handler parameters", 5, resourceMethod.getInvocable().getHandler().getParameters().size());
    assertSources(resourceMethod.getInvocable().getHandler().getParameters(), Parameter.Source.CONTEXT, Parameter.Source.PATH, Parameter.Source.QUERY, // @Inject on field
    Parameter.Source.UNKNOWN, // @Inject on setter
    Parameter.Source.UNKNOWN);
}
Also used : Resource(org.glassfish.jersey.server.model.Resource) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Test(org.junit.Test)

Example 37 with Resource

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

the class OAuth1ServerFeature method configure.

@Override
public boolean configure(FeatureContext context) {
    if (oAuth1Provider != null) {
        context.register(oAuth1Provider);
    }
    context.register(OAuth1ServerFilter.class);
    if (!context.getConfiguration().isRegistered(OAuth1SignatureFeature.class)) {
        context.register(OAuth1SignatureFeature.class);
    }
    final Map<String, Object> properties = context.getConfiguration().getProperties();
    final Boolean propertyResourceEnabled = OAuth1ServerProperties.getValue(properties, OAuth1ServerProperties.ENABLE_TOKEN_RESOURCES, null, Boolean.class);
    boolean registerResources = propertyResourceEnabled != null ? propertyResourceEnabled : requestTokenUri != null & accessTokenUri != null;
    if (registerResources) {
        String requestUri = OAuth1ServerProperties.getValue(properties, OAuth1ServerProperties.REQUEST_TOKEN_URI, null, String.class);
        if (requestUri == null) {
            requestUri = requestTokenUri == null ? "requestToken" : requestTokenUri;
        }
        String accessUri = OAuth1ServerProperties.getValue(properties, OAuth1ServerProperties.ACCESS_TOKEN_URI, null, String.class);
        if (accessUri == null) {
            accessUri = accessTokenUri == null ? "accessToken" : accessTokenUri;
        }
        final Resource requestResource = Resource.builder(RequestTokenResource.class).path(requestUri).build();
        final Resource accessResource = Resource.builder(AccessTokenResource.class).path(accessUri).build();
        context.register(new OAuthModelProcessor(requestResource, accessResource));
    }
    return true;
}
Also used : OAuth1SignatureFeature(org.glassfish.jersey.oauth1.signature.OAuth1SignatureFeature) AccessTokenResource(org.glassfish.jersey.server.oauth1.internal.AccessTokenResource) RequestTokenResource(org.glassfish.jersey.server.oauth1.internal.RequestTokenResource) Resource(org.glassfish.jersey.server.model.Resource)

Example 38 with Resource

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

the class SingletonResourceTest method configure.

@Override
protected ResourceConfig configure() {
    final ResourceConfig resourceConfig = new ResourceConfig(SingletonResource.class, ChildInheritsParentAnnotation.class, ChildImplementsInterfaceAnnotation.class, TestResource.class, RequestScopeResource.class, PerLookupScopeResource.class, SingletonScopeResource.class);
    final Resource.Builder resourceBuilder1 = Resource.builder();
    resourceBuilder1.name("resource-programmatic/instance/").path("programmatic/instance/").addMethod("GET").handledBy(new Inflector<ContainerRequestContext, Response>() {

        private int counter = 1;

        @Override
        public Response apply(ContainerRequestContext data) {
            return Response.ok("prg-instance:" + counter++).build();
        }
    });
    resourceConfig.registerResources(resourceBuilder1.build());
    final Resource.Builder resourceBuilder2 = Resource.builder();
    resourceBuilder2.name("resource-programmatic/singleton/").path("programmatic/singleton/").addMethod("GET").handledBy(SingletonProgrammatic.class);
    resourceConfig.registerResources(resourceBuilder2.build());
    final Resource.Builder resourceBuilder3 = Resource.builder();
    resourceBuilder3.name("resource-programmatic/reused-singleton/").path("programmatic/reused-singleton/").addMethod("GET").handledBy(SubResourceSingleton.class);
    resourceConfig.registerResources(resourceBuilder3.build());
    final Resource.Builder resourceBuilder4 = Resource.builder();
    resourceBuilder4.name("resource-programmatic/not-singleton/").path("programmatic/not-singleton/").addMethod("GET").handledBy(NotSingletonProgrammatic.class);
    resourceConfig.registerResources(resourceBuilder4.build());
    return resourceConfig;
}
Also used : Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Resource(org.glassfish.jersey.server.model.Resource) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 39 with Resource

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

the class PrefixAddingModelProcessor method processResourceModel.

@Override
public ResourceModel processResourceModel(ResourceModel model, Configuration config) {
    // Create new resource model.
    final ResourceModel.Builder resourceModelBuilder = new ResourceModel.Builder(false);
    for (final Resource resource : model.getResources()) {
        for (Class handlerClass : resource.getHandlerClasses()) {
            final String packageName = handlerClass.getPackage().getName();
            final Optional<String> packagePrefix = packagePrefixes.entrySet().stream().filter(entry -> packageName.startsWith(entry.getKey())).sorted((o1, o2) -> -o1.getKey().compareTo(o2.getKey())).map(Map.Entry::getValue).findFirst();
            if (packagePrefix.isPresent()) {
                final String prefixedPath = prefixPath(packagePrefix.get(), resource.getPath());
                final Resource newResource = Resource.builder(resource).path(prefixedPath).build();
                resourceModelBuilder.addResource(newResource);
            } else {
                resourceModelBuilder.addResource(resource);
            }
        }
    }
    return resourceModelBuilder.build();
}
Also used : Provider(javax.ws.rs.ext.Provider) ImmutableMap(com.google.common.collect.ImmutableMap) Configuration(javax.ws.rs.core.Configuration) Map(java.util.Map) ResourceModel(org.glassfish.jersey.server.model.ResourceModel) Optional(java.util.Optional) ModelProcessor(org.glassfish.jersey.server.model.ModelProcessor) Resource(org.glassfish.jersey.server.model.Resource) Resource(org.glassfish.jersey.server.model.Resource) ResourceModel(org.glassfish.jersey.server.model.ResourceModel) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 40 with Resource

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

the class JerseyService method startUpApi.

private void startUpApi() throws Exception {
    final boolean startWebInterface = configuration.isWebEnable() && configuration.isRestAndWebOnSamePort();
    final List<String> resourcePackages = new ArrayList<>(Arrays.asList(restControllerPackages));
    if (startWebInterface) {
        resourcePackages.add(RESOURCE_PACKAGE_WEB);
    }
    final Set<Resource> pluginResources = prefixPluginResources(PLUGIN_PREFIX, pluginRestResources);
    final SSLEngineConfigurator sslEngineConfigurator = configuration.isRestEnableTls() ? buildSslEngineConfigurator(configuration.getRestTlsCertFile(), configuration.getRestTlsKeyFile(), configuration.getRestTlsKeyPassword()) : null;
    final URI restListenUri = configuration.getRestListenUri();
    final URI listenUri = new URI(restListenUri.getScheme(), restListenUri.getUserInfo(), restListenUri.getHost(), restListenUri.getPort(), null, null, null);
    apiHttpServer = setUp("rest", listenUri, sslEngineConfigurator, configuration.getRestThreadPoolSize(), configuration.getRestSelectorRunnersCount(), configuration.getRestMaxInitialLineLength(), configuration.getRestMaxHeaderSize(), configuration.isRestEnableGzip(), configuration.isRestEnableCors(), pluginResources, resourcePackages.toArray(new String[0]));
    apiHttpServer.start();
    LOG.info("Started REST API at <{}>", configuration.getRestListenUri());
    if (startWebInterface) {
        LOG.info("Started Web Interface at <{}>", configuration.getWebListenUri());
    }
}
Also used : SSLEngineConfigurator(org.glassfish.grizzly.ssl.SSLEngineConfigurator) ArrayList(java.util.ArrayList) PluginRestResource(org.graylog2.plugin.rest.PluginRestResource) Resource(org.glassfish.jersey.server.model.Resource) URI(java.net.URI)

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