Search in sources :

Example 21 with UpdateRequest

use of ddf.catalog.operation.UpdateRequest in project ddf by codice.

the class FederationAdminServiceImpl method updateRegistryEntry.

@Override
public void updateRegistryEntry(Metacard updateMetacard, Set<String> destinations) throws FederationAdminException {
    validateRegistryMetacards(Collections.singletonList(updateMetacard));
    Map<String, Serializable> properties = new HashMap<>();
    String mcardId = updateMetacard.getId();
    if (isRemoteMetacard(updateMetacard) || CollectionUtils.isNotEmpty(destinations)) {
        Filter idFilter = filterBuilder.attribute(RegistryObjectMetacardType.REMOTE_METACARD_ID).is().equalTo().text(updateMetacard.getId());
        Filter tagFilter = filterBuilder.attribute(Metacard.TAGS).is().like().text(RegistryConstants.REGISTRY_TAG_INTERNAL);
        List<Metacard> results = this.getRegistryMetacardsByFilter(filterBuilder.allOf(tagFilter, idFilter), destinations);
        if (results.size() != 1) {
            throw new FederationAdminException("Could not find metacard to update.");
        }
        mcardId = results.get(0).getId();
        LOGGER.debug("Looked up remote-mcard-id {} and got id {}", updateMetacard.getId(), mcardId);
    }
    List<Map.Entry<Serializable, Metacard>> updateList = new ArrayList<>();
    updateList.add(new AbstractMap.SimpleEntry<>(mcardId, updateMetacard));
    UpdateRequest updateRequest = new UpdateRequestImpl(updateList, Metacard.ID, properties, destinations);
    try {
        UpdateResponse updateResponse = security.runWithSubjectOrElevate(() -> catalogFramework.update(updateRequest));
        if (!updateResponse.getProcessingErrors().isEmpty()) {
            throw new FederationAdminException("Processing error occurred while updating registry entry. Details:" + System.lineSeparator() + stringifyProcessingErrors(updateResponse.getProcessingErrors()));
        }
    } catch (SecurityServiceException | InvocationTargetException e) {
        String message = "Error updating registry entry.";
        LOGGER.debug("{} Metacard ID: {}", message, updateMetacard.getId());
        throw new FederationAdminException(message, e);
    }
}
Also used : FederationAdminException(org.codice.ddf.registry.federationadmin.service.internal.FederationAdminException) Serializable(java.io.Serializable) SecurityServiceException(ddf.security.service.SecurityServiceException) HashMap(java.util.HashMap) UpdateRequest(ddf.catalog.operation.UpdateRequest) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) AbstractMap(java.util.AbstractMap) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) Filter(org.opengis.filter.Filter) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl)

Example 22 with UpdateRequest

use of ddf.catalog.operation.UpdateRequest in project ddf by codice.

the class InMemoryProcessingFramework method storeMetacardUpdates.

