use of ddf.catalog.data.MetacardCreationException 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);
}
use of ddf.catalog.data.MetacardCreationException 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);
}
use of ddf.catalog.data.MetacardCreationException in project ddf by codice.
the class RESTEndpoint method generateMetacard.
private Metacard generateMetacard(MimeType mimeType, String id, InputStream message, String transformerId) throws MetacardCreationException {
Metacard generatedMetacard = null;
List<InputTransformer> listOfCandidates = mimeTypeToTransformerMapper.findMatches(InputTransformer.class, mimeType);
List<String> stackTraceList = new ArrayList<>();
LOGGER.trace("Entering generateMetacard.");
LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
try {
if (null != message) {
IOUtils.copy(message, fileBackedOutputStream);
} else {
throw new MetacardCreationException("Could not copy bytes of content message. Message was NULL.");
}
} catch (IOException e) {
throw new MetacardCreationException("Could not copy bytes of content message.", e);
}
Iterator<InputTransformer> it = listOfCandidates.iterator();
if (StringUtils.isNotEmpty(transformerId)) {
BundleContext bundleContext = getBundleContext();
Collection<ServiceReference<InputTransformer>> serviceReferences = bundleContext.getServiceReferences(InputTransformer.class, "(id=" + transformerId + ")");
it = serviceReferences.stream().map(bundleContext::getService).iterator();
}
while (it.hasNext()) {
InputTransformer transformer = it.next();
try (InputStream inputStreamMessageCopy = fileBackedOutputStream.asByteSource().openStream()) {
generatedMetacard = transformer.transform(inputStreamMessageCopy);
} catch (CatalogTransformerException | IOException e) {
List<String> stackTraces = Arrays.asList(ExceptionUtils.getRootCauseStackTrace(e));
stackTraceList.add(String.format("Transformer [%s] could not create metacard.", transformer));
stackTraceList.addAll(stackTraces);
LOGGER.debug("Transformer [{}] could not create metacard.", transformer, e);
}
if (generatedMetacard != null) {
break;
}
}
if (generatedMetacard == null) {
throw new MetacardCreationException(String.format("Could not create metacard with mimeType %s : %s", mimeType, StringUtils.join(stackTraceList, "\n")));
}
if (id != null) {
generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, id));
} else {
LOGGER.debug("Metacard had a null id");
}
} catch (IOException e) {
throw new MetacardCreationException("Could not create metacard.", e);
} catch (InvalidSyntaxException e) {
throw new MetacardCreationException("Could not determine transformer", e);
}
return generatedMetacard;
}
use of ddf.catalog.data.MetacardCreationException in project ddf by codice.
the class RESTEndpoint 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, "assigned-when-ingested", inputStream, transformer);
} catch (MimeTypeParseException | MetacardCreationException e) {
LOGGER.debug("Unable to parse metadata {}", attachment.getContentType().toString());
} finally {
IOUtils.closeQuietly(inputStream);
}
return metacard;
}
use of ddf.catalog.data.MetacardCreationException in project alliance by codice.
the class ParentMetacardStreamCreationPlugin method doOnCreate.
@Override
protected void doOnCreate(Context context) throws StreamCreationException {
MetacardImpl metacard;
try {
metacard = createInitialMetacard();
} catch (MetacardCreationException e) {
throw new StreamCreationException("unable to create initial parent metacard", e);
}
setParentResourceUri(context, metacard);
setParentTitle(context, metacard);
setParentContentType(metacard);
CreateRequest createRequest = new CreateRequestImpl(metacard);
try {
submitParentCreateRequest(context, createRequest);
} catch (IngestException | SourceUnavailableException e) {
throw new StreamCreationException("unable to submit parent metacard to catalog framework", e);
}
}
Aggregations