Search in sources :

Example 1 with IngestException

use of ddf.catalog.source.IngestException in project ddf by codice.

the class FtpRequestHandlerTest method testCreateStorageRequestFail.

@SuppressWarnings("unchecked")
@Test(expected = FtpException.class)
public void testCreateStorageRequestFail() throws Exception {
    Subject subject = mock(Subject.class);
    FtpFile ftpFile = mock(FtpFile.class);
    when(session.getAttribute(SUBJECT)).thenReturn(subject);
    when(request.getArgument()).thenReturn(FILE_NAME);
    when(session.getFileSystemView().getFile(FILE_NAME)).thenReturn(ftpFile);
    when(ftpFile.isWritable()).thenReturn(true);
    when(ftpFile.getAbsolutePath()).thenReturn(FILE_NAME);
    when(subject.execute(any(Callable.class))).thenAnswer(invocationOnMock -> ((Callable) invocationOnMock.getArguments()[0]).call());
    when(catalogFramework.create((CreateStorageRequest) anyObject())).thenThrow(new IngestException());
    ftplet.onUploadStart(session, request);
}
Also used : IngestException(ddf.catalog.source.IngestException) FtpFile(org.apache.ftpserver.ftplet.FtpFile) Subject(ddf.security.Subject) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 2 with IngestException

use of ddf.catalog.source.IngestException in project ddf by codice.

the class SolrCatalogProvider method create.

@Override
public CreateResponse create(CreateRequest request) throws IngestException {
    if (request == null) {
        throw new IngestException(REQUEST_MUST_NOT_BE_NULL_MESSAGE);
    }
    List<Metacard> metacards = request.getMetacards();
    List<Metacard> output = new ArrayList<>();
    if (metacards == null) {
        return new CreateResponseImpl(request, null, output);
    }
    for (Metacard metacard : metacards) {
        boolean isSourceIdSet = (metacard.getSourceId() != null && !"".equals(metacard.getSourceId()));
        /*
             * If an ID is not provided, then one is generated so that documents are unique. Solr
             * will not accept documents unless the id is unique.
             */
        if (metacard.getId() == null || metacard.getId().equals("")) {
            if (isSourceIdSet) {
                throw new IngestException("Metacard from a separate distribution must have ID");
            }
            metacard.setAttribute(new AttributeImpl(Metacard.ID, generatePrimaryKey()));
        }
        if (!isSourceIdSet) {
            metacard.setSourceId(getId());
        }
        output.add(metacard);
    }
    try {
        client.add(output, isForcedAutoCommit());
    } catch (SolrServerException | SolrException | IOException | MetacardCreationException e) {
        LOGGER.info("Solr could not ingest metacard(s) during create.", e);
        throw new IngestException("Could not ingest metacard(s).");
    }
    pendingNrtIndex.putAll(output.stream().collect(Collectors.toMap(Metacard::getId, m -> copyMetacard(m))));
    return new CreateResponseImpl(request, request.getProperties(), output);
}
Also used : Metacard(ddf.catalog.data.Metacard) MetacardCreationException(ddf.catalog.data.MetacardCreationException) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException) CreateResponseImpl(ddf.catalog.operation.impl.CreateResponseImpl)

Example 3 with IngestException

use of ddf.catalog.source.IngestException in project ddf by codice.

the class SolrCatalogProvider method update.

