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."));
}
}
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);
}
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;
}
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;
}
Aggregations