Java源码示例:org.opensaml.xml.io.Marshaller

示例1
/**
 * Serialize XML objects
 *
 * @param xmlObject : XACML or SAML objects to be serialized
 * @return serialized XACML or SAML objects
 */
private String marshall(XMLObject xmlObject) throws EntitlementProxyException {

    try {
        doBootstrap();
        System.setProperty(DOCUMENT_BUILDER_FACTORY, DOCUMENT_BUILDER_FACTORY_IMPL);

        MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
        Element element = marshaller.marshall(xmlObject);

        ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream();
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(byteArrayOutputStrm);
        writer.write(element, output);
        return new String(byteArrayOutputStrm.toByteArray(), Charset.forName("UTF-8"));
    } catch (Exception e) {
        log.error("Error Serializing the SAML Response");
        throw new EntitlementProxyException("Error Serializing the SAML Response", e);
    }
}
 
示例2
/**
 * Helper method that marshalls the given message.
 * 
 * @param message message the marshall and serialize
 * 
 * @return marshalled message
 * 
 * @throws MessageEncodingException thrown if the give message can not be marshalled into its DOM representation
 */
protected Element marshallMessage(XMLObject message) throws MessageEncodingException {
    log.debug("Marshalling message");

    try {
        Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(message);
        if (marshaller == null) {
            log.error("Unable to marshall message, no marshaller registered for message object: "
                    + message.getElementQName());
            throw new MessageEncodingException(
                    "Unable to marshall message, no marshaller registered for message object: "
                    + message.getElementQName());
        }
        Element messageElem = marshaller.marshall(message);
        if (log.isTraceEnabled()) {
            log.trace("Marshalled message into DOM:\n{}", XMLHelper.nodeToString(messageElem));
        }
        return messageElem;
    } catch (MarshallingException e) {
        log.error("Encountered error marshalling message to its DOM representation", e);
        throw new MessageEncodingException("Encountered error marshalling message into its DOM representation", e);
    }
}
 
示例3
/**
 * Log the decoded message to the protocol message logger.
 * 
 * @param messageContext the message context to process
 */
protected void logDecodedMessage(MessageContext messageContext) {
    if(protocolMessageLog.isDebugEnabled() && messageContext.getInboundMessage() != null){
        if (messageContext.getInboundMessage().getDOM() == null) {
            XMLObject message = messageContext.getInboundMessage();
            Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(message);
            if (marshaller != null) {
                try {
                    marshaller.marshall(message);
                } catch (MarshallingException e) {
                    log.error("Unable to marshall message for logging purposes: " + e.getMessage());
                }
            }
            else {
                log.error("Unable to marshall message for logging purposes, no marshaller registered for message object: "
                        + message.getElementQName());
            }
            if (message.getDOM() == null) {
                return;
            }
        }
        protocolMessageLog.debug("\n" + XMLHelper.prettyPrintXML(messageContext.getInboundMessage().getDOM()));
    }
}
 
示例4
/**
 * Creates the request entity that makes up the POST message body.
 * 
 * @param message message to be sent
 * @param charset character set used for the message
 * 
 * @return request entity that makes up the POST message body
 * 
 * @throws SOAPClientException thrown if the message could not be marshalled
 */
protected RequestEntity createRequestEntity(Envelope message, Charset charset) throws SOAPClientException {
    try {
        Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(message);
        ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(arrayOut, charset);

        if (log.isDebugEnabled()) {
            log.debug("Outbound SOAP message is:\n" + XMLHelper.prettyPrintXML(marshaller.marshall(message)));
        }
        XMLHelper.writeNode(marshaller.marshall(message), writer);
        return new ByteArrayRequestEntity(arrayOut.toByteArray(), "text/xml");
    } catch (MarshallingException e) {
        throw new SOAPClientException("Unable to marshall SOAP envelope", e);
    }
}
 
示例5
/**
 * Ensure that the XMLObject is marshalled.
 * 
 * @param xmlObject the object to check and marshall
 * @throws DecryptionException thrown if there is an error when marshalling the XMLObject
 */
