Search in sources :

Example 56 with CatalogTransformerException

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

the class ExportCommand method writeToZip.

private void writeToZip(/*Mutable,IO*/
ZipFile zipFile, Result result) {
    ZipParameters parameters = new ZipParameters();
    parameters.setSourceExternalStream(true);
    String id = result.getMetacard().getId();
    parameters.setFileNameInZip(Paths.get("metacards", id.substring(0, 3), id, "metacard", id + ".xml").toString());
    try {
        BinaryContent binaryMetacard = transformer.transform(result.getMetacard(), Collections.emptyMap());
        zipFile.addStream(binaryMetacard.getInputStream(), parameters);
    } catch (ZipException e) {
        LOGGER.error("Error processing result and adding to ZIP", e);
        throw new CatalogCommandRuntimeException(e);
    } catch (CatalogTransformerException e) {
        LOGGER.warn("Could not transform metacard. Metacard will not be added to zip [{}]", result.getMetacard().getId());
        console.printf("%sCould not transform metacard. Metacard will not be included in export. %s - %s%s%n", Ansi.ansi().fg(Ansi.Color.RED).toString(), result.getMetacard().getId(), result.getMetacard().getTitle(), Ansi.ansi().reset().toString());
    }
}
Also used : ZipException(net.lingala.zip4j.exception.ZipException) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent) ZipParameters(net.lingala.zip4j.model.ZipParameters)

Example 57 with CatalogTransformerException

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

the class OpenSearchEndpoint method executeQuery.

/**
     * Executes the OpenSearchQuery and formulates the response
     *
     * @param format     - of the results in the response
     * @param query      - the query to execute
     * @param ui         -the ui information to use to format the results
     * @param properties
     * @return the response on the query
     */
private Response executeQuery(String format, OpenSearchQuery query, UriInfo ui, Map<String, Serializable> properties) {
    Response response = null;
    String queryFormat = format;
    MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
    List<String> subscriptionList = queryParams.get(Constants.SUBSCRIPTION_KEY);
    LOGGER.debug("Attempting to execute query: {}", query.toString());
    try {
        Map<String, Serializable> arguments = new HashMap<String, Serializable>();
        String organization = framework.getOrganization();
        String url = ui.getRequestUri().toString();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("organization: {}", organization);
            LOGGER.debug("url: {}", url);
        }
        arguments.put("organization", organization);
        arguments.put("url", url);
        // interval
        if (subscriptionList != null && !subscriptionList.isEmpty()) {
            String subscription = subscriptionList.get(0);
            LOGGER.debug("Subscription: {}", subscription);
            arguments.put(Constants.SUBSCRIPTION_KEY, subscription);
            List<String> intervalList = queryParams.get(UPDATE_QUERY_INTERVAL);
            if (intervalList != null && !intervalList.isEmpty()) {
                arguments.put(UPDATE_QUERY_INTERVAL, intervalList.get(0));
            }
        }
        if (StringUtils.isEmpty(queryFormat)) {
            queryFormat = DEFAULT_FORMAT;
        }
        if (query.getFilter() != null) {
            QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), properties);
            QueryResponse queryResponse;
            LOGGER.debug("Sending query");
            queryResponse = framework.query(queryRequest);
            // pass in the format for the transform
            BinaryContent content = framework.transform(queryResponse, queryFormat, arguments);
            response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
        } else {
            // No query was specified
            QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), null);
            // Create a dummy QueryResponse with zero results
            QueryResponseImpl queryResponseQueue = new QueryResponseImpl(queryRequest, new ArrayList<Result>(), 0);
            // pass in the format for the transform
            BinaryContent content = framework.transform(queryResponseQueue, queryFormat, arguments);
            if (null != content) {
                response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
            }
        }
    } catch (UnsupportedQueryException ce) {
        LOGGER.info("Unsupported query", ce);
        response = Response.status(Response.Status.BAD_REQUEST).entity(wrapStringInPreformattedTags("Unsupported query")).build();
    } catch (CatalogTransformerException e) {
        LOGGER.info("Error transforming response", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("Error transforming response")).build();
    } catch (FederationException e) {
        LOGGER.info("Error executing query", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query")).build();
    } catch (SourceUnavailableException e) {
        LOGGER.info("Error executing query because the underlying source was unavailable.", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query because the underlying source was unavailable.")).build();
    } catch (RuntimeException e) {
        // Account for any runtime exceptions and send back a server error
        // this prevents full stacktraces returning to the client
        // this allows for a graceful server error to be returned
        LOGGER.info("RuntimeException on executing query", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("RuntimeException on executing query")).build();
    }
    return response;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Serializable(java.io.Serializable) QueryRequest(ddf.catalog.operation.QueryRequest) HashMap(java.util.HashMap) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent) FederationException(ddf.catalog.federation.FederationException) Result(ddf.catalog.data.Result) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse)

Example 58 with CatalogTransformerException

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

the class KMLTransformerImpl method createKmlGeo.

