Search in sources :

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

Example 37 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project tomee by apache.

the class Sxc method marshal.

public static void marshal(final JAXBObject objectType, final Object object, final Result result) throws JAXBException {
    if (result == null)
        throw new IllegalArgumentException("result is null");
    if (!(result instanceof StreamResult))
        throw new IllegalArgumentException("result is null");
    if (object == null)
        throw new IllegalArgumentException("object is null");
    if (objectType == null)
        throw new IllegalArgumentException("jaxbObject is null");
    final StreamResult streamResult = (StreamResult) result;
    XMLStreamWriter writer = null;
    try {
        final XMLOutputFactory xof = getXmOutputFactory();
        writer = xof.createXMLStreamWriter(streamResult.getOutputStream(), "UTF-8");
        writer = new PrettyPrintXMLStreamWriter(writer);
        final XoXMLStreamWriter w = new XoXMLStreamWriterImpl(writer);
        try {
            w.writeStartDocument("UTF-8", null);
            // write xsi:type if there is no default root element for this type
            final RuntimeContext context = new RuntimeContext((ExtendedMarshaller) null);
            try {
                final QName name = objectType.getXmlRootElement();
                // open element
                w.writeStartElementWithAutoPrefix(name.getNamespaceURI(), name.getLocalPart());
                objectType.write(w, object, context);
                w.writeEndElement();
            } catch (Exception e) {
                if (e instanceof JAXBException) {
                    // assume event handler has already been notified
                    throw (JAXBException) e;
                }
                if (e instanceof RuntimeXMLStreamException) {
                    // simply unwrap and handle below
                    e = ((RuntimeXMLStreamException) e).getCause();
                }
                if (e instanceof XMLStreamException) {
                    final Throwable cause = e.getCause();
                    if (cause instanceof JAXBException) {
                        throw (JAXBException) e;
                    }
                    throw new MarshalException(cause == null ? e : cause);
                }
                throw new MarshalException(e);
            }
            w.writeEndDocument();
        } catch (final Exception e) {
            throw new MarshalException(e);
        }
    } catch (final XMLStreamException e) {
        throw new JAXBException("Could not close XMLStreamWriter.", e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (final XMLStreamException ignored) {
            }
        }
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) MarshalException(javax.xml.bind.MarshalException) StreamResult(javax.xml.transform.stream.StreamResult) QName(javax.xml.namespace.QName) JAXBException(javax.xml.bind.JAXBException) RuntimeXMLStreamException(org.metatype.sxc.util.RuntimeXMLStreamException) MarshalException(javax.xml.bind.MarshalException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) XoXMLStreamWriterImpl(org.metatype.sxc.util.XoXMLStreamWriterImpl) PrettyPrintXMLStreamWriter(org.metatype.sxc.util.PrettyPrintXMLStreamWriter) RuntimeXMLStreamException(org.metatype.sxc.util.RuntimeXMLStreamException) XoXMLStreamWriter(org.metatype.sxc.util.XoXMLStreamWriter) RuntimeXMLStreamException(org.metatype.sxc.util.RuntimeXMLStreamException) XMLStreamException(javax.xml.stream.XMLStreamException) XoXMLStreamWriter(org.metatype.sxc.util.XoXMLStreamWriter) PrettyPrintXMLStreamWriter(org.metatype.sxc.util.PrettyPrintXMLStreamWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) RuntimeContext(org.metatype.sxc.jaxb.RuntimeContext)

Example 38 with XMLOutputFactory

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

the class TestCreateXMLStreamWriterThreadSafety method runTest.

@SuppressWarnings("deprecation")
protected void runTest() throws Throwable {
    final XMLOutputFactory factory = staxImpl.getDialect().makeThreadSafe(staxImpl.newNormalizedXMLOutputFactory());
    ConcurrentTestUtils.testThreadSafety(new Action() {

        public void execute() throws Exception {
            String text = String.valueOf((int) (Math.random() * 10000));
            StringWriter out = new StringWriter();
            XMLStreamWriter writer = factory.createXMLStreamWriter(out);
            writer.writeStartElement("root");
            writer.writeCharacters(text);
            writer.writeEndElement();
            writer.writeEndDocument();
            writer.flush();
            writer.close();
            assertEquals("<root>" + text + "</root>", out.toString());
        }
    });
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) Action(org.apache.axiom.testutils.concurrent.Action) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 39 with XMLOutputFactory

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

the class TestCreateXMLStreamWriterWithNullEncoding method runTest.

protected void runTest() throws Throwable {
    XMLOutputFactory factory = staxImpl.newNormalizedXMLOutputFactory();
    // This should cause an exception
    try {
        factory.createXMLStreamWriter(System.out, null);
    } catch (Throwable ex) {
        // Expected
        return;
    }
    // Attention here: since the fail method works by throwing an exception and we
    // catch Throwable, it must be invoked outside of the catch block!
    fail("Expected createXMLStreamWriter to throw an exception");
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory)

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

Aggregations

XMLOutputFactory (javax.xml.stream.XMLOutputFactory)42 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)28 StringWriter (java.io.StringWriter)18 XMLStreamException (javax.xml.stream.XMLStreamException)15 XMLEventWriter (javax.xml.stream.XMLEventWriter)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Test (org.junit.Test)8 StAXResult (javax.xml.transform.stax.StAXResult)7 IOException (java.io.IOException)6 DOMResult (javax.xml.transform.dom.DOMResult)6 StreamResult (javax.xml.transform.stream.StreamResult)6 Result (javax.xml.transform.Result)5 PcrManifest (com.intel.mountwilson.manifest.data.PcrManifest)3 OutputStream (java.io.OutputStream)3 SAXResult (javax.xml.transform.sax.SAXResult)3 IManifest (com.intel.mountwilson.manifest.data.IManifest)2 Headers (com.sun.net.httpserver.Headers)2 OutputStreamWriter (java.io.OutputStreamWriter)2 InetSocketAddress (java.net.InetSocketAddress)2 Path (java.nio.file.Path)2