use of ddf.catalog.operation.impl.DeleteRequestImpl in project ddf by codice.
the class CatalogComponentFrameworkTest method testDeleteWithListOfIds.
@Test
public /**
* Operation: DELETE
* Body contains: List<String>
*/
void testDeleteWithListOfIds() throws Exception {
resetMocks();
// Setup expectations to verify
final MockEndpoint mockVerifierEndpoint = getMockEndpoint("mock:result");
mockVerifierEndpoint.expectedMessageCount(1);
final List<Metacard> metacards = new ArrayList<Metacard>();
metacards.add(metacard1);
metacards.add(metacard2);
// setup mock catalog framework
final String[] metacardIds = new String[metacards.size()];
for (int i = 0; i < metacards.size(); i++) {
metacardIds[i] = metacards.get(i).getId();
}
final List<String> metacardIdList = Arrays.asList(metacardIds);
DeleteRequest deleteRequest = new DeleteRequestImpl(metacardIds);
DeleteResponse deleteResponse = new DeleteResponseImpl(deleteRequest, new HashMap(), metacards);
when(catalogFramework.delete(any(DeleteRequest.class))).thenReturn(deleteResponse);
// Exercise the route with a DELETE operation
template.sendBodyAndHeader("direct:sampleInput", metacardIdList, "Operation", "DELETE");
// Verify that the number of metacards in the exchange after the records
// is identical to the input
assertListSize(mockVerifierEndpoint.getExchanges(), 1);
final Exchange exchange = mockVerifierEndpoint.getExchanges().get(0);
final List<Update> cardsDeleted = (List<Update>) exchange.getIn().getBody();
assertListSize(cardsDeleted, 2);
mockVerifierEndpoint.assertIsSatisfied();
}
use of ddf.catalog.operation.impl.DeleteRequestImpl in project ddf by codice.
the class FrameworkProducer method delete.
/**
* Deletes metacard(s) in the catalog using the Catalog Framework.
*
* @param exchange
* The {@link org.apache.camel.Exchange} can contain a
* {@link org.apache.camel.Message} with a body of type {@link java.util.List} of
* {@link String} or a single {@link String}. Each String represents the ID of a
* Metacard to be deleted.
* @throws ddf.catalog.source.SourceUnavailableException
* @throws ddf.catalog.source.IngestException
* @throws ddf.camel.component.catalog.framework.FrameworkProducerException
*/
private void delete(final Exchange exchange) throws SourceUnavailableException, IngestException, FrameworkProducerException {
DeleteResponse deleteResponse = null;
// read in data
final List<String> metacardIdsToBeDeleted = readBodyDataAsMetacardIds(exchange);
// process if data is valid
if (!validateList(metacardIdsToBeDeleted, String.class)) {
LOGGER.debug("Validation of Metacard id list failed");
processCatalogResponse(deleteResponse, exchange);
throw new FrameworkProducerException("Validation of Metacard id list failed");
}
LOGGER.debug("Validation of Metacard id list passed...");
final String[] metacardIdsToBeDeletedArray = new String[metacardIdsToBeDeleted.size()];
final DeleteRequest deleteRequest = new DeleteRequestImpl(metacardIdsToBeDeleted.toArray(metacardIdsToBeDeletedArray));
final int expectedNumberOfDeletedMetacards = metacardIdsToBeDeleted.size();
if (expectedNumberOfDeletedMetacards < 1) {
LOGGER.debug("Empty list of Metacard id...nothing to process");
processCatalogResponse(deleteResponse, exchange);
return;
}
LOGGER.debug("Making DELETE call to Catalog Framework...");
deleteResponse = catalogFramework.delete(deleteRequest);
if (deleteResponse == null) {
LOGGER.debug("DeleteResponse is null from catalog framework");
processCatalogResponse(deleteResponse, exchange);
return;
}
final List<Metacard> deletedMetacards = deleteResponse.getDeletedMetacards();
if (deletedMetacards == null) {
LOGGER.debug("DeleteResponse returned null metacards list");
processCatalogResponse(deleteResponse, exchange);
return;
}
final int numberOfDeletedMetacards = deletedMetacards.size();
if (numberOfDeletedMetacards != expectedNumberOfDeletedMetacards) {
LOGGER.debug("Expected {} metacards deleted but only {} were successfully deleted", expectedNumberOfDeletedMetacards, numberOfDeletedMetacards);
processCatalogResponse(deleteResponse, exchange);
return;
}
LOGGER.debug("Deleted {} metacards", numberOfDeletedMetacards);
processCatalogResponse(deleteResponse, exchange);
}
use of ddf.catalog.operation.impl.DeleteRequestImpl in project ddf by codice.
the class RemoveAllCommand method executeRemoveAllFromStore.
private Object executeRemoveAllFromStore() throws Exception {
CatalogFacade catalog = getCatalog();
QueryRequest firstQuery = getIntendedQuery(filterBuilder, true);
QueryRequest subsequentQuery = getIntendedQuery(filterBuilder, false);
long totalAmountDeleted = 0;
long start = System.currentTimeMillis();
SourceResponse response;
try {
response = catalog.query(firstQuery);
} catch (UnsupportedQueryException e) {
firstQuery = getAlternateQuery(filterBuilder, true);
subsequentQuery = getAlternateQuery(filterBuilder, false);
response = catalog.query(firstQuery);
}
if (response == null) {
printErrorMessage("No response from Catalog.");
return null;
}
if (needsAlternateQueryAndResponse(response)) {
firstQuery = getAlternateQuery(filterBuilder, true);
subsequentQuery = getAlternateQuery(filterBuilder, false);
response = catalog.query(firstQuery);
}
String totalAmount = getTotalAmount(response.getHits());
while (response.getResults().size() > 0) {
// Add metacard ids to string array
List<String> ids = response.getResults().stream().filter(Objects::nonNull).map(Result::getMetacard).filter(Objects::nonNull).map(Metacard::getId).collect(Collectors.toList());
// Delete the records
DeleteRequestImpl request = new DeleteRequestImpl(ids.toArray(new String[ids.size()]));
DeleteResponse deleteResponse = catalog.delete(request);
int amountDeleted = deleteResponse.getDeletedMetacards().size();
totalAmountDeleted += amountDeleted;
console.print(String.format(PROGRESS_FORMAT, totalAmountDeleted, totalAmount));
console.flush();
// Break out if there are no more records to delete
if (amountDeleted < batchSize || batchSize < 1) {
break;
}
// Re-query when necessary
response = catalog.query(subsequentQuery);
}
long end = System.currentTimeMillis();
String info = String.format(" %d file(s) removed in %3.3f seconds%n", totalAmountDeleted, (end - start) / MS_PER_SECOND);
LOGGER.info(info);
LOGGER.info(totalAmountDeleted + " files removed using cache:removeAll command");
console.println();
console.print(info);
return null;
}
use of ddf.catalog.operation.impl.DeleteRequestImpl in project ddf by codice.
the class RemoveCommand method executeRemoveFromStore.
private Object executeRemoveFromStore() throws Exception {
CatalogFacade catalogProvider = getCatalog();
if (hasFilter()) {
QueryImpl query = new QueryImpl(getFilter());
query.setRequestsTotalResultsCount(true);
query.setPageSize(-1);
Map<String, Serializable> properties = new HashMap<>();
properties.put("mode", "native");
SourceResponse queryResponse = catalogProvider.query(new QueryRequestImpl(query, properties));
final List<String> idsFromFilteredQuery = queryResponse.getResults().stream().map(result -> result.getMetacard().getId()).collect(Collectors.toList());
if (ids == null) {
ids = idsFromFilteredQuery;
} else {
ids = ids.stream().filter(id -> idsFromFilteredQuery.contains(id)).collect(Collectors.toList());
}
}
final int numberOfMetacardsToRemove = ids.size();
if (numberOfMetacardsToRemove > 0) {
printSuccessMessage("Found " + numberOfMetacardsToRemove + " metacards to remove.");
} else {
printErrorMessage("No records found meeting filter criteria.");
return null;
}
DeleteRequestImpl request = new DeleteRequestImpl(ids.toArray(new String[numberOfMetacardsToRemove]));
DeleteResponse response = catalogProvider.delete(request);
if (response.getDeletedMetacards().size() > 0) {
printSuccessMessage(ids + " successfully deleted.");
LOGGER.info(ids + " removed using catalog:remove command");
} else {
printErrorMessage(ids + " could not be deleted.");
LOGGER.info(ids + " could not be deleted using catalog:remove command");
}
return null;
}
use of ddf.catalog.operation.impl.DeleteRequestImpl in project ddf by codice.
the class MetacardApplication method init.
@Override
public void init() {
get("/metacardtype", (req, res) -> {
return util.getJson(util.getMetacardTypeMap());
});
get("/metacard/:id", (req, res) -> {
String id = req.params(":id");
return util.metacardToJson(id);
});
get("/metacard/:id/attribute/validation", (req, res) -> {
String id = req.params(":id");
return util.getJson(validator.getValidation(util.getMetacard(id)));
});
get("/metacard/:id/validation", (req, res) -> {
String id = req.params(":id");
return util.getJson(validator.getFullValidation(util.getMetacard(id)));
});
post("/metacards", APPLICATION_JSON, (req, res) -> {
List<String> ids = JsonFactory.create().parser().parseList(String.class, req.body());
List<Metacard> metacards = util.getMetacards(ids, "*").entrySet().stream().map(Map.Entry::getValue).map(Result::getMetacard).collect(Collectors.toList());
return util.metacardsToJson(metacards);
});
delete("/metacards", APPLICATION_JSON, (req, res) -> {
List<String> ids = JsonFactory.create().parser().parseList(String.class, req.body());
DeleteResponse deleteResponse = catalogFramework.delete(new DeleteRequestImpl(new ArrayList<>(ids), Metacard.ID, null));
if (deleteResponse.getProcessingErrors() != null && !deleteResponse.getProcessingErrors().isEmpty()) {
res.status(500);
return ImmutableMap.of("message", "Unable to archive metacards.");
}
return ImmutableMap.of("message", "Successfully archived metacards.");
}, util::getJson);
patch("/metacards", APPLICATION_JSON, (req, res) -> {
List<MetacardChanges> metacardChanges = JsonFactory.createUseJSONDates().parser().parseList(MetacardChanges.class, req.body());
UpdateResponse updateResponse = patchMetacards(metacardChanges);
if (updateResponse.getProcessingErrors() != null && !updateResponse.getProcessingErrors().isEmpty()) {
res.status(500);
return updateResponse.getProcessingErrors();
}
return req.body();
});
put("/validate/attribute/:attribute", TEXT_PLAIN, (req, res) -> {
String attribute = req.params(":attribute");
String value = req.body();
return util.getJson(validator.validateAttribute(attribute, value));
});
get("/history/:id", (req, res) -> {
String id = req.params(":id");
List<Result> queryResponse = getMetacardHistory(id);
if (queryResponse.isEmpty()) {
res.status(204);
return "[]";
}
List<HistoryResponse> response = queryResponse.stream().map(Result::getMetacard).map(mc -> new HistoryResponse(mc.getId(), (String) mc.getAttribute(MetacardVersion.EDITED_BY).getValue(), (Date) mc.getAttribute(MetacardVersion.VERSIONED_ON).getValue())).sorted(Comparator.comparing(HistoryResponse::getVersioned)).collect(Collectors.toList());
return util.getJson(response);
});
get("/history/revert/:id/:revertid", (req, res) -> {
String id = req.params(":id");
String revertId = req.params(":revertid");
Metacard versionMetacard = util.getMetacard(revertId);
List<Result> queryResponse = getMetacardHistory(id);
if (queryResponse == null || queryResponse.isEmpty()) {
throw new NotFoundException("Could not find metacard with id: " + id);
}
Optional<Metacard> contentVersion = queryResponse.stream().map(Result::getMetacard).filter(mc -> getVersionedOnDate(mc).isAfter(getVersionedOnDate(versionMetacard)) || getVersionedOnDate(mc).equals(getVersionedOnDate(versionMetacard))).filter(mc -> CONTENT_ACTIONS.contains(Action.ofMetacard(mc))).filter(mc -> mc.getResourceURI() != null).filter(mc -> ContentItem.CONTENT_SCHEME.equals(mc.getResourceURI().getScheme())).sorted(Comparator.comparing((Metacard mc) -> util.parseToDate(mc.getAttribute(MetacardVersion.VERSIONED_ON).getValue()))).findFirst();
if (!contentVersion.isPresent()) {
/* no content versions, just restore metacard */
revertMetacard(versionMetacard, id, false);
} else {
revertContentandMetacard(contentVersion.get(), versionMetacard, id);
}
return util.metacardToJson(MetacardVersionImpl.toMetacard(versionMetacard, types));
});
get("/associations/:id", (req, res) -> {
String id = req.params(":id");
return util.getJson(associated.getAssociations(id));
});
put("/associations/:id", (req, res) -> {
String id = req.params(":id");
List<Associated.Edge> edges = JsonFactory.create().parser().parseList(Associated.Edge.class, req.body());
associated.putAssociations(id, edges);
return req.body();
});
post("/subscribe/:id", (req, res) -> {
String email = getSubjectEmail();
if (isEmpty(email)) {
throw new NotFoundException("Login to subscribe to workspace.");
}
String id = req.params(":id");
subscriptions.addEmail(id, email);
return ImmutableMap.of("message", String.format("Successfully subscribed to id = %s.", id));
}, util::getJson);
post("/unsubscribe/:id", (req, res) -> {
String email = getSubjectEmail();
if (isEmpty(email)) {
throw new NotFoundException("Login to un-subscribe from workspace.");
}
String id = req.params(":id");
subscriptions.removeEmail(id, email);
return ImmutableMap.of("message", String.format("Successfully un-subscribed to id = %s.", id));
}, util::getJson);
get("/workspaces/:id", (req, res) -> {
String id = req.params(":id");
String email = getSubjectEmail();
Metacard metacard = util.getMetacard(id);
// NOTE: the isEmpty is to guard against users with no email (such as guest).
boolean isSubscribed = !isEmpty(email) && subscriptions.getEmails(metacard.getId()).contains(email);
return ImmutableMap.builder().putAll(transformer.transform(metacard)).put("subscribed", isSubscribed).build();
}, util::getJson);
get("/workspaces", (req, res) -> {
String email = getSubjectEmail();
Map<String, Result> workspaceMetacards = util.getMetacardsByFilter(WorkspaceAttributes.WORKSPACE_TAG);
// NOTE: the isEmpty is to guard against users with no email (such as guest).
Set<String> ids = isEmpty(email) ? Collections.emptySet() : subscriptions.getSubscriptions(email);
return workspaceMetacards.entrySet().stream().map(Map.Entry::getValue).map(Result::getMetacard).map(metacard -> {
boolean isSubscribed = ids.contains(metacard.getId());
try {
return ImmutableMap.builder().putAll(transformer.transform(metacard)).put("subscribed", isSubscribed).build();
} catch (RuntimeException e) {
LOGGER.debug("Could not transform metacard. WARNING: This indicates there is invalid data in the system. Metacard title: '{}', id:'{}'", metacard.getTitle(), metacard.getId(), e);
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList());
}, util::getJson);
post("/workspaces", APPLICATION_JSON, (req, res) -> {
Map<String, Object> incoming = JsonFactory.create().parser().parseMap(req.body());
Metacard saved = saveMetacard(transformer.transform(incoming));
Map<String, Object> response = transformer.transform(saved);
res.status(201);
return util.getJson(response);
});
put("/workspaces/:id", APPLICATION_JSON, (req, res) -> {
String id = req.params(":id");
Map<String, Object> workspace = JsonFactory.create().parser().parseMap(req.body());
Metacard metacard = transformer.transform(workspace);
metacard.setAttribute(new AttributeImpl(Metacard.ID, id));
Metacard updated = updateMetacard(id, metacard);
return util.getJson(transformer.transform(updated));
});
delete("/workspaces/:id", APPLICATION_JSON, (req, res) -> {
String id = req.params(":id");
catalogFramework.delete(new DeleteRequestImpl(id));
return ImmutableMap.of("message", "Successfully deleted.");
}, util::getJson);
get("/enumerations/metacardtype/:type", APPLICATION_JSON, (req, res) -> {
return util.getJson(enumExtractor.getEnumerations(req.params(":type")));
});
get("/enumerations/attribute/:attribute", APPLICATION_JSON, (req, res) -> {
return util.getJson(enumExtractor.getAttributeEnumerations(req.params(":attribute")));
});
get("/localcatalogid", (req, res) -> {
return String.format("{\"%s\":\"%s\"}", "local-catalog-id", catalogFramework.getId());
});
after((req, res) -> {
res.type(APPLICATION_JSON);
});
exception(IngestException.class, (ex, req, res) -> {
res.status(404);
res.header(CONTENT_TYPE, APPLICATION_JSON);
LOGGER.debug("Failed to ingest metacard", ex);
res.body(util.getJson(ImmutableMap.of("message", UPDATE_ERROR_MESSAGE)));
});
exception(NotFoundException.class, (ex, req, res) -> {
res.status(404);
res.header(CONTENT_TYPE, APPLICATION_JSON);
LOGGER.debug("Failed to find metacard.", ex);
res.body(util.getJson(ImmutableMap.of("message", ex.getMessage())));
});
exception(NumberFormatException.class, (ex, req, res) -> {
res.status(400);
res.header(CONTENT_TYPE, APPLICATION_JSON);
res.body(util.getJson(ImmutableMap.of("message", "Invalid values for numbers")));
});
exception(RuntimeException.class, (ex, req, res) -> {
LOGGER.debug("Exception occured.", ex);
res.status(404);
res.header(CONTENT_TYPE, APPLICATION_JSON);
res.body(util.getJson(ImmutableMap.of("message", "Could not find what you were looking for")));
});
}
Aggregations