Search in sources :

Example 6 with ResourceImpl

use of ddf.catalog.resource.impl.ResourceImpl in project ddf by codice.

the class CswEndpoint method queryProductById.

private CswRecordCollection queryProductById(String id, String rangeValue) throws CswException, UnsupportedQueryException {
    final ResourceRequestById resourceRequest = new ResourceRequestById(id);
    long bytesToSkip = getRange(rangeValue);
    if (bytesToSkip > 0) {
        LOGGER.debug("Bytes to skip: {}", String.valueOf(bytesToSkip));
        resourceRequest.getProperties().put(CswConstants.BYTES_TO_SKIP, bytesToSkip);
    }
    ResourceResponse resourceResponse;
    try {
        resourceResponse = framework.getLocalResource(resourceRequest);
    } catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
        throw new CswException(String.format(ERROR_ID_PRODUCT_RETRIEVAL, id), e);
    }
    Resource resource = resourceResponse.getResource();
    MimeType mimeType = resource.getMimeType();
    if (mimeType == null) {
        try {
            mimeType = new MimeType(MediaType.APPLICATION_OCTET_STREAM);
            resource = new ResourceImpl(resource.getInputStream(), mimeType, resource.getName());
        } catch (MimeTypeParseException e) {
            throw new CswException(String.format("Could not create mime type upon null mimeType, for mime %s.", MediaType.APPLICATION_OCTET_STREAM), e);
        }
    }
    CswRecordCollection cswRecordCollection = new CswRecordCollection();
    cswRecordCollection.setResource(resource);
    cswRecordCollection.setOutputSchema(OCTET_STREAM_OUTPUT_SCHEMA);
    LOGGER.debug("{} successfully retrieved product for ID: {}", id);
    return cswRecordCollection;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) Resource(ddf.catalog.resource.Resource) IOException(java.io.IOException) MimeType(javax.activation.MimeType) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 7 with ResourceImpl

use of ddf.catalog.resource.impl.ResourceImpl in project ddf by codice.

the class WfsSource method retrieveResource.

@Override
public ResourceResponse retrieveResource(URI uri, Map<String, Serializable> arguments) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
    StringBuilder strBuilder = new StringBuilder();
    strBuilder.append("<html><script type=\"text/javascript\">window.location.replace(\"");
    strBuilder.append(uri);
    strBuilder.append("\");</script></html>");
    Resource resource = new ResourceImpl(IOUtils.toInputStream(strBuilder.toString()), MediaType.TEXT_HTML, getId() + " Resource");
    return new ResourceResponseImpl(resource);
}
Also used : ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) Resource(ddf.catalog.resource.Resource) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl)

Example 8 with ResourceImpl

use of ddf.catalog.resource.impl.ResourceImpl in project ddf by codice.

the class ReliableResourceDownloader method setupDownload.

public ResourceResponse setupDownload(Metacard metacard, DownloadStatusInfo downloadStatusInfo) {
    Resource resource = resourceResponse.getResource();
    MimeType mimeType = resource.getMimeType();
    String resourceName = resource.getName();
    fbos = new FileBackedOutputStream(DEFAULT_FILE_BACKED_OUTPUT_STREAM_THRESHOLD);
    countingFbos = new CountingOutputStream(fbos);
    streamReadByClient = new ReliableResourceInputStream(fbos, countingFbos, downloadState, downloadIdentifier, resourceResponse);
    this.metacard = metacard;
    // Create new ResourceResponse to return that will encapsulate the
    // ReliableResourceInputStream that will be read by the client simultaneously as the product
    // is cached to disk (if caching is enabled)
    ResourceImpl newResource = new ResourceImpl(streamReadByClient, mimeType, resourceName);
    resourceResponse = new ResourceResponseImpl(resourceResponse.getRequest(), resourceResponse.getProperties(), newResource);
    // Get handle to retrieved product's InputStream
    resourceInputStream = resource.getInputStream();
    eventListener.setDownloadMap(downloadIdentifier, resourceResponse);
    downloadStatusInfo.addDownloadInfo(downloadIdentifier, this, resourceResponse);
    if (downloaderConfig.isCacheEnabled()) {
        CacheKey keyMaker = null;
        String key = null;
        try {
            keyMaker = new CacheKey(metacard, resourceResponse.getRequest());
            key = keyMaker.generateKey();
        } catch (IllegalArgumentException e) {
            LOGGER.info("Cannot create cache key for resource with metacard ID = {}", metacard.getId());
            return resourceResponse;
        }
        if (!resourceCache.isPending(key)) {
            // Fully qualified path to cache file that will be written to.
            // Example:
            // <INSTALL-DIR>/data/product-cache/<source-id>-<metacard-id>
            // <INSTALL-DIR>/data/product-cache/ddf.distribution-abc123
            filePath = FilenameUtils.concat(resourceCache.getProductCacheDirectory(), key);
            if (filePath == null) {
                LOGGER.info("Unable to create cache for cache directory {} and key {} - no caching will be done.", resourceCache.getProductCacheDirectory(), key);
                return resourceResponse;
            }
            reliableResource = new ReliableResource(key, filePath, mimeType, resourceName, metacard);
            resourceCache.addPendingCacheEntry(reliableResource);
            try {
                fos = FileUtils.openOutputStream(new File(filePath));
                doCaching = true;
                this.downloadState.setCacheEnabled(true);
            } catch (IOException e) {
                LOGGER.info("Unable to open cache file {} - no caching will be done.", filePath);
            }
        } else {
            LOGGER.debug("Cache key {} is already pending caching", key);
        }
    }
    return resourceResponse;
}
Also used : ReliableResource(ddf.catalog.resource.data.ReliableResource) Resource(ddf.catalog.resource.Resource) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) IOException(java.io.IOException) MimeType(javax.activation.MimeType) ReliableResource(ddf.catalog.resource.data.ReliableResource) CountingOutputStream(com.google.common.io.CountingOutputStream) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) File(java.io.File) CacheKey(ddf.catalog.cache.impl.CacheKey)

