Search in sources :

Example 6 with PrintWriter

use of ddf.catalog.transformer.api.PrintWriter in project ddf by codice.

the class MetacardMarshallerImpl method geoToXml.

private String geoToXml(BinaryContent content, XmlPullParser parser) throws UnsupportedEncodingException {
    PrintWriter destination;
    try (InputStreamReader inputStreamReader = new InputStreamReader(content.getInputStream(), StandardCharsets.UTF_8.name())) {
        XppReader source = new XppReader(inputStreamReader, parser);
        // if multi-threading, cannot abstract PrintWriter to class member
        destination = writerProvider.build(Metacard.class);
        new HierarchicalStreamCopier().copy(source, destination);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return destination.makeString();
}
Also used : Metacard(ddf.catalog.data.Metacard) InputStreamReader(java.io.InputStreamReader) XppReader(com.thoughtworks.xstream.io.xml.XppReader) HierarchicalStreamCopier(com.thoughtworks.xstream.io.copy.HierarchicalStreamCopier) IOException(java.io.IOException) PrintWriter(ddf.catalog.transformer.api.PrintWriter)

Example 7 with PrintWriter

use of ddf.catalog.transformer.api.PrintWriter in project ddf by codice.

the class TestCswQueryResponseTransformer method testMarshalAcknowledgementWithFailedTransforms.

@Test
public void testMarshalAcknowledgementWithFailedTransforms() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
    GetRecordsType query = new GetRecordsType();
    query.setResultType(ResultType.RESULTS);
    query.setMaxRecords(BigInteger.valueOf(6));
    query.setStartPosition(BigInteger.valueOf(0));
    SourceResponse sourceResponse = createSourceResponse(query, 6);
    Map<String, Serializable> args = new HashMap<>();
    args.put(CswConstants.RESULT_TYPE_PARAMETER, ResultType.RESULTS);
    args.put(CswConstants.GET_RECORDS, query);
    PrintWriter printWriter = getSimplePrintWriter();
    MetacardTransformer mockMetacardTransformer = mock(MetacardTransformer.class);
    final AtomicLong atomicLong = new AtomicLong(0);
    when(mockMetacardTransformer.transform(any(Metacard.class), anyMap())).then(invocationOnMock -> {
        if (atomicLong.incrementAndGet() == 2) {
            throw new CatalogTransformerException("");
        }
        Metacard metacard = (Metacard) invocationOnMock.getArguments()[0];
        BinaryContentImpl bci = new BinaryContentImpl(IOUtils.toInputStream(metacard.getId() + ","), new MimeType("application/xml"));
        return bci;
    });
    when(mockPrintWriterProvider.build((Class<Metacard>) notNull())).thenReturn(printWriter);
    when(mockTransformerManager.getTransformerBySchema(anyString())).thenReturn(mockMetacardTransformer);
    CswQueryResponseTransformer cswQueryResponseTransformer = new CswQueryResponseTransformer(mockTransformerManager, mockPrintWriterProvider);
    cswQueryResponseTransformer.init();
    BinaryContent content = cswQueryResponseTransformer.transform(sourceResponse, args);
    cswQueryResponseTransformer.destroy();
    String xml = new String(content.getByteArray());
    assertThat(xml, containsString(CswQueryResponseTransformer.NUMBER_OF_RECORDS_MATCHED_ATTRIBUTE + " 6"));
    assertThat(xml, containsString(CswQueryResponseTransformer.NUMBER_OF_RECORDS_RETURNED_ATTRIBUTE + " 5"));
    assertThat(xml, containsString(CswQueryResponseTransformer.NEXT_RECORD_ATTRIBUTE + " 0"));
}
Also used : Serializable(java.io.Serializable) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) SourceResponse(ddf.catalog.operation.SourceResponse) HashMap(java.util.HashMap) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) BinaryContent(ddf.catalog.data.BinaryContent) MimeType(javax.activation.MimeType) AtomicLong(java.util.concurrent.atomic.AtomicLong) Metacard(ddf.catalog.data.Metacard) PrintWriter(ddf.catalog.transformer.api.PrintWriter) Test(org.junit.Test)

Example 8 with PrintWriter

use of ddf.catalog.transformer.api.PrintWriter in project ddf by codice.

the class TestEscapingPrintWriter method testXmlMetaCharacters.

@Test
public void testXmlMetaCharacters() throws CatalogTransformerException {
    String unescaped = "& > < \" \'";
    String escaped = "&amp; &gt; &lt; &quot; &apos;";
    StringWriter stringWriter = new StringWriter(128);
    PrintWriter escapingPrintWriter = new EscapingPrintWriter(stringWriter);
    escapingPrintWriter.setValue(unescaped);
    escapingPrintWriter.flush();
    String processed = stringWriter.toString();
    assertEquals(escaped, processed);
}
Also used : StringWriter(java.io.StringWriter) EscapingPrintWriter(ddf.catalog.transformer.xml.EscapingPrintWriter) EscapingPrintWriter(ddf.catalog.transformer.xml.EscapingPrintWriter) PrintWriter(ddf.catalog.transformer.api.PrintWriter) Test(org.junit.Test)

Example 9 with PrintWriter

use of ddf.catalog.transformer.api.PrintWriter in project ddf by codice.

the class TestEscapingPrintWriter method testSurrogateCharacter.

@Test
public void testSurrogateCharacter() throws CatalogTransformerException {
    String input = new String(Character.toChars(888));
    String expected = "&#x378;";
    StringWriter stringWriter = new StringWriter(8);
    PrintWriter escapingPrintWriter = new EscapingPrintWriter(stringWriter);
    escapingPrintWriter.setValue(input);
    escapingPrintWriter.flush();
    String output = stringWriter.toString();
    assertEquals(expected, output);
}
Also used : StringWriter(java.io.StringWriter) EscapingPrintWriter(ddf.catalog.transformer.xml.EscapingPrintWriter) EscapingPrintWriter(ddf.catalog.transformer.xml.EscapingPrintWriter) PrintWriter(ddf.catalog.transformer.api.PrintWriter) Test(org.junit.Test)

Aggregations

PrintWriter (ddf.catalog.transformer.api.PrintWriter)9 StringWriter (java.io.StringWriter)5 Test (org.junit.Test)5 EscapingPrintWriter (ddf.catalog.transformer.xml.EscapingPrintWriter)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Metacard (ddf.catalog.data.Metacard)2 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)2 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)2 IOException (java.io.IOException)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 HierarchicalStreamCopier (com.thoughtworks.xstream.io.copy.HierarchicalStreamCopier)1 XppReader (com.thoughtworks.xstream.io.xml.XppReader)1 Attribute (ddf.catalog.data.Attribute)1 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)1 AttributeType (ddf.catalog.data.AttributeType)1 BinaryContent (ddf.catalog.data.BinaryContent)1 SourceResponse (ddf.catalog.operation.SourceResponse)1 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)1