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;
}
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);
}
}
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);
}
}
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;
}
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);
}
Aggregations