use of ddf.catalog.resource.Resource in project ddf by codice.
the class CswRecordCollectionMessageBodyWriterTest method testWriteToProductData.
@Test
public void testWriteToProductData() throws MimeTypeParseException, IOException {
CswRecordCollectionMessageBodyWriter writer = new CswRecordCollectionMessageBodyWriter(mockManager);
byte[] data = "SampleData".getBytes();
ByteArrayInputStream productData = new ByteArrayInputStream(data);
MimeType mimeType = new MimeType("text", "plain");
MultivaluedMap<String, Object> httpHeaders = new MultivaluedHashMap<>();
Resource resource = new ResourceImpl(productData, mimeType, "ResourceName");
CswRecordCollection collection = new CswRecordCollection();
collection.setMimeType(mimeType.toString());
collection.setResource(resource);
collection.setOutputSchema("http://www.iana.org/assignments/media-types/application/octet-stream");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
writer.writeTo(collection, null, null, null, null, httpHeaders, stream);
assertThat(stream.toByteArray(), is(equalTo(resource.getByteArray())));
}
use of ddf.catalog.resource.Resource 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: {}", 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: {}", LogSanitizer.sanitize(id));
return cswRecordCollection;
}
use of ddf.catalog.resource.Resource in project ddf by codice.
the class AbstractCswSource method retrieveResourceById.
private ResourceResponse retrieveResourceById(Map<String, Serializable> requestProperties, String metacardId) throws ResourceNotFoundException {
Subject subject = (Subject) requestProperties.get(SecurityConstants.SECURITY_SUBJECT);
Csw csw = (subject != null) ? factory.getClientForSubject(subject) : factory.getClient();
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())));
} else {
return null;
}
} catch (CswException | IOException e) {
throw new ResourceNotFoundException(String.format(ERROR_ID_PRODUCT_RETRIEVAL, metacardId), e);
}
}
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) {
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(), 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 RESTEndpointTest method headTest.
@SuppressWarnings({ "unchecked" })
private Response headTest(boolean local) throws Exception {
MetacardImpl metacard;
List<Result> list = new ArrayList<>();
Result result = mock(Result.class);
InputStream inputStream;
UriInfo uriInfo;
Response response;
CatalogFramework framework = givenCatalogFramework();
list.add(result);
QueryResponse queryResponse = mock(QueryResponse.class);
when(queryResponse.getResults()).thenReturn(list);
when(framework.query(isA(QueryRequest.class), isNull())).thenReturn(queryResponse);
metacard = new MetacardImpl();
metacard.setSourceId(GET_SITENAME);
when(result.getMetacard()).thenReturn(metacard);
Resource resource = mock(Resource.class);
inputStream = new ByteArrayInputStream(GET_STREAM.getBytes(GET_OUTPUT_TYPE));
when(resource.getInputStream()).thenReturn(inputStream);
when(resource.getMimeTypeValue()).thenReturn(GET_MIME_TYPE);
when(resource.getName()).thenReturn(GET_FILENAME);
when(framework.transform(isA(Metacard.class), anyString(), isA(Map.class))).thenReturn(resource);
MimeTypeMapper mimeTypeMapper = mock(MimeTypeMapper.class);
when(mimeTypeMapper.getMimeTypeForFileExtension("txt")).thenReturn("text/plain");
when(mimeTypeMapper.getMimeTypeForFileExtension("xml")).thenReturn("text/xml");
CatalogServiceImpl catalogServiceImpl = new CatalogServiceImpl(framework, new AttachmentParserImpl(mimeTypeMapper), mock(AttributeRegistry.class));
catalogServiceImpl.setTikaMimeTypeResolver(new TikaMimeTypeResolver());
FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
catalogServiceImpl.setFilterBuilder(filterBuilder);
uriInfo = createSpecificUriInfo(LOCAL_RETRIEVE_ADDRESS);
RESTEndpoint restEndpoint = new RESTEndpoint(catalogServiceImpl);
if (local) {
response = restEndpoint.getHeaders(GET_ID, uriInfo, null);
} else {
response = restEndpoint.getHeaders(null, GET_ID, uriInfo, null);
}
return response;
}
Aggregations