Working with Resources
Overview
A Resource is a URI-addressable entity that is represented by a Metacard. Resources may also be known as products or data. Resources may exist either locally or on a remote data store. Examples of Resources include:
A resource object in DDF contains an InputStream
with the binary data of the resource. It describes that resource with a name, which could be a file name, URI, or another identifier. It also contains a mime type or content type which a client can use to know how to interpret the binary data.
Metacards and Resources
Metacards are used to describe a resource through metadata. This metadata includes the time the resource was created, the location where the resource was created, etc. A DDF Metacard
contains the getResourceUri
method which is used to locate and retrieve its corresponding resource.
Retrieve Resource
When a client attempts to retrieve a resource, it must provide a metacard ID or URI corresponding to a unique resource. As mentioned above, the resource URI is obtained from a Metacard's
getResourceUri
method. The CatalogFramework
has three methods that can be used by clients to obtain a resource: getEnterpriseResource
, getResource
, and getLocalResource
. The getEnterpriseResource
method invokes the retrieveResource
method on a local ResourceReader
as well as all the Federated
and Connected Sources
in the DDF enterprise. The second method, getResource
, takes in a source ID as a parameter and only invokes retrieveResource
on the specified Source
. The third method invokes retrieveResource
on a local ResourceReader
.
The parameter for each of these methods in the CatalogFramework
is a ResourceRequest
. DDF includes two implementations of ResourceRequest
: ResourceRequestById
and ResourceRequestByProductUri
. Since these implementations extend OperationImpl
, they can pass a Map
of generic properties through the CatalogFramework
to customize how the resource request is carried out. One example of this is explained under the Options heading below. The following is a basic example of how to create a ResourceRequest
and invoke the CatalogFramework
resource retrieval methods to process the request.
Map<String, Serializable> properties = new HashMap<String, Serializable>(); properties.put("PropertyKey1", "propertyA"); //properties to customize Resource retrieval ResourceRequestById resourceRequest = new ResourceRequestById("0123456789abcdef0123456789abcdef", properties); //object containing ID of Resource to be retrieved String sourceName = "LOCAL_SOURCE"; //the Source ID or name of the local Catalog or a Federated Source ResourceResponse resourceResponse; //object containing the retrieved Resource and the request that was made to get it. resourceResponse = catalogFramework.getResource(resourceRequest, sourceName); //Source-based retrieve Resource request Resource resource = resourceResponse.getResource(); //actual Resource object containing InputStream, mime type, and Resource name
ddf.catalog.resource.ResourceReader
instances can be discovered via the OSGi Service Registry. The system can contain multiple ResourceReaders
. The CatalogFramework
determines which one to call based on the scheme of the resource's URI and what schemes the ResourceReader
supports. The supported schemes are obtained by a ResourceReader's
getSupportedSchemes
method. As an example, one ResourceReader
may know how to handle file-based URIs with the scheme file
, whereas another ResourceReader
may support HTTP based URIs with the scheme http
.
The ResourceReader
or Source
is responsible for locating the resource, reading its bytes, adding the binary data to a Resource
implementation, then returning that Resource
in a ResourceResponse
. The ResourceReader
or Source
is also responsible for determining the Resource's
name and mime type, which it sends back in the Resource
implementation.
See the Developing a Resource Reader section or the Developing a Source section in the Developer's Guide for more information and examples.
Options
Options can be specified on a retrieve resource request made through any of the supporting endpoint. To specify an option for a retrieve resource request, the endpoint needs to first instantiate a ResourceRequestByProductUri
or a ResourceRequestById
. Both of these ResourceRequest
implementations allow a Map
of properties to be specified. Put the specified option into the Map
under the key RESOURCE_OPTION
.
Map<String, Serializable> properties = new HashMap<String, Serializable>(); properties.put("RESOURCE_OPTION", "OptionA"); ResourceRequestById resourceRequest = new ResourceRequestById("0123456789abcdef0123456789abcdef", properties);
Depending on the support that the ResourceReader
or Source
provides for options, the properties
Map
will be checked for the RESOURCE_OPTION
entry. If that entry is found, the option will be handled however the ResourceReader
or Source
supports options. If the ResourceReader
or Source
does not support options, that entry will be ignored.
A new ResourceReader
or Source
implementation can be created to support options however is most appropriate. Since the option is passed through the catalog framework as a property, the ResourceReader
or Source
will have access to that option as long as the endpoint supports options.
Store Resource
Resources are saved using a ResourceWriter
. ddf.catalog.resource.ResourceWriter
instances can be discovered via the OSGi Service Registry. Once retrieved, the ResourceWriter
instance provides clients a way to store resources and get a corresponding URI that can be used to subsequently retrieve the resource via a ResourceReader
. Simply invoke either of the storeResource
methods with a resource and any potential arguments.
The ResourceWriter
implementation is responsible for determining where the resource is saved and how. This allows flexibility for a resource to be saved in any one of a variety of data stores or file systems. The following is an example of how to use a generic implementation of ResourceWriter
.
InputStream inputStream = <Video_Input_Stream>; //InputStream of raw Resource data MimeType mimeType = new MimeType("video/mpeg"); //Mime Type or content type of Resource String name = "Facility_Video"; //Descriptive Resource name Resource resource = new ResourceImpl(inputStream, mimeType, name); Map<String, Object> optionalArguments = new HashMap<String, Object>(); ResourceWriter writer = new ResourceWriterImpl(); URI resourceUri; //URI that can be used to retrieve Resource resourceUri = writer.storeResource(resource, optionalArguments); //Null can be passed in here
See the Developing a Resource Writer section in the Developer's Guide for more information and examples.
BinaryContent
BinaryContent
is an object used as a container to store translated or transformed DDF components. Resource
extends BinaryContent
and includes a getName
method. BinaryContent
has methods to get the InputStream
, byte
array, MIME type, and size of the represented binary data. An implementation of BinaryContent
(BinaryContentImpl
) can be found in the Catalog API in the ddf.catalog.data
package.