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);
}
}
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>());
}
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);
}
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;
}
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;
}
Aggregations