Class Resource
- java.lang.Object
-
- org.glassfish.jersey.server.model.Resource
-
- All Implemented Interfaces:
ResourceModelComponent
,Routed
public final class Resource extends java.lang.Object implements Routed, ResourceModelComponent
Model of a single resource component.Resource component model represents a collection of
methods
grouped under the same parent request path template.Resource
class is also the main entry point to the programmatic resource modeling API that provides ability to programmatically extend the existing JAX-RS annotated resource classes or build new resource models that may be utilized by Jersey runtime.For example:
@Path("hello") public class HelloResource { @GET @Produces("text/plain") public String sayHello() { return "Hello!"; } } ... // Register the annotated resource. ResourceConfig resourceConfig = new ResourceConfig(HelloResource.class); // Add new "hello2" resource using the annotated resource class // and overriding the resource path. Resource.Builder resourceBuilder = Resource.builder(HelloResource.class, new LinkedList<ResourceModelIssue>()) .path("hello2"); // Add a new (virtual) sub-resource method to the "hello2" resource. resourceBuilder.addChildResource("world").addMethod("GET") .produces("text/plain") .handledBy(new Inflector<Request, String>() { @Override public String apply(Request request) { return "Hello World!"; } }); // Register the new programmatic resource in the application's configuration. resourceConfig.registerResources(resourceBuilder.build());
The following table illustrates the supported requests and provided responses for the application configured in the example above.Request Response Method invoked "GET /hello"
"Hello!"
HelloResource.sayHello()
"GET /hello2"
"Hello!"
HelloResource.sayHello()
"GET /hello2/world"
"Hello World!"
Inflector.apply()
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Resource.Builder
Resource model component builder.private static class
Resource.Data
Immutable resource data holder.
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<Resource>
childResources
private Resource.Data
data
private ResourceMethod
locator
private Value<java.lang.String>
name
private Resource
parent
private java.util.List<ResourceMethod>
resourceMethods
-
Constructor Summary
Constructors Modifier Constructor Description private
Resource(Resource parent, Resource.Data data)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
accept(ResourceModelVisitor visitor)
A component should call the visitor back with an appropriate visitor interface method to give it a chance to process.static Resource.Builder
builder()
Get a new unbound resource model builder.static Resource.Builder
builder(java.lang.Class<?> resourceClass)
Create a resource model builder initialized by introspecting an annotated JAX-RS resource class.static Resource.Builder
builder(java.lang.Class<?> resourceClass, boolean disableValidation)
Create a resource model builder initialized by introspecting an annotated JAX-RS resource class.static Resource.Builder
builder(java.lang.String path)
Get a new resource model builder for a resource bound to a given path.static Resource.Builder
builder(java.util.List<Resource> resources)
Creates aresource builder
instance from the list ofresource
which can be merged into a single resource.static Resource.Builder
builder(Resource resource)
Get a new resource model builder initialized from a given resource model.private static Resource.Builder
builder(Resource.Data resourceData)
static Resource
from(java.lang.Class<?> resourceClass)
Create a resource model initialized by introspecting an annotated JAX-RS resource class.static Resource
from(java.lang.Class<?> resourceClass, boolean disableValidation)
Create a resource model initialized by introspecting an annotated JAX-RS resource class.java.util.List<ResourceMethod>
getAllMethods()
Provides resource methods and resource locator are available on the resource.java.util.List<Resource>
getChildResources()
Returns the list of child resources available on this resource.java.util.List<? extends ResourceModelComponent>
getComponents()
Should return all existing resource model sub-components.java.util.Set<java.lang.Class<?>>
getHandlerClasses()
Get the method handler classes for the resource methods registered on the resource.java.util.Set<java.lang.Object>
getHandlerInstances()
Get the method handler (singleton) instances for the resource methods registered on the resource.java.lang.String
getName()
Get the resource name.java.util.List<java.lang.String>
getNames()
Return a list of resource names.Resource
getParent()
Get the parent resource for this resource model ornull
in case this resource is a top-level resource and does not have a parent.java.lang.String
getPath()
Get the path direct assigned to the component.static javax.ws.rs.Path
getPath(java.lang.Class<?> resourceClass)
Get the resource class@Path
annotation.PathPattern
getPathPattern()
Get the path pattern that can be used for matching the remaining request URI against this component represented by this model.ResourceMethod
getResourceLocator()
Provides a resource locator available on the resource.java.util.List<ResourceMethod>
getResourceMethods()
Provides a non-null list of resource methods available on the resource.private static <T> java.util.List<T>
immutableCopy(java.util.List<T> list)
private static <T> java.util.Set<T>
immutableCopy(java.util.Set<T> set)
static boolean
isAcceptable(java.lang.Class<?> c)
Check if the class is acceptable as a JAX-RS provider or resource.boolean
isExtended()
Get the flag indicating whether the resource is extended.java.lang.String
toString()
private static java.util.List<Resource>
transform(Resource parent, java.util.List<Resource.Data> list)
-
-
-
Field Detail
-
parent
private final Resource parent
-
data
private final Resource.Data data
-
name
private final Value<java.lang.String> name
-
resourceMethods
private final java.util.List<ResourceMethod> resourceMethods
-
locator
private final ResourceMethod locator
-
childResources
private final java.util.List<Resource> childResources
-
-
Constructor Detail
-
Resource
private Resource(Resource parent, Resource.Data data)
-
-
Method Detail
-
builder
public static Resource.Builder builder()
Get a new unbound resource model builder.- Returns:
- new unbound resource model builder.
- See Also:
Resource.Builder.path(java.lang.String)
-
builder
public static Resource.Builder builder(java.lang.String path)
Get a new resource model builder for a resource bound to a given path.- Parameters:
path
- resource path.- Returns:
- new resource model builder.
- See Also:
Resource.Builder.path(java.lang.String)
-
builder
public static Resource.Builder builder(java.util.List<Resource> resources)
Creates aresource builder
instance from the list ofresource
which can be merged into a single resource. It must be possible to merge theresources
into a single valid resource. For example all resources must have the samepath
, they cannot have ambiguous methods on the same path, etc.- Parameters:
resources
- Resources with the same path.- Returns:
- Resource builder initialized from merged resources.
-
builder
public static Resource.Builder builder(java.lang.Class<?> resourceClass)
Create a resource model builder initialized by introspecting an annotated JAX-RS resource class.- Parameters:
resourceClass
- resource class to be modelled.- Returns:
- resource model builder initialized by the class or
null
if the class does not represent a resource.
-
builder
public static Resource.Builder builder(java.lang.Class<?> resourceClass, boolean disableValidation)
Create a resource model builder initialized by introspecting an annotated JAX-RS resource class.- Parameters:
resourceClass
- resource class to be modelled.disableValidation
- if set totrue
, then any model validation checks will be disabled.- Returns:
- resource model builder initialized by the class or
null
if the class does not represent a resource.
-
from
public static Resource from(java.lang.Class<?> resourceClass)
Create a resource model initialized by introspecting an annotated JAX-RS resource class.- Parameters:
resourceClass
- resource class to be modelled.- Returns:
- resource model initialized by the class or
null
if the class does not represent a resource.
-
from
public static Resource from(java.lang.Class<?> resourceClass, boolean disableValidation)
Create a resource model initialized by introspecting an annotated JAX-RS resource class.- Parameters:
resourceClass
- resource class to be modelled.disableValidation
- if set totrue
, then any model validation checks will be disabled.- Returns:
- resource model initialized by the class or
null
if the class does not represent a resource.
-
isAcceptable
public static boolean isAcceptable(java.lang.Class<?> c)
Check if the class is acceptable as a JAX-RS provider or resource. Method returnsfalse
if the class is either- abstract
- interface
- annotation
- primitive
- local class
- non-static member class
- Parameters:
c
- class to be checked.- Returns:
true
if the class is an acceptable JAX-RS provider or resource,false
otherwise.
-
getPath
public static javax.ws.rs.Path getPath(java.lang.Class<?> resourceClass)
Get the resource class@Path
annotation. May returnnull
in case there is no@Path
annotation on the resource.- Parameters:
resourceClass
- resource class.- Returns:
@Path
annotation instance if present on the resource class (i.e. the class is a root resource class), ornull
otherwise.
-
builder
public static Resource.Builder builder(Resource resource)
Get a new resource model builder initialized from a given resource model.- Parameters:
resource
- resource model initializing the resource builder.- Returns:
- new resource model builder.
-
builder
private static Resource.Builder builder(Resource.Data resourceData)
-
transform
private static java.util.List<Resource> transform(Resource parent, java.util.List<Resource.Data> list)
-
immutableCopy
private static <T> java.util.List<T> immutableCopy(java.util.List<T> list)
-
immutableCopy
private static <T> java.util.Set<T> immutableCopy(java.util.Set<T> set)
-
getPath
public java.lang.String getPath()
Description copied from interface:Routed
Get the path direct assigned to the component.
-
getPathPattern
public PathPattern getPathPattern()
Description copied from interface:Routed
Get the path pattern that can be used for matching the remaining request URI against this component represented by this model.- Specified by:
getPathPattern
in interfaceRouted
- Returns:
- component path pattern.
-
getParent
public Resource getParent()
Get the parent resource for this resource model ornull
in case this resource is a top-level resource and does not have a parent.- Returns:
- parent resource or
null
if the resource does not have a parent. - Since:
- 2.1
-
getName
public java.lang.String getName()
Get the resource name. If the resource was constructed from a JAX-RS annotated resource class, the resource name will be set to thefully-qualified name
of the resource class.- Returns:
- reference JAX-RS resource handler class.
-
getNames
public java.util.List<java.lang.String> getNames()
Return a list of resource names.- Returns:
- a list of resource names.
-
getResourceMethods
public java.util.List<ResourceMethod> getResourceMethods()
Provides a non-null list of resource methods available on the resource.- Returns:
- non-null abstract resource method list.
-
getResourceLocator
public ResourceMethod getResourceLocator()
Provides a resource locator available on the resource.- Returns:
- Resource locator if it is present, null otherwise.
-
getAllMethods
public java.util.List<ResourceMethod> getAllMethods()
Provides resource methods and resource locator are available on the resource. The list is ordered so that resource methods are positioned first before resource locator.- Returns:
- List of resource methods and resource locator.
-
getChildResources
public java.util.List<Resource> getChildResources()
Returns the list of child resources available on this resource.- Returns:
- Non-null list of child resources (may be empty).
-
getHandlerClasses
public java.util.Set<java.lang.Class<?>> getHandlerClasses()
Get the method handler classes for the resource methods registered on the resource.- Returns:
- resource method handler classes.
-
getHandlerInstances
public java.util.Set<java.lang.Object> getHandlerInstances()
Get the method handler (singleton) instances for the resource methods registered on the resource.- Returns:
- resource method handler instances.
-
accept
public void accept(ResourceModelVisitor visitor)
Description copied from interface:ResourceModelComponent
A component should call the visitor back with an appropriate visitor interface method to give it a chance to process.- Specified by:
accept
in interfaceResourceModelComponent
- Parameters:
visitor
- resource model visitor.
-
isExtended
public boolean isExtended()
Get the flag indicating whether the resource is extended. I.e. it is not part of the resource set that forms the REST API explicitly defined by the application developer (resource classes and instances returned from theApplication
subclass getters).Extended resource model components are helper components that are not part of the explicitly defined REST API of a JAX-RS application, instead they are generated by Jersey runtime. For example, extended resource model components include
OPTIONS
resource methods
automatically generated by Jerseyresource model processors
orapplication.wadl
resource API that exposes the application WADL descriptor.- Returns:
true
if the resource is part of the application's extended REST API,false
otherwise.- Since:
- 2.5.1
- See Also:
ExtendedResource
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
getComponents
public java.util.List<? extends ResourceModelComponent> getComponents()
Description copied from interface:ResourceModelComponent
Should return all existing resource model sub-components.- Specified by:
getComponents
in interfaceResourceModelComponent
- Returns:
- list of all sub-components
-
-