@Override
public UpdateResponse update(UpdateRequest updateRequest) throws IngestException {
    if (updateRequest == null) {
        throw new IngestException(REQUEST_MUST_NOT_BE_NULL_MESSAGE);
    }
    List<Entry<Serializable, Metacard>> updates = updateRequest.getUpdates();
    // the list of updates, both new and old metacards
    ArrayList<Update> updateList = new ArrayList<>();
    String attributeName = updateRequest.getAttributeName();
    // need an attribute name in order to do query
    if (attributeName == null) {
        throw new IngestException("Attribute name cannot be null. " + "Please provide the name of the attribute.");
    }
    List<String> identifiers = new ArrayList<>();
    // if we have nothing to update, send the empty list
    if (updates == null || updates.size() == 0) {
        return new UpdateResponseImpl(updateRequest, null, new ArrayList<>());
    }
    // Loop to get all identifiers
    for (Entry<Serializable, Metacard> updateEntry : updates) {
        identifiers.add(updateEntry.getKey().toString());
    }
    /* 1a. Create the old Metacard Query */
    String attributeQuery = getQuery(attributeName, identifiers);
    SolrQuery query = new SolrQuery(attributeQuery);
    // Set number of rows to the result size + 1.  The default row size in Solr is 10, so this
    // needs to be set in situations where the number of metacards to update is > 10.  Since there
    // could be more results in the query response than the number of metacards in the update request,
    // 1 is added to the row size, so we can still determine whether we found more metacards than
    // updated metacards provided
    query.setRows(updates.size() + 1);
    QueryResponse idResults = null;
    /* 1b. Execute Query */
    try {
        idResults = solr.query(query, METHOD.POST);
    } catch (SolrServerException | IOException e) {
        LOGGER.info("Failed to query for metacard(s) before update.", e);
    }
    // map of old metacards to be populated
    Map<Serializable, Metacard> idToMetacardMap = new HashMap<>();
    /* 1c. Populate list of old metacards */
    if (idResults != null && idResults.getResults() != null && idResults.getResults().size() != 0) {
        LOGGER.debug("Found {} current metacard(s).", idResults.getResults().size());
        // CHECK updates size assertion
        if (idResults.getResults().size() > updates.size()) {
            throw new IngestException("Found more metacards than updated metacards provided. Please ensure your attribute values match unique records.");
        }
        for (SolrDocument doc : idResults.getResults()) {
            Metacard old;
            try {
                old = client.createMetacard(doc);
            } catch (MetacardCreationException e) {
                LOGGER.info("Unable to create metacard(s) from Solr responses during update.", e);
                throw new IngestException("Could not create metacard(s).");
            }
            if (!idToMetacardMap.containsKey(old.getAttribute(attributeName).getValue())) {
                idToMetacardMap.put(old.getAttribute(attributeName).getValue(), old);
            } else {
                throw new IngestException("The attribute value given [" + old.getAttribute(attributeName).getValue() + "] matched multiple records. Attribute values must at most match only one unique Metacard.");
            }
        }
    }
    if (Metacard.ID.equals(attributeName)) {
        idToMetacardMap.putAll(pendingNrtIndex.getAllPresent(identifiers));
    }
    if (idToMetacardMap.size() == 0) {
        LOGGER.debug("No results found for given attribute values.");
        // return an empty list
        return new UpdateResponseImpl(updateRequest, null, new ArrayList<>());
    }
    /* 2. Update the cards */
    List<Metacard> newMetacards = new ArrayList<>();
    for (Entry<Serializable, Metacard> updateEntry : updates) {
        String localKey = updateEntry.getKey().toString();
        /* 2a. Prepare new Metacard */
        MetacardImpl newMetacard = new MetacardImpl(updateEntry.getValue());
        // Find the exact oldMetacard that corresponds with this newMetacard
        Metacard oldMetacard = idToMetacardMap.get(localKey);
        // matched but another did not
        if (oldMetacard != null) {
            // overwrite the id, in case it has not been done properly/already
            newMetacard.setId(oldMetacard.getId());
            newMetacard.setSourceId(getId());
            newMetacards.add(newMetacard);
            updateList.add(new UpdateImpl(newMetacard, oldMetacard));
        }
    }
    try {
        client.add(newMetacards, isForcedAutoCommit());
    } catch (SolrServerException | SolrException | IOException | MetacardCreationException e) {
        LOGGER.info("Failed to update metacard(s) with Solr.", e);
        throw new IngestException("Failed to update metacard(s).");
    }
    pendingNrtIndex.putAll(updateList.stream().collect(Collectors.toMap(u -> u.getNewMetacard().getId(), u -> copyMetacard(u.getNewMetacard()))));
    return new UpdateResponseImpl(updateRequest, updateRequest.getProperties(), updateList);
}
Also used : UpdateImpl(ddf.catalog.operation.impl.UpdateImpl) Serializable(java.io.Serializable) HashMap(java.util.HashMap) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Entry(java.util.Map.Entry) SolrDocument(org.apache.solr.common.SolrDocument) UpdateResponseImpl(ddf.catalog.operation.impl.UpdateResponseImpl) IngestException(ddf.catalog.source.IngestException) SolrException(org.apache.solr.common.SolrException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) IOException(java.io.IOException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Metacard(ddf.catalog.data.Metacard) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse)

Example 4 with IngestException

use of ddf.catalog.source.IngestException in project ddf by codice.

the class ReuterSolrImport method ingest.

public void ingest() throws IOException {
    List<Metacard> metacards = new ArrayList<Metacard>();
    for (File localFile : arrayOfFile) {
        Metacard mc = readFile(localFile);
        if (mc != null) {
            metacards.add(mc);
        }
    }
    try {
        solrProvider.create(new CreateRequestImpl(metacards));
    } catch (IngestException e) {
        LOGGER.info("Unexpected IngestException", e);
    }
    solr.close();
}
Also used : Metacard(ddf.catalog.data.Metacard) ArrayList(java.util.ArrayList) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) IngestException(ddf.catalog.source.IngestException) File(java.io.File)

Example 5 with IngestException

use of ddf.catalog.source.IngestException in project ddf by codice.

the class RegistryStoreImpl method update.

