Search in sources :

Example 36 with CreateResponse

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

the class Historian method version.

/**
 * Versions deleted {@link Metacard}s.
 *
 * @param deleteResponse Versions this responses deleted metacards
 */
public DeleteResponse version(DeleteResponse deleteResponse) throws SourceUnavailableException, IngestException {
    if (doSkip(deleteResponse)) {
        return deleteResponse;
    }
    setSkipFlag(deleteResponse);
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Versioning Deleted Metacards {}", deleteResponse);
    }
    List<Metacard> originalMetacards = deleteResponse.getDeletedMetacards().stream().filter(isNotVersionNorDeleted).collect(Collectors.toList());
    if (originalMetacards.isEmpty()) {
        LOGGER.trace("No deleted metacards applicable to versioning");
        securityLogger.audit("Skipping versioning deleted metacards with ids: {}", deleteResponse.getDeletedMetacards().stream().map(Metacard::getId).collect(TO_A_STRING));
        return deleteResponse;
    }
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Versioning following deleted metacards: {}", originalMetacards.stream().map(Metacard::getId).collect(TO_A_STRING));
    }
    // [OriginalMetacardId: Original Metacard]
    Map<String, Metacard> originalMetacardsMap = originalMetacards.stream().collect(Collectors.toMap(Metacard::getId, Function.identity(), Historian::firstInWinsMerge));
    // [ContentItem.getId: content items]
    Map<String, List<ContentItem>> contentItems = getContent(getReadStorageRequests(originalMetacards));
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Got content under the following ID's: {}", contentItems.keySet().stream().collect(TO_A_STRING));
    }
    Function<String, Action> getAction = id -> contentItems.containsKey(id) ? Action.DELETED_CONTENT : Action.DELETED;
    // VERSION_OF_ID is equivalent to the Original Metacard ID
    // [MetacardVersion.VERSION_OF_ID: versioned metacard]
    Map<String, Metacard> versionedMap = getVersionMetacards(originalMetacards, getAction, (Subject) deleteResponse.getRequest().getProperties().get(SecurityConstants.SECURITY_SUBJECT));
    if (LOGGER.isDebugEnabled() && !versionedMap.keySet().equals(originalMetacardsMap.keySet())) {
        LOGGER.debug("There is not a one to one mapping between original metacards and their versions!" + " (Some metacards may not have been versioned or too many versions may have been created). " + "More information regarding the IDs is available by setting log level to trace " + "(log:set trace ddf.catalog.history)");
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Original Metacards: {}", originalMetacards.stream().map(Metacard::getId).collect(TO_A_STRING));
            LOGGER.trace("Version Metacards: {}", versionedMap.keySet().stream().collect(TO_A_STRING));
        }
    }
    CreateStorageResponse createStorageResponse = versionContentItems(contentItems, versionedMap);
    if (createStorageResponse != null) {
        setResourceUriForContent(/*Mutable*/
        versionedMap, createStorageResponse);
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Successfully stored content under ids: {}", createStorageResponse.getCreatedContentItems());
        }
    }
    executeAsSystem(() -> catalogProvider().create(new CreateRequestImpl(new ArrayList<>(versionedMap.values()))));
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Successfully created versioned metacards under ids: {}", versionedMap.values().stream().map(Metacard::getId).collect(TO_A_STRING));
    }
    String userid = subjectIdentity.getUniqueIdentifier((Subject) deleteResponse.getProperties().get(SecurityConstants.SECURITY_SUBJECT));
    List<Metacard> deletionMetacards = versionedMap.entrySet().stream().map(s -> new DeletedMetacardImpl(uuidGenerator.generateUuid(), s.getKey(), userid, s.getValue().getId(), originalMetacardsMap.get(s.getKey()))).collect(Collectors.toList());
    CreateResponse deletionMetacardsCreateResponse = executeAsSystem(() -> catalogProvider().create(new CreateRequestImpl(deletionMetacards, new HashMap<>())));
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Successfully created deletion metacards under ids: {}", deletionMetacardsCreateResponse.getCreatedMetacards().stream().map(Metacard::getId).collect(TO_A_STRING));
    }
    return deleteResponse;
}
Also used : UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) LoggerFactory(org.slf4j.LoggerFactory) ReadStorageRequest(ddf.catalog.content.operation.ReadStorageRequest) StringUtils(org.apache.commons.lang3.StringUtils) SubjectOperations(ddf.security.SubjectOperations) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) DeletedMetacardImpl(ddf.catalog.core.versioning.impl.DeletedMetacardImpl) LogSanitizer(org.codice.ddf.log.sanitizer.LogSanitizer) Map(java.util.Map) Action(ddf.catalog.core.versioning.MetacardVersion.Action) Collector(java.util.stream.Collector) UpdateStorageResponse(ddf.catalog.content.operation.UpdateStorageResponse) Bundle(org.osgi.framework.Bundle) MetacardVersionImpl(ddf.catalog.core.versioning.impl.MetacardVersionImpl) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) Predicate(java.util.function.Predicate) Collection(java.util.Collection) PrivilegedAction(java.security.PrivilegedAction) Collectors(java.util.stream.Collectors) MetacardType(ddf.catalog.data.MetacardType) BundleContext(org.osgi.framework.BundleContext) Objects(java.util.Objects) StorageException(ddf.catalog.content.StorageException) Operation(ddf.catalog.operation.Operation) List(java.util.List) CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) Optional(java.util.Optional) UpdateResponse(ddf.catalog.operation.UpdateResponse) AccessController(java.security.AccessController) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl) Security(org.codice.ddf.security.Security) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) DeleteResponse(ddf.catalog.operation.DeleteResponse) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Update(ddf.catalog.operation.Update) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) SKIP_VERSIONING(ddf.catalog.core.versioning.MetacardVersion.SKIP_VERSIONING) ContentItem(ddf.catalog.content.data.ContentItem) CreateResponse(ddf.catalog.operation.CreateResponse) Metacard(ddf.catalog.data.Metacard) SecurityConstants(ddf.security.SecurityConstants) StorageProvider(ddf.catalog.content.StorageProvider) ByteSource(com.google.common.io.ByteSource) Result(ddf.catalog.data.Result) Hashtable(java.util.Hashtable) Nullable(javax.annotation.Nullable) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SubjectIdentity(ddf.security.SubjectIdentity) Logger(org.slf4j.Logger) SecurityLogger(ddf.security.audit.SecurityLogger) IngestException(ddf.catalog.source.IngestException) Subject(ddf.security.Subject) IOException(java.io.IOException) ReadStorageRequestImpl(ddf.catalog.content.operation.impl.ReadStorageRequestImpl) TimeUnit(java.util.concurrent.TimeUnit) SourceResponse(ddf.catalog.operation.SourceResponse) CatalogProvider(ddf.catalog.source.CatalogProvider) Filter(org.opengis.filter.Filter) Collections(java.util.Collections) FrameworkUtil(org.osgi.framework.FrameworkUtil) ReadStorageResponse(ddf.catalog.content.operation.ReadStorageResponse) InputStream(java.io.InputStream) UuidGenerator(org.codice.ddf.platform.util.uuidgenerator.UuidGenerator) Action(ddf.catalog.core.versioning.MetacardVersion.Action) PrivilegedAction(java.security.PrivilegedAction) CreateResponse(ddf.catalog.operation.CreateResponse) CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) Metacard(ddf.catalog.data.Metacard) DeletedMetacardImpl(ddf.catalog.core.versioning.impl.DeletedMetacardImpl) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) List(java.util.List) ArrayList(java.util.ArrayList)

