use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ResourceReaderTest method testNameInContentDisposition.
@Test
public void testNameInContentDisposition() throws Exception {
URI uri = new URI(HTTP_SCHEME_PLUS_SEP + HOST + TEST_PATH + BAD_FILE_NAME);
Response mockResponse = mock(Response.class);
when(mockWebClient.get()).thenReturn(mockResponse);
MultivaluedMap<String, Object> map = new MultivaluedHashMap<>();
map.put(HttpHeaders.CONTENT_DISPOSITION, Arrays.<Object>asList("inline; filename=\"" + JPEG_FILE_NAME_1 + "\""));
when(mockResponse.getHeaders()).thenReturn(map);
when(mockResponse.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
when(mockResponse.getEntity()).thenReturn(getBinaryData());
ResourceResponse response = verifyFileFromURLResourceReader(uri, JPEG_FILE_NAME_1, JPEG_MIME_TYPE, null);
// verify that we got the entire resource
assertEquals(5, response.getResource().getByteArray().length);
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ResourceReaderTest method testServerSupportsPartialContentResponseWithNotEnoughOffset.
/**
* Tests that a Partial Content response that has a smaller byte offset than what was requested
* still returns an input stream starting at the requested byte offset by skipping ahead in the
* input stream.
* @throws Exception
*/
@Test
public void testServerSupportsPartialContentResponseWithNotEnoughOffset() throws Exception {
URI uri = new URI(HTTP_SCHEME_PLUS_SEP + HOST + TEST_PATH + BAD_FILE_NAME);
Response mockResponse = mock(Response.class);
when(mockWebClient.get()).thenReturn(mockResponse);
MultivaluedMap<String, Object> map = new MultivaluedHashMap<>();
map.put(HttpHeaders.CONTENT_DISPOSITION, Arrays.asList("inline; filename=\"" + JPEG_FILE_NAME_1 + "\""));
map.put(HttpHeaders.CONTENT_RANGE, Arrays.asList("Bytes 1-4/5"));
when(mockResponse.getHeaders()).thenReturn(map);
when(mockResponse.getStatus()).thenReturn(Response.Status.PARTIAL_CONTENT.getStatusCode());
when(mockResponse.getEntity()).thenReturn(getBinaryDataWithOffset(1));
String bytesToSkip = "2";
ResourceResponse response = verifyFileFromURLResourceReader(uri, JPEG_FILE_NAME_1, JPEG_MIME_TYPE, bytesToSkip);
// verify that the requested bytes 3-5 were returned
assertEquals(3, response.getResource().getByteArray().length);
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ResourceReaderTest method testServerSupportsPartialContentResponseWithCorrectOffset.
/**
* Tests that a Partial Content response that has the same byte offset as what was requested
* returns an input stream starting at the requested byte offset.
* @throws Exception
*/
@Test
public void testServerSupportsPartialContentResponseWithCorrectOffset() throws Exception {
URI uri = new URI(HTTP_SCHEME_PLUS_SEP + HOST + TEST_PATH + BAD_FILE_NAME);
Response mockResponse = mock(Response.class);
when(mockWebClient.get()).thenReturn(mockResponse);
MultivaluedMap<String, Object> map = new MultivaluedHashMap<>();
map.put(HttpHeaders.CONTENT_DISPOSITION, Arrays.asList("inline; filename=\"" + JPEG_FILE_NAME_1 + "\""));
map.put(HttpHeaders.CONTENT_RANGE, Arrays.asList("Bytes 2-4/5"));
when(mockResponse.getHeaders()).thenReturn(map);
when(mockResponse.getStatus()).thenReturn(Response.Status.PARTIAL_CONTENT.getStatusCode());
when(mockResponse.getEntity()).thenReturn(getBinaryDataWithOffset(2));
String bytesToSkip = "2";
ResourceResponse response = verifyFileFromURLResourceReader(uri, JPEG_FILE_NAME_1, JPEG_MIME_TYPE, bytesToSkip);
// verify that the requested bytes 3-5 were returned
assertEquals(3, response.getResource().getByteArray().length);
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ResourceReaderTest method verifyFile.
private void verifyFile(String filePath, String filename, String expectedMimeType, String... rootResourceDirectories) throws Exception {
URLResourceReader resourceReader = new URLResourceReader(mimeTypeMapper);
resourceReader.setRootResourceDirectories(new HashSet<String>(Arrays.asList(rootResourceDirectories)));
HashMap<String, Serializable> arguments = new HashMap<String, Serializable>();
LOGGER.info("Getting resource: {}", filePath);
// Test using the URL ResourceReader
File file = new File(filePath);
URI uri = file.toURI();
LOGGER.info("URI: {}", uri.toString());
ResourceResponse resourceResponse = resourceReader.retrieveResource(uri, arguments);
Resource resource = resourceResponse.getResource();
assert (resource != null);
LOGGER.info("MimeType: {}", resource.getMimeType());
LOGGER.info("Got resource: {}", resource.getName());
String name = resource.getName();
assertNotNull(name);
assertThat(name, is(filename));
assertThat(resource.getMimeType().toString(), containsString(expectedMimeType));
}
use of ddf.catalog.operation.ResourceResponse 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;
}
Aggregations