private void storeMetacardUpdates(Map<String, Metacard> metacardsToUpdate, Map<String, Serializable> properties) {
    if (MapUtils.isNotEmpty(metacardsToUpdate)) {
        LOGGER.trace("Storing metacard updates");
        List<Map.Entry<Serializable, Metacard>> updateList = metacardsToUpdate.values().stream().map(metacard -> new AbstractMap.SimpleEntry<Serializable, Metacard>(metacard.getId(), metacard)).collect(Collectors.toList());
        UpdateRequest updateMetacardsRequest = new UpdateRequestImpl(updateList, UpdateRequest.UPDATE_BY_ID, properties);
        Subject subject = (Subject) updateMetacardsRequest.getProperties().get(SecurityConstants.SECURITY_SUBJECT);
        if (subject == null) {
            LOGGER.debug("No subject to send UpdateRequest. Updates will not be sent back to the catalog.");
        } else {
            subject.execute(() -> {
                try {
                    catalogFramework.update(updateMetacardsRequest);
                    LOGGER.debug("Successfully completed update metacards request");
                } catch (IngestException | SourceUnavailableException | RuntimeException e) {
                    LOGGER.info("Unable to complete update request", e);
                }
                return null;
            });
        }
    } else {
        LOGGER.debug("No metacards to update");
    }
}
Also used : ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) CatalogFramework(ddf.catalog.CatalogFramework) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) LoggerFactory(org.slf4j.LoggerFactory) UpdateStorageRequestImpl(ddf.catalog.content.operation.impl.UpdateStorageRequestImpl) HashMap(java.util.HashMap) ProcessDeleteItem(org.codice.ddf.catalog.async.data.api.internal.ProcessDeleteItem) PostProcessPlugin(org.codice.ddf.catalog.async.plugin.api.internal.PostProcessPlugin) ArrayList(java.util.ArrayList) ProcessingFramework(org.codice.ddf.catalog.async.processingframework.api.internal.ProcessingFramework) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) ContentItem(ddf.catalog.content.data.ContentItem) Metacard(ddf.catalog.data.Metacard) Map(java.util.Map) SecurityConstants(ddf.security.SecurityConstants) UpdateRequest(ddf.catalog.operation.UpdateRequest) ProcessResourceItem(org.codice.ddf.catalog.async.data.api.internal.ProcessResourceItem) ByteSource(com.google.common.io.ByteSource) ExecutorService(java.util.concurrent.ExecutorService) ProcessResource(org.codice.ddf.catalog.async.data.api.internal.ProcessResource) Logger(org.slf4j.Logger) MapUtils(org.apache.commons.collections.MapUtils) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException) Subject(ddf.security.Subject) ProcessCreateItem(org.codice.ddf.catalog.async.data.api.internal.ProcessCreateItem) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) IOUtils(org.apache.commons.io.IOUtils) AbstractMap(java.util.AbstractMap) List(java.util.List) Validate.notNull(org.apache.commons.lang3.Validate.notNull) ProcessRequest(org.codice.ddf.catalog.async.data.api.internal.ProcessRequest) ProcessUpdateItem(org.codice.ddf.catalog.async.data.api.internal.ProcessUpdateItem) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) UpdateRequest(ddf.catalog.operation.UpdateRequest) IngestException(ddf.catalog.source.IngestException) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Subject(ddf.security.Subject)

Example 23 with UpdateRequest

use of ddf.catalog.operation.UpdateRequest in project ddf by codice.

the class CatalogComponentFrameworkTest method testUpdateWithIngestException.

@Test
public /**
     * Operation: UPDATE
     * Body contains:  Metacard
     */
void testUpdateWithIngestException() 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);
    // setup mock catalog framework
    final Update update = new UpdateImpl(metacard1, metacard2);
    List<Update> updates = new ArrayList<Update>();
    updates.add(update);
    final String[] metacardIds = new String[metacards.size()];
    for (int i = 0; i < metacards.size(); i++) {
        metacardIds[i] = metacards.get(i).getId();
    }
    UpdateRequest updateRequest = new UpdateRequestImpl(metacardIds, metacards);
    UpdateResponse updateResponse = new UpdateResponseImpl(updateRequest, new HashMap(), updates);
    when(catalogFramework.update(any(UpdateRequest.class))).thenThrow(new IngestException());
    // Exercise the route with a UPDATE operation
    template.sendBodyAndHeader("direct:sampleInput", metacards, "Operation", "UPDATE");
    // 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> cardsUpdated = (List<Update>) exchange.getIn().getBody();
    assertListSize(cardsUpdated, 0);
    mockVerifierEndpoint.assertIsSatisfied();
}
Also used : UpdateImpl(ddf.catalog.operation.impl.UpdateImpl) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) UpdateRequest(ddf.catalog.operation.UpdateRequest) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Exchange(org.apache.camel.Exchange) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) UpdateResponseImpl(ddf.catalog.operation.impl.UpdateResponseImpl) IngestException(ddf.catalog.source.IngestException) ArrayList(java.util.ArrayList) List(java.util.List) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Test(org.junit.Test)

