Search in sources :

Example 6 with MetacardCreationException

use of ddf.catalog.data.MetacardCreationException in project ddf by codice.

the class MetacardFactory method generateMetacard.

Metacard generateMetacard(String mimeTypeRaw, String id, String fileName, Path tmpContentPath) throws MetacardCreationException, MimeTypeParseException {
    Metacard generatedMetacard = null;
    MimeType mimeType = new MimeType(mimeTypeRaw);
    List<InputTransformer> listOfCandidates = mimeTypeToTransformerMapper.findMatches(InputTransformer.class, mimeType);
    List<String> stackTraceList = new ArrayList<>();
    LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
    for (InputTransformer candidate : listOfCandidates) {
        try (InputStream transformerStream = com.google.common.io.Files.asByteSource(tmpContentPath.toFile()).openStream()) {
            generatedMetacard = candidate.transform(transformerStream);
        } catch (RuntimeException | CatalogTransformerException | IOException e) {
            List<String> stackTraces = Arrays.asList(ExceptionUtils.getRootCauseStackTrace(e));
            stackTraceList.add(String.format("Transformer [%s] could not create metacard.", candidate));
            stackTraceList.addAll(stackTraces);
            LOGGER.debug("Transformer [{}] could not create metacard.", candidate, e);
        }
        if (generatedMetacard != null) {
            break;
        }
    }
    if (generatedMetacard == null) {
        throw new MetacardCreationException(String.format("Could not create metacard with mimeType %s : %s", mimeTypeRaw, StringUtils.join(stackTraceList, "\n")));
    }
    if (id != null) {
        generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, id));
    } else {
        generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, uuidGenerator.generateUuid()));
    }
    if (StringUtils.isBlank(generatedMetacard.getTitle())) {
        generatedMetacard.setAttribute(new AttributeImpl(Metacard.TITLE, fileName));
    }
    return generatedMetacard;
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) MimeType(javax.activation.MimeType) Metacard(ddf.catalog.data.Metacard) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with MetacardCreationException

use of ddf.catalog.data.MetacardCreationException in project ddf by codice.

the class AbstractCatalogService method updateDocument.

private void updateDocument(Map.Entry<AttachmentInfo, Metacard> attachmentInfoAndMetacard, String id, List<String> contentTypeList, String transformerParam, InputStream message) throws CatalogServiceException {
    try {
        MimeType mimeType = getMimeType(contentTypeList);
        if (attachmentInfoAndMetacard == null) {
            UpdateRequest updateRequest = new UpdateRequestImpl(id, generateMetacard(mimeType, id, message, transformerParam));
            catalogFramework.update(updateRequest);
        } else {
            UpdateStorageRequest streamUpdateRequest = new UpdateStorageRequestImpl(Collections.singletonList(new IncomingContentItem(id, attachmentInfoAndMetacard.getKey().getStream(), attachmentInfoAndMetacard.getKey().getContentType(), attachmentInfoAndMetacard.getKey().getFilename(), 0, attachmentInfoAndMetacard.getValue())), null);
            catalogFramework.update(streamUpdateRequest);
        }
        LOGGER.debug("Metacard {} updated.", LogSanitizer.sanitize(id));
    } catch (SourceUnavailableException e) {
        String exceptionMessage = "Cannot update catalog entry: Source is unavailable: ";
        LOGGER.info(exceptionMessage, e);
        throw new InternalServerErrorException(exceptionMessage);
    } catch (InternalIngestException e) {
        String exceptionMessage = "Error cataloging updated metadata: ";
        LOGGER.info(exceptionMessage, e);
        throw new InternalServerErrorException(exceptionMessage);
    } catch (MetacardCreationException | IngestException e) {
        String errorMessage = "Error cataloging updated metadata: ";
        LOGGER.info(errorMessage, e);
        throw new CatalogServiceException(errorMessage);
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) UpdateStorageRequestImpl(ddf.catalog.content.operation.impl.UpdateStorageRequestImpl) InternalIngestException(ddf.catalog.source.InternalIngestException) UpdateRequest(ddf.catalog.operation.UpdateRequest) MimeType(javax.activation.MimeType) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) IngestException(ddf.catalog.source.IngestException) InternalIngestException(ddf.catalog.source.InternalIngestException) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl)

