我这样形成LogoutRequest

我初始化NameID

NameID nameId = buildSAMLObjectWithDefaultName(NameID.class);
nameId.setFormat("urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress");
nameId.setValue("xxxxxx");
String sessionIndex = "idxxxx";
LogoutRequest logoutRequest = genererateLogoutRequest(nameId, sessionIndex, metaData);


方法generateLogoutRequest

private static LogoutRequest genererateLogoutRequest(final NameID nameId,
        final String sessionIndex, final SAMLMetaData metaData)
        throws Exception {
    LogoutRequest logoutRequest = buildSAMLObjectWithDefaultName(LogoutRequest.class);

    logoutRequest.setID("SOME RANDOM ID");

    logoutRequest.setDestination("xxxxxxx/sso/saml");
    logoutRequest.setIssueInstant(new DateTime());

    Issuer issuer = buildSAMLObjectWithDefaultName(Issuer.class);
    issuer.setValue("xxxxx");
    logoutRequest.setIssuer(issuer);

    SessionIndex sessionIndexElement = buildSAMLObjectWithDefaultName(SessionIndex.class);

    sessionIndexElement.setSessionIndex(sessionIndex);
    logoutRequest.getSessionIndexes().add(sessionIndexElement);

    logoutRequest.setNameID(nameId);
    return logoutRequest;
}


其中buildSAMLObjectWithDefaultName

public static <T> T buildSAMLObjectWithDefaultName(final Class<T> clazz)
        throws IllegalArgumentException, IllegalAccessException,
        NoSuchFieldException, SecurityException {
    XMLObjectBuilderFactory builderFactory = Configuration
            .getBuilderFactory();

    QName defaultElementName = (QName) clazz.getDeclaredField(
            "DEFAULT_ELEMENT_NAME").get(null);
    T object = (T) builderFactory.getBuilder(defaultElementName)
            .buildObject(defaultElementName);

    return object;
}


但是如何在控制台中显示xmlLogoutRequest表示形式?
当我这样做的时候

System.out.println(logoutRequest.getDOM().toString());


抛出NullPointerException

最佳答案

我用这个设施

Marshaller marshaller = org.opensaml.Configuration
            .getMarshallerFactory().getMarshaller(logoutRequest);
org.w3c.dom.Element authDOM = marshaller.marshall(logoutRequest);
StringWriter rspWrt = new StringWriter();
XMLHelper.writeNode(authDOM, rspWrt);
System.out.println(rspWrt.toString());

08-05 23:09