Search in sources :

Example 36 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class TestRestEndpoint method headTest.

private Response headTest(boolean local) throws CatalogTransformerException, URISyntaxException, UnsupportedEncodingException, UnsupportedQueryException, SourceUnavailableException, FederationException, IngestException {
    MetacardImpl metacard = null;
    List<Result> list = new ArrayList<Result>();
    Result result = mock(Result.class);
    InputStream inputStream = null;
    UriInfo uriInfo;
    Response response;
    CatalogFramework framework = givenCatalogFramework(SAMPLE_ID);
    list.add(result);
    QueryResponse queryResponse = mock(QueryResponse.class);
    when(queryResponse.getResults()).thenReturn(list);
    when(framework.query(isA(QueryRequest.class), isNull(FederationStrategy.class))).thenReturn(queryResponse);
    metacard = new MetacardImpl();
    metacard.setSourceId(GET_SITENAME);
    when(result.getMetacard()).thenReturn(metacard);
    Resource resource = mock(Resource.class);
    inputStream = new ByteArrayInputStream(GET_STREAM.getBytes(GET_OUTPUT_TYPE));
    when(resource.getInputStream()).thenReturn(inputStream);
    when(resource.getMimeTypeValue()).thenReturn(GET_MIME_TYPE);
    when(resource.getName()).thenReturn(GET_FILENAME);
    when(framework.transform(isA(Metacard.class), anyString(), isA(Map.class))).thenReturn(resource);
    RESTEndpoint restEndpoint = new RESTEndpoint(framework);
    restEndpoint.setTikaMimeTypeResolver(new TikaMimeTypeResolver());
    FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
    restEndpoint.setFilterBuilder(filterBuilder);
    uriInfo = createSpecificUriInfo(LOCAL_RETRIEVE_ADDRESS);
    if (local) {
        response = restEndpoint.getHeaders(GET_ID, uriInfo, null);
    } else {
        response = restEndpoint.getHeaders(null, GET_ID, uriInfo, null);
    }
    return response;
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) FederationStrategy(ddf.catalog.federation.FederationStrategy) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Resource(ddf.catalog.resource.Resource) TikaMimeTypeResolver(ddf.mime.tika.TikaMimeTypeResolver) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) FilterBuilder(ddf.catalog.filter.FilterBuilder) QueryResponse(ddf.catalog.operation.QueryResponse) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) CatalogFramework(ddf.catalog.CatalogFramework) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) UriInfo(javax.ws.rs.core.UriInfo)

Example 37 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class TestGetRecordsMessageBodyReader method testPartialContentResponseHandling.

@Test
public void testPartialContentResponseHandling() throws Exception {
    CswSourceConfiguration config = new CswSourceConfiguration(encryptionService);
    config.setMetacardCswMappings(DefaultCswRecordMap.getCswToMetacardAttributeNames());
    config.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA);
    GetRecordsMessageBodyReader reader = new GetRecordsMessageBodyReader(mockProvider, config);
    String sampleData = "SampleData";
    byte[] data = sampleData.getBytes();
    ByteArrayInputStream dataInputStream = new ByteArrayInputStream(data);
    MultivaluedMap<String, String> httpHeaders = new MultivaluedHashMap<>();
    httpHeaders.add(CswConstants.PRODUCT_RETRIEVAL_HTTP_HEADER, "TRUE");
    httpHeaders.add(HttpHeaders.CONTENT_DISPOSITION, String.format("inline; filename=ResourceName"));
    httpHeaders.add(HttpHeaders.CONTENT_RANGE, String.format("bytes 1-%d/%d", sampleData.length() - 1, sampleData.length()));
    MediaType mediaType = new MediaType("text", "plain");
    CswRecordCollection cswRecords = reader.readFrom(CswRecordCollection.class, null, null, mediaType, httpHeaders, dataInputStream);
    Resource resource = cswRecords.getResource();
    // assert that the CswRecordCollection properly extracted the bytes skipped from the Partial Content response
    assertThat(cswRecords.getResourceProperties().get(GetRecordsMessageBodyReader.BYTES_SKIPPED), is(1L));
    // assert that the input stream has not been skipped at this stage. Since AbstractCswSource has the number
    // of bytes that was attempted to be skipped, the stream must be aligned there instead.
    assertThat(resource.getByteArray(), is(data));
}
Also used : CswSourceConfiguration(org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) Resource(ddf.catalog.resource.Resource) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Example 38 with Resource