Example 24 with UpdateRequest

use of ddf.catalog.operation.UpdateRequest in project ddf by codice.

the class CatalogComponentFrameworkTest method testUpdateWithListOfMetacards.

@Test
public /**
     * Operation: UPDATE
     * Body contains:  List<Metacard>
     */
void testUpdateWithListOfMetacards() 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);
    // setup mock catalog framework
    final Update update = new UpdateImpl(metacard1, metacard2);
    List<Update> updates = new ArrayList<Update>();
    updates.add(update);
    final String[] metacardIds = new String[metacards.size()];
    for (int i = 0; i < metacards.size(); i++) {
        metacardIds[i] = metacards.get(i).getId();
    }
    UpdateRequest updateRequest = new UpdateRequestImpl(metacardIds, metacards);
    UpdateResponse updateResponse = new UpdateResponseImpl(updateRequest, new HashMap(), updates);
    when(catalogFramework.update(any(UpdateRequest.class))).thenReturn(updateResponse);
    // Exercise the route with a UPDATE operation
    template.sendBodyAndHeader("direct:sampleInput", metacards, "Operation", "UPDATE");
    // 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> cardsUpdated = (List<Update>) exchange.getIn().getBody();
    assertListSize(cardsUpdated, 1);
    mockVerifierEndpoint.assertIsSatisfied();
}
Also used : UpdateImpl(ddf.catalog.operation.impl.UpdateImpl) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) UpdateRequest(ddf.catalog.operation.UpdateRequest) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Exchange(org.apache.camel.Exchange) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) UpdateResponseImpl(ddf.catalog.operation.impl.UpdateResponseImpl) ArrayList(java.util.ArrayList) List(java.util.List) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Test(org.junit.Test)

Example 25 with UpdateRequest

use of ddf.catalog.operation.UpdateRequest in project ddf by codice.

the class FanoutCatalogFrameworkTest method testBlacklistedTagUpdateRequestFails.

@Test(expected = IngestException.class)
public void testBlacklistedTagUpdateRequestFails() throws Exception {
    Metacard metacard = new MetacardImpl();
    metacard.setAttribute(new AttributeImpl(Metacard.ID, "metacardId"));
    metacard.setAttribute(new AttributeImpl(Metacard.TAGS, "blacklisted"));
    UpdateRequest request = new UpdateRequestImpl(metacard.getId(), metacard);
    framework.setFanoutTagBlacklist(Collections.singletonList("blacklisted"));
    framework.update(request);
}
Also used : Metacard(ddf.catalog.data.Metacard) UpdateRequest(ddf.catalog.operation.UpdateRequest) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Test(org.junit.Test)

Aggregations

UpdateRequest (ddf.catalog.operation.UpdateRequest)61 Test (org.junit.Test)44 Metacard (ddf.catalog.data.Metacard)41 ArrayList (java.util.ArrayList)32 Serializable (java.io.Serializable)25 UpdateResponse (ddf.catalog.operation.UpdateResponse)23 UpdateRequestImpl (ddf.catalog.operation.impl.UpdateRequestImpl)23 HashMap (java.util.HashMap)21 Update (ddf.catalog.operation.Update)17 List (java.util.List)16 Entry (java.util.Map.Entry)16 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)15 Map (java.util.Map)12 SimpleEntry (java.util.AbstractMap.SimpleEntry)11 IngestException (ddf.catalog.source.IngestException)10 URI (java.net.URI)10 Result (ddf.catalog.data.Result)9 CreateRequest (ddf.catalog.operation.CreateRequest)9 CatalogFramework (ddf.catalog.CatalogFramework)8 CreateResponse (ddf.catalog.operation.CreateResponse)8