Search in sources :

Example 46 with CreateResponse

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);
    }
}
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 47 with CreateResponse

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));
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) DeleteResponse(ddf.catalog.operation.DeleteResponse) CreateResponse(ddf.catalog.operation.CreateResponse) ArrayList(java.util.ArrayList)

Example 48 with CreateResponse

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));
}
Also used : UuidGenerator(org.codice.ddf.platform.util.uuidgenerator.UuidGenerator) QueryRequest(ddf.catalog.operation.QueryRequest) GeoEntryAttributes(org.codice.ddf.spatial.geocoding.GeoEntryAttributes) CreateResponse(ddf.catalog.operation.CreateResponse) CreateRequest(ddf.catalog.operation.CreateRequest) ResultImpl(ddf.catalog.data.impl.ResultImpl) GeoEntryCreator(org.codice.ddf.spatial.geocoding.GeoEntryCreator) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) DeleteResponse(ddf.catalog.operation.DeleteResponse) CatalogProvider(ddf.catalog.source.CatalogProvider) QueryResponse(ddf.catalog.operation.QueryResponse) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) GeoNamesFileExtractor(org.codice.ddf.spatial.geocoding.extract.GeoNamesFileExtractor) CatalogFramework(ddf.catalog.CatalogFramework) DeleteRequest(ddf.catalog.operation.DeleteRequest) Before(org.junit.Before)

Example 49 with CreateResponse

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;
}
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 50 with CreateResponse

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);
}
Also used : QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) SolrProviderTestUtil.deleteAll(ddf.catalog.source.solr.provider.SolrProviderTestUtil.deleteAll) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) BeforeClass(org.junit.BeforeClass) SolrClient(org.codice.solr.client.solrj.SolrClient) MiniSolrCloudCluster(org.apache.solr.cloud.MiniSolrCloudCluster) LoggerFactory(org.slf4j.LoggerFactory) SolrProviderTestUtil.queryAndVerifyCount(ddf.catalog.source.solr.provider.SolrProviderTestUtil.queryAndVerifyCount) ArrayList(java.util.ArrayList) BASIC_METACARD(ddf.catalog.data.impl.MetacardImpl.BASIC_METACARD) GeotoolsFilterAdapterImpl(ddf.catalog.filter.proxy.adapter.GeotoolsFilterAdapterImpl) Lists(com.google.common.collect.Lists) CreateResponse(ddf.catalog.operation.CreateResponse) Metacard(ddf.catalog.data.Metacard) QueryRequest(ddf.catalog.operation.QueryRequest) SolrProviderTestUtil.create(ddf.catalog.source.solr.provider.SolrProviderTestUtil.create) ClassRule(org.junit.ClassRule) JettyConfig(org.apache.solr.client.solrj.embedded.JettyConfig) Awaitility.await(org.awaitility.Awaitility.await) QueryImpl(ddf.catalog.operation.impl.QueryImpl) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Collectors(java.util.stream.Collectors) MetacardType(ddf.catalog.data.MetacardType) TimeUnit(java.util.concurrent.TimeUnit) MockMetacard(ddf.catalog.source.solr.provider.MockMetacard) List(java.util.List) Rule(org.junit.Rule) TestCase.assertTrue(junit.framework.TestCase.assertTrue) Library(ddf.catalog.source.solr.provider.Library) Paths(java.nio.file.Paths) SolrCloudClientFactory(org.codice.solr.factory.impl.SolrCloudClientFactory) Optional(java.util.Optional) Filter(org.opengis.filter.Filter) SolrProviderTestUtil.getFilterBuilder(ddf.catalog.source.solr.provider.SolrProviderTestUtil.getFilterBuilder) Assert(org.junit.Assert) TemporaryFolder(org.junit.rules.TemporaryFolder) Metacard(ddf.catalog.data.Metacard) MockMetacard(ddf.catalog.source.solr.provider.MockMetacard) Filter(org.opengis.filter.Filter) CreateResponse(ddf.catalog.operation.CreateResponse) ArrayList(java.util.ArrayList) MockMetacard(ddf.catalog.source.solr.provider.MockMetacard) Test(org.junit.Test)

Aggregations

CreateResponse (ddf.catalog.operation.CreateResponse)111 Test (org.junit.Test)82 Metacard (ddf.catalog.data.Metacard)76 CreateRequestImpl (ddf.catalog.operation.impl.CreateRequestImpl)44 ArrayList (java.util.ArrayList)42 CreateRequest (ddf.catalog.operation.CreateRequest)36 QueryImpl (ddf.catalog.operation.impl.QueryImpl)29 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)29 CreateResponseImpl (ddf.catalog.operation.impl.CreateResponseImpl)27 HashMap (java.util.HashMap)25 Serializable (java.io.Serializable)23 List (java.util.List)23 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)22 DeleteResponse (ddf.catalog.operation.DeleteResponse)22 UpdateResponse (ddf.catalog.operation.UpdateResponse)21 Filter (org.opengis.filter.Filter)21 IngestException (ddf.catalog.source.IngestException)20 SourceResponse (ddf.catalog.operation.SourceResponse)18 QueryRequest (ddf.catalog.operation.QueryRequest)17 MetacardType (ddf.catalog.data.MetacardType)16