Example 37 with CreateResponse

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

the class CreateOperations method performRemoteCreate.

private CreateResponse performRemoteCreate(CreateRequest createRequest, CreateResponse createResponse) {
    if (!opsCatStoreSupport.isCatalogStoreRequest(createRequest)) {
        return createResponse;
    }
    CreateResponse remoteCreateResponse = doRemoteCreate(createRequest);
    if (createResponse == null) {
        createResponse = remoteCreateResponse;
    } else {
        createResponse.getProperties().putAll(remoteCreateResponse.getProperties());
        createResponse.getProcessingErrors().addAll(remoteCreateResponse.getProcessingErrors());
    }
    return createResponse;
}
Also used : CreateResponse(ddf.catalog.operation.CreateResponse)

Example 38 with CreateResponse

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

the class CreateOperations method doRemoteCreate.

private CreateResponse doRemoteCreate(CreateRequest createRequest) {
    HashSet<ProcessingDetails> exceptions = new HashSet<>();
    Map<String, Serializable> properties = new HashMap<>();
    List<CatalogStore> stores = opsCatStoreSupport.getCatalogStoresForRequest(createRequest, exceptions);
    for (CatalogStore store : stores) {
        try {
            if (!store.isAvailable()) {
                exceptions.add(new ProcessingDetailsImpl(store.getId(), null, "CatalogStore is not available"));
            } else {
                CreateResponse response = store.create(createRequest);
                properties.put(store.getId(), new ArrayList<>(response.getCreatedMetacards()));
            }
        } catch (IngestException e) {
            INGEST_LOGGER.error("Error creating metacards for CatalogStore {}", store.getId(), e);
            exceptions.add(new ProcessingDetailsImpl(store.getId(), e));
        }
    }
    return new CreateResponseImpl(createRequest, properties, createRequest.getMetacards(), exceptions);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) CreateResponse(ddf.catalog.operation.CreateResponse) ProcessingDetailsImpl(ddf.catalog.operation.impl.ProcessingDetailsImpl) ProcessingDetails(ddf.catalog.operation.ProcessingDetails) CatalogStore(ddf.catalog.source.CatalogStore) InternalIngestException(ddf.catalog.source.InternalIngestException) IngestException(ddf.catalog.source.IngestException) HashSet(java.util.HashSet) CreateResponseImpl(ddf.catalog.operation.impl.CreateResponseImpl)

