Search in sources :

Example 16 with ResourceImpl

use of ddf.catalog.resource.impl.ResourceImpl in project ddf by codice.

the class ResourceImplTest method testResourceImplNullMimeType.

@Test
public void testResourceImplNullMimeType() {
    InputStream is = null;
    try {
        is = new FileInputStream(content);
    } catch (IOException e) {
        LOGGER.error("IO Failure", e);
        new Failure(null, e);
    }
    ResourceImpl ri = new ResourceImpl(is, (MimeType) null, TEST_NAME);
    assertEquals(null, ri.getMimeType());
    ri = new ResourceImpl(is, (String) null, TEST_NAME);
    assertEquals(null, ri.getMimeType());
}
Also used : ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Failure(org.junit.runner.notification.Failure) Test(org.junit.Test)

Example 17 with ResourceImpl

use of ddf.catalog.resource.impl.ResourceImpl in project ddf by codice.

the class ResourceImplTest method testResourceImplNullInputStream.

@Test
public void testResourceImplNullInputStream() {
    InputStream is = null;
    ResourceImpl ri = new ResourceImpl(is, mimeType, TEST_NAME);
    ri.setSize(content.length());
    assertEquals(is, ri.getInputStream());
    assertEquals(mimeType.toString(), ri.getMimeType().toString());
    assertEquals(content.length(), ri.getSize());
}
Also used : ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 18 with ResourceImpl

use of ddf.catalog.resource.impl.ResourceImpl 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)

Aggregations

ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)18 IOException (java.io.IOException)10 Resource (ddf.catalog.resource.Resource)8 InputStream (java.io.InputStream)8 Test (org.junit.Test)8 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)7 FileInputStream (java.io.FileInputStream)7 Failure (org.junit.runner.notification.Failure)6 ResourceResponse (ddf.catalog.operation.ResourceResponse)5 MimeType (javax.activation.MimeType)5 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)4 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)2 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)2 BufferedInputStream (java.io.BufferedInputStream)2 Serializable (java.io.Serializable)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 MimeTypeParseException (javax.activation.MimeTypeParseException)2