Search in sources :

Example 26 with ResourceResponse

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);
}
Also used : Response(javax.ws.rs.core.Response) ResourceResponse(ddf.catalog.operation.ResourceResponse) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResourceResponse(ddf.catalog.operation.ResourceResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) Test(org.junit.Test)

Example 27 with ResourceResponse

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);
}
Also used : Response(javax.ws.rs.core.Response) ResourceResponse(ddf.catalog.operation.ResourceResponse) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResourceResponse(ddf.catalog.operation.ResourceResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) Test(org.junit.Test)

Example 28 with ResourceResponse

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);
}
Also used : Response(javax.ws.rs.core.Response) ResourceResponse(ddf.catalog.operation.ResourceResponse) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResourceResponse(ddf.catalog.operation.ResourceResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) Test(org.junit.Test)

Example 29 with ResourceResponse

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));
}
Also used : Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Resource(ddf.catalog.resource.Resource) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) URI(java.net.URI)

Example 30 with ResourceResponse

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;
}
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)

Aggregations

ResourceResponse (ddf.catalog.operation.ResourceResponse)71 Test (org.junit.Test)36 HashMap (java.util.HashMap)17 Resource (ddf.catalog.resource.Resource)16 URI (java.net.URI)16 Serializable (java.io.Serializable)15 Metacard (ddf.catalog.data.Metacard)13 IOException (java.io.IOException)13 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)13 ResourceRequest (ddf.catalog.operation.ResourceRequest)12 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)12 MimeType (javax.activation.MimeType)11 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)10 Response (javax.ws.rs.core.Response)10 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)8 URLResourceReader (ddf.catalog.resource.impl.URLResourceReader)7 Matchers.anyString (org.mockito.Matchers.anyString)7 InputStream (java.io.InputStream)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6