Example usage for javax.xml.bind JAXBElement JAXBElement

List of usage examples for javax.xml.bind JAXBElement JAXBElement

Introduction

In this page you can find the example usage for javax.xml.bind JAXBElement JAXBElement.

Prototype

public JAXBElement(QName name, Class<T> declaredType, T value) 

Source Link

Document

Construct an xml element instance.

Usage

From source file:org.atricore.idbus.capabilities.josso.test.JOSSO11WebSSORouteTest.java

protected String marshal(Class endpointInterface, Object msg, String msgQName, String msgLocalName,
        String[] userPackages) throws Exception {

    WebService ws = getWebServiceAnnotation(endpointInterface);
    JAXBContext jaxbContext = createJAXBContext(endpointInterface, userPackages);

    JAXBElement jaxbRequest = new JAXBElement(new QName(msgQName, msgLocalName), msg.getClass(), msg);
    StringWriter writer = new StringWriter();
    jaxbContext.createMarshaller().marshal(jaxbRequest, writer);

    return writer.toString();
}

From source file:org.atricore.idbus.capabilities.josso.test.JOSSO11WebSSORouteTest.java

protected String marshal(Object msg, String msgQName, String msgLocalName, String[] userPackages)
        throws Exception {

    JAXBContext jaxbContext = createJAXBContext(userPackages);

    JAXBElement jaxbRequest = new JAXBElement(new QName(msgQName, msgLocalName), msg.getClass(), msg);
    StringWriter writer = new StringWriter();
    jaxbContext.createMarshaller().marshal(jaxbRequest, writer);

    return writer.toString();
}

From source file:org.atricore.idbus.capabilities.sso.support.core.encryption.XmlSecurityEncrypterImpl.java

public EncryptedElementType encrypt(AssertionType assertion, SSOKeyResolver keyResolver)
        throws SamlR2EncrypterException {
    // Marshall the Assertion object as a DOM tree:
    if (logger.isDebugEnabled())
        logger.debug("Marshalling SAMLR2 Assertion to DOM Tree [" + assertion.getID() + "]");

    org.w3c.dom.Document doc = createNewDocument();

    ObjectFactory of = new ObjectFactory();

    JAXBElement<AssertionType> jaxbAssertion = new JAXBElement<AssertionType>(
            new QName(SAMLR2Constants.SAML_ASSERTION_NS, "Assertion"), AssertionType.class, assertion);

    marshal(jaxbAssertion, doc);/*from  w  w  w  . j a  va  2s.c om*/

    Key encriptionKey = generateDataEncryptionKey();
    EncryptedDataType encData = encryptElement(doc, encriptionKey, false);
    EncryptedKeyType encKey = encryptKey(doc, encriptionKey, keyResolver);

    EncryptedElementType eet = of.createEncryptedElementType();
    eet.setEncryptedData(encData);
    eet.getEncryptedKey().add(encKey);

    return eet;
}

From source file:org.atricore.idbus.capabilities.sso.support.core.encryption.XmlSecurityEncrypterImpl.java

public AssertionType decryptAssertion(EncryptedElementType encryptedAssertion, SSOKeyResolver keyResolver)
        throws SamlR2EncrypterException {
    org.w3c.dom.Document doc = createNewDocument();

    JAXBElement<EncryptedElementType> jaxbAssertion = new JAXBElement<EncryptedElementType>(
            new QName(SAMLR2Constants.SAML_ASSERTION_NS, "EncryptedAssertion"), EncryptedElementType.class,
            encryptedAssertion);/*from w w  w. j  a  va 2s . c  om*/

    marshal(jaxbAssertion, doc);

    Node assertionelement = decryptAssertion(doc, keyResolver);

    JAXBElement<AssertionType> assertion = (JAXBElement<AssertionType>) unmarshal(assertionelement);

    return assertion.getValue();
}

From source file:org.atricore.idbus.capabilities.sso.support.core.encryption.XmlSecurityEncrypterImpl.java

public NameIDType decryptNameID(EncryptedElementType encryptedNameID, SSOKeyResolver keyResolver)
        throws SamlR2EncrypterException {
    org.w3c.dom.Document doc = createNewDocument();

    JAXBElement<EncryptedElementType> jaxbNameID = new JAXBElement<EncryptedElementType>(
            new QName(SAMLR2Constants.SAML_ASSERTION_NS, "EncryptedID"), EncryptedElementType.class,
            encryptedNameID);//  w  ww .  ja  v a2 s.c o m

    marshal(jaxbNameID, doc);

    Node nameIDElement = decryptNameID(doc, keyResolver);

    JAXBElement<NameIDType> nameID = (JAXBElement<NameIDType>) unmarshal(nameIDElement);

    return nameID.getValue();
}

From source file:org.atricore.idbus.capabilities.sso.support.core.signature.JSR105SamlR2SignerImpl.java

