use of ddf.catalog.source.UnsupportedQueryException in project ddf by codice.
the class CatalogFrameworkImplTest method testNoSitesAvailableFederatedQuery.
/**
* Tests that the framework properly throws a catalog exception when there are no sites
* (federated or local) that are available to perform the query.
*
* @throws SourceUnavailableException
*/
@Ignore
@Test(expected = SourceUnavailableException.class)
public void testNoSitesAvailableFederatedQuery() throws SourceUnavailableException {
CatalogFramework framework = this.createDummyCatalogFramework(null, null, null, false);
QueryRequest request = new QueryRequestImpl(null);
try {
framework.query(request);
} catch (UnsupportedQueryException e) {
// we don't even care what the query was
} catch (FederationException e) {
fail();
}
}
use of ddf.catalog.source.UnsupportedQueryException 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;
}
use of ddf.catalog.source.UnsupportedQueryException in project ddf by codice.
the class AbstractCswStore method update.
@Override
public UpdateResponse update(UpdateRequest updateRequest) throws IngestException {
Map<String, Serializable> properties = new HashMap<>();
validateOperation();
Subject subject = (Subject) updateRequest.getPropertyValue(SecurityConstants.SECURITY_SUBJECT);
Csw csw = factory.getClientForSubject(subject);
CswTransactionRequest transactionRequest = getTransactionRequest();
OperationTransaction opTrans = (OperationTransaction) updateRequest.getPropertyValue(Constants.OPERATION_TRANSACTION_KEY);
String insertTypeName = schemaTransformerManager.getTransformerIdForSchema(cswSourceConfiguration.getOutputSchema());
HashSet<ProcessingDetails> errors = new HashSet<>();
if (insertTypeName == null) {
insertTypeName = CswConstants.CSW_RECORD;
}
ArrayList<Metacard> updatedMetacards = new ArrayList<>(updateRequest.getUpdates().size());
ArrayList<Filter> updatedMetacardFilters = new ArrayList<>(updateRequest.getUpdates().size());
for (Map.Entry<Serializable, Metacard> update : updateRequest.getUpdates()) {
Metacard metacard = update.getValue();
properties.put(metacard.getId(), metacard);
updatedMetacardFilters.add(filterBuilder.attribute(updateRequest.getAttributeName()).is().equalTo().text(update.getKey().toString()));
transactionRequest.getUpdateActions().add(new UpdateAction(metacard, insertTypeName, null));
}
try {
TransactionResponseType response = csw.transaction(transactionRequest);
if (response.getTransactionSummary().getTotalUpdated().longValue() != updateRequest.getUpdates().size()) {
errors.add(new ProcessingDetailsImpl(this.getId(), null, "One or more updates failed"));
}
} catch (CswException e) {
throw new IngestException("Csw Transaction Failed.", e);
}
try {
updatedMetacards.addAll(transactionQuery(updatedMetacardFilters, subject));
} catch (UnsupportedQueryException e) {
errors.add(new ProcessingDetailsImpl(this.getId(), e, "Failed to retrieve updated metacards"));
}
return new UpdateResponseImpl(updateRequest, properties, updatedMetacards, new ArrayList(opTrans.getPreviousStateMetacards()), errors);
}
use of ddf.catalog.source.UnsupportedQueryException in project ddf by codice.
the class AbstractCswStore method delete.
@Override
public DeleteResponse delete(DeleteRequest deleteRequest) throws IngestException {
Map<String, Serializable> properties = new HashMap<>();
validateOperation();
Subject subject = (Subject) deleteRequest.getPropertyValue(SecurityConstants.SECURITY_SUBJECT);
Csw csw = factory.getClientForSubject(subject);
CswTransactionRequest transactionRequest = getTransactionRequest();
OperationTransaction opTrans = (OperationTransaction) deleteRequest.getPropertyValue(Constants.OPERATION_TRANSACTION_KEY);
String typeName = schemaTransformerManager.getTransformerIdForSchema(cswSourceConfiguration.getOutputSchema());
if (typeName == null) {
typeName = CswConstants.CSW_RECORD;
}
for (Serializable itemToDelete : deleteRequest.getAttributeValues()) {
try {
DeleteType deleteType = new DeleteType();
deleteType.setTypeName(typeName);
QueryConstraintType queryConstraintType = new QueryConstraintType();
Filter filter;
FilterType filterType;
filter = filterBuilder.attribute(deleteRequest.getAttributeName()).is().equalTo().text(itemToDelete.toString());
filterType = filterAdapter.adapt(filter, cswFilterDelegate);
queryConstraintType.setCqlText(CswCqlTextFilter.getInstance().getCqlText(filterType));
deleteType.setConstraint(queryConstraintType);
DeleteAction deleteAction = new DeleteAction(deleteType, DefaultCswRecordMap.getPrefixToUriMapping());
transactionRequest.getDeleteActions().add(deleteAction);
} catch (UnsupportedQueryException e) {
throw new IngestException("Unsupported Query.", e);
}
}
try {
TransactionResponseType response = csw.transaction(transactionRequest);
if (response.getTransactionSummary().getTotalDeleted().intValue() != deleteRequest.getAttributeValues().size()) {
throw new IngestException("Csw Transaction Failed. Number of metacards deleted did not match number requested.");
}
} catch (CswException e) {
throw new IngestException("Csw Transaction Failed", e);
}
return new DeleteResponseImpl(deleteRequest, properties, new ArrayList(opTrans.getPreviousStateMetacards()));
}
use of ddf.catalog.source.UnsupportedQueryException in project ddf by codice.
the class AbstractCswSource method createGetRecordsRequest.
private GetRecordsType createGetRecordsRequest(Query query, ElementSetType elementSetName, List<QName> elementNames) throws UnsupportedQueryException {
GetRecordsType getRecordsType = new GetRecordsType();
getRecordsType.setVersion(cswVersion);
getRecordsType.setService(CswConstants.CSW);
getRecordsType.setResultType(ResultType.RESULTS);
getRecordsType.setStartPosition(BigInteger.valueOf(query.getStartIndex()));
getRecordsType.setMaxRecords(BigInteger.valueOf(query.getPageSize()));
getRecordsType.setOutputFormat(MediaType.APPLICATION_XML);
if (!isOutputSchemaSupported()) {
String msg = "CSW Source: " + cswSourceConfiguration.getId() + " does not support output schema: " + cswSourceConfiguration.getOutputSchema() + ".";
throw new UnsupportedQueryException(msg);
}
getRecordsType.setOutputSchema(cswSourceConfiguration.getOutputSchema());
getRecordsType.setAbstractQuery(createQuery(query, elementSetName, elementNames));
return getRecordsType;
}
Aggregations