Example usage for javax.xml.stream XMLStreamWriter writeStartElement

List of usage examples for javax.xml.stream XMLStreamWriter writeStartElement

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamWriter writeStartElement.

Prototype

public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException;

Source Link

Document

Writes a start tag to the output

Usage

From source file:org.asimba.wa.integrationtest.saml2.model.Assertion.java

public void writeSubject(XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("saml2", "Subject", "urn:oasis:names:tc:SAML:2.0:assertion");

    writer.writeStartElement("saml2", "NameID", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeAttribute("Format", getSubjectNameIdFormat());
    writer.writeAttribute("NameQualifier", getIssuer());
    writer.writeCharacters(getSubjectNameId());
    writer.writeEndElement(); // </NameID>

    // TODO: Consider whether SubjectConfirmation should also be included or not

    writer.writeEndElement(); // </Subject>
}

From source file:org.asimba.wa.integrationtest.saml2.model.Assertion.java

public void writeConditions(XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("saml2", "Conditions", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeAttribute("NotAfter", getCondNotAfter());
    writer.writeAttribute("NotBefore", getCondNotBefore());

    writer.writeStartElement("saml2", "AudienceRestriction", "urn:oasis:names:tc:SAML:2.0:assertion");

    writer.writeStartElement("saml2", "Audience", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeCharacters(getAudience());
    writer.writeEndElement(); // </Audience>

    writer.writeEndElement(); // </AudienceRestriction>

    writer.writeEndElement(); // </Conditions>
}

From source file:org.asimba.wa.integrationtest.saml2.model.Assertion.java

public void writeAuthnStatement(XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("saml2", "AuthnStatement", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeAttribute("AuthnInstant", getIssueInstant());
    writer.writeAttribute("SessionIndex", getSessionIndex());
    writer.writeAttribute("SessionNotOnOrAfter", getSessionNotOnOrAfter());

    writer.writeStartElement("saml2", "AuthnContext", "urn:oasis:names:tc:SAML:2.0:assertion");

    writer.writeStartElement("saml2", "AuthnContextClassRef", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeCharacters(getAuthnContextClassRef());
    writer.writeEndElement(); // </AuthnContextClassRef>

    writer.writeEndElement(); // </AuthnContext>

    writer.writeEndElement(); // </AuthnStatement>
}

From source file:org.asimba.wa.integrationtest.saml2.model.Assertion.java

public void writeAttribute(XMLStreamWriter writer, String key, String value) throws XMLStreamException {
    writer.writeStartElement("saml2", "Attribute", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeAttribute("Name", key);
    writer.writeAttribute("NameFormat", "urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified");

    writer.writeStartElement("saml2", "AttributeValue", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    writer.writeAttribute("xsi", "http://www.w3.org/2001/XMLSchema-instance", "type", "xs:string");
    writer.writeCharacters(value);//from w w w. j av  a2  s. c o m
    writer.writeEndElement(); // </AttributeValue>

    writer.writeEndElement(); // </Attribute>
}

From source file:org.asimba.wa.integrationtest.saml2.model.Assertion.java

public void writeAttributeStatement(XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("saml2", "AttributeStatement", "urn:oasis:names:tc:SAML:2.0:assertion");

    // Write each Attribute
    for (Entry<String, String> attribute : getAttributes().entrySet()) {
        writeAttribute(writer, attribute.getKey(), attribute.getValue());
    }/*from   ww  w.jav a 2 s .c o  m*/

    writer.writeEndElement(); // </AttributeStatement>
}

From source file:org.asimba.wa.integrationtest.saml2.model.Assertion.java

public void writeAssertion(XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("saml2", "Assertion", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeNamespace("saml2", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeAttribute("ID", getId());
    writer.writeAttribute("IssueInstant", getIssueInstant());
    writer.writeAttribute("Version", "2.0");

    writeIssuer(writer);//from   w  w w .ja va2 s  . c o  m

    writeSubject(writer);

    writeConditions(writer);

    writeAuthnStatement(writer);

    writeAttributeStatement(writer);

    writer.writeEndElement(); // </Assertion>

}

From source file:org.asimba.wa.integrationtest.saml2.model.AuthnRequest.java

/**
 * Get String with the SAML2 AuthnRequest message
 * @param format -1=plain, 1=base64//  www .  j  a  va 2  s.co  m
 * @return
 * @throws XMLStreamException
 * @throws IOException
 */
public String getRequest(int format) throws XMLStreamException, IOException {
    _logger.info("For ID: " + this._id);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION, true);
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(baos, compresser);
    StringWriter sw = new StringWriter();

    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = null;

    // ugly but effective:
    if (format == base64) {
        writer = factory.createXMLStreamWriter(deflaterOutputStream);
    } else {
        writer = factory.createXMLStreamWriter(sw);
    }

    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", _acsUrl);

    writeIssuer(writer);

    writeNameIDPolicy(writer);

    writeRequestedAuthnContext(writer);

    writer.writeEndElement();
    writer.flush();

    if (format == base64) {
        deflaterOutputStream.close();
        byte[] bain = baos.toByteArray();
        byte[] encoded = Base64.encodeBase64(bain, false);
        String result = new String(encoded, Charset.forName("UTF-8"));

        return result;
    } else {
        return sw.toString();
    }

}

From source file:org.asimba.wa.integrationtest.saml2.model.AuthnRequest.java

protected void writeIssuer(XMLStreamWriter writer) throws XMLStreamException {
    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(_issuer);//w w  w  .  j  ava2 s  .  co  m
    writer.writeEndElement();
}

From source file:org.asimba.wa.integrationtest.saml2.model.AuthnRequest.java

protected void writeNameIDPolicy(XMLStreamWriter writer) throws XMLStreamException {
    if (_requestedNameIdFormat == null) {
        _logger.info("Skipping NameIDPolicy in request");
        return;/*  w ww  . j ava 2s .c o m*/
    }

    _logger.info("Adding {} as NameIDPolicy@Format", _requestedNameIdFormat);

    writer.writeStartElement("samlp", "NameIDPolicy", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeAttribute("Format", _requestedNameIdFormat);
    writer.writeAttribute("AllowCreate", "true");
    writer.writeEndElement();
}

From source file:org.asimba.wa.integrationtest.saml2.model.AuthnRequest.java

protected void writeRequestedAuthnContext(XMLStreamWriter writer) throws XMLStreamException {
    if (_requestedAuthnContext == null) {
        _logger.info("Skipping RequestedAuthnContext in request");
        return;/* ww w . ja v a  2 s.  c  o  m*/
    }

    _logger.info("Adding {} as RequestedAuthnContext@AuthnContextClassRef", _requestedAuthnContext);

    writer.writeStartElement("samlp", "RequestedAuthnContext", "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(_requestedAuthnContext);
    writer.writeEndElement();

    writer.writeEndElement();
}