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;
}
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;
}
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));
}
Aggregations