use of ddf.catalog.resource.ResourceNotFoundException in project ddf by codice.
the class URLResourceReaderTest method testUrlToNonExistentFile.
@Test
public void testUrlToNonExistentFile() throws Exception {
URLResourceReader resourceReader = new URLResourceReader(mimeTypeMapper, clientBuilderFactory);
String filePath = TEST_PATH.resolve("NonExistentFile.jpg").toAbsolutePath().toString();
HashMap<String, Serializable> arguments = new HashMap<String, Serializable>();
try {
LOGGER.info("Getting resource: {}", filePath);
File file = new File(filePath);
URI uri = file.toURI();
resourceReader.retrieveResource(uri, arguments);
} catch (IOException e) {
LOGGER.info("Successfully caught IOException");
fail();
} catch (ResourceNotFoundException e) {
LOGGER.info("Caught ResourceNotFoundException");
assert (true);
}
}
use of ddf.catalog.resource.ResourceNotFoundException in project ddf by codice.
the class ContentResourceReader method retrieveResource.
@Override
public ResourceResponse retrieveResource(URI resourceUri, Map<String, Serializable> arguments) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
LOGGER.trace("ENTERING: retrieveResource");
ResourceResponse response = null;
if (resourceUri == null) {
throw new ResourceNotFoundException("Unable to find resource - resource URI was null");
}
if (resourceUri.getScheme().equals(ContentItem.CONTENT_SCHEME)) {
LOGGER.debug("Resource URI is content scheme");
String contentId = resourceUri.getSchemeSpecificPart();
if (contentId != null && !contentId.isEmpty()) {
if (arguments != null && arguments.get(ContentItem.QUALIFIER_KEYWORD) instanceof String && StringUtils.isNotBlank((String) arguments.get(ContentItem.QUALIFIER_KEYWORD))) {
try {
resourceUri = new URI(resourceUri.getScheme(), resourceUri.getSchemeSpecificPart(), (String) arguments.get(ContentItem.QUALIFIER_KEYWORD));
} catch (URISyntaxException e) {
throw new ResourceNotFoundException("Unable to create with qualifier", e);
}
}
ReadStorageRequest readRequest = new ReadStorageRequestImpl(resourceUri, arguments);
try {
ReadStorageResponse readResponse = storage.read(readRequest);
ContentItem contentItem = readResponse.getContentItem();
String fileName = contentItem.getFilename();
LOGGER.debug("resource name: {}", fileName);
InputStream is = contentItem.getInputStream();
response = new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(is), contentItem.getMimeType(), fileName));
} catch (StorageException e) {
throw new ResourceNotFoundException(e);
}
}
}
LOGGER.trace("EXITING: retrieveResource");
return response;
}
use of ddf.catalog.resource.ResourceNotFoundException in project ddf by codice.
the class OpenSearchSource method retrieveResource.
@Override
public ResourceResponse retrieveResource(URI uri, Map<String, Serializable> requestProperties) throws ResourceNotFoundException, ResourceNotSupportedException, IOException {
final String methodName = "retrieveResource";
LOGGER.trace("ENTRY: {}", methodName);
if (requestProperties == null) {
throw new ResourceNotFoundException("Could not retrieve resource with null properties.");
}
Serializable serializableId = requestProperties.get(Metacard.ID);
if (serializableId != null) {
String metacardId = serializableId.toString();
WebClient restClient = null;
try {
restClient = newRestClient(null, metacardId, true, null);
} catch (URISyntaxException e) {
throw new IOException(UNABLE_TO_CREATE_RWC, e);
}
if (StringUtils.isNotBlank(username)) {
requestProperties.put(USERNAME_PROPERTY, username);
requestProperties.put(PASSWORD_PROPERTY, password);
}
if (OAUTH_AUTH_TYPE.equals(authenticationType) && StringUtils.isNotBlank(oauthDiscoveryUrl) && StringUtils.isNotBlank(oauthClientId) && StringUtils.isNotBlank(oauthClientSecret)) {
requestProperties.put(ID_PROPERTY, shortname);
requestProperties.put(OAUTH_DISCOVERY_URL, oauthDiscoveryUrl);
requestProperties.put(OAUTH_CLIENT_ID, oauthClientId);
requestProperties.put(OAUTH_CLIENT_SECRET, oauthClientSecret);
requestProperties.put(OAUTH_FLOW, oauthFlow);
}
return resourceReader.retrieveResource(restClient.getCurrentURI(), requestProperties);
}
LOGGER.trace("EXIT: {}", methodName);
throw new ResourceNotFoundException(COULD_NOT_RETRIEVE_RESOURCE_MESSAGE);
}
use of ddf.catalog.resource.ResourceNotFoundException in project ddf by codice.
the class URLResourceReader method retrieveResource.
/**
* Retrieves a {@link ddf.catalog.resource.Resource} based on a {@link URI} and provided
* arguments. A connection is made to the {@link URI} to obtain the {@link
* ddf.catalog.resource.Resource}'s {@link InputStream} and build a {@link ResourceResponse} from
* that. If the {@link URI}'s scheme is HTTP or HTTPS, the {@link ddf.catalog.resource.Resource}'s
* name gets set to the {@link URI} passed in, otherwise, if it is a file scheme, the name is set
* to the actual file name.
*
* @param resourceURI A {@link URI} that defines what {@link ddf.catalog.resource.Resource} to
* retrieve and how to do it.
* @param properties Any additional arguments that should be passed to the {@link ResourceReader}.
* @return A {@link ResourceResponse} containing the retrieved {@link
* ddf.catalog.resource.Resource}.
*/
@Override
public ResourceResponse retrieveResource(URI resourceURI, Map<String, Serializable> properties) throws IOException, ResourceNotFoundException {
String bytesToSkip;
if (resourceURI == null) {
LOGGER.debug("Resource URI was null");
throw new ResourceNotFoundException("Unable to find resource");
}
if (properties.containsKey(BYTES_TO_SKIP)) {
bytesToSkip = properties.get(BYTES_TO_SKIP).toString();
LOGGER.debug("bytesToSkip: {}", bytesToSkip);
} else {
bytesToSkip = "0";
}
switch(resourceURI.getScheme()) {
case URL_HTTP_SCHEME:
case URL_HTTPS_SCHEME:
LOGGER.debug("Resource URI is HTTP or HTTPS");
final Serializable qualifierSerializable = properties.get(ContentItem.QUALIFIER_KEYWORD);
if (qualifierSerializable instanceof String) {
final String qualifier = (String) qualifierSerializable;
if (StringUtils.isNotBlank(qualifier)) {
resourceURI = UriBuilder.fromUri(resourceURI).queryParam(ContentItem.QUALIFIER_KEYWORD, qualifier).build();
}
}
String fileAddress = resourceURI.toURL().getFile();
LOGGER.debug("resource name: {}", fileAddress);
return retrieveHttpProduct(resourceURI, fileAddress, bytesToSkip, properties);
case URL_FILE_SCHEME:
LOGGER.debug("Resource URI is a File");
File filePathName = new File(resourceURI);
if (validateFilePath(filePathName)) {
String fileName = filePathName.getName();
LOGGER.debug("resource name: {}", fileName);
return retrieveFileProduct(resourceURI, fileName, bytesToSkip);
} else {
throw new ResourceNotFoundException("Error retrieving resource [" + resourceURI.toString() + "]. Invalid Resource URI of [" + resourceURI.toString() + "]. Resources must be in one of the following directories: " + this.rootResourceDirectories.toString());
}
default:
throw new ResourceNotFoundException("Resource qualifier ( " + resourceURI.getScheme() + " ) not valid. " + URLResourceReader.TITLE + " requires a qualifier of " + URL_HTTP_SCHEME + " or " + URL_HTTPS_SCHEME + " or " + URL_FILE_SCHEME);
}
}
use of ddf.catalog.resource.ResourceNotFoundException 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);
}
}
Aggregations