Example 9 with ResourceImpl

use of ddf.catalog.resource.impl.ResourceImpl in project ddf by codice.

the class GetRecordsMessageBodyReader method readFrom.

@Override
public CswRecordCollection readFrom(Class<CswRecordCollection> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream inStream) throws IOException, WebApplicationException {
    CswRecordCollection cswRecords = null;
    Map<String, Serializable> resourceProperties = new HashMap<>();
    // Check if the server returned a Partial Content response (hopefully in response to a range header)
    String contentRangeHeader = httpHeaders.getFirst(HttpHeaders.CONTENT_RANGE);
    if (StringUtils.isNotBlank(contentRangeHeader)) {
        contentRangeHeader = StringUtils.substringBetween(contentRangeHeader.toLowerCase(), "bytes ", "-");
        long bytesSkipped = Long.parseLong(contentRangeHeader);
        resourceProperties.put(BYTES_SKIPPED, Long.valueOf(bytesSkipped));
    }
    // If the following HTTP header exists and its value is true, the input stream will contain
    // raw product data
    String productRetrievalHeader = httpHeaders.getFirst(CswConstants.PRODUCT_RETRIEVAL_HTTP_HEADER);
    if (productRetrievalHeader != null && productRetrievalHeader.equalsIgnoreCase("TRUE")) {
        String fileName = handleContentDispositionHeader(httpHeaders);
        cswRecords = new CswRecordCollection();
        cswRecords.setResource(new ResourceImpl(inStream, mediaType.toString(), fileName));
        cswRecords.setResourceProperties(resourceProperties);
        return cswRecords;
    }
    // Save original input stream for any exception message that might need to be
    // created
    String originalInputStream = IOUtils.toString(inStream, "UTF-8");
    LOGGER.debug("Converting to CswRecordCollection: \n {}", originalInputStream);
    // Re-create the input stream (since it has already been read for potential
    // exception message creation)
    inStream = new ByteArrayInputStream(originalInputStream.getBytes("UTF-8"));
    try {
        HierarchicalStreamReader reader = new XppReader(new InputStreamReader(inStream, StandardCharsets.UTF_8), XmlPullParserFactory.newInstance().newPullParser());
        cswRecords = (CswRecordCollection) xstream.unmarshal(reader, null, argumentHolder);
    } catch (XmlPullParserException e) {
        LOGGER.debug("Unable to create XmlPullParser, and cannot parse CSW Response.", e);
    } catch (XStreamException e) {
        // If an ExceptionReport is sent from the remote CSW site it will be sent with an
        // JAX-RS "OK" status, hence the ErrorResponse exception mapper will not fire.
        // Instead the ExceptionReport will come here and be treated like a GetRecords
        // response, resulting in an XStreamException since ExceptionReport cannot be
        // unmarshalled. So this catch clause is responsible for catching that XStream
        // exception and creating a JAX-RS response containing the original stream
        // (with the ExceptionReport) and rethrowing it as a WebApplicatioNException,
        // which CXF will wrap as a ClientException that the CswSource catches, converts
        // to a CswException, and logs.
        ByteArrayInputStream bis = new ByteArrayInputStream(originalInputStream.getBytes(StandardCharsets.UTF_8));
        ResponseBuilder responseBuilder = Response.ok(bis);
        responseBuilder.type("text/xml");
        Response response = responseBuilder.build();
        throw new WebApplicationException(e, response);
    } finally {
        IOUtils.closeQuietly(inStream);
    }
    return cswRecords;
}
Also used : Serializable(java.io.Serializable) InputStreamReader(java.io.InputStreamReader) WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) Response(javax.ws.rs.core.Response) XStreamException(com.thoughtworks.xstream.XStreamException) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) XppReader(com.thoughtworks.xstream.io.xml.XppReader) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 10 with ResourceImpl

use of ddf.catalog.resource.impl.ResourceImpl 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

ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)18 IOException (java.io.IOException)10 Resource (ddf.catalog.resource.Resource)8 InputStream (java.io.InputStream)8 Test (org.junit.Test)8 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)7 FileInputStream (java.io.FileInputStream)7 Failure (org.junit.runner.notification.Failure)6 ResourceResponse (ddf.catalog.operation.ResourceResponse)5 MimeType (javax.activation.MimeType)5 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)4 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)2 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)2 BufferedInputStream (java.io.BufferedInputStream)2 Serializable (java.io.Serializable)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 MimeTypeParseException (javax.activation.MimeTypeParseException)2