use of org.glassfish.jersey.server.model.Resource in project jersey by jersey.
the class RuntimeLocatorModelBuilder method buildRouting.
private LocatorRouting buildRouting(final Resource subResource) {
final ResourceModel model = new ResourceModel.Builder(true).addResource(subResource).build();
final ResourceModel enhancedModel = enhance(model);
if (!disableValidation) {
validateResource(enhancedModel);
}
final Resource enhancedLocator = enhancedModel.getResources().get(0);
for (final Class<?> handlerClass : enhancedLocator.getHandlerClasses()) {
resourceContext.bindResource(handlerClass);
}
return new LocatorRouting(enhancedModel, runtimeModelBuilder.buildModel(enhancedModel.getRuntimeResourceModel(), true));
}
use of org.glassfish.jersey.server.model.Resource 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;
}
use of org.glassfish.jersey.server.model.Resource in project jersey by jersey.
the class ExtendedWadlWebappTest method configureDeployment.
@Override
protected DeploymentContext configureDeployment() {
final ResourceConfig resourceConfig = new ResourceConfig(new MyApplication().getClasses());
resourceConfig.property(ServerProperties.WADL_GENERATOR_CONFIG, "org.glassfish.jersey.examples.extendedwadl" + ".SampleWadlGeneratorConfig");
final Resource.Builder resourceBuilder = Resource.builder();
resourceBuilder.name("resource-programmatic").path("programmatic").addMethod("GET").handledBy(new ProgrammaticResource());
resourceConfig.registerResources(resourceBuilder.build());
return DeploymentContext.builder(resourceConfig).contextPath("extended-wadl-webapp").build();
}
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 Resource.Builder resourceBuilder = Resource.builder(ROOT_PATH);
resourceBuilder.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, Response>() {
@Override
public Response apply(ContainerRequestContext data) {
getMethodCalled = true;
return Response.ok("Hello World!").build();
}
});
Inflector<ContainerRequestContext, Response> noContentResponder = new Inflector<ContainerRequestContext, Response>() {
@Override
public Response apply(ContainerRequestContext data) {
headMethodCalled = true;
return Response.noContent().build();
}
};
resourceBuilder.addMethod("HEAD").handledBy(noContentResponder);
resourceBuilder.addMethod("OPTIONS").handledBy(noContentResponder);
return new ResourceConfig().registerResources(resourceBuilder.build());
}
use of org.glassfish.jersey.server.model.Resource 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);
}
}
Aggregations