use of ddf.catalog.util.impl.CatalogQueryException in project ddf by codice.
the class CswEndpoint method queryCsw.
private CswRecordCollection queryCsw(GetRecordsType request) throws CswException {
if (LOGGER.isDebugEnabled()) {
try {
Writer writer = new StringWriter();
try {
Marshaller marshaller = CswQueryFactory.getJaxBContext().createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
JAXBElement<GetRecordsType> jaxbElement = new ObjectFactory().createGetRecords(request);
marshaller.marshal(jaxbElement, writer);
} catch (JAXBException e) {
LOGGER.debug("Unable to marshall {} to XML", GetRecordsType.class, e);
}
LOGGER.debug(writer.toString());
} catch (Exception e) {
LOGGER.debug("Unable to create debug message for getRecordsType", e);
}
}
QueryType query = (QueryType) request.getAbstractQuery().getValue();
CswRecordCollection response = new CswRecordCollection();
response.setRequest(request);
response.setOutputSchema(request.getOutputSchema());
response.setMimeType(request.getOutputFormat());
response.setElementName(query.getElementName());
response.setElementSetType((query.getElementSetName() != null) ? query.getElementSetName().getValue() : null);
response.setResultType((ResultType) ObjectUtils.defaultIfNull(request.getResultType(), ResultType.HITS));
if (ResultType.HITS.equals(request.getResultType()) || ResultType.RESULTS.equals(request.getResultType())) {
QueryRequest queryRequest = queryFactory.getQuery(request);
try {
queryRequest = queryFactory.updateQueryRequestTags(queryRequest, request.getOutputSchema());
LOGGER.debug("Attempting to execute paged query: {}", queryRequest);
AtomicLong hitCount = new AtomicLong(0);
QueryFunction qf = qr -> {
SourceResponse sr = framework.query(qr);
hitCount.compareAndSet(0, sr.getHits());
return sr;
};
ResultIterable results = ResultIterable.resultIterable(qf, queryRequest, request.getMaxRecords().intValue());
List<Result> resultList = results.stream().collect(Collectors.toList());
// The hitCount Atomic is used here instead of just defaulting
// to the size of the resultList because the size of the resultList
// can be limited to request.getMaxRecords().intValue() which would
// lead to an incorrect response for hits.
// hitCount is set within the QueryFunction and will correspond to
// all responses.
long totalHits = hitCount.get();
totalHits = totalHits != 0 ? totalHits : resultList.size();
QueryResponse queryResponse = new QueryResponseImpl(queryRequest, resultList, totalHits);
response.setSourceResponse(queryResponse);
} catch (UnsupportedQueryException | CatalogQueryException e) {
LOGGER.debug("Unable to query", e);
throw new CswException(e);
}
}
return response;
}
use of ddf.catalog.util.impl.CatalogQueryException in project ddf by codice.
the class CswEndpoint method transaction.
@Override
@POST
@Consumes({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
@Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
public TransactionResponseType transaction(CswTransactionRequest request) throws CswException {
if (request == null) {
throw new CswException("TransactionRequest request is null");
}
TransactionResponseType response = new TransactionResponseType();
TransactionSummaryType summary = new TransactionSummaryType();
summary.setTotalInserted(BigInteger.valueOf(0));
summary.setTotalUpdated(BigInteger.valueOf(0));
summary.setTotalDeleted(BigInteger.valueOf(0));
response.setTransactionSummary(summary);
response.setVersion(CswConstants.VERSION_2_0_2);
int numInserted = 0;
final Subject subject = SecurityUtils.getSubject();
for (InsertAction insertAction : request.getInsertActions()) {
final InsertAction transformInsertAction = transformInsertAction(insertAction);
List<Metacard> metacards = transformInsertAction.getRecords();
CompletionService<CreateResponse> completionService = new ExecutorCompletionService<>(queryExecutor);
for (Metacard record : metacards) {
CreateRequest createRequest = new CreateRequestImpl(record);
Callable<CreateResponse> callable = () -> {
try {
return framework.create(createRequest);
} catch (IngestException | SourceUnavailableException e) {
LOGGER.debug("Unable to insert record(s)", e);
throw new CswException("Unable to insert record(s).", CswConstants.TRANSACTION_FAILED, transformInsertAction.getHandle());
}
};
Callable<CreateResponse> createCallable = subject.associateWith(callable);
completionService.submit(createCallable);
}
for (int i = 0; i < metacards.size(); i++) {
try {
Future<CreateResponse> completedFuture = completionService.take();
try {
CreateResponse futureResponse = completedFuture.get();
numInserted += futureResponse.getCreatedMetacards().size();
if (request.isVerbose()) {
response.getInsertResult().add(getInsertResultFromResponse(futureResponse));
}
} catch (ExecutionException | CancellationException e) {
LOGGER.debug("Error ingesting Metacard", e);
throw new CswException("Unable to insert record(s).", CswConstants.TRANSACTION_FAILED, insertAction.getHandle());
}
} catch (InterruptedException e) {
LOGGER.debug("Metacard ingest interrupted", e);
Thread.currentThread().interrupt();
break;
}
}
}
LOGGER.debug("{} records inserted.", numInserted);
response.getTransactionSummary().setTotalInserted(BigInteger.valueOf(numInserted));
int numUpdated = 0;
List<UpdateAction> updateActions = request.getUpdateActions();
CompletionService<Integer> updateCompletionService = new ExecutorCompletionService<>(queryExecutor);
for (final UpdateAction updateAction : updateActions) {
Callable<Integer> callable = () -> {
try {
return updateRecords(subject, updateAction);
} catch (CswException | FederationException | IngestException | SourceUnavailableException | UnsupportedQueryException | CatalogQueryException e) {
LOGGER.debug(UNABLE_TO_UPDATE_MSG, e);
throw new CswException(UNABLE_TO_UPDATE_MSG, CswConstants.TRANSACTION_FAILED, updateAction.getHandle());
}
};
Callable<Integer> updateCallable = subject.associateWith(callable);
updateCompletionService.submit(updateCallable);
}
for (int i = 0; i < updateActions.size(); i++) {
try {
Future<Integer> completedFuture = updateCompletionService.take();
try {
numUpdated += completedFuture.get();
} catch (ExecutionException | CancellationException e) {
LOGGER.debug("Error updating Metacard", e);
throw new CswException(UNABLE_TO_UPDATE_MSG, CswConstants.TRANSACTION_FAILED, "Update");
}
} catch (InterruptedException e) {
LOGGER.debug("Metacard update interrupted", e);
Thread.currentThread().interrupt();
break;
}
}
LOGGER.debug("{} records updated.", numUpdated);
response.getTransactionSummary().setTotalUpdated(BigInteger.valueOf(numUpdated));
int numDeleted = 0;
for (DeleteAction deleteAction : request.getDeleteActions()) {
try {
numDeleted += deleteRecords(deleteAction);
} catch (Exception e) {
LOGGER.debug(UNABLE_TO_DELETE_MSG, e);
throw new CswException(UNABLE_TO_DELETE_MSG, CswConstants.TRANSACTION_FAILED, deleteAction.getHandle());
}
}
LOGGER.debug("{} records deleted.", numDeleted);
response.getTransactionSummary().setTotalDeleted(BigInteger.valueOf(numDeleted));
return response;
}
Aggregations