use of ddf.catalog.data.MetacardCreationException in project ddf by codice.
the class DynamicSchemaResolver method addFields.
/**
* Adds the fields of the Metacard into the {@link SolrInputDocument}
*/
void addFields(Metacard metacard, SolrInputDocument solrInputDocument) throws MetacardCreationException {
MetacardType schema = metacard.getMetacardType();
for (AttributeDescriptor ad : schema.getAttributeDescriptors()) {
if (metacard.getAttribute(ad.getName()) != null) {
List<Serializable> attributeValues = metacard.getAttribute(ad.getName()).getValues();
if (CollectionUtils.isNotEmpty(attributeValues) && attributeValues.get(0) != null) {
AttributeFormat format = ad.getType().getAttributeFormat();
String formatIndexName = ad.getName() + getFieldSuffix(format);
if (AttributeFormat.XML.equals(format) && solrInputDocument.getFieldValue(formatIndexName + getSpecialIndexSuffix(AttributeFormat.STRING)) == null) {
List<String> parsedTexts = parseTextFrom(attributeValues);
// parsedTexts => *_txt_tokenized
String specialStringIndexName = ad.getName() + getFieldSuffix(AttributeFormat.STRING) + getSpecialIndexSuffix(AttributeFormat.STRING);
solrInputDocument.addField(specialStringIndexName, parsedTexts);
} else if (AttributeFormat.STRING.equals(format) && solrInputDocument.getFieldValue(ad.getName() + getFieldSuffix(AttributeFormat.STRING)) == null) {
List<Serializable> truncatedValues = attributeValues.stream().map(value -> value != null ? truncateAsUTF8(value.toString()) : value).collect(Collectors.toList());
// *_txt
solrInputDocument.addField(ad.getName() + getFieldSuffix(AttributeFormat.STRING), truncatedValues);
// *_txt_tokenized
solrInputDocument.addField(ad.getName() + getFieldSuffix(AttributeFormat.STRING) + getSpecialIndexSuffix(AttributeFormat.STRING), attributeValues);
} else if (AttributeFormat.OBJECT.equals(format)) {
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
List<Serializable> byteArrays = new ArrayList<>();
try (ObjectOutputStream out = new ObjectOutputStream(byteArrayOS)) {
for (Serializable serializable : attributeValues) {
out.writeObject(serializable);
byteArrays.add(byteArrayOS.toByteArray());
out.reset();
}
} catch (IOException e) {
throw new MetacardCreationException(COULD_NOT_SERIALIZE_OBJECT_MESSAGE, e);
}
attributeValues = byteArrays;
}
if (AttributeFormat.GEOMETRY.equals(format) && solrInputDocument.getFieldValue(formatIndexName + SchemaFields.SORT_SUFFIX) == null) {
solrInputDocument.addField(formatIndexName + SchemaFields.SORT_SUFFIX, createCenterPoint(attributeValues));
}
if (AttributeFormat.STRING.equals(format) && caseInsensitiveSort && solrInputDocument.getFieldValue(formatIndexName + SchemaFields.SORT_SUFFIX) == null) {
solrInputDocument.addField(formatIndexName + SchemaFields.SORT_SUFFIX, attributeValues.stream().filter(Objects::nonNull).map(Object::toString).map(value -> truncate(value, textSortCharacterLimit)).map(String::toLowerCase).collect(Collectors.toList()));
}
// Prevent adding a field already on document
if (solrInputDocument.getFieldValue(formatIndexName) == null) {
solrInputDocument.addField(formatIndexName, attributeValues);
} else {
LOGGER.trace("Skipping adding field already found on document ({})", formatIndexName);
}
}
}
}
/*
* Lastly the metacardType must be added to the solr document. These are internal fields
*/
String schemaName = String.format("%s#%s", schema.getName(), schema.hashCode());
solrInputDocument.addField(SchemaFields.METACARD_TYPE_FIELD_NAME, schemaName);
byte[] metacardTypeBytes = metacardTypeNameToSerialCache.getIfPresent(schemaName);
if (metacardTypeBytes == null) {
MetacardType coreMetacardType = new MetacardTypeImpl(schema.getName(), convertAttributeDescriptors(schema.getAttributeDescriptors()));
metacardTypesCache.put(schemaName, coreMetacardType);
metacardTypeBytes = serialize(coreMetacardType);
metacardTypeNameToSerialCache.put(schemaName, metacardTypeBytes);
addToFieldsCache(coreMetacardType.getAttributeDescriptors());
}
solrInputDocument.addField(SchemaFields.METACARD_TYPE_OBJECT_FIELD_NAME, metacardTypeBytes);
}
use of ddf.catalog.data.MetacardCreationException in project ddf by codice.
the class ReindexCommand method getData.
private List<Metacard> getData(org.codice.solr.client.solrj.SolrClient sourceSolr) throws IOException, SolrServerException {
final SolrQuery query = getQuery();
List<Metacard> data = new ArrayList<>();
LOGGER.trace("Retrieving data with query: {}", query);
/**
* Always query the default collection, unable to use overload function that takes in a core
* name due to how HttpSolrClientFactory create its client
* https://github.com/codice/ddf/blob/master/platform/solr/solr-factory-impl/src/main/java/org/codice/solr/factory/impl/HttpSolrClientFactory.java#L130
* this limitation also spill over when using solr Cloud
*/
QueryResponse response = sourceSolr.query(query);
cursorMark = response.getNextCursorMark();
SolrDocumentList docList = response.getResults();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Query returned: {} items", docList.size());
}
for (SolrDocument doc : docList) {
try {
Metacard metacard = metacardClient.createMetacard(doc);
data.add(metacard);
} catch (MetacardCreationException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Unable to convert: {} to metacard", doc, e);
}
}
}
return data;
}
use of ddf.catalog.data.MetacardCreationException in project ddf by codice.
the class RESTEndpoint method updateDocument.
/**
* REST Put. Updates the specified entry with the provided document.
*
* @param id
* @param message
* @return
*/
@PUT
@Path("/{id}")
@Consumes("multipart/*")
public Response updateDocument(@PathParam("id") String id, @Context HttpHeaders headers, @Context HttpServletRequest httpRequest, MultipartBody multipartBody, @QueryParam("transform") String transformerParam, InputStream message) {
LOGGER.trace("PUT");
Response response;
try {
if (id != null && message != null) {
MimeType mimeType = getMimeType(headers);
CreateInfo createInfo = null;
if (multipartBody != null) {
List<Attachment> contentParts = multipartBody.getAllAttachments();
if (contentParts != null && contentParts.size() > 0) {
createInfo = parseAttachments(contentParts, transformerParam);
} else {
LOGGER.debug("No file contents attachment found");
}
}
if (createInfo == null) {
UpdateRequest updateRequest = new UpdateRequestImpl(id, generateMetacard(mimeType, id, message, transformerParam));
catalogFramework.update(updateRequest);
} else {
UpdateStorageRequest streamUpdateRequest = new UpdateStorageRequestImpl(Collections.singletonList(new IncomingContentItem(id, createInfo.getStream(), createInfo.getContentType(), createInfo.getFilename(), 0, createInfo.getMetacard())), null);
catalogFramework.update(streamUpdateRequest);
}
LOGGER.debug("Metacard {} updated.", id);
response = Response.ok().build();
} else {
String errorResponseString = "Both ID and content are needed to perform UPDATE.";
LOGGER.info(errorResponseString);
throw new ServerErrorException(errorResponseString, Status.BAD_REQUEST);
}
} catch (SourceUnavailableException e) {
String exceptionMessage = "Cannot update catalog entry: Source is unavailable: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (InternalIngestException e) {
String exceptionMessage = "Error cataloging updated metadata: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (MetacardCreationException | IngestException e) {
String exceptionMessage = "Error cataloging updated metadata: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.BAD_REQUEST);
}
return response;
}
use of ddf.catalog.data.MetacardCreationException in project ddf by codice.
the class SolrCache method create.
public void create(Collection<Metacard> metacards) {
if (metacards == null || metacards.size() == 0) {
return;
}
List<Metacard> updatedMetacards = new ArrayList<>();
for (Metacard metacard : metacards) {
if (metacard != null) {
if (StringUtils.isNotBlank(metacard.getSourceId()) && StringUtils.isNotBlank(metacard.getId())) {
updatedMetacards.add(metacard);
}
} else {
LOGGER.debug("metacard in result was null");
}
}
try {
solrClientAdaptor.getSolrMetacardClient().add(updatedMetacards, false);
dirty.set(true);
} catch (SolrServerException | SolrException | IOException | MetacardCreationException e) {
LOGGER.info("Solr solrClientAdaptor exception caching metacard(s)", e);
}
}
use of ddf.catalog.data.MetacardCreationException in project ddf by codice.
the class DynamicSchemaResolver method serialize.
private byte[] serialize(MetacardType anywhereMType) throws MetacardCreationException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(anywhereMType);
byte[] bytes = baos.toByteArray();
baos.close();
out.close();
return bytes;
} catch (IOException e) {
throw new MetacardCreationException(COULD_NOT_READ_METACARD_TYPE_MESSAGE, e);
}
}
Aggregations