Example 39 with CreateResponse

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

the class CreateOperations method doCreate.

// 
// Private helper methods
// 
private CreateResponse doCreate(CreateRequest createRequest) throws IngestException, SourceUnavailableException {
    CreateResponse createResponse;
    Exception ingestError = null;
    createRequest = queryOperations.setFlagsOnRequest(createRequest);
    createRequest = validateCreateRequest(createRequest);
    createRequest = validateLocalSource(createRequest);
    String fileNames = "";
    if (createRequest != null) {
        List<String> fileList = createRequest.getMetacards().stream().filter(Objects::nonNull).map(Metacard::getTitle).collect(Collectors.toList());
        fileNames = String.join(", ", fileList);
    }
    try {
        INGEST_LOGGER.info("Started ingesting metacard with titles: {}.", fileNames);
        createRequest = injectAttributes(createRequest);
        createRequest = setDefaultValues(createRequest);
        createRequest = processPreAuthorizationPlugins(createRequest);
        createRequest = updateCreateRequestPolicyMap(createRequest);
        createRequest = processPrecreateAccessPlugins(createRequest);
        createRequest.getProperties().put(Constants.OPERATION_TRANSACTION_KEY, new OperationTransactionImpl(OperationTransaction.OperationType.CREATE, Collections.emptyList()));
        createRequest = processPreIngestPlugins(createRequest);
        createRequest = validateCreateRequest(createRequest);
        createResponse = getCreateResponse(createRequest);
        createResponse = performRemoteCreate(createRequest, createResponse);
    } catch (IngestException iee) {
        ingestError = iee;
        throw iee;
    } catch (StopProcessingException see) {
        ingestError = see;
        throw new IngestException(PRE_INGEST_ERROR, see);
    } catch (RuntimeException re) {
        ingestError = re;
        throw new InternalIngestException("Exception during runtime while performing create", re);
    } finally {
        if (createRequest != null && ingestError != null && INGEST_LOGGER.isInfoEnabled()) {
            INGEST_LOGGER.info("Error on create operation. {} metacards failed to ingest. {}", createRequest.getMetacards().size(), buildIngestLog(createRequest), ingestError);
        }
    }
    try {
        createResponse = validateFixCreateResponse(createResponse, createRequest);
    } catch (RuntimeException re) {
        LOGGER.info("Exception during runtime while performing doing post create operations (plugins and pubsub)", re);
    }
    if (createResponse == null) {
        // suppress all npe findings for this method.
        throw new IngestException("CatalogProvider returned null CreateResponse Object.");
    }
    return createResponse;
}
Also used : OperationTransactionImpl(ddf.catalog.operation.impl.OperationTransactionImpl) InternalIngestException(ddf.catalog.source.InternalIngestException) CreateResponse(ddf.catalog.operation.CreateResponse) Objects(java.util.Objects) InternalIngestException(ddf.catalog.source.InternalIngestException) IngestException(ddf.catalog.source.IngestException) StopProcessingException(ddf.catalog.plugin.StopProcessingException) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) StorageException(ddf.catalog.content.StorageException) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) InternalIngestException(ddf.catalog.source.InternalIngestException) IngestException(ddf.catalog.source.IngestException) StopProcessingException(ddf.catalog.plugin.StopProcessingException) IOException(java.io.IOException)

Example 40 with CreateResponse

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

the class CreateOperations method create.