use of ddf.catalog.resource.Resource in project alliance by codice.

the class CatalogOutputAdapterTest method testGetImage.

@Test
public void testGetImage() throws IOException {
    InputStream is = getInputStream(I_3001A);
    ResourceResponse resourceResponse = mock(ResourceResponse.class);
    Resource resource = mock(Resource.class);
    when(resourceResponse.getResource()).thenReturn(resource);
    when(resource.getInputStream()).thenReturn(is);
    BufferedImage image = catalogOutputAdapter.getImage(resourceResponse);
    assertThat(image, is(notNullValue()));
    assertThat(image.getWidth(), is(1024));
    assertThat(image.getHeight(), is(1024));
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Resource(ddf.catalog.resource.Resource) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 39 with Resource

use of ddf.catalog.resource.Resource in project alliance by codice.

the class CatalogOutputAdapterTest method testGetImageNullInputStream.

@Test(expected = IllegalStateException.class)
public void testGetImageNullInputStream() throws IOException {
    ResourceResponse resourceResponse = mock(ResourceResponse.class);
    Resource resource = mock(Resource.class);
    when(resourceResponse.getResource()).thenReturn(resource);
    when(resource.getInputStream()).thenReturn(null);
    catalogOutputAdapter.getImage(resourceResponse);
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) Resource(ddf.catalog.resource.Resource) Test(org.junit.Test)

Example 40 with Resource

use of ddf.catalog.resource.Resource in project ddf by codice.

the class DumpCommand method getDerivedResources.

@Nullable
private Map.Entry<String, Resource> getDerivedResources(Metacard metacard, Serializable serializable) {
    if (!(serializable instanceof String)) {
        LOGGER.debug("Input ({}) should have been a string but was a {}", serializable, serializable.getClass());
        return null;
    }
    URI uri = null;
    try {
        uri = new URI((String) serializable);
    } catch (URISyntaxException e) {
        LOGGER.debug("Invalid Derived Resource URI Syntax for metacard : {}", metacard.getId(), e);
        return null;
    }
    String derivedResourceFragment = uri.getFragment();
    if (!ContentItem.CONTENT_SCHEME.equals(uri.getScheme()) || StringUtils.isBlank(derivedResourceFragment)) {
        return null;
    }
    String fragment = CONTENT + File.separator + derivedResourceFragment + File.separator;
    Resource resource = getResource(metacard);
    if (resource == null) {
        return null;
    }
    return new SimpleEntry<>(fragment + uri.getSchemeSpecificPart() + "-" + resource.getName(), resource);
}
Also used : SimpleEntry(java.util.AbstractMap.SimpleEntry) Resource(ddf.catalog.resource.Resource) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Nullable(javax.annotation.Nullable)

Aggregations

Resource (ddf.catalog.resource.Resource)59 ResourceResponse (ddf.catalog.operation.ResourceResponse)29 Test (org.junit.Test)21 ByteArrayInputStream (java.io.ByteArrayInputStream)16 ResourceRequest (ddf.catalog.operation.ResourceRequest)14 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)13 URI (java.net.URI)11 HashMap (java.util.HashMap)11 InputStream (java.io.InputStream)10 Serializable (java.io.Serializable)10 MimeType (javax.activation.MimeType)10 Result (ddf.catalog.data.Result)9 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)9 ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)9 IOException (java.io.IOException)9 Map (java.util.Map)9 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)9 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)8 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)8 Metacard (ddf.catalog.data.Metacard)7