Search in sources :

Example 1 with PrintWriter

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

the class MetacardMarshallerImpl method marshal.

@Override
public String marshal(Metacard metacard, Map<String, Serializable> arguments) throws XmlPullParserException, IOException, CatalogTransformerException {
    PrintWriter writer = this.writerProvider.build(Metacard.class);
    if (arguments != null && arguments.get(OMIT_XML_DECL) != null) {
        Boolean omitXmlDec = Boolean.valueOf(String.valueOf(arguments.get(OMIT_XML_DECL)));
        if (omitXmlDec == null || !omitXmlDec) {
            writer.setRawValue(XML_DECL);
        }
    }
    writer.startNode("metacard");
    for (Map.Entry<String, String> nsRow : NAMESPACE_MAP.entrySet()) {
        writer.addAttribute(nsRow.getKey(), nsRow.getValue());
    }
    if (metacard.getId() != null) {
        writer.addAttribute(GML_PREFIX + ":id", metacard.getId());
    }
    writer.startNode("type");
    if (StringUtils.isBlank(metacard.getMetacardType().getName())) {
        writer.setValue(MetacardType.DEFAULT_METACARD_TYPE_NAME);
    } else {
        writer.setValue(metacard.getMetacardType().getName());
    }
    // type
    writer.endNode();
    if (StringUtils.isNotBlank(metacard.getSourceId())) {
        writer.startNode("source");
        writer.setValue(metacard.getSourceId());
        // source
        writer.endNode();
    }
    // if multi-threading, cannot abstract XmlPullParser creation to class member.
    // xmlPullParser used only for geometry
    XmlPullParser xmlPullParser = XppFactory.createDefaultParser();
    Set<AttributeDescriptor> attributeDescriptors = metacard.getMetacardType().getAttributeDescriptors();
    for (AttributeDescriptor attributeDescriptor : attributeDescriptors) {
        String attributeName = attributeDescriptor.getName();
        if (attributeName.equals("id")) {
            continue;
        }
        Attribute attribute = metacard.getAttribute(attributeName);
        if (attribute != null && attribute.getValue() != null) {
            AttributeType.AttributeFormat format = attributeDescriptor.getType().getAttributeFormat();
            writeAttributeToXml(writer, xmlPullParser, attribute, format);
        }
    }
    // metacard
    writer.endNode();
    return writer.makeString();
}
Also used : Attribute(ddf.catalog.data.Attribute) AttributeType(ddf.catalog.data.AttributeType) XmlPullParser(org.xmlpull.v1.XmlPullParser) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) PrintWriter(ddf.catalog.transformer.api.PrintWriter)

Example 2 with PrintWriter

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

the class XmlResponseQueueTransformer method transform.

@Override
public BinaryContent transform(SourceResponse response, Map<String, Serializable> args) throws CatalogTransformerException {
    try {
        PrintWriter writer = printWriterProvider.build(Metacard.class);
        writer.setRawValue("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
        writer.startNode("metacards");
        for (Map.Entry<String, String> nsRow : NAMESPACE_MAP.entrySet()) {
            writer.addAttribute(nsRow.getKey(), nsRow.getValue());
        }
        if (response.getResults() != null && !response.getResults().isEmpty()) {
            StringWriter metacardContent = fjp.invoke(new MetacardForkTask(ImmutableList.copyOf(response.getResults()), fjp, geometryTransformer, threshold, metacardMarshaller));
            writer.setRawValue(metacardContent.getBuffer().toString());
        }
        // metacards
        writer.endNode();
        ByteArrayInputStream bais = new ByteArrayInputStream(writer.makeString().getBytes(StandardCharsets.UTF_8));
        return new BinaryContentImpl(bais, mimeType);
    } catch (Exception e) {
        LOGGER.info("Failed Query response transformation", e);
        throw new CatalogTransformerException("Failed Query response transformation");
    }
}
Also used : StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) IOException(java.io.IOException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) MimeTypeParseException(javax.activation.MimeTypeParseException) PrintWriter(ddf.catalog.transformer.api.PrintWriter)

Example 3 with PrintWriter

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

the class TestEscapingPrintWriter method testUndefinedCharacter.

@Test
public void testUndefinedCharacter() throws CatalogTransformerException {
    String input = new String(Character.toChars(55296));
    String expected = "&#xd800;";
    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)

Example 4 with PrintWriter

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

the class TestEscapingPrintWriter method testControlCharacters.

@Test
public void testControlCharacters() throws CatalogTransformerException {
    String input = "\0 \t \n \r";
    String expected = "&#x0; \t \n &#xd;";
    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)

Example 5 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)

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