Search in sources :

Example 56 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter 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 57 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project midpoint by Evolveum.

the class CustomWrappedOutInterceptor method writeParts.

@Override
protected void writeParts(Message message, Exchange exchange, BindingOperationInfo operation, MessageContentsList objs, List<MessagePartInfo> parts) {
    // TODO Auto-generated method stub
    OutputStream out = message.getContent(OutputStream.class);
    XMLStreamWriter origXmlWriter = message.getContent(XMLStreamWriter.class);
    Service service = exchange.getService();
    XMLStreamWriter xmlWriter = origXmlWriter;
    CachingXmlEventWriter cache = null;
    Object en = message.getContextualProperty(OUT_BUFFERING);
    boolean allowBuffer = true;
    boolean buffer = false;
    if (en != null) {
        buffer = Boolean.TRUE.equals(en) || "true".equals(en);
        allowBuffer = !(Boolean.FALSE.equals(en) || "false".equals(en));
    }
    // need to cache the events in case validation fails or buffering is enabled
    if (buffer || (allowBuffer && shouldValidate(message) && !isRequestor(message))) {
        cache = new CachingXmlEventWriter();
        try {
            cache.setNamespaceContext(origXmlWriter.getNamespaceContext());
        } catch (XMLStreamException e) {
        //ignorable, will just get extra namespace decls
        }
        xmlWriter = cache;
        out = null;
    }
    if (out != null && writeToOutputStream(message, operation.getBinding(), service) && !MessageUtils.isTrue(message.getContextualProperty(DISABLE_OUTPUTSTREAM_OPTIMIZATION))) {
        if (xmlWriter != null) {
            try {
                xmlWriter.writeCharacters("");
                xmlWriter.flush();
            } catch (XMLStreamException e) {
                throw new Fault(e);
            }
        }
        DataWriter<OutputStream> osWriter = getDataWriter(message, service, OutputStream.class);
        for (MessagePartInfo part : parts) {
            if (objs.hasValue(part)) {
                Object o = objs.get(part);
                osWriter.write(o, part, out);
            }
        }
    } else {
        DataWriter<XMLStreamWriter> dataWriter = new CustomDataWriter(prismContext);
        for (MessagePartInfo part : parts) {
            if (objs.hasValue(part)) {
                Object o = objs.get(part);
                dataWriter.write(o, part, xmlWriter);
            }
        }
    }
    if (cache != null) {
        try {
            for (XMLEvent event : cache.getEvents()) {
                StaxUtils.writeEvent(event, origXmlWriter);
            }
        } catch (XMLStreamException e) {
            throw new Fault(e);
        }
    }
}
Also used : OutputStream(java.io.OutputStream) Service(org.apache.cxf.service.Service) Fault(org.apache.cxf.interceptor.Fault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) CachingXmlEventWriter(org.apache.cxf.staxutils.CachingXmlEventWriter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 58 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project voltdb by VoltDB.

the class JDBCSQLXML method createSAXResult.

/**
     *  Retrieves a new SAXResult for setting the XML value designated by this
     *  SQLXML instance.
     *
     *  @param resultClass The class of the result, or null.
     *  @throws java.sql.SQLException if there is an error processing the XML
     *          value
     *  @return for setting the XML value designated by this SQLXML instance.
     */
@SuppressWarnings("unchecked")
protected <T extends Result> T createSAXResult(Class<T> resultClass) throws SQLException {
    SAXResult result = null;
    try {
        result = (resultClass == null) ? new SAXResult() : (SAXResult) resultClass.newInstance();
    } catch (SecurityException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (InstantiationException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (IllegalAccessException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (ClassCastException ex) {
        throw Exceptions.resultInstantiation(ex);
    }
    StAXResult staxResult = createStAXResult(null);
    XMLStreamWriter xmlWriter = staxResult.getXMLStreamWriter();
    SAX2XMLStreamWriter handler = new SAX2XMLStreamWriter(xmlWriter);
    result.setHandler(handler);
    return (T) result;
}
Also used : StAXResult(javax.xml.transform.stax.StAXResult) SAXResult(javax.xml.transform.sax.SAXResult) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 59 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project aries by apache.

the class PersistenceContextHandler method handleFieldAnnotation.

@Override
public void handleFieldAnnotation(Class<?> clazz, List<Field> fields, ContextEnricher contextEnricher, BeanEnricher beanEnricher) {
    final String nsJpa1 = Namespaces.getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), Namespaces.PATTERN_NS_JPA1);
    if (nsJpa1 != null) {
        for (final Field field : fields) {
            final String name = field.getName();
            final PersistenceContext persistenceContext = field.getAnnotation(PersistenceContext.class);
            beanEnricher.addBeanContentWriter("javax.persistence.field.context/" + name, new XmlWriter() {

                @Override
                public void write(XMLStreamWriter writer) throws XMLStreamException {
                    writer.writeEmptyElement("context");
                    writer.writeDefaultNamespace(nsJpa1);
                    writer.writeAttribute("unitname", persistenceContext.unitName());
                    writer.writeAttribute("property", name);
                }
            });
        }
    }
    final String nsJpa2 = Namespaces.getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), Namespaces.PATTERN_NS_JPA2);
    if (nsJpa2 != null) {
        contextEnricher.addBlueprintContentWriter("javax.persistence.enableJpa2", new XmlWriter() {

            @Override
            public void write(XMLStreamWriter writer) throws XMLStreamException {
                writer.writeEmptyElement("enable");
                writer.writeDefaultNamespace(nsJpa2);
            }
        });
    }
}
Also used : Field(java.lang.reflect.Field) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) PersistenceContext(javax.persistence.PersistenceContext) XmlWriter(org.apache.aries.blueprint.plugin.spi.XmlWriter)

