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);
}
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;
}
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;
}
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();
}
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());
}
}
Aggregations