Search in sources :

Example 6 with ResourceNotSupportedException

use of ddf.catalog.resource.ResourceNotSupportedException 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: {}", String.valueOf(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: {}", 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 7 with ResourceNotSupportedException

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

the class ResourceDownload method copyToLocalSite.

@Override
public void copyToLocalSite(String sourceId, String metacardId) throws MBeanException {
    LOGGER.debug("Downloading resource associated with metacard id [{}] from source [{}] to the local site.", metacardId, sourceId);
    ResourceRequest resourceRequest = new ResourceRequestById(metacardId);
    if (!resourceCacheMBean.isCacheEnabled()) {
        String message = "Caching of resources is not enabled.";
        LOGGER.info(message);
        throw new MBeanException(new DownloadToLocalSiteException(Status.BAD_REQUEST, message), message);
    }
    try {
        LOGGER.debug("Attempting to download the resource associated with metacard [{}] from source [{}] to the local site.", metacardId, sourceId);
        ResourceResponse resourceResponse = catalogFramework.getResource(resourceRequest, sourceId);
        if (resourceResponse == null) {
            String message = String.format(ERROR_MESSAGE_TEMPLATE, metacardId, sourceId);
            LOGGER.debug(message);
            throw new MBeanException(new DownloadToLocalSiteException(Status.INTERNAL_SERVER_ERROR, message), message);
        }
    } catch (IOException | ResourceNotSupportedException e) {
        String message = String.format(ERROR_MESSAGE_TEMPLATE, metacardId, sourceId);
        LOGGER.debug(message, e);
        throw new MBeanException(new DownloadToLocalSiteException(Status.INTERNAL_SERVER_ERROR, message), message);
    } catch (ResourceNotFoundException e) {
        String message = String.format(ERROR_MESSAGE_TEMPLATE + " The resource could not be found.", metacardId, sourceId);
        LOGGER.debug(message, e);
        throw new MBeanException(new DownloadToLocalSiteException(Status.NOT_FOUND, message), message);
    }
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanException(javax.management.MBeanException) ResourceRequest(ddf.catalog.operation.ResourceRequest) DownloadToLocalSiteException(org.codice.ddf.catalog.resource.download.DownloadToLocalSiteException) IOException(java.io.IOException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 8 with ResourceNotSupportedException

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

the class ZipCompression method getResource.

private Resource getResource(Metacard metacard) {
    Resource resource = null;
    try {
        ResourceRequest resourceRequest = new ResourceRequestById(metacard.getId());
        ResourceResponse resourceResponse = catalogFramework.getLocalResource(resourceRequest);
        resource = resourceResponse.getResource();
    } catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
        LOGGER.debug("Unable to retrieve content from metacard : {}", metacard.getId(), e);
    }
    return resource;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Resource(ddf.catalog.resource.Resource) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ResourceRequest(ddf.catalog.operation.ResourceRequest) IOException(java.io.IOException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 9 with ResourceNotSupportedException

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

the class ExceptionsTest method testResourceNotSupportedException.

@Test
public void testResourceNotSupportedException() {
    ResourceNotSupportedException rnse = new ResourceNotSupportedException();
    assertNotNull(rnse);
    rnse = new ResourceNotSupportedException(msg);
    assertEquals(rnse.getMessage(), msg);
    rnse = new ResourceNotSupportedException(testCause);
    assertEquals(rnse.getCause(), testCause);
    rnse = new ResourceNotSupportedException(msg, testCause);
    assertEquals(rnse.getMessage(), msg);
    assertEquals(rnse.getCause(), testCause);
}
Also used : ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Test(org.junit.Test)

Example 10 with ResourceNotSupportedException

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

the class SeedCommand method executeWithSubject.

@Override
protected Object executeWithSubject() throws Exception {
    if (resourceLimit <= 0) {
        printErrorMessage("A limit of " + resourceLimit + " was supplied. The limit must be greater than 0.");
        return null;
    }
    final long start = System.currentTimeMillis();
    int resourceDownloads = 0;
    int downloadErrors = 0;
    int pageCount = 0;
    while (resourceDownloads < resourceLimit) {
        final QueryImpl query = new QueryImpl(getFilter());
        query.setPageSize(resourceLimit);
        query.setStartIndex(pageCount * resourceLimit + 1);
        ++pageCount;
        final QueryRequestImpl queryRequest;
        if (isNotEmpty(sources)) {
            queryRequest = new QueryRequestImpl(query, sources);
        } else {
            queryRequest = new QueryRequestImpl(query, true);
        }
        queryRequest.setProperties(new HashMap<>(CACHE_UPDATE_PROPERTIES));
        final QueryResponse queryResponse = catalogFramework.query(queryRequest);
        if (queryResponse.getResults().isEmpty()) {
            break;
        }
        final List<Entry<? extends ResourceRequest, String>> resourceRequests = queryResponse.getResults().stream().map(Result::getMetacard).filter(isNonCachedResource).map(metacard -> new SimpleEntry<>(new ResourceRequestById(metacard.getId()), metacard.getSourceId())).collect(toList());
        for (Entry<? extends ResourceRequest, String> requestAndSourceId : resourceRequests) {
            final ResourceRequest request = requestAndSourceId.getKey();
            try {
                ResourceResponse response = catalogFramework.getResource(request, requestAndSourceId.getValue());
                ++resourceDownloads;
                EXECUTOR.execute(new ResourceCloseHandler(response));
            } catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
                ++downloadErrors;
                LOGGER.debug("Could not download resource for metacard [id={}]", request.getAttributeValue(), e);
            }
            printProgressAndFlush(start, resourceLimit, resourceDownloads);
            if (resourceDownloads == resourceLimit) {
                break;
            }
        }
    }
    printProgressAndFlush(start, resourceDownloads, resourceDownloads);
    console.println();
    if (downloadErrors > 0) {
        printErrorMessage(downloadErrors + " resource download(s) had errors. Check the logs for details.");
    }
    printSuccessMessage("Done seeding. " + resourceDownloads + " resource download(s) started.");
    return null;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Command(org.apache.karaf.shell.api.action.Command) Metacard(ddf.catalog.data.Metacard) Map(java.util.Map) ResourceRequest(ddf.catalog.operation.ResourceRequest) SimpleEntry(java.util.AbstractMap.SimpleEntry) Result(ddf.catalog.data.Result) CollectionUtils.isNotEmpty(org.apache.commons.collections.CollectionUtils.isNotEmpty) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) IOException(java.io.IOException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Serializable(java.io.Serializable) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) TimeUnit(java.util.concurrent.TimeUnit) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) QueryResponse(ddf.catalog.operation.QueryResponse) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Attribute(ddf.catalog.data.Attribute) Entry(java.util.Map.Entry) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Option(org.apache.karaf.shell.api.action.Option) Collections(java.util.Collections) InputStream(java.io.InputStream) SimpleEntry(java.util.AbstractMap.SimpleEntry) IOException(java.io.IOException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SimpleEntry(java.util.AbstractMap.SimpleEntry) Entry(java.util.Map.Entry) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ResourceRequest(ddf.catalog.operation.ResourceRequest) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Aggregations

ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)14 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)11 ResourceResponse (ddf.catalog.operation.ResourceResponse)10 IOException (java.io.IOException)10 ResourceRequest (ddf.catalog.operation.ResourceRequest)6 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)6 Metacard (ddf.catalog.data.Metacard)5 Resource (ddf.catalog.resource.Resource)5 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)3 URI (java.net.URI)3 HashMap (java.util.HashMap)3 Result (ddf.catalog.data.Result)2 QueryResponse (ddf.catalog.operation.QueryResponse)2 QueryImpl (ddf.catalog.operation.impl.QueryImpl)2 ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)2 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)2 Serializable (java.io.Serializable)2 URISyntaxException (java.net.URISyntaxException)2 Collections (java.util.Collections)2 MimeType (javax.activation.MimeType)2