use of ddf.catalog.operation.impl.CreateRequestImpl in project ddf by codice.
the class CatalogFrameworkQueryTest method testBeginsQuery.
@Test
public void testBeginsQuery() {
Calendar beginsStart = Calendar.getInstance();
Calendar card1Exp = Calendar.getInstance();
if (beginsStart.equals(card1Exp)) {
card1Exp.add(Calendar.MILLISECOND, 1);
}
Calendar card2Exp = Calendar.getInstance();
card2Exp.add(Calendar.YEAR, 3);
Calendar beginsEnd = Calendar.getInstance();
beginsEnd.add(Calendar.YEAR, 4);
List<Metacard> metacards = new ArrayList<Metacard>();
MetacardImpl newCard1 = new MetacardImpl();
newCard1.setId(null);
newCard1.setExpirationDate(card1Exp.getTime());
metacards.add(newCard1);
MetacardImpl newCard2 = new MetacardImpl();
newCard2.setId(null);
newCard2.setExpirationDate(card2Exp.getTime());
metacards.add(newCard2);
String mcId1 = null;
String mcId2 = null;
CreateResponse createResponse = null;
try {
createResponse = framework.create(new CreateRequestImpl(metacards, null));
} catch (IngestException e1) {
LOGGER.error("Failure", e1);
fail();
} catch (SourceUnavailableException e1) {
LOGGER.error("Failure", e1);
fail();
}
assertEquals(createResponse.getCreatedMetacards().size(), metacards.size());
for (Metacard curCard : createResponse.getCreatedMetacards()) {
if (curCard.getExpirationDate().equals(card1Exp.getTime())) {
mcId1 = curCard.getId();
} else {
mcId2 = curCard.getId();
}
assertNotNull(curCard.getId());
}
FilterFactory filterFactory = new FilterFactoryImpl();
Period beginsPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(beginsStart.getTime())), new DefaultInstant(new DefaultPosition(beginsEnd.getTime())));
QueryImpl query = new QueryImpl(filterFactory.begins(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beginsPeriod)));
QueryRequest queryReq = new QueryRequestImpl(query, false);
try {
QueryResponse response = framework.query(queryReq);
assertEquals("Expecting return 0 results.", 0, response.getHits());
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
LOGGER.error("Failure", e);
fail();
}
beginsPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(card1Exp.getTime())), new DefaultInstant(new DefaultPosition(beginsEnd.getTime())));
query = new QueryImpl(filterFactory.begins(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beginsPeriod)));
queryReq = new QueryRequestImpl(query, false);
try {
QueryResponse response = framework.query(queryReq);
assertEquals("Begins filter should return 1 result", 1, response.getHits());
assertEquals("Begins filter should return metacard[" + mcId1 + "]", mcId1, response.getResults().get(0).getMetacard().getId());
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
LOGGER.error("Failure", e);
fail();
}
beginsPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(card2Exp.getTime())), new DefaultInstant(new DefaultPosition(beginsEnd.getTime())));
query = new QueryImpl(filterFactory.begins(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beginsPeriod)));
queryReq = new QueryRequestImpl(query, false);
try {
QueryResponse response = framework.query(queryReq);
assertEquals("Begins filter should return 1 result", 1, response.getHits());
assertEquals("Begins filter should return metacard[" + mcId2 + "]", mcId2, response.getResults().get(0).getMetacard().getId());
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
LOGGER.error("Failure", e);
fail();
}
}
use of ddf.catalog.operation.impl.CreateRequestImpl 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.impl.CreateRequestImpl 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.impl.CreateRequestImpl in project ddf by codice.
the class SecurityPluginTest method testNominalCaseCreateWithEmailAndResourceTag.
@Test
public void testNominalCaseCreateWithEmailAndResourceTag() throws Exception {
Subject mockSubject = setupMockSubject();
ThreadContext.bind(mockSubject);
MetacardImpl metacardWithTags = new MetacardImpl();
Set<String> setOfTags = new HashSet<String>();
setOfTags.add("resource");
metacardWithTags.setTags(setOfTags);
CreateRequest request = new CreateRequestImpl(metacardWithTags);
SecurityPlugin plugin = new SecurityPlugin(subjectIdentity);
request = plugin.processPreCreate(request);
assertThat(request.getPropertyValue(SecurityConstants.SECURITY_SUBJECT), equalTo(mockSubject));
assertThat(request.getMetacards().size(), is(1));
assertThat(request.getMetacards().get(0).getAttribute(Metacard.POINT_OF_CONTACT).getValue(), equalTo(TEST_USER));
}
use of ddf.catalog.operation.impl.CreateRequestImpl in project ddf by codice.
the class SecurityPluginTest method testMetacardPointOfContactNotOverridden.
@Test
public void testMetacardPointOfContactNotOverridden() {
Subject mockSubject = setupMockSubject();
ThreadContext.bind(mockSubject);
MetacardImpl metacardWithPoc = new MetacardImpl();
metacardWithPoc.setTags(Collections.emptySet());
metacardWithPoc.setAttribute(new AttributeImpl(Metacard.POINT_OF_CONTACT, "originalPoc"));
MetacardImpl metacardWithNoPoc = new MetacardImpl();
metacardWithNoPoc.setTags(Collections.emptySet());
CreateRequest request = new CreateRequestImpl(Arrays.asList(metacardWithPoc, metacardWithNoPoc));
SecurityPlugin plugin = new SecurityPlugin(subjectIdentity);
request = plugin.processPreCreate(request);
assertThat(request.getPropertyValue(SecurityConstants.SECURITY_SUBJECT), equalTo(mockSubject));
assertThat(request.getMetacards().size(), is(2));
assertThat(request.getMetacards().get(0).getAttribute(Metacard.POINT_OF_CONTACT).getValue(), equalTo("originalPoc"));
assertThat(request.getMetacards().get(1).getAttribute(Metacard.POINT_OF_CONTACT).getValue(), equalTo(TEST_USER));
}
Aggregations