use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.
the class DuplicateCommands method ingestSingly.
private List<Metacard> ingestSingly(CatalogFacade provider, List<Metacard> metacards) {
if (metacards.isEmpty()) {
return Collections.emptyList();
}
List<Metacard> createdMetacards = new ArrayList<>();
LOGGER.debug("Preparing to ingest {} records one at time.", metacards.size());
for (Metacard metacard : metacards) {
CreateRequest createRequest = new CreateRequestImpl(Arrays.asList(metacard));
CreateResponse createResponse;
try {
createResponse = provider.create(createRequest);
createdMetacards.addAll(createResponse.getCreatedMetacards());
} catch (IngestException | SourceUnavailableException e) {
LOGGER.debug("Error during ingest:", e);
} catch (Exception e) {
LOGGER.debug("Unexpected Exception during ingest:", e);
}
}
return createdMetacards;
}
use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.
the class DuplicateCommands method ingestMetacards.
protected List<Metacard> ingestMetacards(CatalogFacade provider, List<Metacard> metacards) {
if (metacards.isEmpty()) {
return Collections.emptyList();
}
List<Metacard> createdMetacards = new ArrayList<>();
LOGGER.debug("Preparing to ingest {} records", metacards.size());
CreateRequest createRequest = new CreateRequestImpl(metacards);
CreateResponse createResponse;
try {
createResponse = provider.create(createRequest);
createdMetacards = createResponse.getCreatedMetacards();
} catch (IngestException e) {
printErrorMessage(String.format("Received error while ingesting: %s%n", e.getMessage()));
LOGGER.debug("Error during ingest. Attempting to ingest batch individually.");
return ingestSingly(provider, metacards);
} catch (SourceUnavailableException e) {
printErrorMessage(String.format("Received error while ingesting: %s%n", e.getMessage()));
LOGGER.debug("Error during ingest:", e);
return createdMetacards;
} catch (Exception e) {
printErrorMessage(String.format("Unexpected Exception received while ingesting: %s%n", e.getMessage()));
LOGGER.debug("Unexpected Exception during ingest:", e);
return createdMetacards;
}
ingestedCount.addAndGet(createdMetacards.size());
failedCount.addAndGet(metacards.size() - createdMetacards.size());
failedMetacards.addAll(subtract(metacards, createdMetacards));
return createdMetacards;
}
use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.
the class IngestCommand method processBatch.
private boolean processBatch(CatalogFacade catalog, ArrayList<Metacard> metacards) throws SourceUnavailableException {
CreateResponse createResponse = null;
try {
createResponse = createMetacards(catalog, metacards);
} catch (IngestException e) {
printErrorMessage("Error executing command: " + e.getMessage());
if (INGEST_LOGGER.isWarnEnabled()) {
INGEST_LOGGER.warn("Error ingesting metacard batch {}", buildIngestLog(metacards), e);
}
} catch (SourceUnavailableException e) {
if (INGEST_LOGGER.isWarnEnabled()) {
INGEST_LOGGER.warn("Error on process batch, local Provider not available. {}" + " metacards failed to ingest. {}", metacards.size(), buildIngestLog(metacards), e);
}
} finally {
IntStream range = IntStream.range(0, metacards.size());
range.forEach(i -> phaser.arriveAndDeregister());
range.close();
}
if (createResponse != null) {
ingestCount.getAndAdd(metacards.size());
}
return createResponse != null;
}
use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.
the class RemoveCommand method executeRemoveFromStore.
private Object executeRemoveFromStore() throws CatalogCommandException {
try {
int batchCount = 0;
int deletedCount = 0;
if (CollectionUtils.isNotEmpty(ids) && !hasFilter()) {
deletedCount = deletedIdsPassedAsArguments();
}
if (hasFilter()) {
QueryRequestImpl queryRequest = new QueryRequestImpl(new QueryImpl(getFilter()), false);
String[] idsToDelete = getNextQueryBatch(queryRequest);
while (idsToDelete.length > 0) {
if (CollectionUtils.isNotEmpty(ids)) {
idsToDelete = Arrays.asList(idsToDelete).stream().filter(id -> ids.contains(id)).toArray(String[]::new);
}
DeleteRequestImpl deleteRequest = new DeleteRequestImpl(idsToDelete);
LOGGER.debug("Attempting to delete {} metacards from batch {}", idsToDelete.length, ++batchCount);
DeleteResponse deleteResponse = catalogFramework.delete(deleteRequest);
deletedCount += deleteResponse.getDeletedMetacards().size();
idsToDelete = getNextQueryBatch(queryRequest);
}
}
if (deletedCount > 0) {
printSuccessMessage(deletedCount + " documents successfully deleted.");
LOGGER.debug("{} documents removed using catalog:remove command", deletedCount);
} else {
printErrorMessage("No documents match provided IDs or filter");
LOGGER.debug("No documents deleted using the catalog:remove command");
}
} catch (IngestException | SourceUnavailableException | ParseException | CQLException e) {
throw new CatalogCommandException("Error executing catalog:remove", e);
}
return null;
}
use of ddf.catalog.source.SourceUnavailableException 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
* @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.trace("Attempting to execute query: {}", query);
try {
Map<String, Serializable> arguments = new HashMap<>();
String organization = framework.getOrganization();
String url = ui.getRequestUri().toString();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("organization: {}", organization);
LOGGER.trace("url: {}", url);
}
arguments.put("organization", organization);
arguments.put("url", url);
// interval
if (CollectionUtils.isNotEmpty(subscriptionList)) {
String subscription = subscriptionList.get(0);
LOGGER.trace("Subscription: {}", subscription);
arguments.put(Constants.SUBSCRIPTION_KEY, subscription);
List<String> intervalList = queryParams.get(UPDATE_QUERY_INTERVAL);
if (CollectionUtils.isNotEmpty(intervalList)) {
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.trace("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<>(), 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.debug("Unsupported query", ce);
response = Response.status(Response.Status.BAD_REQUEST).entity(wrapStringInPreformattedTags("Unsupported query")).build();
} catch (CatalogTransformerException e) {
LOGGER.debug("Error transforming response", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error transforming response")).build();
} catch (FederationException e) {
LOGGER.debug("Error executing query", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query")).build();
} catch (SourceUnavailableException e) {
LOGGER.debug("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.debug("RuntimeException on executing query", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("RuntimeException on executing query")).build();
}
return response;
}
Aggregations