use of ddf.catalog.federation.FederationException 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.federation.FederationException in project ddf by codice.
the class QueryOperations method populateQueryRequestPolicyMap.
private QueryRequest populateQueryRequestPolicyMap(QueryRequest queryReq) throws FederationException {
HashMap<String, Set<String>> requestPolicyMap = new HashMap<>();
Map<String, Serializable> unmodifiableProperties = Collections.unmodifiableMap(queryReq.getProperties());
for (PolicyPlugin plugin : frameworkProperties.getPolicyPlugins()) {
try {
PolicyResponse policyResponse = plugin.processPreQuery(queryReq.getQuery(), unmodifiableProperties);
opsSecuritySupport.buildPolicyMap(requestPolicyMap, policyResponse.operationPolicy().entrySet());
} catch (StopProcessingException e) {
throw new FederationException("Query could not be executed.", e);
}
}
queryReq.getProperties().put(PolicyPlugin.OPERATION_SECURITY, requestPolicyMap);
return queryReq;
}
use of ddf.catalog.federation.FederationException 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.federation.FederationException 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.federation.FederationException in project ddf by codice.
the class DeleteOperations method populateMetacards.
private DeleteRequest populateMetacards(DeleteRequest deleteRequest, List<String> fanoutTagBlacklist) throws IngestException, StopProcessingException {
QueryRequestImpl queryRequest = createQueryRequest(deleteRequest);
QueryResponse query;
try {
query = 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 delete");
}
List<Metacard> metacards = query.getResults().stream().map(Result::getMetacard).collect(Collectors.toList());
if (blockDeleteMetacards(metacards, fanoutTagBlacklist)) {
String message = "Fanout proxy does not support delete operations with blacklisted metacard tag";
LOGGER.debug("{}. Tags blacklist: {}", message, fanoutTagBlacklist);
throw new IngestException(message);
}
if (!foundAllDeleteRequestMetacards(deleteRequest, query)) {
logFailedQueryInfo(deleteRequest, query);
throw new StopProcessingException("Could not find all metacards specified in request");
}
deleteRequest = rewriteRequestToAvoidHistoryConflicts(deleteRequest, query);
deleteRequest.getProperties().put(Constants.OPERATION_TRANSACTION_KEY, new OperationTransactionImpl(OperationTransaction.OperationType.DELETE, metacards));
return deleteRequest;
}
Aggregations