Search in sources :

Example 1 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class TestOpenSearchSource method testRetrieveResource.

/**
     * Basic retrieve product case. Tests the url sent to the connection is correct.
     *
     * @throws ResourceNotSupportedException
     * @throws IOException
     * @throws ResourceNotFoundException
     */
@Test
public void testRetrieveResource() throws ResourceNotSupportedException, IOException, ResourceNotFoundException {
    // given
    FirstArgumentCapture answer = new FirstArgumentCapture(getBinaryData());
    OpenSearchSource source = givenSource(answer);
    Map<String, Serializable> requestProperties = new HashMap<String, Serializable>();
    requestProperties.put(Metacard.ID, SAMPLE_ID);
    // when
    ResourceResponse response = source.retrieveResource(null, requestProperties);
    Assert.assertEquals(3, response.getResource().getByteArray().length);
}
Also used : Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 2 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class TestResourceMetacardTransformer method getResourceResponse.

private ResourceResponse getResourceResponse(Resource resource) {
    ResourceResponse resourceResponse = mock(ResourceResponse.class);
    when(resourceResponse.getResource()).thenReturn(resource);
    return resourceResponse;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse)

Example 3 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class URLResourceReader method retrieveHttpProduct.

private ResourceResponse retrieveHttpProduct(URI resourceURI, String productName, String bytesToSkip, Map<String, Serializable> properties) throws ResourceNotFoundException {
    try {
        LOGGER.debug("Opening connection to: {}", resourceURI.toString());
        WebClient client = getWebClient(resourceURI.toString(), properties);
        Object subjectObj = properties.get(SecurityConstants.SECURITY_SUBJECT);
        if (subjectObj != null) {
            Subject subject = (Subject) subjectObj;
            LOGGER.debug("Setting Subject on webclient: {}", subject);
            RestSecurity.setSubjectOnClient(subject, client);
        }
        Response response = client.get();
        MultivaluedMap<String, Object> headers = response.getHeaders();
        List<Object> cdHeaders = headers.get(HttpHeaders.CONTENT_DISPOSITION);
        if (cdHeaders != null && !cdHeaders.isEmpty()) {
            String contentHeader = (String) cdHeaders.get(0);
            productName = StringUtils.defaultIfBlank(handleContentDispositionHeader(contentHeader), productName);
        }
        String mimeType = getMimeType(resourceURI, productName);
        Response clientResponse = client.get();
        InputStream is = null;
        Object entityObj = clientResponse.getEntity();
        if (entityObj instanceof InputStream) {
            is = (InputStream) entityObj;
            if (Response.Status.OK.getStatusCode() != clientResponse.getStatus() && Response.Status.PARTIAL_CONTENT.getStatusCode() != clientResponse.getStatus()) {
                String error = null;
                try {
                    if (is != null) {
                        error = IOUtils.toString(is);
                    }
                } catch (IOException ioe) {
                    LOGGER.debug("Could not convert error message to a string for output.", ioe);
                }
                String errorMsg = "Received error code while retrieving resource (status " + clientResponse.getStatus() + "): " + error;
                throw new ResourceNotFoundException(errorMsg);
            }
        } else {
            throw new ResourceNotFoundException("Received null response while retrieving resource.");
        }
        long responseBytesSkipped = 0L;
        if (headers.getFirst(HttpHeaders.CONTENT_RANGE) != null) {
            String contentRangeHeader = String.valueOf(headers.getFirst(HttpHeaders.CONTENT_RANGE));
            responseBytesSkipped = Long.parseLong(StringUtils.substringBetween(contentRangeHeader.toLowerCase(), "bytes ", "-"));
        }
        alignStream(is, Long.parseLong(bytesToSkip), responseBytesSkipped);
        return new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(is), mimeType, FilenameUtils.getName(productName)));
    } catch (MimeTypeResolutionException | IOException | WebApplicationException e) {
        LOGGER.info("Error retrieving resource", e);
        throw new ResourceNotFoundException("Unable to retrieve resource at: " + resourceURI.toString(), e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) WebClient(org.apache.cxf.jaxrs.client.WebClient) Subject(ddf.security.Subject) ResourceResponse(ddf.catalog.operation.ResourceResponse) Response(javax.ws.rs.core.Response) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) BufferedInputStream(java.io.BufferedInputStream) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 4 with ResourceResponse

use of ddf.catalog.operation.ResourceResponse in project ddf by codice.

the class ResourceReaderTest method doVerification.

private ResourceResponse doVerification(URI uri, String filename, String expectedMimeType, Map<String, Serializable> arguments) throws URISyntaxException, IOException, ResourceNotFoundException {
    URLResourceReader resourceReader = new TestURLResourceReader(mimeTypeMapper);
    resourceReader.setRootResourceDirectories(ImmutableSet.of(ABSOLUTE_PATH + TEST_PATH));
    // Test using the URL ResourceReader
    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));
    assertTrue(resource.getMimeType().toString().contains(expectedMimeType));
    return resourceResponse;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) Resource(ddf.catalog.resource.Resource) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 5 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)

Aggregations

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