Example 8 with MetacardCreationException

use of ddf.catalog.data.MetacardCreationException 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 9 with MetacardCreationException

use of ddf.catalog.data.MetacardCreationException in project ddf by codice.

the class AbstractCatalogService method parseMetadata.

private Metacard parseMetadata(String transformerParam, Metacard metacard, Attachment attachment, InputStream inputStream) {
    String transformer = DEFAULT_METACARD_TRANSFORMER;
    if (transformerParam != null) {
        transformer = transformerParam;
    }
    try {
        MimeType mimeType = new MimeType(attachment.getContentType().toString());
        metacard = generateMetacard(mimeType, null, inputStream, transformer);
    } catch (MimeTypeParseException | MetacardCreationException e) {
        LOGGER.debug("Unable to parse metadata {}", attachment.getContentType());
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    return metacard;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) MimeType(javax.activation.MimeType)

Example 10 with MetacardCreationException

use of ddf.catalog.data.MetacardCreationException in project ddf by codice.

the class SolrCatalogProviderImpl method update.

@Override
public UpdateResponse update(UpdateRequest updateRequest) throws IngestException {
    nonNull(updateRequest);
    List<Entry<Serializable, Metacard>> updates = updateRequest.getUpdates();
    // the list of updates, both new and old metacards
    List<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.");
    }
    // if we have nothing to update, send the empty list
    if (CollectionUtils.isEmpty(updates)) {
        return new UpdateResponseImpl(updateRequest, null, new ArrayList<>());
    }
    /* 1. QUERY */
    // Loop to get all identifiers
    Set<String> identifiers = updates.stream().map(Entry::getKey).map(Serializable::toString).collect(Collectors.toSet());
    Map<Serializable, Metacard> idToMetacardMap = new HashMap<>();
    if (Metacard.ID.equals(attributeName)) {
        try {
            idToMetacardMap = client.getIds(identifiers).stream().filter(Objects::nonNull).collect(Collectors.toMap(Metacard::getId, Function.identity()));
        } catch (UnsupportedQueryException e) {
            LOGGER.debug("Solr query by list of IDs failed.", e);
            LOGGER.info("Failed to query for metacard(s) by ID before update.");
        }
    } else {
        /* 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;
        try {
            idResults = solr.query(query, METHOD.POST);
        } catch (SolrServerException | SolrException | IOException e) {
            LOGGER.info("Failed to query for metacard(s) before update.", e);
        }
        // map of old metacards to be populated
        idToMetacardMap.putAll(computeOldMetacardIds(attributeName, updates, idResults));
    }
    if (idToMetacardMap.isEmpty()) {
        LOGGER.debug("No results found for given attribute values.");
        // return an empty list
        return new UpdateResponseImpl(updateRequest, null, new ArrayList<>());
    }
    List<Metacard> newMetacards = computeMetacardsToUpdate(updates, idToMetacardMap, updateList);
    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).");
    }
    return new UpdateResponseImpl(updateRequest, updateRequest.getProperties(), updateList);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) 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) 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) Metacard(ddf.catalog.data.Metacard) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Objects(java.util.Objects)

Aggregations

MetacardCreationException (ddf.catalog.data.MetacardCreationException)25 Metacard (ddf.catalog.data.Metacard)13 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)10 MimeType (javax.activation.MimeType)9 IngestException (ddf.catalog.source.IngestException)8 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)6 SolrServerException (org.apache.solr.client.solrj.SolrServerException)6 SolrException (org.apache.solr.common.SolrException)6 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)5 InputStream (java.io.InputStream)5 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)4 InputTransformer (ddf.catalog.transform.InputTransformer)4 List (java.util.List)4 MimeTypeParseException (javax.activation.MimeTypeParseException)4 SolrQuery (org.apache.solr.client.solrj.SolrQuery)4 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)4 SolrDocument (org.apache.solr.common.SolrDocument)4 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)3 InternalIngestException (ddf.catalog.source.InternalIngestException)3