Search in sources :

Example 6 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class CacheQueryRunnable method run.

@Override
public void run() {
    // check if there are any currently cached results
    QueryResponse response = queryCatalog(null, request, subject, new HashMap<>(CACHE_PROPERTIES));
    search.update(response);
    try {
        searchController.publishResults(request.getId(), search.transform(request.getId()), session);
    } catch (CatalogTransformerException e) {
        LOGGER.debug("Failed to transform cached search results.", e);
    }
    addResults(response.getResults());
    indexResults(response);
}
Also used : QueryResponse(ddf.catalog.operation.QueryResponse) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException)

Example 7 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class KMLTransformerImpl method getKmlGeoFromWkt.

private Geometry getKmlGeoFromWkt(final String wkt) throws CatalogTransformerException {
    if (StringUtils.isBlank(wkt)) {
        throw new CatalogTransformerException("WKT was null or empty. Unable to preform KML Transform on Metacard.");
    }
    com.vividsolutions.jts.geom.Geometry geo = readGeoFromWkt(wkt);
    Geometry kmlGeo = createKmlGeo(geo);
    if (!Point.class.getSimpleName().equals(geo.getGeometryType())) {
        kmlGeo = addPointToKmlGeo(kmlGeo, geo.getCoordinate());
    }
    return kmlGeo;
}
Also used : Geometry(de.micromata.opengis.kml.v_2_2_0.Geometry) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException)

Example 8 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException 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;
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) 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) ServiceReference(org.osgi.framework.ServiceReference) Metacard(ddf.catalog.data.Metacard) ArrayList(java.util.ArrayList) List(java.util.List) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext)

Example 9 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class QueryResponseTransformerProducer method transform.

protected Object transform(Message in, Object obj, String mimeType, String transformerId, MimeTypeToTransformerMapper mapper) throws MimeTypeParseException, CatalogTransformerException {
    // Look up the QueryResponseTransformer for the request's mime type.
    // If a transformer is found, then transform the request's payload into a BinaryContent
    // Otherwise, throw an exception.
    MimeType derivedMimeType = new MimeType(mimeType);
    if (transformerId != null) {
        derivedMimeType = new MimeType(mimeType + ";" + MimeTypeToTransformerMapper.ID_KEY + "=" + transformerId);
    }
    List<QueryResponseTransformer> matches = mapper.findMatches(QueryResponseTransformer.class, derivedMimeType);
    Object binaryContent = null;
    if (matches != null && matches.size() == 1) {
        Map<String, Serializable> arguments = new HashMap<String, Serializable>();
        for (Entry<String, Object> entry : in.getHeaders().entrySet()) {
            if (entry.getValue() instanceof Serializable) {
                arguments.put(entry.getKey(), (Serializable) entry.getValue());
            }
        }
        LOGGER.debug("Found a matching QueryResponseTransformer for [{}]", transformerId);
        QueryResponseTransformer transformer = matches.get(0);
        SourceResponse srcResp = in.getBody(SourceResponse.class);
        if (null != srcResp) {
            binaryContent = transformer.transform(srcResp, arguments);
        }
    } else {
        LOGGER.debug("Did not find an QueryResponseTransformer for [{}]", transformerId);
        throw new CatalogTransformerException("Did not find an QueryResponseTransformer for [" + transformerId + "]");
    }
    return binaryContent;
}
Also used : Serializable(java.io.Serializable) SourceResponse(ddf.catalog.operation.SourceResponse) QueryResponseTransformer(ddf.catalog.transform.QueryResponseTransformer) HashMap(java.util.HashMap) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) MimeType(javax.activation.MimeType)

Example 10 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class CqlResult method metacardToMap.

private Map<String, Object> metacardToMap(Result result) {
    Map<String, Object> geoJson = null;
    MetacardImpl resultMetacard = new MetacardImpl(result.getMetacard(), result.getMetacard().getMetacardType());
    try {
        for (AttributeDescriptor descriptor : resultMetacard.getMetacardType().getAttributeDescriptors()) {
            switch(descriptor.getType().getAttributeFormat()) {
                case BINARY:
                case XML:
                case OBJECT:
                    resultMetacard.setAttribute(descriptor.getName(), null);
                default:
                    break;
            }
        }
        geoJson = PropertyJsonMetacardTransformer.convertToJSON(resultMetacard, ImmutableList.of(AttributeType.AttributeFormat.BINARY, AttributeType.AttributeFormat.XML, AttributeType.AttributeFormat.OBJECT));
        addCachedDate(resultMetacard, geoJson);
    } catch (CatalogTransformerException e) {
        LOGGER.debug("Unable to convert metacard to GeoJSON", e);
    }
    return geoJson;
}
Also used : AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Aggregations

CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)112 IOException (java.io.IOException)53 Metacard (ddf.catalog.data.Metacard)44 InputStream (java.io.InputStream)40 ByteArrayInputStream (java.io.ByteArrayInputStream)29 BinaryContent (ddf.catalog.data.BinaryContent)25 InputTransformer (ddf.catalog.transform.InputTransformer)21 Serializable (java.io.Serializable)21 HashMap (java.util.HashMap)21 Result (ddf.catalog.data.Result)16 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)15 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)14 ArrayList (java.util.ArrayList)13 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)12 Test (org.junit.Test)12 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)10 MimeType (javax.activation.MimeType)10 SourceResponse (ddf.catalog.operation.SourceResponse)9 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)8 List (java.util.List)8