Search in sources :

Example 11 with MetacardCreationException

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);
}
Also used : Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) WKTReader(org.locationtech.jts.io.WKTReader) AttributeFormat(ddf.catalog.data.AttributeType.AttributeFormat) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) XMLInputFactory(javax.xml.stream.XMLInputFactory) SolrClient(org.codice.solr.client.solrj.SolrClient) ObjectInputStream(java.io.ObjectInputStream) LoggerFactory(org.slf4j.LoggerFactory) MetacardCreationException(ddf.catalog.data.MetacardCreationException) Extracted(ddf.catalog.data.types.experimental.Extracted) AttributeType(ddf.catalog.data.AttributeType) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) XMLStreamConstants(javax.xml.stream.XMLStreamConstants) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) AttributeDescriptorImpl(ddf.catalog.data.impl.AttributeDescriptorImpl) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) Collection(java.util.Collection) Set(java.util.Set) Point(org.locationtech.jts.geom.Point) PrivilegedAction(java.security.PrivilegedAction) Collectors(java.util.stream.Collectors) MetacardType(ddf.catalog.data.MetacardType) Serializable(java.io.Serializable) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Stream(java.util.stream.Stream) ParseException(org.locationtech.jts.io.ParseException) MetacardTypeMapperFactory(ddf.catalog.source.solr.json.MetacardTypeMapperFactory) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Entry(java.util.Map.Entry) Geometry(org.locationtech.jts.geom.Geometry) CacheBuilder(com.google.common.cache.CacheBuilder) AccessController(java.security.AccessController) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) METHOD(org.apache.solr.client.solrj.SolrRequest.METHOD) DistanceUtils(org.locationtech.spatial4j.distance.DistanceUtils) Metacard(ddf.catalog.data.Metacard) CollectionUtils(org.apache.commons.collections.CollectionUtils) XMLInputFactory2(org.codehaus.stax2.XMLInputFactory2) ObjectOutputStream(java.io.ObjectOutputStream) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Logger(org.slf4j.Logger) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) NamedList(org.apache.solr.common.util.NamedList) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) SolrDocument(org.apache.solr.common.SolrDocument) StringReader(java.io.StringReader) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Cache(com.google.common.cache.Cache) Collections(java.util.Collections) Serializable(java.io.Serializable) MetacardCreationException(ddf.catalog.data.MetacardCreationException) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) MetacardType(ddf.catalog.data.MetacardType) AttributeFormat(ddf.catalog.data.AttributeType.AttributeFormat) List(java.util.List) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList)

Example 12 with MetacardCreationException

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;
}
Also used : Metacard(ddf.catalog.data.Metacard) SolrDocument(org.apache.solr.common.SolrDocument) MetacardCreationException(ddf.catalog.data.MetacardCreationException) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ArrayList(java.util.ArrayList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 13 with MetacardCreationException

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;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) UpdateStorageRequestImpl(ddf.catalog.content.operation.impl.UpdateStorageRequestImpl) InternalIngestException(ddf.catalog.source.InternalIngestException) UpdateRequest(ddf.catalog.operation.UpdateRequest) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) MimeType(javax.activation.MimeType) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) CreateResponse(ddf.catalog.operation.CreateResponse) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) IngestException(ddf.catalog.source.IngestException) InternalIngestException(ddf.catalog.source.InternalIngestException) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 14 with MetacardCreationException

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);
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) MetacardCreationException(ddf.catalog.data.MetacardCreationException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException)

Example 15 with MetacardCreationException

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);
    }
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

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