private Geometry createKmlGeo(com.vividsolutions.jts.geom.Geometry geo) throws CatalogTransformerException {
    Geometry kmlGeo = null;
    if (Point.class.getSimpleName().equals(geo.getGeometryType())) {
        Point jtsPoint = (Point) geo;
        kmlGeo = KmlFactory.createPoint().addToCoordinates(jtsPoint.getX(), jtsPoint.getY());
    } else if (LineString.class.getSimpleName().equals(geo.getGeometryType())) {
        LineString jtsLS = (LineString) geo;
        de.micromata.opengis.kml.v_2_2_0.LineString kmlLS = KmlFactory.createLineString();
        List<Coordinate> kmlCoords = kmlLS.createAndSetCoordinates();
        for (com.vividsolutions.jts.geom.Coordinate coord : jtsLS.getCoordinates()) {
            kmlCoords.add(new Coordinate(coord.x, coord.y));
        }
        kmlGeo = kmlLS;
    } else if (Polygon.class.getSimpleName().equals(geo.getGeometryType())) {
        Polygon jtsPoly = (Polygon) geo;
        de.micromata.opengis.kml.v_2_2_0.Polygon kmlPoly = KmlFactory.createPolygon();
        List<Coordinate> kmlCoords = kmlPoly.createAndSetOuterBoundaryIs().createAndSetLinearRing().createAndSetCoordinates();
        for (com.vividsolutions.jts.geom.Coordinate coord : jtsPoly.getCoordinates()) {
            kmlCoords.add(new Coordinate(coord.x, coord.y));
        }
        kmlGeo = kmlPoly;
    } else if (geo instanceof GeometryCollection) {
        List<Geometry> geos = new ArrayList<Geometry>();
        for (int xx = 0; xx < geo.getNumGeometries(); xx++) {
            geos.add(createKmlGeo(geo.getGeometryN(xx)));
        }
        kmlGeo = KmlFactory.createMultiGeometry().withGeometry(geos);
    } else {
        throw new CatalogTransformerException("Unknown / Unsupported Geometry Type '" + geo.getGeometryType() + "'. Unale to preform KML Transform.");
    }
    return kmlGeo;
}
Also used : CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Point(com.vividsolutions.jts.geom.Point) Geometry(de.micromata.opengis.kml.v_2_2_0.Geometry) GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) LineString(com.vividsolutions.jts.geom.LineString) Coordinate(de.micromata.opengis.kml.v_2_2_0.Coordinate) List(java.util.List) ArrayList(java.util.ArrayList) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 59 with CatalogTransformerException

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

the class ZipDecompression method transform.

/**
     * Transforms a Zip InputStream into a List of {@link Metacard}s.  This method expects there to be a
     * filePath and fileName key-value pair passed in the arguments map.
     *
     * @param inputStream - the InputStream to transform
     * @param arguments   - the arguments for the transformation ("filePath" and "fileName").
     * @return the List of {@link Metacard}s produced from the transformation.
     * @throws CatalogTransformerException when the transformation fails.
     */
@Override
public List<Metacard> transform(InputStream inputStream, Map<String, Serializable> arguments) throws CatalogTransformerException {
    if (inputStream == null) {
        throw new CatalogTransformerException("Unable to transform InputStream : InputStream was null.");
    }
    if (MapUtils.isEmpty(arguments) || !arguments.containsKey(FILE_PATH) || !arguments.containsKey(FILE_NAME)) {
        throw new CatalogTransformerException("Unable to transform InputStream : Invalid arguments passed.");
    }
    String zipFileName = (String) arguments.get(FILE_PATH);
    String zipPath = zipFileName + arguments.get(FILE_NAME);
    try {
        zipValidator.validateZipFile(zipPath);
    } catch (ZipValidationException e) {
        throw new CatalogTransformerException("Unable to validate Zip InputStream.  Ensure that the zip file is signed properly using jarsigner and the appropriate certificates.", e);
    }
    Map<String, Metacard> metacards = decompressFile(inputStream, zipFileName);
    return metacards.values().stream().filter(Objects::nonNull).collect(Collectors.toList());
}
Also used : Metacard(ddf.catalog.data.Metacard) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException)

Example 60 with CatalogTransformerException

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

the class InputTransformerProducer method generateMetacard.

private Metacard generateMetacard(MimeType mimeType, MimeTypeToTransformerMapper mapper, InputStream message) throws MetacardCreationException {
    LOGGER.trace("ENTERING: generateMetacard");
    List<InputTransformer> listOfCandidates = mapper.findMatches(InputTransformer.class, mimeType);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
    }
    Metacard generatedMetacard = null;
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            IOUtils.copy(message, fileBackedOutputStream);
        } catch (IOException e) {
            throw new MetacardCreationException("Could not copy bytes of content message.", e);
        }
        // can create the metacard, then do not need to try any remaining InputTransformers.
        for (InputTransformer transformer : listOfCandidates) {
            try (InputStream inputStreamMessageCopy = fileBackedOutputStream.asByteSource().openStream()) {
                generatedMetacard = transformer.transform(inputStreamMessageCopy);
            } catch (IOException | CatalogTransformerException e) {
                LOGGER.debug("Transformer [" + transformer + "] could not create metacard.", e);
            }
            if (generatedMetacard != null) {
                break;
            }
        }
        if (generatedMetacard == null) {
            throw new MetacardCreationException("Could not create metacard with mimeType " + mimeType + ". No valid transformers found.");
        }
        LOGGER.trace("EXITING: generateMetacard");
    } catch (IOException e) {
        throw new MetacardCreationException("Could not create metacard.", e);
    }
    return generatedMetacard;
}
Also used : Metacard(ddf.catalog.data.Metacard) MetacardCreationException(ddf.catalog.data.MetacardCreationException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer)

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