Search in sources :

Example 31 with BinaryContentImpl

use of ddf.catalog.data.impl.BinaryContentImpl in project ddf by codice.

the class ZipCompression method transform.

/**
     * Transforms a SourceResponse with a list of {@link Metacard}s into a {@link BinaryContent} item
     * with an {@link InputStream}.  This transformation expects a key-value pair "fileName"-zipFileName to be present.
     *
     * @param upstreamResponse - a SourceResponse with a list of {@link Metacard}s to compress
     * @param arguments        - a map of arguments to use for processing.  This method expects "fileName" to be set
     * @return - a {@link BinaryContent} item with the {@link InputStream} for the Zip file
     * @throws CatalogTransformerException when the transformation fails
     */
@Override
public BinaryContent transform(SourceResponse upstreamResponse, Map<String, Serializable> arguments) throws CatalogTransformerException {
    if (upstreamResponse == null || CollectionUtils.isEmpty(upstreamResponse.getResults())) {
        throw new CatalogTransformerException("No Metacards were found to transform.");
    }
    if (MapUtils.isEmpty(arguments) || !arguments.containsKey(ZipDecompression.FILE_PATH)) {
        throw new CatalogTransformerException("No 'filePath' argument found in arguments map.");
    }
    ZipFile zipFile;
    String filePath = (String) arguments.get(ZipDecompression.FILE_PATH);
    try {
        zipFile = new ZipFile(filePath);
    } catch (ZipException e) {
        LOGGER.debug("Unable to create zip file with path : {}", filePath, e);
        throw new CatalogTransformerException(String.format("Unable to create zip file at %s", filePath), e);
    }
    List<Result> resultList = upstreamResponse.getResults();
    Map<String, Resource> resourceMap = new HashMap<>();
    resultList.stream().map(Result::getMetacard).forEach(metacard -> {
        ZipParameters zipParameters = new ZipParameters();
        zipParameters.setSourceExternalStream(true);
        zipParameters.setFileNameInZip(METACARD_PATH + metacard.getId());
        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) {
            objectOutputStream.writeObject(new MetacardImpl(metacard));
            InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            zipFile.addStream(inputStream, zipParameters);
            if (hasLocalResources(metacard)) {
                resourceMap.putAll(getAllMetacardContent(metacard));
            }
        } catch (IOException | ZipException e) {
            LOGGER.debug("Failed to add metacard with id {}.", metacard.getId(), e);
        }
    });
    resourceMap.forEach((filename, resource) -> {
        try {
            ZipParameters zipParameters = new ZipParameters();
            zipParameters.setSourceExternalStream(true);
            zipParameters.setFileNameInZip(filename);
            zipFile.addStream(resource.getInputStream(), zipParameters);
        } catch (ZipException e) {
            LOGGER.debug("Failed to add resource with id {} to zip.", resource.getName(), e);
        }
    });
    BinaryContent binaryContent;
    try {
        InputStream fileInputStream = new ZipInputStream(new FileInputStream(zipFile.getFile()));
        binaryContent = new BinaryContentImpl(fileInputStream);
        jarSigner.signJar(zipFile.getFile(), System.getProperty("org.codice.ddf.system.hostname"), System.getProperty("javax.net.ssl.keyStorePassword"), System.getProperty("javax.net.ssl.keyStore"), System.getProperty("javax.net.ssl.keyStorePassword"));
    } catch (FileNotFoundException e) {
        throw new CatalogTransformerException("Unable to get ZIP file from ZipInputStream.", e);
    }
    return binaryContent;
}
Also used : HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Resource(ddf.catalog.resource.Resource) FileNotFoundException(java.io.FileNotFoundException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ZipException(net.lingala.zip4j.exception.ZipException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) ObjectOutputStream(java.io.ObjectOutputStream) BinaryContent(ddf.catalog.data.BinaryContent) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) FileInputStream(java.io.FileInputStream) Result(ddf.catalog.data.Result) ZipParameters(net.lingala.zip4j.model.ZipParameters) ZipInputStream(java.util.zip.ZipInputStream) ZipFile(net.lingala.zip4j.core.ZipFile) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 32 with BinaryContentImpl

use of ddf.catalog.data.impl.BinaryContentImpl in project ddf by codice.

the class BinaryContentStringTypeConverter method convertTo.

@Override
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
    String mimeTypeString = exchange.getOut().getHeader(HttpHeaders.CONTENT_TYPE, String.class);
    if (null == mimeTypeString) {
        mimeTypeString = MediaType.TEXT_PLAIN;
    }
    MimeType mimeType = null;
    try {
        mimeType = new MimeType(mimeTypeString);
    } catch (MimeTypeParseException e) {
        LOGGER.debug("Failed to parse mimetype: " + mimeTypeString, e);
    }
    T result = null;
    try {
        result = type.cast(new BinaryContentImpl(exchange.getOut().getBody(InputStream.class), mimeType));
    } catch (ClassCastException e) {
        LOGGER.debug("Failed to create BinaryContent", e);
    }
    return result;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) InputStream(java.io.InputStream) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) MimeType(javax.activation.MimeType)

Example 33 with BinaryContentImpl

use of ddf.catalog.data.impl.BinaryContentImpl in project alliance by codice.

the class CatalogOutputAdapter method nitfToBinaryContent.

private BinaryContent nitfToBinaryContent(NitfHeader header, ImageSegment imageSegment) throws IOException, MimeTypeParseException {
    byte[] data;
    File tmpFile = File.createTempFile("nitfchip-", ".ntf");
    try {
        new NitfCreationFlowImpl().fileHeader(() -> header).imageSegment(() -> imageSegment).write(tmpFile.getAbsolutePath());
        try (FileInputStream fis = new FileInputStream(tmpFile)) {
            data = IOUtils.toByteArray(fis);
        }
    } finally {
        if (!tmpFile.delete()) {
            LOGGER.debug("unable to delete the temporary file '{}'", tmpFile);
        }
    }
    return new BinaryContentImpl(new ByteArrayInputStream(data), new MimeType(IMAGE_NITF));
}
Also used : NitfCreationFlowImpl(org.codice.imaging.nitf.fluent.impl.NitfCreationFlowImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) File(java.io.File) FileInputStream(java.io.FileInputStream) MimeType(javax.activation.MimeType)

Aggregations

BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)33 ByteArrayInputStream (java.io.ByteArrayInputStream)19 InputStream (java.io.InputStream)12 Test (org.junit.Test)12 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)11 IOException (java.io.IOException)11 BinaryContent (ddf.catalog.data.BinaryContent)10 HashMap (java.util.HashMap)10 FileInputStream (java.io.FileInputStream)9 Metacard (ddf.catalog.data.Metacard)8 MimeType (javax.activation.MimeType)8 Map (java.util.Map)7 Matchers.anyString (org.mockito.Matchers.anyString)6 Result (ddf.catalog.data.Result)5 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)4 TreeMarshaller (com.thoughtworks.xstream.core.TreeMarshaller)4 StringWriter (java.io.StringWriter)4 MimeTypeParseException (javax.activation.MimeTypeParseException)4 Failure (org.junit.runner.notification.Failure)4 CatalogFramework (ddf.catalog.CatalogFramework)3