Search in sources :

Example 6 with GetRecordByIdRequest

use of org.codice.ddf.spatial.ogc.csw.catalog.common.GetRecordByIdRequest in project ddf by codice.

the class AbstractCswSource method retrieveResource.

@Override
public ResourceResponse retrieveResource(URI resourceUri, Map<String, Serializable> requestProperties) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
    if (canRetrieveResourceById()) {
        // If no resource reader was found, retrieve the product through a GetRecordById request
        Serializable serializableId = null;
        if (requestProperties != null) {
            serializableId = requestProperties.get(Core.ID);
        }
        if (serializableId == null) {
            throw new ResourceNotFoundException("Unable to retrieve resource because no metacard ID was found.");
        }
        String metacardId = serializableId.toString();
        LOGGER.debug("Retrieving resource for ID : {}", metacardId);
        Csw csw = factory.getClientForSubject((Subject) requestProperties.get(SecurityConstants.SECURITY_SUBJECT));
        GetRecordByIdRequest getRecordByIdRequest = new GetRecordByIdRequest();
        getRecordByIdRequest.setService(CswConstants.CSW);
        getRecordByIdRequest.setOutputSchema(OCTET_STREAM_OUTPUT_SCHEMA);
        getRecordByIdRequest.setOutputFormat(MediaType.APPLICATION_OCTET_STREAM);
        getRecordByIdRequest.setId(metacardId);
        String rangeValue = "";
        long requestedBytesToSkip = 0;
        if (requestProperties.containsKey(CswConstants.BYTES_TO_SKIP)) {
            requestedBytesToSkip = (Long) requestProperties.get(CswConstants.BYTES_TO_SKIP);
            rangeValue = String.format("%s%s-", CswConstants.BYTES_EQUAL, requestProperties.get(CswConstants.BYTES_TO_SKIP).toString());
            LOGGER.debug("Range: {}", rangeValue);
        }
        CswRecordCollection recordCollection;
        try {
            recordCollection = csw.getRecordById(getRecordByIdRequest, rangeValue);
            Resource resource = recordCollection.getResource();
            if (resource != null) {
                long responseBytesSkipped = 0L;
                if (recordCollection.getResourceProperties().get(BYTES_SKIPPED) != null) {
                    responseBytesSkipped = (Long) recordCollection.getResourceProperties().get(BYTES_SKIPPED);
                }
                alignStream(resource.getInputStream(), requestedBytesToSkip, responseBytesSkipped);
                return new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(resource.getInputStream()), resource.getMimeTypeValue(), FilenameUtils.getName(resource.getName())));
            }
        } catch (CswException | IOException e) {
            throw new ResourceNotFoundException(String.format(ERROR_ID_PRODUCT_RETRIEVAL, metacardId), e);
        }
    }
    LOGGER.debug("Retrieving resource at : {}", resourceUri);
    return resourceReader.retrieveResource(resourceUri, requestProperties);
}
Also used : Serializable(java.io.Serializable) Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) Resource(ddf.catalog.resource.Resource) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) IOException(java.io.IOException) GetRecordByIdRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.GetRecordByIdRequest) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) BufferedInputStream(java.io.BufferedInputStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Aggregations

GetRecordByIdRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.GetRecordByIdRequest)6 Test (org.junit.Test)5 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)4 Metacard (ddf.catalog.data.Metacard)1 Result (ddf.catalog.data.Result)1 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 ResultImpl (ddf.catalog.data.impl.ResultImpl)1 QueryRequest (ddf.catalog.operation.QueryRequest)1 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)1 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)1 Resource (ddf.catalog.resource.Resource)1 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)1 ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Csw (org.codice.ddf.spatial.ogc.csw.catalog.common.Csw)1 CswException (org.codice.ddf.spatial.ogc.csw.catalog.common.CswException)1