protected void checkAndMarshall(XMLObject xmlObject) throws DecryptionException {
    Element targetElement = xmlObject.getDOM();
    if (targetElement == null) {
        Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(xmlObject);
        if (marshaller == null) {
            marshaller =
                    Configuration.getMarshallerFactory().getMarshaller(Configuration.getDefaultProviderQName());
            if (marshaller == null) {
                String errorMsg = "No marshaller available for " + xmlObject.getElementQName();
                log.error(errorMsg);
                throw new DecryptionException(errorMsg);
            }
        }
        try {
            targetElement = marshaller.marshall(xmlObject);
        } catch (MarshallingException e) {
            log.error("Error marshalling target XMLObject", e);
            throw new DecryptionException("Error marshalling target XMLObject", e);
        }
    }
}
 
示例6
/**
 * Marshall an XMLObject.  If the XMLObject already has a cached DOM via {@link XMLObject#getDOM()},
 * that Element will be returned.  Otherwise the object will be fully marshalled and that Element returned.
 * 
 * @param xmlObject the XMLObject to marshall
 * @return the marshalled Element
 * @throws MarshallingException if there is a problem marshalling the XMLObject
 */
public static Element marshall(XMLObject xmlObject) throws MarshallingException {
    Logger log = getLogger();
    log.debug("Marshalling XMLObject");
    
    if (xmlObject.getDOM() != null) {
        log.debug("XMLObject already had cached DOM, returning that element");
        return xmlObject.getDOM();
    }

    Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(xmlObject);
    if (marshaller == null) {
        log.error("Unable to marshall XMLOBject, no marshaller registered for object: "
                + xmlObject.getElementQName());
    }
    
    Element messageElem = marshaller.marshall(xmlObject);
    
    if (log.isTraceEnabled()) {
        log.trace("Marshalled XMLObject into DOM:");
        log.trace(XMLHelper.nodeToString(messageElem));
    }
    
    return messageElem;
}
 
示例7
public Document marshallerMetadata(EntityDescriptor entityDescriptor) {
	Document document = null;
	try {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

		DocumentBuilder documentBuilder = factory.newDocumentBuilder();

		document = documentBuilder.newDocument();

		Marshaller marshaller = marshallerFactory.getMarshaller(entityDescriptor);
		marshaller.marshall(entityDescriptor, document);
	} catch (Exception e) {
		e.printStackTrace();
	}

	return document;

}
 
示例8
public static Element marshallerElement( XMLObject xmlObject) {
    Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
    if(marshaller == null){
        logger.error("Unable to locate marshaller for " + xmlObject.getElementQName() + " can not perform equality check assertion");
    }
    
    Element generatedDOM=null;
    try {
        generatedDOM = marshaller.marshall(xmlObject, parser.newDocument());
        if(logger.isDebugEnabled()) {
     	   logger.debug("Marshalled DOM was " + XMLHelper.nodeToString(generatedDOM));
        }
       // assertXMLEqual(failMessage, expectedDOM, generatedDOM.getOwnerDocument());
    } catch (Exception e) {
 	   logger.error("Marshalling failed with the following error:", e);
    }
    return generatedDOM;
}
 
示例9
/**
 * `
 * Serialize XML objects
 *
 * @param xmlObject : XACML or SAML objects to be serialized
 * @return serialized XACML or SAML objects
 * @throws EntitlementException
 */