public CreateResponse create(CreateStorageRequest streamCreateRequest, List<String> fanoutTagBlacklist) throws IngestException, SourceUnavailableException {
    Map<String, Metacard> metacardMap = new HashMap<>();
    List<ContentItem> contentItems = new ArrayList<>(streamCreateRequest.getContentItems().size());
    HashMap<String, Map<String, Path>> tmpContentPaths = new HashMap<>();
    CreateResponse createResponse;
    CreateStorageRequest createStorageRequest = null;
    CreateStorageResponse createStorageResponse;
    CreateRequest createRequest = null;
    Exception ingestError = null;
    String fileNames = "";
    streamCreateRequest = opsStorageSupport.prepareStorageRequest(streamCreateRequest, streamCreateRequest::getContentItems);
    if (!streamCreateRequest.getContentItems().isEmpty()) {
        List<String> fileList = streamCreateRequest.getContentItems().stream().map(ContentItem::getFilename).collect(Collectors.toList());
        fileNames = String.join(", ", fileList);
    }
    INGEST_LOGGER.info("Started ingesting resources with titles: {}.", fileNames);
    // Operation populates the metacardMap, contentItems, and tmpContentPaths
    opsMetacardSupport.generateMetacardAndContentItems(streamCreateRequest.getContentItems(), metacardMap, contentItems, tmpContentPaths);
    if (blockCreateMetacards(metacardMap.values(), fanoutTagBlacklist)) {
        String message = "Fanout proxy does not support create operations with blacklisted metacard tag";
        LOGGER.debug("{}. Tags blacklist: {}", message, fanoutTagBlacklist);
        throw new IngestException(message);
    }
    streamCreateRequest.getProperties().put(CONTENT_PATHS, tmpContentPaths);
    injectAttributes(metacardMap);
    setDefaultValues(metacardMap);
    streamCreateRequest = applyAttributeOverrides(streamCreateRequest, metacardMap);
    try {
        if (!contentItems.isEmpty()) {
            createStorageRequest = new CreateStorageRequestImpl(contentItems, streamCreateRequest.getId(), streamCreateRequest.getProperties());
            createStorageRequest = processPreCreateStoragePlugins(createStorageRequest);
            try {
                createStorageResponse = sourceOperations.getStorage().create(createStorageRequest);
                createStorageResponse.getProperties().put(CONTENT_PATHS, tmpContentPaths);
            } catch (StorageException e) {
                INGEST_LOGGER.debug("Could not store content items: {}.", fileNames, e);
                throw new IngestException("Could not store content items.", e);
            }
            createStorageResponse = processPostCreateStoragePlugins(createStorageResponse);
            populateMetacardMap(metacardMap, createStorageResponse);
        }
        createRequest = new CreateRequestImpl(new ArrayList<>(metacardMap.values()), Optional.ofNullable(createStorageRequest).map(StorageRequest::getProperties).orElseGet(HashMap::new));
        createResponse = doCreate(createRequest);
    } catch (IngestException e) {
        ingestError = e;
        rollbackStorage(createStorageRequest);
        throw e;
    } catch (IOException | RuntimeException e) {
        LOGGER.debug("Unhandled runtime exception or IOException during create", e);
        ingestError = e;
        rollbackStorage(createStorageRequest);
        throw new IngestException("Unable to store products for request: " + fileNames, e);
    } finally {
        opsStorageSupport.commitAndCleanup(createStorageRequest, tmpContentPaths);
        if (INGEST_LOGGER.isInfoEnabled()) {
            if (createRequest != null && ingestError != null) {
                INGEST_LOGGER.info("Error during ingesting resource: {}", fileNames, ingestError);
            }
            INGEST_LOGGER.info("Completed ingesting resource: {}.", fileNames);
        }
    }
    createResponse = doPostIngest(createResponse);
    return createResponse;
}
Also used : HashMap(java.util.HashMap) CreateResponse(ddf.catalog.operation.CreateResponse) CreateRequest(ddf.catalog.operation.CreateRequest) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) StorageException(ddf.catalog.content.StorageException) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) InternalIngestException(ddf.catalog.source.InternalIngestException) IngestException(ddf.catalog.source.IngestException) StopProcessingException(ddf.catalog.plugin.StopProcessingException) IOException(java.io.IOException) CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) Metacard(ddf.catalog.data.Metacard) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) InternalIngestException(ddf.catalog.source.InternalIngestException) IngestException(ddf.catalog.source.IngestException) Map(java.util.Map) HashMap(java.util.HashMap) StorageException(ddf.catalog.content.StorageException) ContentItem(ddf.catalog.content.data.ContentItem) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest)

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