public oasis.names.tc.saml._1_0.protocol.ResponseType sign(
        oasis.names.tc.saml._1_0.protocol.ResponseType response) throws SamlR2SignatureException {
    try {/* w  w w  .j  av a2s .c om*/

        // Marshall the Assertion object as a DOM tree:
        if (logger.isDebugEnabled())
            logger.debug("Marshalling SAMLR11 Response to DOM Tree [" + response.getResponseID() + "]");

        // Instantiate the document to be signed
        javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();

        // XML Signature needs to be namespace aware
        dbf.setNamespaceAware(true);

        javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();

        JAXBContext context = JAXBContext.newInstance(SAMLR11Constants.SAML_PROTOCOL_PKG,
                response.getClass().getClassLoader());

        Marshaller m = context.createMarshaller();

        Class<oasis.names.tc.saml._1_0.protocol.ResponseType> clazz = (Class<oasis.names.tc.saml._1_0.protocol.ResponseType>) response
                .getClass();

        // Remove the 'Type' suffix from the xml type name and use it as XML element!
        XmlType t = clazz.getAnnotation(XmlType.class);
        String element = t.name().substring(0, t.name().length() - 4);

        JAXBElement<oasis.names.tc.saml._1_0.protocol.ResponseType> jaxbResponse = new JAXBElement<oasis.names.tc.saml._1_0.protocol.ResponseType>(
                new QName(SAMLR11Constants.SAML_PROTOCOL_NS, element), clazz, response);

        // remove prefixes from signature elements of embedded signed assertion so that signature validation -
        // which removes those prefixes - doesn't fail
        StringWriter swrsp = new StringWriter();
        XMLStreamWriter sw = new NamespaceFilterXMLStreamWriter(swrsp);
        // TODO : Use XML Utils!!!!
        m.marshal(jaxbResponse, sw);
        sw.flush();

        Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(swrsp.toString().getBytes()));

        doc = sign(doc, response.getResponseID());

        if (logger.isDebugEnabled())
            logger.debug("Unmarshalling SAMLR11 Response from DOM Tree [" + response.getResponseID() + "]");

        // Unmarshall the assertion
        Unmarshaller u = context.createUnmarshaller();
        jaxbResponse = (JAXBElement<oasis.names.tc.saml._1_0.protocol.ResponseType>) u.unmarshal(doc);

        return jaxbResponse.getValue();
    } catch (JAXBException e) {
        throw new SamlR2SignatureException("JAXB Error signing SAMLR11 Response " + response.getResponseID(),
                e);
    } catch (ParserConfigurationException e) {
        throw new SamlR2SignatureException(
                "XML Parser Error signing SAMLR11 Response " + response.getResponseID(), e);
    } catch (XMLStreamException e) {
        throw new SamlR2SignatureException(
                "XML Parser Error signing SAMLR11 Response " + response.getResponseID(), e);
    } catch (IOException e) {
        throw new SamlR2SignatureException("I/O Error signing SAMLR11 Response " + response.getResponseID(), e);
    } catch (SAXException e) {
        throw new SamlR2SignatureException(
                "XML Parser Error signing SAMLR11 Response " + response.getResponseID(), e);
    }
}

From source file:org.atricore.idbus.capabilities.sso.support.core.util.XmlUtils.java

public static JAXBElement<RequestAbstractType> createJAXBelement(RequestAbstractType request) {

    Class<RequestAbstractType> clazz = (Class<RequestAbstractType>) request.getClass();

    // Remove the 'Type' suffix from the xml type name and use it as XML element!
    XmlType t = clazz.getAnnotation(XmlType.class);
    String element = t.name().substring(0, t.name().length() - 4);

    if (request.getClass().getPackage().getName().equals(SAMLR2Constants.SAML_IDBUS_PKG))
        return new JAXBElement<RequestAbstractType>(new QName(SAMLR2Constants.SAML_IDBUS_NS, element), clazz,
                request);/*from   w ww. j  a  v  a 2  s.co  m*/
    else
        return new JAXBElement<RequestAbstractType>(new QName(SAMLR2Constants.SAML_PROTOCOL_NS, element), clazz,
                request);
}

From source file:org.atricore.idbus.capabilities.sso.support.core.util.XmlUtils.java

