Search in sources :

Example 1 with ResourceImpl

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

the class TestOpenSearchSource method givenSource.

private OpenSearchSource givenSource(Answer<BinaryContent> answer) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
    WebClient client = mock(WebClient.class);
    ResourceReader mockReader = mock(ResourceReader.class);
    Response clientResponse = mock(Response.class);
    when(clientResponse.getEntity()).thenReturn(getBinaryData());
    when(clientResponse.getHeaderString(eq(OpenSearchSource.HEADER_ACCEPT_RANGES))).thenReturn(OpenSearchSource.BYTES);
    when(client.get()).thenReturn(clientResponse);
    SecureCxfClientFactory factory = getMockFactory(client);
    when(mockReader.retrieveResource(any(URI.class), any(Map.class))).thenReturn(new ResourceResponseImpl(new ResourceImpl(getBinaryData(), "")));
    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<String, Object>();
    headers.put(HttpHeaders.CONTENT_TYPE, Arrays.<Object>asList("application/octet-stream"));
    when(clientResponse.getHeaders()).thenReturn(headers);
    OverriddenOpenSearchSource source = new OverriddenOpenSearchSource(FILTER_ADAPTER, encryptionService);
    source.setEndpointUrl("http://localhost:8181/services/catalog/query");
    source.setParameters(DEFAULT_PARAMETERS);
    source.init();
    source.setLocalQueryOnly(true);
    source.setInputTransformer(getMockInputTransformer());
    source.factory = factory;
    source.setResourceReader(mockReader);
    return source;
}
Also used : ResourceReader(ddf.catalog.resource.ResourceReader) SecureCxfClientFactory(org.codice.ddf.cxf.SecureCxfClientFactory) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) Matchers.containsString(org.hamcrest.Matchers.containsString) WebClient(org.apache.cxf.jaxrs.client.WebClient) URI(java.net.URI) Response(javax.ws.rs.core.Response) ResourceResponse(ddf.catalog.operation.ResourceResponse) SourceResponse(ddf.catalog.operation.SourceResponse) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 2 with ResourceImpl

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

the class WfsSource method retrieveResource.

@Override
public ResourceResponse retrieveResource(URI uri, Map<String, Serializable> arguments) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
    StringBuilder strBuilder = new StringBuilder();
    strBuilder.append("<html><script type=\"text/javascript\">window.location.replace(\"");
    strBuilder.append(uri);
    strBuilder.append("\");</script></html>");
    Resource resource = new ResourceImpl(IOUtils.toInputStream(strBuilder.toString()), MediaType.TEXT_HTML, getId() + " Resource");
    return new ResourceResponseImpl(resource);
}
Also used : ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) Resource(ddf.catalog.resource.Resource) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl)

Example 3 with ResourceImpl

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

the class ContentResourceReader method retrieveResource.

@Override
public ResourceResponse retrieveResource(URI resourceUri, Map<String, Serializable> arguments) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
    LOGGER.trace("ENTERING: retrieveResource");
    ResourceResponse response = null;
    if (resourceUri == null) {
        throw new ResourceNotFoundException("Unable to find resource - resource URI was null");
    }
    if (resourceUri.getScheme().equals(ContentItem.CONTENT_SCHEME)) {
        LOGGER.debug("Resource URI is content scheme");
        String contentId = resourceUri.getSchemeSpecificPart();
        if (contentId != null && !contentId.isEmpty()) {
            if (arguments != null && arguments.get(ContentItem.QUALIFIER_KEYWORD) instanceof String && StringUtils.isNotBlank((String) arguments.get(ContentItem.QUALIFIER_KEYWORD))) {
                try {
                    resourceUri = new URI(resourceUri.getScheme(), resourceUri.getSchemeSpecificPart(), (String) arguments.get(ContentItem.QUALIFIER_KEYWORD));
                } catch (URISyntaxException e) {
                    throw new ResourceNotFoundException("Unable to create with qualifier", e);
                }
            }
            ReadStorageRequest readRequest = new ReadStorageRequestImpl(resourceUri, arguments);
            try {
                ReadStorageResponse readResponse = storage.read(readRequest);
                ContentItem contentItem = readResponse.getContentItem();
                String fileName = contentItem.getFilename();
                LOGGER.debug("resource name: {}", fileName);
                InputStream is = contentItem.getInputStream();
                response = new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(is), contentItem.getMimeType(), fileName));
            } catch (StorageException e) {
                throw new ResourceNotFoundException(e);
            }
        }
    }
    LOGGER.trace("EXITING: retrieveResource");
    return response;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) ReadStorageResponse(ddf.catalog.content.operation.ReadStorageResponse) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) URI(java.net.URI) ReadStorageRequest(ddf.catalog.content.operation.ReadStorageRequest) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) BufferedInputStream(java.io.BufferedInputStream) ReadStorageRequestImpl(ddf.catalog.content.operation.impl.ReadStorageRequestImpl) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) StorageException(ddf.catalog.content.StorageException) ContentItem(ddf.catalog.content.data.ContentItem)

Example 4 with ResourceImpl

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

the class ResourceImplTest method testResourceImplNegativeSize.

@Test
public void testResourceImplNegativeSize() {
    InputStream is;
    try {
        is = new FileInputStream(content);
        ResourceImpl ri = new ResourceImpl(is, mimeType, TEST_NAME);
        ri.setSize(-20L);
        assertEquals(-1, ri.getSize());
    } catch (IOException e) {
        LOGGER.error("IO Failure", e);
        new Failure(null, e);
    }
}
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 5 with ResourceImpl

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

the class CswRecordCollectionMessageBodyWriterTest method testWriteToProductData.

@Test
public void testWriteToProductData() throws MimeTypeParseException, IOException {
    CswRecordCollectionMessageBodyWriter writer = new CswRecordCollectionMessageBodyWriter(mockManager);
    byte[] data = "SampleData".getBytes();
    ByteArrayInputStream productData = new ByteArrayInputStream(data);
    MimeType mimeType = new MimeType("text", "plain");
    MultivaluedMap<String, Object> httpHeaders = new MultivaluedHashMap<>();
    Resource resource = new ResourceImpl(productData, mimeType, "ResourceName");
    CswRecordCollection collection = new CswRecordCollection();
    collection.setMimeType(mimeType.toString());
    collection.setResource(resource);
    collection.setOutputSchema("http://www.iana.org/assignments/media-types/application/octet-stream");
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    writer.writeTo(collection, null, null, null, null, httpHeaders, stream);
    assertThat(stream.toByteArray(), is(equalTo(resource.getByteArray())));
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) Resource(ddf.catalog.resource.Resource) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Aggregations

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