@Override
public UpdateResponse update(UpdateRequest request) throws IngestException {
    if (request.getUpdates().stream().map(e -> RegistryUtility.getRegistryId(e.getValue())).anyMatch(Objects::isNull)) {
        throw new IngestException("One or more of the metacards is not a registry metacard");
    }
    Map<String, Metacard> updatedMetacards = request.getUpdates().stream().collect(Collectors.toMap(e -> RegistryUtility.getRegistryId(e.getValue()), Map.Entry::getValue));
    Map<String, Metacard> origMetacards = ((OperationTransaction) request.getPropertyValue(Constants.OPERATION_TRANSACTION_KEY)).getPreviousStateMetacards().stream().collect(Collectors.toMap(RegistryUtility::getRegistryId, e -> e));
    //update the new metacards with the id from the orig so that they can be found on the remote system
    try {
        for (Map.Entry<String, Metacard> entry : updatedMetacards.entrySet()) {
            setMetacardExtID(entry.getValue(), origMetacards.get(entry.getKey()).getId());
        }
    } catch (ParserException e) {
        throw new IngestException("Could not update metacards id", e);
    }
    return super.update(request);
}
Also used : CreateRequest(ddf.catalog.operation.CreateRequest) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) LoggerFactory(org.slf4j.LoggerFactory) TagsFilterDelegate(ddf.catalog.filter.delegate.TagsFilterDelegate) Locale(java.util.Locale) Map(java.util.Map) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) URI(java.net.URI) Bundle(org.osgi.framework.Bundle) Converter(com.thoughtworks.xstream.converters.Converter) ImmutableSet(com.google.common.collect.ImmutableSet) RegistryUtility(org.codice.ddf.registry.common.metacard.RegistryUtility) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) SourceMonitor(ddf.catalog.source.SourceMonitor) ParserException(org.codice.ddf.parser.ParserException) AbstractCswStore(org.codice.ddf.spatial.ogc.csw.catalog.common.source.AbstractCswStore) Collectors(java.util.stream.Collectors) BundleContext(org.osgi.framework.BundleContext) Serializable(java.io.Serializable) Objects(java.util.Objects) SecureCxfClientFactory(org.codice.ddf.cxf.SecureCxfClientFactory) DeleteRequest(ddf.catalog.operation.DeleteRequest) List(java.util.List) ExternalIdentifierType(oasis.names.tc.ebxml_regrep.xsd.rim._3.ExternalIdentifierType) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) RegistryObjectMetacardType(org.codice.ddf.registry.common.metacard.RegistryObjectMetacardType) UpdateResponse(ddf.catalog.operation.UpdateResponse) Dictionary(java.util.Dictionary) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) HashMap(java.util.HashMap) DeleteResponse(ddf.catalog.operation.DeleteResponse) OperationTransaction(ddf.catalog.operation.OperationTransaction) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) ArrayList(java.util.ArrayList) MetacardMarshaller(org.codice.ddf.registry.schemabindings.helper.MetacardMarshaller) Configuration(org.osgi.service.cm.Configuration) CreateResponse(ddf.catalog.operation.CreateResponse) CollectionUtils(org.apache.commons.collections.CollectionUtils) Constants(ddf.catalog.Constants) Metacard(ddf.catalog.data.Metacard) SecurityConstants(ddf.security.SecurityConstants) QueryRequest(ddf.catalog.operation.QueryRequest) UpdateRequest(ddf.catalog.operation.UpdateRequest) EncryptionService(ddf.security.encryption.EncryptionService) RegistryConstants(org.codice.ddf.registry.common.RegistryConstants) Result(ddf.catalog.data.Result) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Logger(org.slf4j.Logger) RegistryPackageType(oasis.names.tc.ebxml_regrep.xsd.rim._3.RegistryPackageType) IngestException(ddf.catalog.source.IngestException) RegistryStore(org.codice.ddf.registry.api.internal.RegistryStore) CswSourceConfiguration(org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration) IOException(java.io.IOException) CreateResponseImpl(ddf.catalog.operation.impl.CreateResponseImpl) MetaTypeService(org.osgi.service.metatype.MetaTypeService) Consumer(java.util.function.Consumer) Query(ddf.catalog.operation.Query) SourceResponse(ddf.catalog.operation.SourceResponse) Filter(org.opengis.filter.Filter) Collections(java.util.Collections) FrameworkUtil(org.osgi.framework.FrameworkUtil) OperationTransaction(ddf.catalog.operation.OperationTransaction) ParserException(org.codice.ddf.parser.ParserException) Metacard(ddf.catalog.data.Metacard) Objects(java.util.Objects) IngestException(ddf.catalog.source.IngestException) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

IngestException (ddf.catalog.source.IngestException)70 Metacard (ddf.catalog.data.Metacard)42 ArrayList (java.util.ArrayList)36 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)29 HashMap (java.util.HashMap)25 CreateResponse (ddf.catalog.operation.CreateResponse)21 IOException (java.io.IOException)21 Test (org.junit.Test)19 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)17 CreateRequestImpl (ddf.catalog.operation.impl.CreateRequestImpl)16 CreateRequest (ddf.catalog.operation.CreateRequest)15 InternalIngestException (ddf.catalog.source.InternalIngestException)15 Serializable (java.io.Serializable)15 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)14 Map (java.util.Map)13 DeleteResponse (ddf.catalog.operation.DeleteResponse)12 UpdateRequest (ddf.catalog.operation.UpdateRequest)12 FederationException (ddf.catalog.federation.FederationException)11 QueryResponse (ddf.catalog.operation.QueryResponse)11 UpdateResponse (ddf.catalog.operation.UpdateResponse)11