public static String marshalSamlR2(Object msg, String msgQName, String msgLocalName) throws Exception {

    //JAXBContext jaxbContext = createJAXBContext(userPackages);
    JAXBContext jaxbContext = JAXBUtils.getJAXBContext(samlContextPackages, constructionType,
            samlContextPackages.toString(), XmlUtils.class.getClassLoader(), new HashMap<String, Object>());
    Marshaller m = JAXBUtils.getJAXBMarshaller(jaxbContext);

    JAXBElement jaxbRequest = new JAXBElement(new QName(msgQName, msgLocalName), msg.getClass(), msg);

    Writer writer = new StringWriter();
    XMLStreamWriter xmlStreamWriter = new NamespaceFilterXMLStreamWriter(writer);

    // Support XMLDsig

    // TODO : What about non-sun XML Bind stacks!
    m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() {

        @Override//from  ww  w .  j  av a 2 s.  co m
        public String[] getPreDeclaredNamespaceUris() {
            return new String[] { SAMLR2Constants.SAML_PROTOCOL_NS, SAMLR2Constants.SAML_ASSERTION_NS,
                    "http://www.w3.org/2000/09/xmldsig#", "http://www.w3.org/2001/04/xmlenc#",
                    "http://www.w3.org/2001/XMLSchema" };
        }

        @Override
        public String getPreferredPrefix(String nsUri, String suggestion, boolean requirePrefix) {

            if (nsUri.equals(SAMLR2Constants.SAML_PROTOCOL_NS))
                return "samlp";
            else if (nsUri.equals(SAMLR2Constants.SAML_ASSERTION_NS))
                return "saml";
            else if (nsUri.equals("http://www.w3.org/2000/09/xmldsig#"))
                return "ds";
            else if (nsUri.equals("http://www.w3.org/2001/04/xmlenc#"))
                return "enc";
            else if (nsUri.equals("http://www.w3.org/2001/XMLSchema"))
                return "xsd";

            return suggestion;
        }
    });

    m.marshal(jaxbRequest, xmlStreamWriter);
    xmlStreamWriter.flush();
    JAXBUtils.releaseJAXBMarshaller(jaxbContext, m);

    return writer.toString();
}

From source file:org.atricore.idbus.capabilities.sso.support.core.util.XmlUtils.java

public static String marshal(Object msg, final String msgQName, final String msgLocalName,
        String[] userPackages) throws Exception {

    TreeSet<String> contextPackages = new TreeSet<String>();
    for (int i = 0; i < userPackages.length; i++) {
        String userPackage = userPackages[i];
        contextPackages.add(userPackage);
    }/*w  w w. jav a  2  s  .c o m*/

    JAXBContext jaxbContext = JAXBUtils.getJAXBContext(contextPackages, constructionType,
            contextPackages.toString(), XmlUtils.class.getClassLoader(), new HashMap<String, Object>());
    Marshaller marshaller = JAXBUtils.getJAXBMarshaller(jaxbContext);

    JAXBElement jaxbRequest = new JAXBElement(new QName(msgQName, msgLocalName), msg.getClass(), msg);

    Writer writer = new StringWriter();

    // Support XMLDsig
    XMLEventWriter xmlWriter = staxOF.createXMLEventWriter(writer);
    marshaller.marshal(jaxbRequest, xmlWriter);
    xmlWriter.flush();
    JAXBUtils.releaseJAXBMarshaller(jaxbContext, marshaller);

    return writer.toString();

}

From source file:org.atricore.idbus.capabilities.sso.support.core.util.XmlUtils.java

public static Document marshalSamlR2AsDom(Object msg, String msgQName, String msgLocalName,
        String[] userPackages) throws Exception {

    // JAXB Element
    JAXBElement jaxbMsg = new JAXBElement(new QName(msgQName, msgLocalName), msg.getClass(), msg);

    JAXBContext jaxbContext = JAXBUtils.getJAXBContext(samlContextPackages, constructionType,
            samlContextPackages.toString(), XmlUtils.class.getClassLoader(), new HashMap<String, Object>());
    Marshaller marshaller = JAXBUtils.getJAXBMarshaller(jaxbContext);

    // Marshal as string and then parse with DOM ...
    StringWriter writer = new StringWriter();
    XMLStreamWriter xmlStreamWriter = new NamespaceFilterXMLStreamWriter(writer);

    // TODO : What about non-sun XML Bind stacks!

    marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() {

        @Override//from w w w .  j ava 2s.  c o  m
        public String[] getPreDeclaredNamespaceUris() {
            return new String[] { SAMLR2Constants.SAML_PROTOCOL_NS, SAMLR2Constants.SAML_ASSERTION_NS,
                    "http://www.w3.org/2000/09/xmldsig#", "http://www.w3.org/2001/04/xmlenc#",
                    "http://www.w3.org/2001/XMLSchema" };
        }

        @Override
        public String getPreferredPrefix(String nsUri, String suggestion, boolean requirePrefix) {

            if (nsUri.equals(SAMLR2Constants.SAML_PROTOCOL_NS))
                return "samlp";
            else if (nsUri.equals(SAMLR2Constants.SAML_ASSERTION_NS))
                return "saml";
            else if (nsUri.equals("http://www.w3.org/2000/09/xmldsig#"))
                return "ds";
            else if (nsUri.equals("http://www.w3.org/2001/04/xmlenc#"))
                return "enc";
            else if (nsUri.equals("http://www.w3.org/2001/XMLSchema"))
                return "xsd";

            return suggestion;
        }
    });

    marshaller.marshal(jaxbMsg, xmlStreamWriter);
    xmlStreamWriter.flush();

    // Instantiate the document to be signed
    javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();

    // XML Signature needs to be namespace aware
    dbf.setNamespaceAware(true);

    Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(writer.toString().getBytes()));

    JAXBUtils.releaseJAXBMarshaller(jaxbContext, marshaller);
    return doc;

}