use of ddf.catalog.resource.DataUsageLimitExceededException in project ddf by codice.
the class RESTEndpoint method getDocument.
/**
* REST Get. Retrieves the metadata entry specified by the id from the federated source
* specified by sourceid. Transformer argument is optional, but is used to specify what format
* the data should be returned.
*
* @param encodedSourceId
* @param encodedId
* @param transformerParam
* @param uriInfo
* @return
*/
@GET
@Path("/sources/{sourceid}/{id}")
public Response getDocument(@Encoded @PathParam("sourceid") String encodedSourceId, @Encoded @PathParam("id") String encodedId, @QueryParam("transform") String transformerParam, @Context UriInfo uriInfo, @Context HttpServletRequest httpRequest) {
Response response = null;
Response.ResponseBuilder responseBuilder;
QueryResponse queryResponse;
Metacard card = null;
LOGGER.trace("GET");
URI absolutePath = uriInfo.getAbsolutePath();
MultivaluedMap<String, String> map = uriInfo.getQueryParameters();
if (encodedId != null) {
LOGGER.debug("Got id: {}", encodedId);
LOGGER.debug("Got service: {}", transformerParam);
LOGGER.debug("Map of query parameters: \n{}", map.toString());
Map<String, Serializable> convertedMap = convert(map);
convertedMap.put("url", absolutePath.toString());
LOGGER.debug("Map converted, retrieving product.");
// default to xml if no transformer specified
try {
String id = URLDecoder.decode(encodedId, CharEncoding.UTF_8);
String transformer = DEFAULT_METACARD_TRANSFORMER;
if (transformerParam != null) {
transformer = transformerParam;
}
Filter filter = getFilterBuilder().attribute(Metacard.ID).is().equalTo().text(id);
Collection<String> sources = null;
if (encodedSourceId != null) {
String sourceid = URLDecoder.decode(encodedSourceId, CharEncoding.UTF_8);
sources = new ArrayList<String>();
sources.add(sourceid);
}
QueryRequestImpl request = new QueryRequestImpl(new QueryImpl(filter), sources);
request.setProperties(convertedMap);
queryResponse = catalogFramework.query(request, null);
// pull the metacard out of the blocking queue
List<Result> results = queryResponse.getResults();
// return null if timeout elapsed)
if (results != null && !results.isEmpty()) {
card = results.get(0).getMetacard();
}
if (card == null) {
throw new ServerErrorException("Unable to retrieve requested metacard.", Status.NOT_FOUND);
}
// Check for Range header set the value in the map appropriately so that the catalogFramework
// can take care of the skipping
long bytesToSkip = getRangeStart(httpRequest);
if (bytesToSkip > 0) {
LOGGER.debug("Bytes to skip: {}", String.valueOf(bytesToSkip));
convertedMap.put(BYTES_TO_SKIP, bytesToSkip);
}
LOGGER.debug("Calling transform.");
final BinaryContent content = catalogFramework.transform(card, transformer, convertedMap);
LOGGER.debug("Read and transform complete, preparing response.");
responseBuilder = Response.ok(content.getInputStream(), content.getMimeTypeValue());
// Add the Accept-ranges header to let the client know that we accept ranges in bytes
responseBuilder.header(HEADER_ACCEPT_RANGES, BYTES);
String filename = null;
if (content instanceof Resource) {
// If we got a resource, we can extract the filename.
filename = ((Resource) content).getName();
} else {
String fileExtension = getFileExtensionForMimeType(content.getMimeTypeValue());
if (StringUtils.isNotBlank(fileExtension)) {
filename = id + fileExtension;
}
}
if (StringUtils.isNotBlank(filename)) {
LOGGER.debug("filename: {}", filename);
responseBuilder.header(HEADER_CONTENT_DISPOSITION, "inline; filename=\"" + filename + "\"");
}
long size = content.getSize();
if (size > 0) {
responseBuilder.header(HEADER_CONTENT_LENGTH, size);
}
response = responseBuilder.build();
} catch (FederationException e) {
String exceptionMessage = "READ failed due to unexpected exception: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (CatalogTransformerException e) {
String exceptionMessage = "Unable to transform Metacard. Try different transformer: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (SourceUnavailableException e) {
String exceptionMessage = "Cannot obtain query results because source is unavailable: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (UnsupportedQueryException e) {
String exceptionMessage = "Specified query is unsupported. Change query and resubmit: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.BAD_REQUEST);
} catch (DataUsageLimitExceededException e) {
String exceptionMessage = "Unable to process request. Data usage limit exceeded: ";
LOGGER.debug(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.REQUEST_ENTITY_TOO_LARGE);
// The catalog framework will throw this if any of the transformers blow up.
// We need to catch this exception here or else execution will return to CXF and
// we'll lose this message and end up with a huge stack trace in a GUI or whatever
// else is connected to this endpoint
} catch (RuntimeException | UnsupportedEncodingException e) {
String exceptionMessage = "Unknown error occurred while processing request: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
}
} else {
throw new ServerErrorException("No ID specified.", Status.BAD_REQUEST);
}
return response;
}
Aggregations