use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class SearchControllerTest method createFramework.
private CatalogFramework createFramework() {
final long COUNT = 2;
CatalogFramework framework = mock(CatalogFramework.class);
List<Result> results = new ArrayList<Result>();
for (int i = 0; i < COUNT; i++) {
Result result = mock(Result.class);
MetacardImpl metacard = new MetacardImpl();
metacard.setId("Metacard_" + i);
metacard.setTitle("Metacard " + i);
metacard.setLocation("POINT(" + i + " " + i + ")");
metacard.setType(BasicTypes.BASIC_METACARD);
metacard.setCreatedDate(TIMESTAMP);
metacard.setEffectiveDate(TIMESTAMP);
metacard.setExpirationDate(TIMESTAMP);
metacard.setModifiedDate(TIMESTAMP);
metacard.setContentTypeName("TEST");
metacard.setContentTypeVersion("1.0");
metacard.setTargetNamespace(URI.create(getClass().getPackage().getName()));
when(result.getDistanceInMeters()).thenReturn(100.0 * i);
when(result.getRelevanceScore()).thenReturn(100.0 * (COUNT - i) / COUNT);
when(result.getMetacard()).thenReturn(metacard);
results.add(result);
}
QueryResponse response = new QueryResponseImpl(mock(QueryRequest.class), new ArrayList<Result>(), COUNT);
response.getResults().addAll(results);
try {
when(framework.query(any(QueryRequest.class))).thenReturn(response);
} catch (UnsupportedQueryException e) {
LOGGER.debug("Error querying framework", e);
} catch (SourceUnavailableException e) {
LOGGER.debug("Error querying framework", e);
} catch (FederationException e) {
LOGGER.debug("Error querying framework", e);
}
return framework;
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class CacheQueryRunnable method run.
@Override
public void run() {
// check if there are any currently cached results
QueryResponse response = queryCatalog(null, request, subject, new HashMap<>(CACHE_PROPERTIES));
search.update(response);
try {
searchController.publishResults(request.getId(), search.transform(request.getId()), session);
} catch (CatalogTransformerException e) {
LOGGER.debug("Failed to transform cached search results.", e);
}
addResults(response.getResults());
indexResults(response);
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class QueryRunnable method queryCatalog.
protected QueryResponse queryCatalog(String sourceId, SearchRequest searchRequest, Subject subject, Map<String, Serializable> properties) {
Query query = searchRequest.getQuery();
QueryResponse response = getEmptyResponse(sourceId);
long startTime = System.currentTimeMillis();
try {
if (query != null) {
List<String> sourceIds;
if (sourceId == null) {
sourceIds = new ArrayList<>(searchRequest.getSourceIds());
} else {
sourceIds = Collections.singletonList(sourceId);
}
QueryRequest request = new QueryRequestImpl(query, false, sourceIds, properties);
if (subject != null) {
LOGGER.debug("Adding {} property with value {} to request.", SecurityConstants.SECURITY_SUBJECT, subject);
request.getProperties().put(SecurityConstants.SECURITY_SUBJECT, subject);
}
LOGGER.debug("Sending query: {}", query);
response = searchController.getFramework().query(request);
}
} catch (UnsupportedQueryException | FederationException e) {
LOGGER.info("Error executing query. {}. Set log level to DEBUG for more information", e.getMessage());
LOGGER.debug("Error executing query", e);
response.getProcessingDetails().add(new ProcessingDetailsImpl(sourceId, e));
} catch (SourceUnavailableException e) {
LOGGER.info("Error executing query because the underlying source was unavailable. {}. Set log level to DEBUG for more information", e.getMessage());
LOGGER.debug("Error executing query because the underlying source was unavailable.", e);
response.getProcessingDetails().add(new ProcessingDetailsImpl(sourceId, e));
} catch (RuntimeException e) {
// Account for any runtime exceptions and send back a server error
// this prevents full stacktraces returning to the client
// this allows for a graceful server error to be returned
LOGGER.info("RuntimeException on executing query. {}. Set log level to DEBUG for more information", e.getMessage());
LOGGER.debug("RuntimeException on executing query", e);
response.getProcessingDetails().add(new ProcessingDetailsImpl(sourceId, e));
}
long estimatedTime = System.currentTimeMillis() - startTime;
response.getProperties().put("elapsed", estimatedTime);
return response;
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class ResourceOperations method getEnterpriseResourceOptions.
public Map<String, Set<String>> getEnterpriseResourceOptions(String metacardId, boolean fanoutEnabled) throws ResourceNotFoundException {
LOGGER.trace("ENTERING: getEnterpriseResourceOptions");
Set<String> supportedOptions = Collections.emptySet();
try {
QueryRequest queryRequest = new QueryRequestImpl(createMetacardIdQuery(metacardId), true, null, null);
QueryResponse queryResponse = queryOperations.query(queryRequest, null, false, fanoutEnabled);
List<Result> results = queryResponse.getResults();
if (!results.isEmpty()) {
Metacard metacard = results.get(0).getMetacard();
String sourceIdOfResult = metacard.getSourceId();
if (sourceIdOfResult != null && sourceIdOfResult.equals(getId())) {
// found entry on local source
supportedOptions = getOptionsFromLocalProvider(metacard);
} else if (sourceIdOfResult != null && !sourceIdOfResult.equals(getId())) {
// found entry on federated source
supportedOptions = getOptionsFromFederatedSource(metacard, sourceIdOfResult);
}
} else {
String message = "Unable to find metacard " + metacardId + " on enterprise.";
LOGGER.debug(message);
LOGGER.trace("EXITING: getEnterpriseResourceOptions");
throw new ResourceNotFoundException(message);
}
} catch (UnsupportedQueryException e) {
LOGGER.debug("Error finding metacard {}", metacardId, e);
LOGGER.trace("EXITING: getEnterpriseResourceOptions");
throw new ResourceNotFoundException("Error finding metacard due to Unsuppported Query", e);
} catch (FederationException e) {
LOGGER.debug("Error federating query for metacard {}", metacardId, e);
LOGGER.trace("EXITING: getEnterpriseResourceOptions");
throw new ResourceNotFoundException("Error finding metacard due to Federation issue", e);
} catch (IllegalArgumentException e) {
LOGGER.debug("Metacard couldn't be found {}", metacardId, e);
LOGGER.trace("EXITING: getEnterpriseResourceOptions");
throw new ResourceNotFoundException("Query returned null metacard", e);
}
LOGGER.trace("EXITING: getEnterpriseResourceOptions");
return Collections.singletonMap(ResourceRequest.OPTION_ARGUMENT, supportedOptions);
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class ResourceOperations method getResourceOptions.
public Map<String, Set<String>> getResourceOptions(String metacardId, String sourceId, boolean fanoutEnabled) throws ResourceNotFoundException {
LOGGER.trace("ENTERING: getResourceOptions");
Map<String, Set<String>> optionsMap;
try {
LOGGER.debug("source id to get options from: {}", sourceId);
QueryRequest queryRequest = new QueryRequestImpl(createMetacardIdQuery(metacardId), false, Collections.singletonList(sourceId == null ? this.getId() : sourceId), null);
QueryResponse queryResponse = queryOperations.query(queryRequest, null, false, fanoutEnabled);
List<Result> results = queryResponse.getResults();
if (!results.isEmpty()) {
Metacard metacard = results.get(0).getMetacard();
// or the local provider.
if (StringUtils.isEmpty(sourceId) || sourceId.equals(getId())) {
optionsMap = Collections.singletonMap(ResourceRequest.OPTION_ARGUMENT, getOptionsFromLocalProvider(metacard));
} else {
optionsMap = Collections.singletonMap(ResourceRequest.OPTION_ARGUMENT, getOptionsFromFederatedSource(metacard, sourceId));
}
} else {
String message = "Could not find metacard " + metacardId + " on source " + sourceId;
throw new ResourceNotFoundException(message);
}
} catch (UnsupportedQueryException e) {
LOGGER.debug("Error finding metacard {}", metacardId, e);
throw new ResourceNotFoundException("Error finding metacard due to Unsuppported Query", e);
} catch (FederationException e) {
LOGGER.debug("Error federating query for metacard {}", metacardId, e);
throw new ResourceNotFoundException("Error finding metacard due to Federation issue", e);
} catch (IllegalArgumentException e) {
LOGGER.debug("Metacard couldn't be found {}", metacardId, e);
throw new ResourceNotFoundException("Query returned null metacard", e);
} finally {
LOGGER.trace("EXITING: getResourceOptions");
}
return optionsMap;
}
Aggregations