private String marshall(XMLObject xmlObject) throws EntitlementException {

    try {
        doBootstrap();
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

        MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
        Element element = marshaller.marshall(xmlObject);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl =
                (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(byteArrayOutputStream);
        writer.write(element, output);
        return byteArrayOutputStream.toString();
    } catch (Exception e) {
        log.error("Error Serializing the SAML Response");
        throw new EntitlementException("Error Serializing the SAML Response", e);
    }
}
 
示例10
/**
 * Serializing a SAML2 object into a String
 *
 * @param xmlObject object that needs to serialized.
 * @return serialized object
 * @throws SAMLSSOException
 */
public static String marshall(XMLObject xmlObject) throws SAMLSSOException {
    try {

        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

        MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration
                .getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
        Element element = marshaller.marshall(xmlObject);

        ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream();
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(byteArrayOutputStrm);
        writer.write(element, output);
        return byteArrayOutputStrm.toString();
    } catch (Exception e) {
        log.error("Error Serializing the SAML Response");
        throw new SAMLSSOException("Error Serializing the SAML Response", e);
    }
}
 
示例11
/**
 * Serializing a SAML2 object into a String
 *
 * @param xmlObject object that needs to serialized.
 * @return serialized object
 * @throws SAML2SSOUIAuthenticatorException
 */
public static String marshall(XMLObject xmlObject) throws SAML2SSOUIAuthenticatorException {

    try {
        doBootstrap();
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

        MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration
                .getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
        Element element = marshaller.marshall(xmlObject);

        ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream();
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(byteArrayOutputStrm);
        writer.write(element, output);
        return byteArrayOutputStrm.toString();
    } catch (Exception e) {
        log.error("Error Serializing the SAML Response");
        throw new SAML2SSOUIAuthenticatorException("Error Serializing the SAML Response", e);
    }
}
 
示例12
private static String marshall(XMLObject xmlObject) throws org.wso2.carbon.identity.base.IdentityException {
    try {
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

        MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
        Element element = marshaller.marshall(xmlObject);

        ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream();
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl =
                (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(byteArrayOutputStrm);
        writer.write(element, output);
        return byteArrayOutputStrm.toString("UTF-8");
    } catch (Exception e) {
        log.error("Error Serializing the SAML Response");
        throw IdentityException.error("Error Serializing the SAML Response", e);
    }
}
 
示例13
/**
 * Sign the SAML AuthnRequest message
 *
 * @param logoutRequest
 * @param signatureAlgorithm
 * @param cred
 * @return
 * @throws SSOAgentException
 */
public static LogoutRequest setSignature(LogoutRequest logoutRequest, String signatureAlgorithm,
                                         X509Credential cred) throws SSOAgentException {
    try {
        Signature signature = setSignatureRaw(signatureAlgorithm,cred);

        logoutRequest.setSignature(signature);

        List<Signature> signatureList = new ArrayList<Signature>();
        signatureList.add(signature);

        // Marshall and Sign
        MarshallerFactory marshallerFactory =
                org.opensaml.xml.Configuration.getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(logoutRequest);

        marshaller.marshall(logoutRequest);

        org.apache.xml.security.Init.init();
        Signer.signObjects(signatureList);
        return logoutRequest;

    } catch (Exception e) {
        throw new SSOAgentException("Error while signing the Logout Request message", e);
    }
}
 
示例14
/**
 * Serializing a SAML2 object into a String
 *
 * @param xmlObject object that needs to serialized.
 * @return serialized object
 * @throws Exception
 */
public static String marshall(XMLObject xmlObject) throws Exception {
    try {
        doBootstrap();
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                           "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

        MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
        Element element = marshaller.marshall(xmlObject);

        ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream();
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl =
                (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(byteArrayOutputStrm);
        writer.write(element, output);
        return byteArrayOutputStrm.toString();
    } catch (Exception e) {
        throw new Exception("Error Serializing the SAML Response", e);
    }
}
 
示例15
public static String encodeSAMLRequest(XMLObject authnRequest)
        throws MarshallingException, IOException {
    Marshaller marshaller = Configuration.getMarshallerFactory()
            .getMarshaller(authnRequest);
    Element authDOM = marshaller.marshall(authnRequest);
    StringWriter requestWriter = new StringWriter();
    XMLHelper.writeNode(authDOM, requestWriter);
    String requestMessage = requestWriter.toString();
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
    deflaterOutputStream.write(requestMessage.getBytes(Charset.forName("UTF-8")));
    deflaterOutputStream.close();
    String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES);
    encodedRequestMessage = URLEncoder.encode(encodedRequestMessage, HttpUtils.UTF_8).trim();
    return encodedRequestMessage;
}
 
示例16
/**
 * Get a KeyInfo marshaller.
 * 
 * @return a KeyInfo marshaller
 * @throws SecurityException thrown if there is an error obtaining the marshaller from the configuration
 */
private Marshaller getMarshaller() throws SecurityException {
    if (keyInfoMarshaller != null) {
        return keyInfoMarshaller;
    }
    keyInfoMarshaller = Configuration.getMarshallerFactory().getMarshaller(KeyInfo.DEFAULT_ELEMENT_NAME);
    if (keyInfoMarshaller == null) {
        throw new SecurityException("Could not obtain KeyInfo marshaller from the configuration");
    }
    return keyInfoMarshaller;
}
 
示例17
/**
 * Ensure that the XMLObject is marshalled.
 * 
 * @param xmlObject the object to check and marshall
 * @throws EncryptionException thrown if there is an error when marshalling the XMLObject
 */
protected void checkAndMarshall(XMLObject xmlObject) throws EncryptionException {
    Element targetElement = xmlObject.getDOM();
    if (targetElement == null) {
        Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(xmlObject);
        try {
            targetElement = marshaller.marshall(xmlObject);
        } catch (MarshallingException e) {
            log.error("Error marshalling target XMLObject", e);
            throw new EncryptionException("Error marshalling target XMLObject", e);
        }
    }
}
 
示例18
/**
 * Serialize XML objects
 *
 * @param xmlObject : XACML or SAML objects to be serialized
 * @return serialized XACML or SAML objects
 */
private String marshall(XMLObject xmlObject) throws EntitlementProxyException {

    try {
        doBootstrap();
        System.setProperty(
                DOCUMENT_BUILDER_FACTORY,
                DOCUMENT_BUILDER_FACTORY_IMPL);

        MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
        Element element = marshaller.marshall(xmlObject);

        ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream();
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl =
                (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(byteArrayOutputStrm);
        writer.write(element, output);
        return new String(byteArrayOutputStrm.toByteArray(), Charset.forName("UTF-8"));
    } catch (Exception e) {
        log.error("Error Serializing the SAML Response");
        throw new EntitlementProxyException("Error Serializing the SAML Response", e);
    }
}
 
示例19
/**
 * Sign the SAML AuthnRequest message
 *
 * @param authnRequest
 * @param signatureAlgorithm
 * @param cred
 * @return
 * @throws org.wso2.carbon.identity.sso.agent.SSOAgentException
 */
public static AuthnRequest setSignature(AuthnRequest authnRequest, String signatureAlgorithm,
                                    X509Credential cred) throws SSOAgentException {
    doBootstrap();
    try {
        Signature signature = setSignatureRaw(signatureAlgorithm,cred);


        authnRequest.setSignature(signature);

        List<Signature> signatureList = new ArrayList<Signature>();
        signatureList.add(signature);

        // Marshall and Sign
        MarshallerFactory marshallerFactory =
                org.opensaml.xml.Configuration.getMarshallerFactory();
        Marshaller marshaller = marshallerFactory.getMarshaller(authnRequest);

        marshaller.marshall(authnRequest);

        org.apache.xml.security.Init.init();
        Signer.signObjects(signatureList);
        return authnRequest;

    } catch (Exception e) {
        throw new SSOAgentException("Error while signing the SAML Request message", e);
    }
}
 
示例20
/**
 * Adds an object provider to this configuration.
 * 
 * @param providerName the name of the object provider, corresponding to the element name or type name that the
 *            builder, marshaller, and unmarshaller operate on
 * @param builder the builder for that given provider
 * @param marshaller the marshaller for the provider
 * @param unmarshaller the unmarshaller for the provider
 */
public static void registerObjectProvider(QName providerName, XMLObjectBuilder builder, Marshaller marshaller,
        Unmarshaller unmarshaller) {
    Logger log = getLogger();
    log.debug("Registering new builder, marshaller, and unmarshaller for {}", providerName);
    builderFactory.registerBuilder(providerName, builder);
    marshallerFactory.registerMarshaller(providerName, marshaller);
    unmarshallerFactory.registerUnmarshaller(providerName, unmarshaller);
}
 
示例21
/**
 * Adds an object provider to this configuration.
 * 
 * @param providerName the name of the object provider, corresponding to the element name or type name that the
 *            builder, marshaller, and unmarshaller operate on
 * @param builder the builder for that given provider
 * @param marshaller the marshaller for the provider
 * @param unmarshaller the unmarshaller for the provider
 * @param configuration optional XML configuration snippet
 * 
 * @deprecated this method is deprecated with no replacement
 */
public static void registerObjectProvider(QName providerName, XMLObjectBuilder builder, Marshaller marshaller,
        Unmarshaller unmarshaller, Element configuration) {
    Logger log = getLogger();
    log.debug("Registering new builder, marshaller, and unmarshaller for {}", providerName);
    if (configuration != null) {
        configuredObjectProviders.put(providerName, configuration);
    }
    builderFactory.registerBuilder(providerName, builder);
    marshallerFactory.registerMarshaller(providerName, marshaller);
    unmarshallerFactory.registerUnmarshaller(providerName, unmarshaller);
}