use of ddf.catalog.data.BinaryContent in project ddf by codice.
the class TestGeoJsonMetacardTransformer method testNoGeo.
/**
* Tests that proper JSON output is received when no {@link Metacard#GEOGRAPHY} is found.
*
* @throws CatalogTransformerException
* @throws IOException
* @throws ParseException
*/
@Test
public void testNoGeo() throws CatalogTransformerException, IOException, ParseException {
Date now = new Date();
MetacardImpl metacard = new MetacardImpl();
setupBasicMetacard(now, metacard);
GeoJsonMetacardTransformer transformer = new GeoJsonMetacardTransformer();
BinaryContent content = transformer.transform(metacard, null);
assertEquals(content.getMimeTypeValue(), GeoJsonMetacardTransformer.DEFAULT_MIME_TYPE.getBaseType());
String jsonText = new String(content.getByteArray());
LOGGER.debug(jsonText);
Object object = PARSER.parse(jsonText);
JSONObject obj2 = (JSONObject) object;
assertThat(obj2.get("geometry"), nullValue());
verifyBasicMetacardJson(now, obj2);
}
use of ddf.catalog.data.BinaryContent in project ddf by codice.
the class TestGeoJsonMetacardTransformer method testWithMultiValueAttributes.
@Test
public void testWithMultiValueAttributes() throws Exception {
Set<AttributeDescriptor> descriptors = new HashSet(BasicTypes.BASIC_METACARD.getAttributeDescriptors());
descriptors.add(new AttributeDescriptorImpl("multi-string", true, true, false, true, /* multivalued */
BasicTypes.STRING_TYPE));
MetacardType type = new MetacardTypeImpl("multi", descriptors);
MetacardImpl metacard = new MetacardImpl(type);
metacard.setAttribute("multi-string", (Serializable) Arrays.asList("foo", "bar"));
GeoJsonMetacardTransformer transformer = new GeoJsonMetacardTransformer();
BinaryContent content = transformer.transform(metacard, null);
String jsonText = new String(content.getByteArray());
JSONObject json = (JSONObject) PARSER.parse(jsonText);
Map properties = (Map) json.get("properties");
List<String> strings = (List<String>) properties.get("multi-string");
assertThat(strings.get(0), is("foo"));
assertThat(strings.get(1), is("bar"));
}
use of ddf.catalog.data.BinaryContent in project ddf by codice.
the class TestXmlResponseQueueTransformer method testStub.
@Test
public void testStub() throws CatalogTransformerException, IOException, XpathException, SAXException {
// given
transformer.setThreshold(2);
SourceResponse response = givenSourceResponse(DEFAULT_SOURCE_ID, DEFAULT_ID);
// when
BinaryContent binaryContent = transformer.transform(response, null);
// then
assertThat(binaryContent.getMimeType(), is(mimeType));
byte[] bytes = binaryContent.getByteArray();
String output = new String(bytes);
print(output, verboseDebug);
assertXpathEvaluatesTo(DEFAULT_SOURCE_ID, "/mc:metacards/mc:metacard/mc:source", output);
assertXpathEvaluatesTo(DEFAULT_ID, "/mc:metacards/mc:metacard/@gml:id", output);
verifyDefaults("1", output);
}
use of ddf.catalog.data.BinaryContent 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.BinaryContent in project ddf by codice.
the class TestZipCompression method testCompressionWithLocalContent.
@Test
public void testCompressionWithLocalContent() throws Exception {
SourceResponse sourceResponse = createSourceResponseWithURISchemes(CONTENT_SCHEME, null);
BinaryContent binaryContent = zipCompression.transform(sourceResponse, filePathArgument);
assertThat(binaryContent, notNullValue());
assertZipContents(binaryContent, METACARD_RESULT_LIST_WITH_CONTENT);
}
Aggregations