use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class SortedFederationStrategyTest method testFederateGetEmptyHits.
@Test
public void testFederateGetEmptyHits() throws Exception {
QueryRequest fedQueryRequest = new QueryRequestImpl(mockQuery, false, null, properties);
List<Source> sourceList = ImmutableList.of();
QueryResponse federateResponse = strategy.federate(sourceList, fedQueryRequest);
assertThat(federateResponse.getHits(), is((long) 0));
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class SortedFederationStrategyTest method testFederateGetHits.
@Test
public void testFederateGetHits() throws Exception {
QueryRequest fedQueryRequest = new QueryRequestImpl(mockQuery, true, null, null);
Source mockSource1 = getMockSource();
Source mockSource2 = getMockSource();
Source mockSource3 = getMockSource();
List<Source> sourceList = ImmutableList.of(mockSource1, mockSource2, mockSource3);
long numHits = 2;
when(mockResponse.getHits()).thenReturn(numHits);
QueryResponse federateResponse = strategy.federate(sourceList, fedQueryRequest);
assertThat(federateResponse.getHits(), is(numHits * sourceList.size()));
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class UpdateOperations method populateMetacards.
private UpdateRequest populateMetacards(UpdateRequest updateRequest) throws IngestException {
QueryRequestImpl queryRequest = createQueryRequest(updateRequest);
QueryResponse queryResponse;
try {
queryResponse = queryOperations.doQuery(queryRequest, frameworkProperties.getFederationStrategy());
} catch (FederationException e) {
LOGGER.debug("Unable to complete query for updated metacards.", e);
throw new IngestException("Exception during runtime while performing update");
}
if (!foundAllUpdateRequestMetacards(updateRequest, queryResponse)) {
logFailedQueryInfo(updateRequest, queryResponse);
throw new IngestException("Could not find all metacards specified in request");
}
updateRequest = rewriteRequestToAvoidHistoryConflicts(updateRequest, queryResponse);
// Construct the metacardMap using the metacard's ID in order to match the UpdateRequest
HashMap<String, Metacard> metacardMap = new HashMap<>(queryResponse.getResults().stream().map(Result::getMetacard).collect(Collectors.toMap(metacard -> getAttributeStringValue(metacard, Core.ID), Function.identity())));
updateRequest.getProperties().put(Constants.ATTRIBUTE_UPDATE_MAP_KEY, metacardMap);
updateRequest.getProperties().put(Constants.OPERATION_TRANSACTION_KEY, new OperationTransactionImpl(OperationTransaction.OperationType.UPDATE, metacardMap.values()));
return updateRequest;
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class ResourceCacheService method queryForMetacard.
private Optional<Metacard> queryForMetacard(String metacardId) {
Filter filter = frameworkProperties.getFilterBuilder().attribute(Metacard.ID).is().equalTo().text(metacardId);
QueryRequest queryRequest = new QueryRequestImpl(new QueryImpl(filter), true);
QueryResponse queryResponse = null;
try {
queryResponse = catalogFramework.query(queryRequest);
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
LOGGER.error("Unable to lookup metacard for metacard id [{}].", metacardId);
return Optional.empty();
}
return queryResponse != null && queryResponse.getResults().size() == 1 ? Optional.of(queryResponse.getResults().get(0).getMetacard()) : Optional.empty();
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class AbstractCatalogService method getDocument.
@Override
public BinaryContent getDocument(String encodedSourceId, String encodedId, String transformerParam, URI absolutePath, MultivaluedMap<String, String> queryParameters, HttpServletRequest httpRequest) throws CatalogServiceException, DataUsageLimitExceededException, InternalServerErrorException {
QueryResponse queryResponse;
Metacard card = null;
LOGGER.trace("GET");
if (encodedId != null) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Got id: {}", LogSanitizer.sanitize(encodedId));
LOGGER.debug("Got service: {}", LogSanitizer.sanitize(transformerParam));
LOGGER.debug("Map of query parameters: \n{}", LogSanitizer.sanitize(queryParameters));
}
Map<String, Serializable> convertedMap = convert(queryParameters);
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<>();
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) {
return null;
}
// 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: {}", 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.");
return content;
} catch (FederationException e) {
String exceptionMessage = "READ failed due to unexpected exception: ";
LOGGER.info(exceptionMessage, e);
throw new InternalServerErrorException(exceptionMessage);
} catch (CatalogTransformerException e) {
String exceptionMessage = "Unable to transform Metacard. Try different transformer: ";
LOGGER.info(exceptionMessage, e);
throw new InternalServerErrorException(exceptionMessage);
} catch (SourceUnavailableException e) {
String exceptionMessage = "Cannot obtain query results because source is unavailable: ";
LOGGER.info(exceptionMessage, e);
throw new InternalServerErrorException(exceptionMessage);
} catch (UnsupportedQueryException e) {
String errorMessage = "Specified query is unsupported. Change query and resubmit: ";
LOGGER.info(errorMessage, e);
throw new CatalogServiceException(errorMessage);
} catch (DataUsageLimitExceededException e) {
String errorMessage = "Unable to process request. Data usage limit exceeded: ";
LOGGER.debug(errorMessage, e);
throw new DataUsageLimitExceededException(errorMessage);
} catch (OAuthPluginException e) {
Map<String, String> parameters = e.getParameters();
String url = constructUrl(httpRequest, e.getBaseUrl(), parameters);
if (url != null) {
parameters.put(REDIRECT_URI, url);
throw new OAuthPluginException(e.getSourceId(), url, e.getBaseUrl(), parameters, e.getErrorType());
}
throw e;
// 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 InternalServerErrorException(exceptionMessage);
}
} else {
throw new CatalogServiceException("No ID specified.");
}
}
Aggregations