use of ddf.catalog.operation.CreateResponse in project ddf by codice.
the class AbstractCatalogService method addDocument.
private String addDocument(Map.Entry<AttachmentInfo, Metacard> attachmentInfoAndMetacard, List<String> contentTypeList, String transformerParam, InputStream message) throws CatalogServiceException {
try {
LOGGER.debug("POST");
MimeType mimeType = getMimeType(contentTypeList);
CreateResponse createResponse;
if (attachmentInfoAndMetacard == null) {
CreateRequest createRequest = new CreateRequestImpl(generateMetacard(mimeType, null, message, transformerParam));
createResponse = catalogFramework.create(createRequest);
} else {
String id = attachmentInfoAndMetacard.getValue() == null ? null : attachmentInfoAndMetacard.getValue().getId();
if (id == null) {
id = uuidGenerator.generateUuid();
}
CreateStorageRequest streamCreateRequest = new CreateStorageRequestImpl(Collections.singletonList(new IncomingContentItem(id, attachmentInfoAndMetacard.getKey().getStream(), attachmentInfoAndMetacard.getKey().getContentType(), attachmentInfoAndMetacard.getKey().getFilename(), 0L, attachmentInfoAndMetacard.getValue())), null);
createResponse = catalogFramework.create(streamCreateRequest);
}
String id = createResponse.getCreatedMetacards().get(0).getId();
LOGGER.debug("Create Response id [{}]", id);
LOGGER.debug("Entry successfully saved, id: {}", id);
if (INGEST_LOGGER.isInfoEnabled()) {
INGEST_LOGGER.info("Entry successfully saved, id: {}", id);
}
return id;
} catch (SourceUnavailableException e) {
String exceptionMessage = "Cannot create catalog entry because source is unavailable: ";
LOGGER.info(exceptionMessage, e);
// Catalog framework logs these exceptions to the ingest logger so we don't have to.
throw new InternalServerErrorException(exceptionMessage);
} catch (InternalIngestException e) {
String exceptionMessage = "Error while storing entry in catalog: ";
LOGGER.info(exceptionMessage, e);
// Catalog framework logs these exceptions to the ingest logger so we don't have to.
throw new InternalServerErrorException(exceptionMessage);
} catch (MetacardCreationException | IngestException e) {
String errorMessage = "Error while storing entry in catalog: ";
LOGGER.info(errorMessage, e);
// Catalog framework logs these exceptions to the ingest logger so we don't have to.
throw new CatalogServiceException(errorMessage);
} finally {
IOUtils.closeQuietly(message);
}
}
use of ddf.catalog.operation.CreateResponse in project ddf by codice.
the class SolrProviderDelete method addAndDeleteMetacards.
private void addAndDeleteMetacards(int metacardCount) throws IngestException, UnsupportedQueryException {
deleteAll(provider);
List<Metacard> metacards = new ArrayList<>();
for (int i = 0; i < metacardCount; i++) {
metacards.add(new MockMetacard(Library.getFlagstaffRecord()));
}
CreateResponse createResponse = create(metacards, provider);
assertThat(createResponse.getCreatedMetacards().size(), is(metacards.size()));
List<String> ids = new ArrayList<>();
for (Metacard mc : createResponse.getCreatedMetacards()) {
ids.add(mc.getId());
}
DeleteResponse deleteResponse = delete(ids.toArray(new String[metacardCount]), provider);
List<Metacard> deletedMetacards = deleteResponse.getDeletedMetacards();
assertThat(deletedMetacards.size(), is(metacards.size()));
for (int i = 0; i < metacardCount; i++) {
assertThat(deletedMetacards.get(i).getId(), isIn(ids));
}
}
use of ddf.catalog.operation.CreateResponse in project ddf by codice.
the class GeoNamesCatalogIndexerTest method setUp.
@Before
public void setUp() throws Exception {
geoEntryCreator = mock(GeoEntryCreator.class);
when(geoEntryCreator.createGeoEntry(anyString(), anyString())).thenReturn(GEO_ENTRY);
geoEntryExtractor = new GeoNamesFileExtractor();
geoEntryExtractor.setGeoEntryCreator(geoEntryCreator);
catalogFramework = mock(CatalogFramework.class);
uuidGenerator = mock(UuidGenerator.class);
createResponse = mock(CreateResponse.class);
when(createResponse.getCreatedMetacards()).thenReturn(Collections.singletonList(METACARD));
when(catalogFramework.create(any(CreateRequest.class))).thenReturn(createResponse);
when(uuidGenerator.generateUuid()).thenReturn(UUID.randomUUID().toString());
catalogProvider = mock(CatalogProvider.class);
DeleteResponse deleteResponse = mock(DeleteResponse.class);
when(deleteResponse.getDeletedMetacards()).thenReturn(Collections.singletonList(METACARD));
when(catalogProvider.delete(any(DeleteRequest.class))).thenReturn(deleteResponse);
queryResponse = mock(QueryResponse.class);
when(queryResponse.getResults()).thenReturn(Collections.singletonList(new ResultImpl(new MetacardImpl())));
when(catalogFramework.query(any(QueryRequest.class))).thenReturn(queryResponse);
progressCallback = progress -> {
};
geoNamesCatalogIndexer = new GeoNamesCatalogIndexer(catalogFramework, uuidGenerator, new GeoEntryAttributes(), new GeotoolsFilterBuilder(), Collections.singletonList(catalogProvider));
}
use of ddf.catalog.operation.CreateResponse in project ddf by codice.
the class CswEndpoint method transaction.
@Override
@POST
@Consumes({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
@Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
public TransactionResponseType transaction(CswTransactionRequest request) throws CswException {
if (request == null) {
throw new CswException("TransactionRequest request is null");
}
TransactionResponseType response = new TransactionResponseType();
TransactionSummaryType summary = new TransactionSummaryType();
summary.setTotalInserted(BigInteger.valueOf(0));
summary.setTotalUpdated(BigInteger.valueOf(0));
summary.setTotalDeleted(BigInteger.valueOf(0));
response.setTransactionSummary(summary);
response.setVersion(CswConstants.VERSION_2_0_2);
int numInserted = 0;
final Subject subject = SecurityUtils.getSubject();
for (InsertAction insertAction : request.getInsertActions()) {
final InsertAction transformInsertAction = transformInsertAction(insertAction);
List<Metacard> metacards = transformInsertAction.getRecords();
CompletionService<CreateResponse> completionService = new ExecutorCompletionService<>(queryExecutor);
for (Metacard record : metacards) {
CreateRequest createRequest = new CreateRequestImpl(record);
Callable<CreateResponse> callable = () -> {
try {
return framework.create(createRequest);
} catch (IngestException | SourceUnavailableException e) {
LOGGER.debug("Unable to insert record(s)", e);
throw new CswException("Unable to insert record(s).", CswConstants.TRANSACTION_FAILED, transformInsertAction.getHandle());
}
};
Callable<CreateResponse> createCallable = subject.associateWith(callable);
completionService.submit(createCallable);
}
for (int i = 0; i < metacards.size(); i++) {
try {
Future<CreateResponse> completedFuture = completionService.take();
try {
CreateResponse futureResponse = completedFuture.get();
numInserted += futureResponse.getCreatedMetacards().size();
if (request.isVerbose()) {
response.getInsertResult().add(getInsertResultFromResponse(futureResponse));
}
} catch (ExecutionException | CancellationException e) {
LOGGER.debug("Error ingesting Metacard", e);
throw new CswException("Unable to insert record(s).", CswConstants.TRANSACTION_FAILED, insertAction.getHandle());
}
} catch (InterruptedException e) {
LOGGER.debug("Metacard ingest interrupted", e);
Thread.currentThread().interrupt();
break;
}
}
}
LOGGER.debug("{} records inserted.", numInserted);
response.getTransactionSummary().setTotalInserted(BigInteger.valueOf(numInserted));
int numUpdated = 0;
List<UpdateAction> updateActions = request.getUpdateActions();
CompletionService<Integer> updateCompletionService = new ExecutorCompletionService<>(queryExecutor);
for (final UpdateAction updateAction : updateActions) {
Callable<Integer> callable = () -> {
try {
return updateRecords(subject, updateAction);
} catch (CswException | FederationException | IngestException | SourceUnavailableException | UnsupportedQueryException | CatalogQueryException e) {
LOGGER.debug(UNABLE_TO_UPDATE_MSG, e);
throw new CswException(UNABLE_TO_UPDATE_MSG, CswConstants.TRANSACTION_FAILED, updateAction.getHandle());
}
};
Callable<Integer> updateCallable = subject.associateWith(callable);
updateCompletionService.submit(updateCallable);
}
for (int i = 0; i < updateActions.size(); i++) {
try {
Future<Integer> completedFuture = updateCompletionService.take();
try {
numUpdated += completedFuture.get();
} catch (ExecutionException | CancellationException e) {
LOGGER.debug("Error updating Metacard", e);
throw new CswException(UNABLE_TO_UPDATE_MSG, CswConstants.TRANSACTION_FAILED, "Update");
}
} catch (InterruptedException e) {
LOGGER.debug("Metacard update interrupted", e);
Thread.currentThread().interrupt();
break;
}
}
LOGGER.debug("{} records updated.", numUpdated);
response.getTransactionSummary().setTotalUpdated(BigInteger.valueOf(numUpdated));
int numDeleted = 0;
for (DeleteAction deleteAction : request.getDeleteActions()) {
try {
numDeleted += deleteRecords(deleteAction);
} catch (Exception e) {
LOGGER.debug(UNABLE_TO_DELETE_MSG, e);
throw new CswException(UNABLE_TO_DELETE_MSG, CswConstants.TRANSACTION_FAILED, deleteAction.getHandle());
}
}
LOGGER.debug("{} records deleted.", numDeleted);
response.getTransactionSummary().setTotalDeleted(BigInteger.valueOf(numDeleted));
return response;
}
use of ddf.catalog.operation.CreateResponse in project ddf by codice.
the class SolrProviderRealTimeQueryTest method testRealTimeQueryMultipleIds.
@Test
public void testRealTimeQueryMultipleIds() throws Exception {
final String metacardTitle1 = "testRealTimeQueryMultipleIds1";
final String metacardTitle2 = "testRealTimeQueryMultipleIds2";
deleteAll(provider);
MockMetacard metacard1 = new MockMetacard(Library.getFlagstaffRecord());
metacard1.setTitle(metacardTitle1);
MockMetacard metacard2 = new MockMetacard(Library.getFlagstaffRecord());
metacard1.setTitle(metacardTitle2);
List<Metacard> metacards = new ArrayList<>(2);
metacards.add(metacard1);
metacards.add(metacard2);
CreateResponse createResponse = create(metacards, provider);
List<String> ids = createResponse.getCreatedMetacards().stream().map(m -> m.getId()).collect(Collectors.toList());
// Real time queries only work when querying by ID, so a real time query by title only will
// return
// 0 results.
queryAndVerifyCount(0, getFilterBuilder().attribute(Metacard.TITLE).is().equalTo().text(metacardTitle1), provider);
queryAndVerifyCount(0, getFilterBuilder().attribute(Metacard.TITLE).is().equalTo().text(metacardTitle2), provider);
Filter filter = getFilterBuilder().anyOf(getFilterBuilder().attribute(Metacard.ID).is().equalTo().text(ids.get(0)), getFilterBuilder().attribute(Metacard.ID).is().equalTo().text(ids.get(1)));
// When performing a real time query by ID, we get the results.
queryAndVerifyCount(2, filter, provider);
}
Aggregations