Search in sources :

Example 26 with ResourceNotFoundException

use of ddf.catalog.resource.ResourceNotFoundException 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 27 with ResourceNotFoundException

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

the class ResourceMetacardTransformerTest method testNullResourceUri.

@Test
public void testNullResourceUri() throws Exception {
    thrown.expect(CatalogTransformerException.class);
    thrown.expectMessage("Unable to retrieve resource.");
    Metacard metacard = getMockMetacard(null);
    CatalogFramework framework = getFrameworkException(new ResourceNotFoundException(""));
    ResourceMetacardTransformer resourceTransformer = new ResourceMetacardTransformer(framework);
    resourceTransformer.transform(metacard, new HashMap<String, Serializable>());
}
Also used : Metacard(ddf.catalog.data.Metacard) Serializable(java.io.Serializable) CatalogFramework(ddf.catalog.CatalogFramework) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) Test(org.junit.Test)

Example 28 with ResourceNotFoundException

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

the class TestResourceMetacardTransformer method testFrameworkThrowsResourceNotFoundException.

@Test
public void testFrameworkThrowsResourceNotFoundException() throws Exception {
    String filePath = ABSOLUTE_PATH + TEST_PATH + JPEG_FILE_NAME_1;
    URI uri = getUri(filePath);
    Metacard metacard = getMockMetacard(uri);
    thrown.expect(CatalogTransformerException.class);
    thrown.expectMessage("Unable to retrieve resource.");
    thrown.expectMessage("Metacard id: " + TEST_ID);
    thrown.expectMessage("Uri: " + uri);
    thrown.expectMessage("Source: " + TEST_SITE);
    boolean expectSuccess = false;
    MimeType mimeType = getMimeType(JPEG_MIME_TYPE);
    CatalogFramework framework = getFrameworkException(new ResourceNotFoundException("Test Resource Not Found Exception"));
    testGetResource(metacard, filePath, mimeType, framework, expectSuccess);
}
Also used : Metacard(ddf.catalog.data.Metacard) CatalogFramework(ddf.catalog.CatalogFramework) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) URI(java.net.URI) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 29 with ResourceNotFoundException

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

the class DAGConverter method getThumbnail.

private byte[] getThumbnail(String thumbnailUrlStr) {
    byte[] thumbnail = null;
    try {
        URI thumbnailURI = new URI(thumbnailUrlStr);
        ResourceResponse resourceResponse = null;
        try {
            resourceResponse = resourceReader.retrieveResource(thumbnailURI, new HashMap<>());
            thumbnail = resourceResponse.getResource().getByteArray();
        } catch (ResourceNotSupportedException e) {
            LOGGER.debug("Resource is not supported: {} ", thumbnailURI, e);
        }
    } catch (IOException | ResourceNotFoundException | URISyntaxException e) {
        LOGGER.debug("Unable to get thumbnail from URL {}", thumbnailUrlStr, e);
    }
    return thumbnail;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) HashMap(java.util.HashMap) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) URI(java.net.URI)

Example 30 with ResourceNotFoundException

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

the class ExportCommand method doContentExport.

