Search in sources :

Example 96 with BinaryContent

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);
}
Also used : JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) LineString(ddf.geo.formatter.LineString) MultiLineString(ddf.geo.formatter.MultiLineString) BinaryContent(ddf.catalog.data.BinaryContent) Date(java.util.Date) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Test(org.junit.Test)

Example 97 with BinaryContent

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"));
}
Also used : AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) AttributeDescriptorImpl(ddf.catalog.data.impl.AttributeDescriptorImpl) LineString(ddf.geo.formatter.LineString) MultiLineString(ddf.geo.formatter.MultiLineString) BinaryContent(ddf.catalog.data.BinaryContent) MetacardType(ddf.catalog.data.MetacardType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) JSONObject(org.json.simple.JSONObject) List(java.util.List) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 98 with BinaryContent

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);
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) Test(org.junit.Test)

Example 99 with BinaryContent

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;
}
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 100 with 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);
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) BinaryContent(ddf.catalog.data.BinaryContent) Test(org.junit.Test)

Aggregations

BinaryContent (ddf.catalog.data.BinaryContent)112 Test (org.junit.Test)79 Metacard (ddf.catalog.data.Metacard)42 SourceResponse (ddf.catalog.operation.SourceResponse)41 HashMap (java.util.HashMap)30 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)28 Map (java.util.Map)24 Result (ddf.catalog.data.Result)22 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)19 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)19 Matchers.anyString (org.mockito.Matchers.anyString)18 Serializable (java.io.Serializable)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 Date (java.util.Date)12 LineString (ddf.geo.formatter.LineString)11 MultiLineString (ddf.geo.formatter.MultiLineString)11 File (java.io.File)11 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)10 ResultImpl (ddf.catalog.data.impl.ResultImpl)10 IOException (java.io.IOException)10