use of ddf.catalog.operation.ResourceResponse 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
*/
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.operation.ResourceResponse in project ddf by codice.
the class TestOpenSearchSource method testRetrieveResource.
/**
* Basic retrieve product case. Tests the url sent to the connection is correct.
*
* @throws ResourceNotSupportedException
* @throws IOException
* @throws ResourceNotFoundException
*/
@Test
public void testRetrieveResource() throws ResourceNotSupportedException, IOException, ResourceNotFoundException {
// given
FirstArgumentCapture answer = new FirstArgumentCapture(getBinaryData());
OpenSearchSource source = givenSource(answer);
Map<String, Serializable> requestProperties = new HashMap<String, Serializable>();
requestProperties.put(Metacard.ID, SAMPLE_ID);
// when
ResourceResponse response = source.retrieveResource(null, requestProperties);
Assert.assertEquals(3, response.getResource().getByteArray().length);
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class TestResourceMetacardTransformer method getResourceResponse.
private ResourceResponse getResourceResponse(Resource resource) {
ResourceResponse resourceResponse = mock(ResourceResponse.class);
when(resourceResponse.getResource()).thenReturn(resource);
return resourceResponse;
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class URLResourceReader method retrieveHttpProduct.
private ResourceResponse retrieveHttpProduct(URI resourceURI, String productName, String bytesToSkip, Map<String, Serializable> properties) throws ResourceNotFoundException {
try {
LOGGER.debug("Opening connection to: {}", resourceURI.toString());
WebClient client = getWebClient(resourceURI.toString(), properties);
Object subjectObj = properties.get(SecurityConstants.SECURITY_SUBJECT);
if (subjectObj != null) {
Subject subject = (Subject) subjectObj;
LOGGER.debug("Setting Subject on webclient: {}", subject);
RestSecurity.setSubjectOnClient(subject, client);
}
Response response = client.get();
MultivaluedMap<String, Object> headers = response.getHeaders();
List<Object> cdHeaders = headers.get(HttpHeaders.CONTENT_DISPOSITION);
if (cdHeaders != null && !cdHeaders.isEmpty()) {
String contentHeader = (String) cdHeaders.get(0);
productName = StringUtils.defaultIfBlank(handleContentDispositionHeader(contentHeader), productName);
}
String mimeType = getMimeType(resourceURI, productName);
Response clientResponse = client.get();
InputStream is = null;
Object entityObj = clientResponse.getEntity();
if (entityObj instanceof InputStream) {
is = (InputStream) entityObj;
if (Response.Status.OK.getStatusCode() != clientResponse.getStatus() && Response.Status.PARTIAL_CONTENT.getStatusCode() != clientResponse.getStatus()) {
String error = null;
try {
if (is != null) {
error = IOUtils.toString(is);
}
} catch (IOException ioe) {
LOGGER.debug("Could not convert error message to a string for output.", ioe);
}
String errorMsg = "Received error code while retrieving resource (status " + clientResponse.getStatus() + "): " + error;
throw new ResourceNotFoundException(errorMsg);
}
} else {
throw new ResourceNotFoundException("Received null response while retrieving resource.");
}
long responseBytesSkipped = 0L;
if (headers.getFirst(HttpHeaders.CONTENT_RANGE) != null) {
String contentRangeHeader = String.valueOf(headers.getFirst(HttpHeaders.CONTENT_RANGE));
responseBytesSkipped = Long.parseLong(StringUtils.substringBetween(contentRangeHeader.toLowerCase(), "bytes ", "-"));
}
alignStream(is, Long.parseLong(bytesToSkip), responseBytesSkipped);
return new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(is), mimeType, FilenameUtils.getName(productName)));
} catch (MimeTypeResolutionException | IOException | WebApplicationException e) {
LOGGER.info("Error retrieving resource", e);
throw new ResourceNotFoundException("Unable to retrieve resource at: " + resourceURI.toString(), e);
}
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ResourceReaderTest method doVerification.
private ResourceResponse doVerification(URI uri, String filename, String expectedMimeType, Map<String, Serializable> arguments) throws URISyntaxException, IOException, ResourceNotFoundException {
URLResourceReader resourceReader = new TestURLResourceReader(mimeTypeMapper);
resourceReader.setRootResourceDirectories(ImmutableSet.of(ABSOLUTE_PATH + TEST_PATH));
// Test using the URL ResourceReader
LOGGER.info("URI: {}", uri.toString());
ResourceResponse resourceResponse = resourceReader.retrieveResource(uri, arguments);
Resource resource = resourceResponse.getResource();
assert (resource != null);
LOGGER.info("MimeType: {}", resource.getMimeType());
LOGGER.info("Got resource: {}", resource.getName());
String name = resource.getName();
assertNotNull(name);
assertThat(name, is(filename));
assertTrue(resource.getMimeType().toString().contains(expectedMimeType));
return resourceResponse;
}
Aggregations