use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ReliableResourceDownloaderTest method getMockResourceResponse.
private ResourceResponse getMockResourceResponse(InputStream stream) throws Exception {
ResourceRequest resourceRequest = mock(ResourceRequest.class);
Map<String, Serializable> requestProperties = new HashMap<String, Serializable>();
when(resourceRequest.getPropertyNames()).thenReturn(requestProperties.keySet());
mockResource = mock(Resource.class);
when(mockResource.getInputStream()).thenReturn(stream);
when(mockResource.getName()).thenReturn("test-resource");
when(mockResource.getMimeType()).thenReturn(new MimeType("text/plain"));
mockResponse = mock(ResourceResponse.class);
when(mockResponse.getRequest()).thenReturn(resourceRequest);
when(mockResponse.getResource()).thenReturn(mockResource);
Map<String, Serializable> responseProperties = new HashMap<String, Serializable>();
when(mockResponse.getProperties()).thenReturn(responseProperties);
return mockResponse;
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ReliableResourceDownloaderTest method testNullReliableResourceCallableAndStatus.
@Test
public void testNullReliableResourceCallableAndStatus() throws Exception {
ResourceResponse mockResponse = getMockResourceResponse(mockStream);
ResourceRetriever mockResourceRetriever = mock(ResourceRetriever.class);
when(mockResourceRetriever.retrieveResource(anyLong())).thenReturn(mockResponse);
ReliableResourceStatus resourceStatus = new ReliableResourceStatus(DownloadStatus.RESOURCE_DOWNLOAD_INTERRUPTED, 0L);
ReliableResourceCallable mockCallable = mock(ReliableResourceCallable.class);
when(mockCallable.getReliableResourceStatus()).thenReturn(resourceStatus);
int retries = 5;
downloaderConfig.setMaxRetryAttempts(retries);
ReliableResourceDownloader downloader = spy(new ReliableResourceDownloader(downloaderConfig, new AtomicBoolean(), DOWNLOAD_ID, mockResponse, mockResourceRetriever));
doReturn(mockCallable).when(downloader).constructReliableResourceCallable(any(InputStream.class), any(CountingOutputStream.class), any(), anyInt(), any(Object.class));
doThrow(new CancellationException()).when(downloader).constructResourceRetrievalMonitor();
DownloadStatusInfoImpl downloadStatusInfo = new DownloadStatusInfoImpl();
downloadStatusInfo.setSubjectOperations(new SubjectUtils());
downloader.setupDownload(mockMetacard, downloadStatusInfo);
downloader.run();
verify(mockPublisher, times(retries)).postRetrievalStatus(any(ResourceResponse.class), eq(ProductRetrievalStatus.RETRYING), any(Metacard.class), anyString(), anyLong(), eq(DOWNLOAD_ID));
}
use of ddf.catalog.operation.ResourceResponse in project ddf by codice.
the class ReliableResourceDownloaderTest method testBadKeyName.
@Test
public void testBadKeyName() throws Exception {
Metacard metacard = getMockMetacard(DOWNLOAD_ID, ":badsourcename");
downloaderConfig.setCacheEnabled(true);
ResourceResponse mockResponse = getMockResourceResponse(mockStream);
ResourceCacheImpl mockCache = mock(ResourceCacheImpl.class);
when(mockCache.isPending(anyString())).thenReturn(false);
when(mockCache.getProductCacheDirectory()).thenReturn(productCacheDirectory);
downloaderConfig.setResourceCache(mockCache);
ReliableResourceDownloader downloader = new ReliableResourceDownloader(downloaderConfig, new AtomicBoolean(), DOWNLOAD_ID, mockResponse, getMockRetriever());
DownloadStatusInfoImpl downloadStatusInfo = new DownloadStatusInfoImpl();
downloadStatusInfo.setSubjectOperations(new SubjectUtils());
downloader.setupDownload(metacard, downloadStatusInfo);
verify(mockCache, never()).addPendingCacheEntry(any(ReliableResource.class));
}
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);
WebClient client = getWebClient(resourceURI, properties);
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;
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 = getResponseErrorMessage(is);
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 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);
}
}
Aggregations