View Javadoc

1   /*
2    * Copyright 2009 University Corporation for Advanced Internet Development, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.ws.wsaddressing.util;
18  
19  import org.opensaml.ws.wsaddressing.IsReferenceParameterBearing;
20  import org.opensaml.xml.AttributeExtensibleXMLObject;
21  import org.opensaml.xml.XMLObject;
22  import org.opensaml.xml.schema.XSBooleanValue;
23  import org.opensaml.xml.util.DatatypeHelper;
24  
25  /**
26   * Helper methods for working with WS-Addressing.
27   */
28  public final class WSAddressingHelper {
29  
30      /**
31       * Private constructor.
32       */
33      private WSAddressingHelper() {
34      }
35  
36      /**
37       * Adds a <code>wsa:IsReferenceParameter</code> attribute to the given SOAP object.
38       * 
39       * @param soapObject the SOAP object to add the attribute to
40       * @param isReferenceParameter whether IsReferenceParameter is true or false
41       */
42      public static void addWSAIsReferenceParameter(XMLObject soapObject, boolean isReferenceParameter) {
43          if (soapObject instanceof IsReferenceParameterBearing) {
44              ((IsReferenceParameterBearing)soapObject).setWSAIsReferenceParameter(
45                      new XSBooleanValue(isReferenceParameter, false));
46          } else if (soapObject instanceof AttributeExtensibleXMLObject) {
47              ((AttributeExtensibleXMLObject)soapObject).getUnknownAttributes()
48                  .put(IsReferenceParameterBearing.WSA_IS_REFERENCE_PARAMETER_ATTR_NAME, 
49                          new XSBooleanValue(isReferenceParameter, false).toString());
50          } else {
51              throw new IllegalArgumentException("Specified object was neither IsReferenceParameterBearing nor AttributeExtensible");
52          }
53      }
54  
55      /**
56       * Get the <code>wsa:IsReferenceParameter</code> attribute from a given SOAP object.
57       * 
58       * @param soapObject the SOAP object to add the attribute to
59       * 
60       * @return value of the IsReferenceParameter attribute, or false if not present
61       */
62      public static boolean getWSAIsReferenceParameter(XMLObject soapObject) {
63          if (soapObject instanceof IsReferenceParameterBearing) {
64              XSBooleanValue value = ((IsReferenceParameterBearing)soapObject).isWSAIsReferenceParameterXSBoolean();
65              if (value != null) {
66                  return value.getValue();
67              }
68          }
69          if (soapObject instanceof AttributeExtensibleXMLObject) {
70              String valueStr = DatatypeHelper.safeTrimOrNullString(((AttributeExtensibleXMLObject)soapObject)
71                      .getUnknownAttributes().get(IsReferenceParameterBearing.WSA_IS_REFERENCE_PARAMETER_ATTR_NAME)); 
72              return DatatypeHelper.safeEquals("1", valueStr) || DatatypeHelper.safeEquals("true", valueStr);
73          }
74          return false;
75      }
76      
77  }