use of ddf.catalog.resource.ResourceNotFoundException in project ddf by codice.
the class ExceptionsTest method testResourceNotFoundException.
@Test
public void testResourceNotFoundException() {
ResourceNotFoundException rnfe = new ResourceNotFoundException();
assertNotNull(rnfe);
rnfe = new ResourceNotFoundException(msg);
assertEquals(rnfe.getMessage(), msg);
rnfe = new ResourceNotFoundException(testCause);
assertEquals(rnfe.getCause(), testCause);
rnfe = new ResourceNotFoundException(msg, testCause);
assertEquals(rnfe.getMessage(), msg);
assertEquals(rnfe.getCause(), testCause);
}
use of ddf.catalog.resource.ResourceNotFoundException in project ddf by codice.
the class ReliableResourceDownloadManager method download.
/**
* @param resourceRequest the original @ResourceRequest to retrieve the resource
* @param metacard the @Metacard associated with the resource being downloaded
* @param retriever the @ResourceRetriever to be used to get the resource
* @return the modified @ResourceResponse with the @ReliableResourceInputStream that the client
* should read from
* @throws DownloadException
*/
@Override
public ResourceResponse download(ResourceRequest resourceRequest, Metacard metacard, ResourceRetriever retriever) throws DownloadException {
ResourceResponse resourceResponse = null;
String downloadIdentifier = UUID.randomUUID().toString();
if (metacard == null) {
throw new DownloadException("Cannot download resource if metacard is null");
} else if (StringUtils.isBlank(metacard.getId())) {
throw new DownloadException("Metacard must have unique id.");
} else if (retriever == null) {
throw new DownloadException("Cannot download resource if retriever is null");
} else if (resourceRequest == null) {
throw new DownloadException("Cannot download resource if request is null");
}
if (downloaderConfig.isCacheEnabled()) {
Resource cachedResource = downloaderConfig.getResourceCache().getValid(new CacheKey(metacard, resourceRequest).generateKey(), metacard);
if (cachedResource != null) {
resourceResponse = new ResourceResponseImpl(resourceRequest, resourceRequest.getProperties(), cachedResource);
LOGGER.debug("Successfully retrieved product from cache for metacard ID = {}", metacard.getId());
} else {
LOGGER.debug("Unable to get resource from cache. Have to retrieve it from source");
}
}
if (resourceResponse == null) {
try {
resourceResponse = retriever.retrieveResource();
} catch (ResourceNotFoundException | ResourceNotSupportedException | IOException e) {
throw new DownloadException("Cannot download resource", e);
}
resourceResponse.getProperties().put(Metacard.ID, metacard.getId());
// Sources do not create ResourceResponses with the original ResourceRequest, hence
// it is added here because it will be needed for caching
resourceResponse = new ResourceResponseImpl(resourceRequest, resourceResponse.getProperties(), resourceResponse.getResource());
resourceResponse = startDownload(downloadIdentifier, resourceResponse, retriever, metacard);
}
return resourceResponse;
}
use of ddf.catalog.resource.ResourceNotFoundException 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, metacardId, sourceId) + " The resource could not be found.";
LOGGER.debug(message, e);
throw new MBeanException(new DownloadToLocalSiteException(Status.NOT_FOUND, message), message);
}
}
use of ddf.catalog.resource.ResourceNotFoundException in project ddf by codice.
the class ReliableResourceDownloader method retrieveResource.
private ReliableResourceCallable retrieveResource(long bytesRead) {
ReliableResourceCallable reliableResourceCallable = null;
try {
LOGGER.debug("Attempting to re-retrieve resource, skipping {} bytes", bytesRead);
// Re-fetch product from the Source after setting up values to indicate the number of
// bytes to skip. This prevents the same bytes being read again and put in the
// PipedOutputStream that the client is still reading from and in the file being cached
// to. It also allows for range headers to be used in the request so that already read
// bytes do not need to be re-retrieved.
ResourceResponse resourceResponse = retriever.retrieveResource(bytesRead);
LOGGER.debug("Name of re-retrieved resource = {}", resourceResponse.getResource().getName());
resourceInputStream = resourceResponse.getResource().getInputStream();
reliableResourceCallable = constructReliableResourceCallable(resourceInputStream, countingFbos, fos, downloaderConfig.getChunkSize(), lock);
// So that Callable can account for bytes read in previous download attempt(s)
reliableResourceCallable.setBytesRead(bytesRead);
} catch (ResourceNotFoundException | ResourceNotSupportedException | IOException e) {
LOGGER.info("Unable to re-retrieve product; cannot download product file {}", filePath);
}
return reliableResourceCallable;
}
use of ddf.catalog.resource.ResourceNotFoundException 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;
}
Aggregations