Search in sources :

Example 56 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class TestCswEndpoint method setUpMocksForProductRetrieval.

private void setUpMocksForProductRetrieval(boolean includeMimeType) throws ResourceNotFoundException, IOException, ResourceNotSupportedException {
    ResourceResponse resourceResponse = mock(ResourceResponse.class);
    Resource resource = mock(Resource.class);
    if (includeMimeType) {
        MimeType mimeType = mock(MimeType.class);
        when(resource.getMimeType()).thenReturn(mimeType);
    }
    when(resourceResponse.getResource()).thenReturn(resource);
    when(catalogFramework.getLocalResource(any(ResourceRequest.class))).thenReturn(resourceResponse);
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) Resource(ddf.catalog.resource.Resource) ResourceRequest(ddf.catalog.operation.ResourceRequest) MimeType(javax.activation.MimeType)

Example 57 with MimeType

use of javax.activation.MimeType 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
     */
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;
}
Also used : ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) Resource(ddf.catalog.resource.Resource) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) MimeType(javax.activation.MimeType)

Example 58 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class TestResourceReader method testRetrieveResourceMimeTypeTextHtml.

/**
     * Tests the case in which the Resource in the ResourceResponse returned by the
     * URLResourceReader has a text/html mime type.
     */
@Test
public void testRetrieveResourceMimeTypeTextHtml() throws Exception {
    // Setup
    String httpUriStr = HTTP_SCHEME_PLUS_SEP + MOCK_HTTP_SERVER_HOST + ":" + MOCK_HTTP_SERVER_PORT + MOCK_HTTP_SERVER_PATH;
    URI uri = new URI(httpUriStr);
    Response mockResponse = getMockResponse();
    setupMockWebClient(mockResponse);
    ResourceResponse mockResourceResponse = getMockResourceResponse(new MimeType("application/octet-stream"));
    URLResourceReader mockUrlResourceReader = getMockUrlResourceReader(uri, mockResourceResponse);
    setupMockTika(MediaType.TEXT_HTML);
    OgcUrlResourceReader resourceReader = new OgcUrlResourceReader(mockUrlResourceReader, mockTika);
    HashMap<String, Serializable> arguments = new HashMap<String, Serializable>();
    // Perform Test
    ResourceResponse resourceResponse = resourceReader.retrieveResource(uri, arguments);
    // Verify
    StringWriter writer = new StringWriter();
    IOUtils.copy(resourceResponse.getResource().getInputStream(), writer, MOCK_HTTP_SERVER_ENCODING);
    String responseString = writer.toString();
    LOGGER.info("Response {}", responseString);
    assertThat(responseString, is("<html><script type=\"text/javascript\">window.location.replace(\"" + httpUriStr + "\");</script></html>"));
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) Response(javax.ws.rs.core.Response) Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) URLResourceReader(ddf.catalog.resource.impl.URLResourceReader) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) URI(java.net.URI) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 59 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class TestResourceReader method testRetrieveResourceApplicationUnknownResourceMimeType.

/**
     * Tests the case in which the Resource in the ResourceResponse returned by the
     * URLResourceReader has an application/unknown mime type.
     */
@Test
public void testRetrieveResourceApplicationUnknownResourceMimeType() throws Exception {
    // Setup
    String httpUriStr = HTTP_SCHEME_PLUS_SEP + MOCK_HTTP_SERVER_HOST + ":" + MOCK_HTTP_SERVER_PORT + MOCK_HTTP_SERVER_PATH;
    URI uri = new URI(httpUriStr);
    Response mockResponse = getMockResponse();
    setupMockWebClient(mockResponse);
    ResourceResponse mockResourceResponse = getMockResourceResponse(new MimeType("application/octet-stream"));
    URLResourceReader mockUrlResourceReader = getMockUrlResourceReader(uri, mockResourceResponse);
    setupMockTika(MediaType.TEXT_HTML);
    OgcUrlResourceReader resourceReader = new OgcUrlResourceReader(mockUrlResourceReader, mockTika);
    HashMap<String, Serializable> arguments = new HashMap<String, Serializable>();
    // Perform Test
    ResourceResponse resourceResponse = resourceReader.retrieveResource(uri, arguments);
    // Verify
    StringWriter writer = new StringWriter();
    IOUtils.copy(resourceResponse.getResource().getInputStream(), writer, MOCK_HTTP_SERVER_ENCODING);
    String responseString = writer.toString();
    LOGGER.info("Response {}", responseString);
    assertThat(responseString, is("<html><script type=\"text/javascript\">window.location.replace(\"" + httpUriStr + "\");</script></html>"));
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) Response(javax.ws.rs.core.Response) Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) URLResourceReader(ddf.catalog.resource.impl.URLResourceReader) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) URI(java.net.URI) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 60 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class TestResourceReader method testRetrieveResourceApplicationOctetStreamResourceMimeType.

/**
     * Tests the case in which the Resource in the ResourceResponse returned by the
     * URLResourceReader has an application/octet-stream mime type.
     */
@Test
public void testRetrieveResourceApplicationOctetStreamResourceMimeType() throws Exception {
    // Setup
    String httpUriStr = HTTP_SCHEME_PLUS_SEP + MOCK_HTTP_SERVER_HOST + ":" + MOCK_HTTP_SERVER_PORT + MOCK_HTTP_SERVER_PATH;
    URI uri = new URI(httpUriStr);
    Response mockResponse = getMockResponse();
    setupMockWebClient(mockResponse);
    ResourceResponse mockResourceResponse = getMockResourceResponse(new MimeType("application/octet-stream"));
    URLResourceReader mockUrlResourceReader = getMockUrlResourceReader(uri, mockResourceResponse);
    setupMockTika(MediaType.TEXT_HTML);
    OgcUrlResourceReader resourceReader = new OgcUrlResourceReader(mockUrlResourceReader, mockTika);
    HashMap<String, Serializable> arguments = new HashMap<String, Serializable>();
    // Perform Test
    ResourceResponse resourceResponse = resourceReader.retrieveResource(uri, arguments);
    // Verify
    StringWriter writer = new StringWriter();
    IOUtils.copy(resourceResponse.getResource().getInputStream(), writer, MOCK_HTTP_SERVER_ENCODING);
    String responseString = writer.toString();
    LOGGER.info("Response {}", responseString);
    assertThat(responseString, is("<html><script type=\"text/javascript\">window.location.replace(\"" + httpUriStr + "\");</script></html>"));
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) Response(javax.ws.rs.core.Response) Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) URLResourceReader(ddf.catalog.resource.impl.URLResourceReader) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) URI(java.net.URI) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Aggregations

MimeType (javax.activation.MimeType)67 Test (org.junit.Test)38 Metacard (ddf.catalog.data.Metacard)21 URI (java.net.URI)14 HashMap (java.util.HashMap)14 MimeTypeParseException (javax.activation.MimeTypeParseException)14 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)13 BundleContext (org.osgi.framework.BundleContext)13 ServiceReference (org.osgi.framework.ServiceReference)13 CatalogFramework (ddf.catalog.CatalogFramework)10 ResourceResponse (ddf.catalog.operation.ResourceResponse)10 Serializable (java.io.Serializable)9 Resource (ddf.catalog.resource.Resource)8 IOException (java.io.IOException)8 File (java.io.File)7 Matchers.anyString (org.mockito.Matchers.anyString)7 MetacardCreationException (ddf.catalog.data.MetacardCreationException)6 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)6 Response (javax.ws.rs.core.Response)6 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)5