Example 60 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project aries by apache.

the class PersistenceUnitHandler method handleFieldAnnotation.

@Override
public void handleFieldAnnotation(Class<?> clazz, List<Field> fields, ContextEnricher contextEnricher, BeanEnricher beanEnricher) {
    final String nsJpa1 = getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), PATTERN_NS_JPA1);
    if (nsJpa1 != null) {
        for (final Field field : fields) {
            final String name = field.getName();
            final PersistenceUnit persistenceUnit = field.getAnnotation(PersistenceUnit.class);
            beanEnricher.addBeanContentWriter("javax.persistence.field.unit/" + name, new XmlWriter() {

                @Override
                public void write(XMLStreamWriter writer) throws XMLStreamException {
                    writer.writeEmptyElement("unit");
                    writer.writeDefaultNamespace(nsJpa1);
                    writer.writeAttribute("unitname", persistenceUnit.unitName());
                    writer.writeAttribute("property", name);
                }
            });
        }
    }
    final String nsJpa2 = getNamespaceByPattern(contextEnricher.getBlueprintConfiguration().getNamespaces(), PATTERN_NS_JPA2);
    if (nsJpa2 != null) {
        contextEnricher.addBlueprintContentWriter("javax.persistence.enableJpa2", new XmlWriter() {

            @Override
            public void write(XMLStreamWriter writer) throws XMLStreamException {
                writer.writeEmptyElement("enable");
                writer.writeDefaultNamespace(nsJpa2);
            }
        });
    }
}
Also used : Field(java.lang.reflect.Field) PersistenceUnit(javax.persistence.PersistenceUnit) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XmlWriter(org.apache.aries.blueprint.plugin.spi.XmlWriter)

Aggregations

XMLStreamWriter (javax.xml.stream.XMLStreamWriter)82 XMLStreamException (javax.xml.stream.XMLStreamException)36 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)28 StringWriter (java.io.StringWriter)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 IOException (java.io.IOException)12 XmlWriter (org.apache.aries.blueprint.plugin.spi.XmlWriter)7 OutputStream (java.io.OutputStream)6 StAXResult (javax.xml.transform.stax.StAXResult)6 Test (org.junit.Test)6 OutputStreamWriter (java.io.OutputStreamWriter)5 JAXBException (javax.xml.bind.JAXBException)5 StreamResult (javax.xml.transform.stream.StreamResult)5 ZipEntry (java.util.zip.ZipEntry)4 XMLStreamReader (javax.xml.stream.XMLStreamReader)4 PcrManifest (com.intel.mountwilson.manifest.data.PcrManifest)3 StringReader (java.io.StringReader)3 ToXmlGenerator (com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator)2 IManifest (com.intel.mountwilson.manifest.data.IManifest)2 Headers (com.sun.net.httpserver.Headers)2