Search in sources :

Example 41 with CreateRequestImpl

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();
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryRequest(ddf.catalog.operation.QueryRequest) CreateResponse(ddf.catalog.operation.CreateResponse) Calendar(java.util.Calendar) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) Period(org.opengis.temporal.Period) DefaultPeriod(org.geotools.temporal.object.DefaultPeriod) DefaultInstant(org.geotools.temporal.object.DefaultInstant) FederationException(ddf.catalog.federation.FederationException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) FilterFactory(org.opengis.filter.FilterFactory) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) DefaultPeriod(org.geotools.temporal.object.DefaultPeriod) DefaultPosition(org.geotools.temporal.object.DefaultPosition) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) IngestException(ddf.catalog.source.IngestException) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) Test(org.junit.Test)

Example 42 with CreateRequestImpl

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);
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) InternalIngestException(ddf.catalog.source.InternalIngestException) CreateResponse(ddf.catalog.operation.CreateResponse) CreateRequest(ddf.catalog.operation.CreateRequest) MimeType(javax.activation.MimeType) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) IngestException(ddf.catalog.source.IngestException) InternalIngestException(ddf.catalog.source.InternalIngestException) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest)

Example 43 with CreateRequestImpl

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;
}
Also used : CreateResponse(ddf.catalog.operation.CreateResponse) CreateRequest(ddf.catalog.operation.CreateRequest) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) TransactionSummaryType(net.opengis.cat.csw.v_2_0_2.TransactionSummaryType) TransactionResponseType(net.opengis.cat.csw.v_2_0_2.TransactionResponseType) ExecutionException(java.util.concurrent.ExecutionException) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction) Subject(org.apache.shiro.subject.Subject) CancellationException(java.util.concurrent.CancellationException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) MimeTypeParseException(javax.activation.MimeTypeParseException) ParseException(org.locationtech.jts.io.ParseException) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException) FederationException(ddf.catalog.federation.FederationException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CatalogQueryException(ddf.catalog.util.impl.CatalogQueryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BigInteger(java.math.BigInteger) Metacard(ddf.catalog.data.Metacard) CancellationException(java.util.concurrent.CancellationException) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.DeleteAction) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 44 with CreateRequestImpl

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));
}
Also used : CreateRequest(ddf.catalog.operation.CreateRequest) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) Subject(ddf.security.Subject) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 45 with CreateRequestImpl

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));
}
Also used : CreateRequest(ddf.catalog.operation.CreateRequest) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) Subject(ddf.security.Subject) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Test(org.junit.Test)

Aggregations

CreateRequestImpl (ddf.catalog.operation.impl.CreateRequestImpl)80 Test (org.junit.Test)60 Metacard (ddf.catalog.data.Metacard)53 CreateResponse (ddf.catalog.operation.CreateResponse)44 CreateRequest (ddf.catalog.operation.CreateRequest)42 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)29 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)26 Serializable (java.io.Serializable)22 CreateResponseImpl (ddf.catalog.operation.impl.CreateResponseImpl)19 IngestException (ddf.catalog.source.IngestException)19 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)17 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)17 InputStream (java.io.InputStream)17 HashSet (java.util.HashSet)16 QueryImpl (ddf.catalog.operation.impl.QueryImpl)15 Subject (ddf.security.Subject)15 QueryRequest (ddf.catalog.operation.QueryRequest)13 QueryResponse (ddf.catalog.operation.QueryResponse)13 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)12