Search in sources :

Example 51 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project webservices-axiom by apache.

the class StreamingOMSerializerTest method runTest.

@Override
protected void runTest() throws Throwable {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    StAXDialect dialect = StAXDialectDetector.getDialect(inputFactory.getClass());
    inputFactory = dialect.normalize(inputFactory);
    // Allow CDATA events
    inputFactory = dialect.enableCDataReporting(inputFactory);
    inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
    XMLOutputFactory outputFactory = dialect.normalize(XMLOutputFactory.newInstance());
    StreamingOMSerializer serializer = new StreamingOMSerializer();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(new StreamSource(file.getUrl().toString()));
    String encoding = reader.getEncoding();
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(out, encoding);
    writer.writeStartDocument(encoding, reader.getVersion());
    serializer.serialize(reader, writer, false);
    writer.writeEndDocument();
    writer.flush();
    InputSource actual = new InputSource();
    actual.setByteStream(new ByteArrayInputStream(out.toByteArray()));
    actual.setSystemId(file.getUrl().toString());
    assertAbout(xml()).that(actual).hasSameContentAs(file.getUrl());
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) InputSource(org.xml.sax.InputSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StAXDialect(org.apache.axiom.util.stax.dialect.StAXDialect) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 52 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project uPortal by Jasig.

the class StAXSerializingComponent method getEventReader.

@Override
public PipelineEventReader<CharacterEventReader, CharacterEvent> getEventReader(HttpServletRequest request, HttpServletResponse response) {
    final PipelineEventReader<XMLEventReader, XMLEvent> eventReader = this.wrappedComponent.getEventReader(request, response);
    // Writer shared by the ChunkingEventReader and the StAX Serializer
    final StringWriter writer = new StringWriter();
    final XMLOutputFactory outputFactory = this.xmlUtilities.getHtmlOutputFactory();
    final XMLEventWriter xmlEventWriter;
    try {
        xmlEventWriter = outputFactory.createXMLEventWriter(writer);
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to create XMLEventWriter", e);
    }
    // Add the chunking wrapper to the XMLEventReader
    final XMLEventReader xmlEventReader = eventReader.getEventReader();
    final ChunkingEventReader chunkingEventReader = new ChunkingEventReader(request, this.chunkingElements, this.chunkingPatternEventSources, this.chunkingPatterns, xmlEventReader, xmlEventWriter, writer);
    try {
        xmlEventWriter.add(chunkingEventReader);
        xmlEventWriter.flush();
        xmlEventWriter.close();
        chunkingEventReader.close();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to write events to Writer", e);
    }
    // Return the chunked data
    final List<CharacterEvent> characterEvents = chunkingEventReader.getCharacterEvents();
    final CharacterEventBufferReader characterEventReader = new CharacterEventBufferReader(characterEvents.listIterator());
    final Map<String, String> outputProperties = eventReader.getOutputProperties();
    return new PipelineEventReaderImpl<CharacterEventReader, CharacterEvent>(characterEventReader, outputProperties);
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) ChunkingEventReader(org.apereo.portal.xml.stream.ChunkingEventReader) CharacterEvent(org.apereo.portal.character.stream.events.CharacterEvent) CharacterEventBufferReader(org.apereo.portal.character.stream.CharacterEventBufferReader) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEventWriter(javax.xml.stream.XMLEventWriter) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader)

Example 53 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project uPortal by Jasig.

the class XmlUtilitiesImpl method convertToDom.

@Override
public Node convertToDom(XMLEventReader xmlEventReader) throws XMLStreamException {
    // Convert the XmlEventReader into a DOM
    final XMLOutputFactory xmlOutputFactory = this.getXmlOutputFactory();
    final DOMResult sourceDom = new DOMResult(DocumentFactory.getThreadDocument());
    final XMLEventWriter sourceWriter = xmlOutputFactory.createXMLEventWriter(sourceDom);
    sourceWriter.add(xmlEventReader);
    sourceWriter.flush();
    sourceWriter.close();
    return sourceDom.getNode();
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) DOMResult(javax.xml.transform.dom.DOMResult) XMLEventWriter(javax.xml.stream.XMLEventWriter) IndentingXMLEventWriter(org.apereo.portal.xml.stream.IndentingXMLEventWriter)

Example 54 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project CFLint by cflint.

the class DefaultCFlintResultMarshaller method output.

@Override
public void output(final BugList bugList, final Writer writer, final CFLintStats stats) throws MarshallerException {
    try {
        final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        XMLStreamWriter xtw = new IndentingXMLStreamWriter(xmlOutputFactory.createXMLStreamWriter(writer));
        writeIssues(bugList, xtw, stats);
        xtw.flush();
    } catch (XMLStreamException e) {
        throw new MarshallerException(e);
    }
}
Also used : IndentingXMLStreamWriter(javanet.staxutils.IndentingXMLStreamWriter) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) MarshallerException(com.cflint.xml.MarshallerException) XMLStreamException(javax.xml.stream.XMLStreamException) IndentingXMLStreamWriter(javanet.staxutils.IndentingXMLStreamWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 55 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project oxCore by GluuFederation.

the class AuthRequest method getStreamedRequest.

public String getStreamedRequest(boolean useBase64) throws XMLStreamException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = factory.createXMLStreamWriter(baos);
    writer.writeStartElement("samlp", "AuthnRequest", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeAttribute("ID", id);
    writer.writeAttribute("Version", "2.0");
    writer.writeAttribute("IssueInstant", this.issueInstant);
    writer.writeAttribute("ProtocolBinding", "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST");
    writer.writeAttribute("AssertionConsumerServiceURL", this.samlSettings.getAssertionConsumerServiceUrl());
    writer.writeStartElement("saml", "Issuer", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeCharacters(this.samlSettings.getIssuer());
    writer.writeEndElement();
    writer.writeStartElement("samlp", "NameIDPolicy", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeAttribute("Format", this.samlSettings.getNameIdentifierFormat());
    writer.writeAttribute("AllowCreate", "true");
    writer.writeEndElement();
    writer.writeStartElement("samlp", "RequestedAuthnContext", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeAttribute("Comparison", "exact");
    writer.writeStartElement("saml", "AuthnContextClassRef", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeCharacters("urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport");
    writer.writeEndElement();
    writer.writeEndElement();
    writer.writeEndElement();
    writer.flush();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Genereated Saml Request " + new String(baos.toByteArray(), "UTF-8"));
    }
    if (useBase64) {
        byte[] deflated = CompressionHelper.deflate(baos.toByteArray(), true);
        String base64 = Base64.encodeBase64String(deflated);
        String encoded = URLEncoder.encode(base64, "UTF-8");
        return encoded;
    }
    return new String(baos.toByteArray(), "UTF-8");
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

XMLOutputFactory (javax.xml.stream.XMLOutputFactory)61 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)40 XMLStreamException (javax.xml.stream.XMLStreamException)24 StringWriter (java.io.StringWriter)23 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 Test (org.junit.Test)15 XMLEventWriter (javax.xml.stream.XMLEventWriter)12 IOException (java.io.IOException)9 DOMResult (javax.xml.transform.dom.DOMResult)7 StAXResult (javax.xml.transform.stax.StAXResult)7 StreamResult (javax.xml.transform.stream.StreamResult)6 HashMap (java.util.HashMap)5 QName (javax.xml.namespace.QName)5 Result (javax.xml.transform.Result)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 OutputStream (java.io.OutputStream)4 Marshaller (javax.xml.bind.Marshaller)4 XMLEventReader (javax.xml.stream.XMLEventReader)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)4 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)4