@SuppressWarnings("squid:S3776")
private List<ExportItem> doContentExport(ZipOutputStream zipOutputStream, List<ExportItem> exportedItems) {
    List<ExportItem> contentItemsToExport = exportedItems.stream().filter(ei -> ei.getResourceUri() != null).filter(ei -> ei.getResourceUri().getScheme() != null).filter(ei -> ei.getResourceUri().getScheme().startsWith(ContentItem.CONTENT_SCHEME)).filter(ei -> !ei.getMetacardTag().equals(DELETED_METACARD)).filter(ei -> !ei.getMetacardTag().equals(REVISION_METACARD) || ei.getResourceUri().getSchemeSpecificPart().equals(ei.getId())).filter(distinctByKey(ei -> ei.getResourceUri().getSchemeSpecificPart())).collect(Collectors.toList());
    List<ExportItem> exportedContentItems = new ArrayList<>();
    for (ExportItem contentItem : contentItemsToExport) {
        ResourceResponse resource;
        try {
            resource = catalogFramework.getLocalResource(new ResourceRequestByProductUri(contentItem.getResourceUri()));
        } catch (IOException | ResourceNotSupportedException e) {
            throw new CatalogCommandRuntimeException("Unable to retrieve resource for " + contentItem.getId(), e);
        } catch (ResourceNotFoundException e) {
            continue;
        }
        writeResourceToZip(zipOutputStream, contentItem, resource);
        exportedContentItems.add(contentItem);
        if (!contentItem.getMetacardTag().equals(REVISION_METACARD)) {
            for (String derivedUri : contentItem.getDerivedUris()) {
                URI uri;
                try {
                    uri = new URI(derivedUri);
                } catch (URISyntaxException e) {
                    LOGGER.debug("Uri [{}] is not a valid URI. Derived content will not be included in export", derivedUri);
                    continue;
                }
                ResourceResponse derivedResource;
                try {
                    derivedResource = catalogFramework.getLocalResource(new ResourceRequestByProductUri(uri));
                } catch (IOException e) {
                    throw new CatalogCommandRuntimeException("Unable to retrieve resource for " + contentItem.getId(), e);
                } catch (ResourceNotFoundException | ResourceNotSupportedException e) {
                    LOGGER.warn("Could not retreive resource [{}]", uri, e);
                    console.printf("%sUnable to retrieve resource for export : %s%s%n", Ansi.ansi().fg(Ansi.Color.RED).toString(), uri, Ansi.ansi().reset().toString());
                    continue;
                }
                writeResourceToZip(zipOutputStream, contentItem, derivedResource);
            }
        }
    }
    return exportedContentItems;
}
Also used : Ansi(org.fusesource.jansi.Ansi) StringUtils(org.apache.commons.lang.StringUtils) DeleteStorageRequestImpl(ddf.catalog.content.operation.impl.DeleteStorageRequestImpl) BinaryContent(ddf.catalog.data.BinaryContent) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) Command(org.apache.karaf.shell.api.action.Command) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) MetacardVersion(ddf.catalog.core.versioning.MetacardVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ExportItem(org.codice.ddf.commands.catalog.export.ExportItem) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) URI(java.net.URI) ZoneOffset(java.time.ZoneOffset) ParseException(java.text.ParseException) ZipEntry(java.util.zip.ZipEntry) Predicate(java.util.function.Predicate) DeletedMetacard(ddf.catalog.core.versioning.DeletedMetacard) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) PrivilegedAction(java.security.PrivilegedAction) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) BundleContext(org.osgi.framework.BundleContext) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Objects(java.util.Objects) StorageException(ddf.catalog.content.StorageException) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) ResultIterable.resultIterable(ddf.catalog.util.impl.ResultIterable.resultIterable) AccessController(java.security.AccessController) FilenameUtils(org.apache.commons.io.FilenameUtils) ResourceResponse(ddf.catalog.operation.ResourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) ZipOutputStream(java.util.zip.ZipOutputStream) FilterBuilder(ddf.catalog.filter.FilterBuilder) CatalogFramework(ddf.catalog.CatalogFramework) LocalDateTime(java.time.LocalDateTime) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Reference(org.apache.karaf.shell.api.action.lifecycle.Reference) ContentItem(ddf.catalog.content.data.ContentItem) Metacard(ddf.catalog.data.Metacard) CQLException(org.geotools.filter.text.cql2.CQLException) StorageProvider(ddf.catalog.content.StorageProvider) QueryRequest(ddf.catalog.operation.QueryRequest) Result(ddf.catalog.data.Result) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Logger(org.slf4j.Logger) SystemBaseUrl(org.codice.ddf.configuration.SystemBaseUrl) SecurityLogger(ddf.security.audit.SecurityLogger) IngestException(ddf.catalog.source.IngestException) ResourceRequestByProductUri(ddf.catalog.operation.impl.ResourceRequestByProductUri) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) IdAndUriMetacard(org.codice.ddf.commands.catalog.export.IdAndUriMetacard) File(java.io.File) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Paths(java.nio.file.Paths) DateTimeFormatter(java.time.format.DateTimeFormatter) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Filter(org.opengis.filter.Filter) Option(org.apache.karaf.shell.api.action.Option) Collections(java.util.Collections) InputStream(java.io.InputStream) DigitalSignature(org.codice.ddf.commands.util.DigitalSignature) ExportItem(org.codice.ddf.commands.catalog.export.ExportItem) ArrayList(java.util.ArrayList) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ResourceRequestByProductUri(ddf.catalog.operation.impl.ResourceRequestByProductUri) URI(java.net.URI) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Aggregations

ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)41 IOException (java.io.IOException)25 ResourceResponse (ddf.catalog.operation.ResourceResponse)17 URI (java.net.URI)16 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)14 Serializable (java.io.Serializable)14 Metacard (ddf.catalog.data.Metacard)13 Test (org.junit.Test)12 HashMap (java.util.HashMap)11 URISyntaxException (java.net.URISyntaxException)10 Result (ddf.catalog.data.Result)8 ResourceRequest (ddf.catalog.operation.ResourceRequest)8 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)8 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)7 Resource (ddf.catalog.resource.Resource)7 QueryRequest (ddf.catalog.operation.QueryRequest)6 QueryResponse (ddf.catalog.operation.QueryResponse)6 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)6 CatalogFramework (ddf.catalog.CatalogFramework)5 FederationException (ddf.catalog.federation.FederationException)5