View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.wss4j.common.token;
21  
22  import javax.xml.namespace.QName;
23  
24  import org.apache.wss4j.common.WSS4JConstants;
25  import org.apache.wss4j.common.ext.WSSecurityException;
26  import org.apache.wss4j.common.util.DOM2Writer;
27  import org.apache.wss4j.common.util.XMLUtils;
28  import org.w3c.dom.Document;
29  import org.w3c.dom.Element;
30  
31  /**
32   * Reference.
33   */
34  public class Reference {
35      public static final QName TOKEN = new QName(WSS4JConstants.WSSE_NS, "Reference");
36      private Element element;
37  
38      /**
39       * Constructor.
40       *
41       * @param elem The Reference element
42       * @throws WSSecurityException
43       */
44      public Reference(Element elem) throws WSSecurityException {
45          if (elem == null) {
46              throw new WSSecurityException(WSSecurityException.ErrorCode.INVALID_SECURITY, "noReference");
47          }
48          element = elem;
49          QName el = new QName(element.getNamespaceURI(), element.getLocalName());
50          if (!el.equals(TOKEN)) {
51              throw new WSSecurityException(
52                  WSSecurityException.ErrorCode.FAILURE, "badElement", new Object[] {TOKEN, el}
53              );
54          }
55  
56          String uri = getURI();
57          // Reference URI cannot be null or empty
58          if (uri == null || uri.length() == 0) {
59              throw new WSSecurityException(
60                  WSSecurityException.ErrorCode.INVALID_SECURITY, "badReferenceURI"
61              );
62          }
63      }
64  
65      /**
66       * Constructor.
67       *
68       * @param doc
69       */
70      public Reference(Document doc) {
71          element = doc.createElementNS(WSS4JConstants.WSSE_NS, "wsse:Reference");
72      }
73  
74      /**
75       * Add the WSSE Namespace to this reference. The namespace is not added by default for
76       * efficiency purposes, as the reference is embedded in a wsse:SecurityTokenReference.
77       */
78      public void addWSSENamespace() {
79          XMLUtils.setNamespace(this.element, WSS4JConstants.WSSE_NS, WSS4JConstants.WSSE_PREFIX);
80      }
81  
82      /**
83       * Get the DOM element.
84       *
85       * @return the DOM element
86       */
87      public Element getElement() {
88          return element;
89      }
90  
91      /**
92       * Get the ValueType attribute.
93       *
94       * @return the ValueType attribute
95       */
96      public String getValueType() {
97          return element.getAttributeNS(null, "ValueType");
98      }
99  
100     /**
101      * Get the URI.
102      *
103      * @return the URI
104      */
105     public String getURI() {
106         return element.getAttributeNS(null, "URI");
107     }
108 
109     /**
110      * Set the Value type.
111      *
112      * @param valueType the ValueType attribute to set
113      */
114     public void setValueType(String valueType) {
115         if (valueType != null) {
116             element.setAttributeNS(null, "ValueType", valueType);
117         }
118     }
119 
120     /**
121      * Set the URI.
122      *
123      * @param uri the URI to set
124      */
125     public void setURI(String uri) {
126         element.setAttributeNS(null, "URI", uri);
127     }
128 
129     /**
130      * Return the string representation.
131      *
132      * @return the string representation.
133      */
134     public String toString() {
135         return DOM2Writer.nodeToString(element);
136     }
137 
138     @Override
139     public int hashCode() {
140         int result = 17;
141         String uri = getURI();
142         if (uri != null) {
143             result = 31 * result + uri.hashCode();
144         }
145         String valueType = getValueType();
146         if (valueType != null) {
147             result = 31 * result + valueType.hashCode();
148         }
149         return result;
150     }
151 
152     @Override
153     public boolean equals(Object object) {
154         if (!(object instanceof Reference)) {
155             return false;
156         }
157         Reference reference = (Reference)object;
158         if (!compare(getURI(), reference.getURI())) {
159             return false;
160         }
161         return compare(getValueType(), reference.getValueType());
162     }
163 
164     private boolean compare(String item1, String item2) {
165         if (item1 == null && item2 != null) {
166             return false;
167         } else if (item1 != null && !item1.equals(item2)) {
168             return false;
169         }
170         return true;
171     }
172 }