Search in sources :

Example 26 with Resource

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

Example 27 with Resource

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

the class CswEndpoint method queryProductById.

private CswRecordCollection queryProductById(String id, String rangeValue) throws CswException, UnsupportedQueryException {
    final ResourceRequestById resourceRequest = new ResourceRequestById(id);
    long bytesToSkip = getRange(rangeValue);
    if (bytesToSkip > 0) {
        LOGGER.debug("Bytes to skip: {}", bytesToSkip);
        resourceRequest.getProperties().put(CswConstants.BYTES_TO_SKIP, bytesToSkip);
    }
    ResourceResponse resourceResponse;
    try {
        resourceResponse = framework.getLocalResource(resourceRequest);
    } catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
        throw new CswException(String.format(ERROR_ID_PRODUCT_RETRIEVAL, id), e);
    }
    Resource resource = resourceResponse.getResource();
    MimeType mimeType = resource.getMimeType();
    if (mimeType == null) {
        try {
            mimeType = new MimeType(MediaType.APPLICATION_OCTET_STREAM);
            resource = new ResourceImpl(resource.getInputStream(), mimeType, resource.getName());
        } catch (MimeTypeParseException e) {
            throw new CswException(String.format("Could not create mime type upon null mimeType, for mime %s.", MediaType.APPLICATION_OCTET_STREAM), e);
        }
    }
    CswRecordCollection cswRecordCollection = new CswRecordCollection();
    cswRecordCollection.setResource(resource);
    cswRecordCollection.setOutputSchema(OCTET_STREAM_OUTPUT_SCHEMA);
    LOGGER.debug("Successfully retrieved product for ID: {}", LogSanitizer.sanitize(id));
    return cswRecordCollection;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) Resource(ddf.catalog.resource.Resource) IOException(java.io.IOException) MimeType(javax.activation.MimeType) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 28 with Resource

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

the class AbstractCswSource method retrieveResourceById.

private ResourceResponse retrieveResourceById(Map<String, Serializable> requestProperties, String metacardId) throws ResourceNotFoundException {
    Subject subject = (Subject) requestProperties.get(SecurityConstants.SECURITY_SUBJECT);
    Csw csw = (subject != null) ? factory.getClientForSubject(subject) : factory.getClient();
    GetRecordByIdRequest getRecordByIdRequest = new GetRecordByIdRequest();
    getRecordByIdRequest.setService(CswConstants.CSW);
    getRecordByIdRequest.setOutputSchema(OCTET_STREAM_OUTPUT_SCHEMA);
    getRecordByIdRequest.setOutputFormat(MediaType.APPLICATION_OCTET_STREAM);
    getRecordByIdRequest.setId(metacardId);
    String rangeValue = "";
    long requestedBytesToSkip = 0;
    if (requestProperties.containsKey(CswConstants.BYTES_TO_SKIP)) {
        requestedBytesToSkip = (Long) requestProperties.get(CswConstants.BYTES_TO_SKIP);
        rangeValue = String.format("%s%s-", CswConstants.BYTES_EQUAL, requestProperties.get(CswConstants.BYTES_TO_SKIP).toString());
        LOGGER.debug("Range: {}", rangeValue);
    }
    CswRecordCollection recordCollection;
    try {
        recordCollection = csw.getRecordById(getRecordByIdRequest, rangeValue);
        Resource resource = recordCollection.getResource();
        if (resource != null) {
            long responseBytesSkipped = 0L;
            if (recordCollection.getResourceProperties().get(BYTES_SKIPPED) != null) {
                responseBytesSkipped = (Long) recordCollection.getResourceProperties().get(BYTES_SKIPPED);
            }
            alignStream(resource.getInputStream(), requestedBytesToSkip, responseBytesSkipped);
            return new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(resource.getInputStream()), resource.getMimeTypeValue(), FilenameUtils.getName(resource.getName())));
        } else {
            return null;
        }
    } catch (CswException | IOException e) {
        throw new ResourceNotFoundException(String.format(ERROR_ID_PRODUCT_RETRIEVAL, metacardId), e);
    }
}
Also used : Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) Resource(ddf.catalog.resource.Resource) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) IOException(java.io.IOException) GetRecordByIdRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.GetRecordByIdRequest) Subject(ddf.security.Subject) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) BufferedInputStream(java.io.BufferedInputStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 29 with Resource

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

the class WfsSource method retrieveResource.

@Override
public ResourceResponse retrieveResource(URI uri, Map<String, Serializable> arguments) {
    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(), StandardCharsets.UTF_8), 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 30 with Resource

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

the class RESTEndpointTest method headTest.

@SuppressWarnings({ "unchecked" })
private Response headTest(boolean local) throws Exception {
    MetacardImpl metacard;
    List<Result> list = new ArrayList<>();
    Result result = mock(Result.class);
    InputStream inputStream;
    UriInfo uriInfo;
    Response response;
    CatalogFramework framework = givenCatalogFramework();
    list.add(result);
    QueryResponse queryResponse = mock(QueryResponse.class);
    when(queryResponse.getResults()).thenReturn(list);
    when(framework.query(isA(QueryRequest.class), isNull())).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);
    MimeTypeMapper mimeTypeMapper = mock(MimeTypeMapper.class);
    when(mimeTypeMapper.getMimeTypeForFileExtension("txt")).thenReturn("text/plain");
    when(mimeTypeMapper.getMimeTypeForFileExtension("xml")).thenReturn("text/xml");
    CatalogServiceImpl catalogServiceImpl = new CatalogServiceImpl(framework, new AttachmentParserImpl(mimeTypeMapper), mock(AttributeRegistry.class));
    catalogServiceImpl.setTikaMimeTypeResolver(new TikaMimeTypeResolver());
    FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
    catalogServiceImpl.setFilterBuilder(filterBuilder);
    uriInfo = createSpecificUriInfo(LOCAL_RETRIEVE_ADDRESS);
    RESTEndpoint restEndpoint = new RESTEndpoint(catalogServiceImpl);
    if (local) {
        response = restEndpoint.getHeaders(GET_ID, uriInfo, null);
    } else {
        response = restEndpoint.getHeaders(null, GET_ID, uriInfo, null);
    }
    return response;
}
Also used : MimeTypeMapper(ddf.mime.MimeTypeMapper) QueryRequest(ddf.catalog.operation.QueryRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Resource(ddf.catalog.resource.Resource) TikaMimeTypeResolver(ddf.mime.tika.TikaMimeTypeResolver) CatalogServiceImpl(org.codice.ddf.rest.service.impl.CatalogServiceImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) AttributeRegistry(ddf.catalog.data.AttributeRegistry) FilterBuilder(ddf.catalog.filter.FilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) 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) AttachmentParserImpl(org.codice.ddf.attachment.impl.AttachmentParserImpl)

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