use of ddf.catalog.source.UnsupportedQueryException in project ddf by codice.
the class ResourceOperations method getResourceInfo.
/**
* Retrieves a resource by URI.
* <p/>
* The {@link ResourceRequest} can specify either the product's URI or ID. If the product ID is
* specified, then the matching {@link Metacard} must first be retrieved and the product URI
* extracted from this {@link Metacard}.
*
* @param resourceRequest
* @param site
* @param isEnterprise
* @param federatedSite
* @param requestProperties
* @param fanoutEnabled
* @return
* @throws ResourceNotSupportedException
* @throws ResourceNotFoundException
*/
protected ResourceInfo getResourceInfo(ResourceRequest resourceRequest, String site, boolean isEnterprise, StringBuilder federatedSite, Map<String, Serializable> requestProperties, boolean fanoutEnabled) throws ResourceNotSupportedException, ResourceNotFoundException {
Metacard metacard;
URI resourceUri;
String name = resourceRequest.getAttributeName();
try {
if (ResourceRequest.GET_RESOURCE_BY_PRODUCT_URI.equals(name)) {
// because this is a get resource by product uri, we already
// have the product uri to return
LOGGER.debug("get resource by product uri");
Object value = resourceRequest.getAttributeValue();
if (value instanceof URI) {
resourceUri = (URI) value;
if (StringUtils.isNotBlank(resourceUri.getFragment())) {
resourceRequest.getProperties().put(ContentItem.QUALIFIER, resourceUri.getFragment());
try {
resourceUri = new URI(resourceUri.getScheme(), resourceUri.getSchemeSpecificPart(), null);
} catch (URISyntaxException e) {
throw new ResourceNotFoundException("Could not resolve URI by doing a URI based query: " + value);
}
}
Query propertyEqualToUriQuery = createPropertyIsEqualToQuery(Metacard.RESOURCE_URI, resourceUri.toString());
// if isEnterprise, go out and obtain the actual source
// where the product's metacard is stored.
QueryRequest queryRequest = new QueryRequestImpl(anyTag(propertyEqualToUriQuery, site, isEnterprise), isEnterprise, Collections.singletonList(site == null ? this.getId() : site), resourceRequest.getProperties());
QueryResponse queryResponse = queryOperations.query(queryRequest, null, true, fanoutEnabled);
if (!queryResponse.getResults().isEmpty()) {
metacard = queryResponse.getResults().get(0).getMetacard();
federatedSite.append(metacard.getSourceId());
LOGGER.debug("Trying to lookup resource URI {} for metacardId: {}", resourceUri, resourceUri);
if (!requestProperties.containsKey(Metacard.ID)) {
requestProperties.put(Metacard.ID, metacard.getId());
}
if (!requestProperties.containsKey(Metacard.RESOURCE_URI)) {
requestProperties.put(Metacard.RESOURCE_URI, metacard.getResourceURI());
}
} else {
throw new ResourceNotFoundException("Could not resolve source id for URI by doing a URI based query: " + resourceUri);
}
} else {
throw new ResourceNotSupportedException("The GetResourceRequest with attribute value of class '" + value.getClass() + "' is not supported by this instance of the CatalogFramework.");
}
} else if (ResourceRequest.GET_RESOURCE_BY_ID.equals(name)) {
// since this is a get resource by id, we need to obtain the
// product URI
LOGGER.debug("get resource by id");
Object value = resourceRequest.getAttributeValue();
if (value instanceof String) {
String metacardId = (String) value;
LOGGER.debug("metacardId = {}, site = {}", metacardId, site);
QueryRequest queryRequest = new QueryRequestImpl(anyTag(createMetacardIdQuery(metacardId), site, isEnterprise), isEnterprise, Collections.singletonList(site == null ? this.getId() : site), resourceRequest.getProperties());
QueryResponse queryResponse = queryOperations.query(queryRequest, null, true, fanoutEnabled);
if (!queryResponse.getResults().isEmpty()) {
metacard = queryResponse.getResults().get(0).getMetacard();
resourceUri = metacard.getResourceURI();
federatedSite.append(metacard.getSourceId());
LOGGER.debug("Trying to lookup resource URI {} for metacardId: {}", resourceUri, metacardId);
} else {
throw new ResourceNotFoundException("Could not resolve source id for URI by doing an id based query: " + metacardId);
}
if (!requestProperties.containsKey(Metacard.ID)) {
requestProperties.put(Metacard.ID, metacardId);
}
if (!requestProperties.containsKey(Metacard.RESOURCE_URI)) {
requestProperties.put(Metacard.RESOURCE_URI, resourceUri);
}
} else {
throw new ResourceNotSupportedException("The GetResourceRequest with attribute value of class '" + value.getClass() + "' is not supported by this instance of the CatalogFramework.");
}
} else {
throw new ResourceNotSupportedException("The GetResourceRequest with attribute name '" + name + "' is not supported by this instance of the CatalogFramework.");
}
} catch (UnsupportedQueryException | FederationException e) {
throw new ResourceNotFoundException(DEFAULT_RESOURCE_NOT_FOUND_MESSAGE, e);
}
LOGGER.debug("Returning resourceURI: {}", resourceUri);
if (resourceUri == null) {
throw new ResourceNotFoundException(DEFAULT_RESOURCE_NOT_FOUND_MESSAGE);
}
return new ResourceInfo(metacard, resourceUri);
}
use of ddf.catalog.source.UnsupportedQueryException in project ddf by codice.
the class QueryOperations method validateQueryRequest.
/**
* Validates that the {@link QueryRequest} is non-null and that the query in it is non-null.
* Also checks that the query's page size is between 1 and the {@link #MAX_PAGE_SIZE}.
* If not, the query's page size is set to the {@link #MAX_PAGE_SIZE}.
*
* @param queryRequest the {@link QueryRequest}
* @throws UnsupportedQueryException if the {@link QueryRequest} is null or the query in it is null
*/
private QueryRequest validateQueryRequest(QueryRequest queryRequest) throws UnsupportedQueryException {
if (queryRequest == null) {
throw new UnsupportedQueryException("QueryRequest was null, either passed in from endpoint, or as output from a PreQuery Plugin");
}
if (queryRequest.getQuery() == null) {
throw new UnsupportedQueryException("Cannot perform query with null query, either passed in from endpoint, or as output from a PreQuery Plugin");
}
int queryPageSize = queryRequest.getQuery().getPageSize();
if (queryPageSize >= 1 && queryPageSize <= MAX_PAGE_SIZE) {
return queryRequest;
}
Query originalQuery = queryRequest.getQuery();
Query modifiedQuery = new QueryImpl(originalQuery, originalQuery.getStartIndex(), MAX_PAGE_SIZE, originalQuery.getSortBy(), originalQuery.requestsTotalResultsCount(), originalQuery.getTimeoutMillis());
return new QueryRequestImpl(modifiedQuery, queryRequest.isEnterprise(), queryRequest.getSourceIds(), queryRequest.getProperties());
}
use of ddf.catalog.source.UnsupportedQueryException in project ddf by codice.
the class QueryOperations method query.
//
// Helper methods
//
QueryResponse query(QueryRequest queryRequest, FederationStrategy strategy, boolean overrideFanoutRename, boolean fanoutEnabled) throws UnsupportedQueryException, FederationException {
FederationStrategy fedStrategy = strategy;
QueryResponse queryResponse;
queryRequest = setFlagsOnRequest(queryRequest);
try {
queryRequest = validateQueryRequest(queryRequest);
queryRequest = getFanoutQuery(queryRequest, fanoutEnabled);
queryRequest = preProcessPreAuthorizationPlugins(queryRequest);
queryRequest = populateQueryRequestPolicyMap(queryRequest);
queryRequest = processPreQueryAccessPlugins(queryRequest);
queryRequest = processPreQueryPlugins(queryRequest);
queryRequest = validateQueryRequest(queryRequest);
if (fedStrategy == null) {
if (frameworkProperties.getFederationStrategy() == null) {
throw new FederationException("No Federation Strategies exist. Cannot execute federated query.");
} else {
LOGGER.debug("FederationStrategy was not specified, using default strategy: " + frameworkProperties.getFederationStrategy().getClass());
fedStrategy = frameworkProperties.getFederationStrategy();
}
}
queryResponse = doQuery(queryRequest, fedStrategy);
queryResponse = injectAttributes(queryResponse);
queryResponse = validateFixQueryResponse(queryResponse, overrideFanoutRename, fanoutEnabled);
queryResponse = postProcessPreAuthorizationPlugins(queryResponse);
queryResponse = populateQueryResponsePolicyMap(queryResponse);
queryResponse = processPostQueryAccessPlugins(queryResponse);
queryResponse = processPostQueryPlugins(queryResponse);
} catch (RuntimeException re) {
throw new UnsupportedQueryException("Exception during runtime while performing query", re);
}
return queryResponse;
}
use of ddf.catalog.source.UnsupportedQueryException in project ddf by codice.
the class OpenSearchEndpoint method executeQuery.
/**
* Executes the OpenSearchQuery and formulates the response
*
* @param format - of the results in the response
* @param query - the query to execute
* @param ui -the ui information to use to format the results
* @param properties
* @return the response on the query
*/
private Response executeQuery(String format, OpenSearchQuery query, UriInfo ui, Map<String, Serializable> properties) {
Response response = null;
String queryFormat = format;
MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
List<String> subscriptionList = queryParams.get(Constants.SUBSCRIPTION_KEY);
LOGGER.debug("Attempting to execute query: {}", query.toString());
try {
Map<String, Serializable> arguments = new HashMap<String, Serializable>();
String organization = framework.getOrganization();
String url = ui.getRequestUri().toString();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("organization: {}", organization);
LOGGER.debug("url: {}", url);
}
arguments.put("organization", organization);
arguments.put("url", url);
// interval
if (subscriptionList != null && !subscriptionList.isEmpty()) {
String subscription = subscriptionList.get(0);
LOGGER.debug("Subscription: {}", subscription);
arguments.put(Constants.SUBSCRIPTION_KEY, subscription);
List<String> intervalList = queryParams.get(UPDATE_QUERY_INTERVAL);
if (intervalList != null && !intervalList.isEmpty()) {
arguments.put(UPDATE_QUERY_INTERVAL, intervalList.get(0));
}
}
if (StringUtils.isEmpty(queryFormat)) {
queryFormat = DEFAULT_FORMAT;
}
if (query.getFilter() != null) {
QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), properties);
QueryResponse queryResponse;
LOGGER.debug("Sending query");
queryResponse = framework.query(queryRequest);
// pass in the format for the transform
BinaryContent content = framework.transform(queryResponse, queryFormat, arguments);
response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
} else {
// No query was specified
QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), null);
// Create a dummy QueryResponse with zero results
QueryResponseImpl queryResponseQueue = new QueryResponseImpl(queryRequest, new ArrayList<Result>(), 0);
// pass in the format for the transform
BinaryContent content = framework.transform(queryResponseQueue, queryFormat, arguments);
if (null != content) {
response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
}
}
} catch (UnsupportedQueryException ce) {
LOGGER.info("Unsupported query", ce);
response = Response.status(Response.Status.BAD_REQUEST).entity(wrapStringInPreformattedTags("Unsupported query")).build();
} catch (CatalogTransformerException e) {
LOGGER.info("Error transforming response", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error transforming response")).build();
} catch (FederationException e) {
LOGGER.info("Error executing query", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query")).build();
} catch (SourceUnavailableException e) {
LOGGER.info("Error executing query because the underlying source was unavailable.", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query because the underlying source was unavailable.")).build();
} 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", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("RuntimeException on executing query")).build();
}
return response;
}
use of ddf.catalog.source.UnsupportedQueryException in project ddf by codice.
the class OpenSearchSource method performRequest.
/**
* Performs a GET request on the client and returns the entity as an InputStream.
*
* @param client Client to perform the GET request on.
* @return The entity of the response as an InputStream.
* @throws UnsupportedQueryException
*/
private InputStream performRequest(WebClient client) throws UnsupportedQueryException {
Response clientResponse = client.get();
InputStream stream = null;
Object entityObj = clientResponse.getEntity();
if (entityObj != null) {
stream = (InputStream) entityObj;
}
if (Response.Status.OK.getStatusCode() != clientResponse.getStatus()) {
String error = "";
try {
if (stream != null) {
error = IOUtils.toString(stream);
}
} catch (IOException ioe) {
LOGGER.debug("Could not convert error message to a string for output.", ioe);
}
String errorMsg = "Received error code from remote source (status " + clientResponse.getStatus() + "): " + error;
throw new UnsupportedQueryException(errorMsg);
}
return stream;
}
Aggregations