Search in sources :

Example 56 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class CswRecordCollectionMessageBodyWriter method writeTo.

@Override
public void writeTo(CswRecordCollection recordCollection, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream outStream) throws IOException, WebApplicationException {
    final String mimeType = recordCollection.getMimeType();
    LOGGER.debug("Attempting to transform RecordCollection with mime-type: {} & outputSchema: {}", mimeType, recordCollection.getOutputSchema());
    QueryResponseTransformer transformer;
    Map<String, Serializable> arguments = new HashMap<String, Serializable>();
    if (StringUtils.isBlank(recordCollection.getOutputSchema()) && StringUtils.isNotBlank(mimeType) && !XML_MIME_TYPES.contains(mimeType)) {
        transformer = transformerManager.getTransformerByMimeType(mimeType);
    } else if (OCTET_STREAM_OUTPUT_SCHEMA.equals(recordCollection.getOutputSchema())) {
        Resource resource = recordCollection.getResource();
        httpHeaders.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(resource.getMimeType()));
        httpHeaders.put(HttpHeaders.CONTENT_DISPOSITION, Arrays.asList(String.format("inline; filename=\"%s\"", resource.getName())));
        // Custom HTTP header to represent that the product data will be returned in the response.
        httpHeaders.put(CswConstants.PRODUCT_RETRIEVAL_HTTP_HEADER, Arrays.asList("true"));
        // Accept-ranges header to represent that ranges in bytes are accepted.
        httpHeaders.put(CswConstants.ACCEPT_RANGES_HEADER, Arrays.asList(CswConstants.BYTES));
        ByteArrayInputStream in = new ByteArrayInputStream(resource.getByteArray());
        IOUtils.copy(in, outStream);
        return;
    } else {
        transformer = transformerManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA);
        if (recordCollection.getElementName() != null) {
            arguments.put(CswConstants.ELEMENT_NAMES, recordCollection.getElementName().toArray());
        }
        arguments.put(CswConstants.OUTPUT_SCHEMA_PARAMETER, recordCollection.getOutputSchema());
        arguments.put(CswConstants.ELEMENT_SET_TYPE, recordCollection.getElementSetType());
        arguments.put(CswConstants.IS_BY_ID_QUERY, recordCollection.isById());
        arguments.put(CswConstants.GET_RECORDS, recordCollection.getRequest());
        arguments.put(CswConstants.RESULT_TYPE_PARAMETER, recordCollection.getResultType());
        arguments.put(CswConstants.WRITE_NAMESPACES, false);
    }
    if (transformer == null) {
        throw new WebApplicationException(new CatalogTransformerException("Unable to locate Transformer."));
    }
    BinaryContent content = null;
    try {
        content = transformer.transform(recordCollection.getSourceResponse(), arguments);
    } catch (CatalogTransformerException e) {
        throw new WebApplicationException(e);
    }
    if (content != null) {
        try (InputStream inputStream = content.getInputStream()) {
            IOUtils.copy(inputStream, outStream);
        }
    } else {
        throw new WebApplicationException(new CatalogTransformerException("Transformer returned null."));
    }
}
Also used : Serializable(java.io.Serializable) WebApplicationException(javax.ws.rs.WebApplicationException) QueryResponseTransformer(ddf.catalog.transform.QueryResponseTransformer) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Resource(ddf.catalog.resource.Resource) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent)

Example 57 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class WfsSource method retrieveResource.

@Override
public ResourceResponse retrieveResource(URI uri, Map<String, Serializable> arguments) {
    String html = "<html><script type=\"text/javascript\">window.location.replace(\"" + uri + "\");</script></html>";
    Resource resource = new ResourceImpl(IOUtils.toInputStream(html, StandardCharsets.UTF_8), 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 58 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class OgcUrlResourceReader method retrieveResource.

/**
 * Retrieves a {@link ddf.catalog.resource.Resource} based on a {@link URI} and provided
 * arguments. A connection is made to the {@link URI} to obtain the {@link
 * ddf.catalog.resource.Resource}'s {@link InputStream} and build a {@link ResourceResponse} from
 * that. The {@link ddf.catalog.resource.Resource}'s name gets set to the {@link URI} passed in.
 * Calls {@link URLResourceReader}, if the mime-type is "text/html" it will inject a simple script
 * to redirect to the resourceURI instead of attempting to download it.
 *
 * @param resourceURI A {@link URI} that defines what {@link Resource} to retrieve and how to do
 *     it.
 * @param properties Any additional arguments that should be passed to the {@link
 *     ddf.catalog.resource.ResourceReader}.
 * @return A {@link ResourceResponse} containing the retrieved {@link Resource}.
 * @throws ResourceNotSupportedException
 */
@Override
public ResourceResponse retrieveResource(URI resourceURI, Map<String, Serializable> properties) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
    LOGGER.debug("Calling URLResourceReader.retrieveResource()");
    ResourceResponse response = urlResourceReader.retrieveResource(resourceURI, properties);
    Resource resource = response.getResource();
    MimeType mimeType = resource.getMimeType();
    LOGGER.debug("mimeType: {}", mimeType);
    if (mimeType != null) {
        String mimeTypeStr = mimeType.toString();
        String detectedMimeType = "";
        if (UNKNOWN_MIME_TYPES.contains(mimeTypeStr)) {
            detectedMimeType = tika.detect(resourceURI.toURL());
        }
        if (StringUtils.contains(detectedMimeType, MediaType.TEXT_HTML) || StringUtils.contains(mimeTypeStr, MediaType.TEXT_HTML)) {
            LOGGER.debug("Detected \"text\\html\". Building redirect script");
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.append("<html><script type=\"text/javascript\">window.location.replace(\"");
            strBuilder.append(resourceURI);
            strBuilder.append("\");</script></html>");
            return new ResourceResponseImpl(new ResourceImpl(new ByteArrayInputStream(strBuilder.toString().getBytes(StandardCharsets.UTF_8)), detectedMimeType, resource.getName()));
        }
    }
    return response;
}
Also used : ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) Resource(ddf.catalog.resource.Resource) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) MimeType(javax.activation.MimeType)

Example 59 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class ResourceReaderTest method getMockResourceResponse.

private ResourceResponse getMockResourceResponse(MimeType mimeType) {
    Resource mockResource = mock(Resource.class);
    when(mockResource.getMimeType()).thenReturn(mimeType);
    ResourceResponse mockResourceResponse = mock(ResourceResponse.class);
    when(mockResourceResponse.getResource()).thenReturn(mockResource);
    return mockResourceResponse;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) Resource(ddf.catalog.resource.Resource)

Aggregations

Resource (ddf.catalog.resource.Resource)59 ResourceResponse (ddf.catalog.operation.ResourceResponse)29 Test (org.junit.Test)21 ByteArrayInputStream (java.io.ByteArrayInputStream)16 ResourceRequest (ddf.catalog.operation.ResourceRequest)14 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)13 URI (java.net.URI)11 HashMap (java.util.HashMap)11 InputStream (java.io.InputStream)10 Serializable (java.io.Serializable)10 MimeType (javax.activation.MimeType)10 Result (ddf.catalog.data.Result)9 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)9 ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)9 IOException (java.io.IOException)9 Map (java.util.Map)9 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)9 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)8 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)8 Metacard (ddf.catalog.data.Metacard)7