2525 * $Id: SAML2MetaUtils.java,v 1.9 2009/09/21 17:28:12 exu Exp $
2626 *
2727 * Portions Copyrighted 2010-2015 ForgeRock AS.
28+ * Portions Copyrighted 2024 Wren Security.
2829 */
2930package com .sun .identity .saml2 .meta ;
3031
@@ -81,14 +82,12 @@ public final class SAML2MetaUtils {
8182 "com.sun.identity.saml2.jaxb.xmlsig:" +
8283 "com.sun.identity.saml2.jaxb.assertion:" +
8384 "com.sun.identity.saml2.jaxb.metadata:" +
84- "com.sun.identity.saml2.jaxb.metadataattr:" +
85+ "com.sun.identity.saml2.jaxb.metadataattr:" +
8586 "com.sun.identity.saml2.jaxb.entityconfig:" +
8687 "com.sun.identity.saml2.jaxb.schema" ;
8788 private static final String JAXB_PACKAGE_LIST_PROP =
8889 "com.sun.identity.liberty.ws.jaxb.packageList" ;
8990 private static JAXBContext jaxbContext = null ;
90- private static final String PROP_JAXB_FORMATTED_OUTPUT =
91- "jaxb.formatted.output" ;
9291 private static final String PROP_NAMESPACE_PREFIX_MAPPER =
9392 "com.sun.xml.bind.namespacePrefixMapper" ;
9493
@@ -171,18 +170,31 @@ public static Object convertNodeToJAXB(Node node)
171170 return u .unmarshal (node );
172171 }
173172
173+ /**
174+ * See {@link #convertJAXBToString(Object, boolean)}.
175+ */
176+ public static String convertJAXBToString (Object jaxbObj ) throws JAXBException {
177+ return convertJAXBToString (jaxbObj , true , false );
178+ }
179+
174180 /**
175181 * Converts a JAXB object to a <code>String</code> object.
176182 * @param jaxbObj a JAXB object
183+ * @param format flag indicating whether the output XML should be formatted.
184+ * @param fragment flag indicating whether the specified JAXB object is the fragment.
177185 * @return a <code>String</code> representing the JAXB object.
178186 * @exception JAXBException if an error occurs while converting JAXB object
179187 */
180- public static String convertJAXBToString (Object jaxbObj )
181- throws JAXBException {
182-
188+ public static String convertJAXBToString (Object jaxbObj , boolean format , boolean fragment ) throws JAXBException {
183189 StringWriter sw = new StringWriter ();
184190 Marshaller marshaller = jaxbContext .createMarshaller ();
185- marshaller .setProperty (PROP_JAXB_FORMATTED_OUTPUT , Boolean .TRUE );
191+ if (format ) {
192+ marshaller .setProperty (Marshaller .JAXB_FORMATTED_OUTPUT , Boolean .TRUE );
193+ }
194+ if (fragment ) {
195+ marshaller .setProperty (Marshaller .JAXB_FRAGMENT , Boolean .TRUE );
196+ marshaller .setProperty ("com.sun.xml.bind.xmlDeclaration" , Boolean .FALSE );
197+ }
186198 marshaller .setProperty (PROP_NAMESPACE_PREFIX_MAPPER , nsPrefixMapper );
187199 marshaller .marshal (jaxbObj , sw );
188200 return sw .toString ();
@@ -194,12 +206,9 @@ public static String convertJAXBToString(Object jaxbObj)
194206 * @param os an <code>OutputStream</code> object
195207 * @exception JAXBException if an error occurs while converting JAXB object
196208 */
197- public static void convertJAXBToOutputStream (Object jaxbObj ,
198- OutputStream os )
199- throws JAXBException {
200-
209+ public static void convertJAXBToOutputStream (Object jaxbObj , OutputStream os ) throws JAXBException {
201210 Marshaller marshaller = jaxbContext .createMarshaller ();
202- marshaller .setProperty (PROP_JAXB_FORMATTED_OUTPUT , Boolean .TRUE );
211+ marshaller .setProperty (Marshaller . JAXB_FORMATTED_OUTPUT , Boolean .TRUE );
203212 marshaller .setProperty (PROP_NAMESPACE_PREFIX_MAPPER , nsPrefixMapper );
204213 marshaller .marshal (jaxbObj , os );
205214 }
@@ -573,36 +582,32 @@ public static IDPSSOConfigElement getIDPSSOConfig(
573582 return null ;
574583 }
575584
576- public static String exportStandardMeta (String realm , String entityID ,
577- boolean sign )
578- throws SAML2MetaException {
579-
580- try {
581- SAML2MetaManager metaManager = new SAML2MetaManager ();
582- EntityDescriptorElement descriptor =
583- metaManager .getEntityDescriptor (realm , entityID );
585+ public static String exportStandardMeta (String realm , String entityID , boolean sign ) throws SAML2MetaException {
586+ try {
587+ SAML2MetaManager metaManager = new SAML2MetaManager ();
588+ EntityDescriptorElement descriptor =
589+ metaManager .getEntityDescriptor (realm , entityID );
584590
585- String xmlstr = null ;
586- if (descriptor == null ) {
587- return null ;
588- }
591+ String xmlstr = null ;
592+ if (descriptor == null ) {
593+ return null ;
594+ }
589595
590- if (sign ) {
591- Document doc = SAML2MetaSecurityUtils .sign (realm , descriptor );
592- if (doc != null ) {
596+ if (sign ) {
597+ Document doc = SAML2MetaSecurityUtils .sign (realm , descriptor );
598+ if (doc != null ) {
593599 xmlstr = XMLUtils .print (doc );
594- }
600+ }
595601 }
596602 if (xmlstr == null ) {
597- xmlstr = convertJAXBToString (descriptor );
598- xmlstr = SAML2MetaSecurityUtils .formatBase64BinaryElement (
599- xmlstr );
603+ xmlstr = convertJAXBToString (descriptor );
604+ xmlstr = SAML2MetaSecurityUtils .formatBase64BinaryElement (xmlstr );
600605 }
601606 xmlstr = workaroundAbstractRoleDescriptor (xmlstr );
602607 return xmlstr ;
603- } catch (JAXBException e ) {
608+ } catch (JAXBException e ) {
604609 throw new SAML2MetaException (e .getMessage ());
605- }
610+ }
606611 }
607612
608613 /**
@@ -734,7 +739,7 @@ private static String importSAML2Entity(SAML2MetaManager metaManager, String rea
734739
735740 return result ;
736741 }
737-
742+
738743 private static Object workaroundJAXBBug (Object obj ) throws JAXBException {
739744
740745 String metadata = convertJAXBToString (obj );
@@ -787,28 +792,28 @@ private static void workaroundAbstractRoleDescriptor(Document doc) {
787792 }
788793
789794 private static String workaroundAbstractRoleDescriptor (String xmlstr ) {
790- int index =
791- xmlstr .indexOf (":" +SAML2MetaConstants .ATTRIBUTE_QUERY_DESCRIPTOR );
792- if (index == -1 ) {
793- return xmlstr ;
794- }
795+ int index =
796+ xmlstr .indexOf (":" +SAML2MetaConstants .ATTRIBUTE_QUERY_DESCRIPTOR );
797+ if (index == -1 ) {
798+ return xmlstr ;
799+ }
795800
796801 int index2 = xmlstr .lastIndexOf ("<" , index );
797- if (index2 == -1 ) {
798- return xmlstr ;
799- }
800-
801- String prefix = xmlstr .substring (index2 + 1 , index );
802- String type = prefix + ":" +
803- SAML2MetaConstants .ATTRIBUTE_QUERY_DESCRIPTOR_TYPE ;
804-
805- xmlstr = xmlstr .replaceAll ("<" + prefix + ":" +
806- SAML2MetaConstants .ATTRIBUTE_QUERY_DESCRIPTOR ,
807- "<" + SAML2MetaConstants .ROLE_DESCRIPTOR + " " +
808- SAML2Constants .XSI_DECLARE_STR + " xsi:type=\" " + type + "\" " );
809- xmlstr = xmlstr .replaceAll ("</" + prefix + ":" +
810- SAML2MetaConstants .ATTRIBUTE_QUERY_DESCRIPTOR ,
811- "</" + SAML2MetaConstants .ROLE_DESCRIPTOR );
812- return xmlstr ;
802+ if (index2 == -1 ) {
803+ return xmlstr ;
804+ }
805+
806+ String prefix = xmlstr .substring (index2 + 1 , index );
807+ String type = prefix + ":" +
808+ SAML2MetaConstants .ATTRIBUTE_QUERY_DESCRIPTOR_TYPE ;
809+
810+ xmlstr = xmlstr .replaceAll ("<" + prefix + ":" +
811+ SAML2MetaConstants .ATTRIBUTE_QUERY_DESCRIPTOR ,
812+ "<" + SAML2MetaConstants .ROLE_DESCRIPTOR + " " +
813+ SAML2Constants .XSI_DECLARE_STR + " xsi:type=\" " + type + "\" " );
814+ xmlstr = xmlstr .replaceAll ("</" + prefix + ":" +
815+ SAML2MetaConstants .ATTRIBUTE_QUERY_DESCRIPTOR ,
816+ "</" + SAML2MetaConstants .ROLE_DESCRIPTOR );
817+ return xmlstr ;
813818 }
814819}
0 commit comments