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");
}
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) {
}
}
}
}
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());
}
});
}
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");
}
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());
}
Aggregations