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.ws.security.message.token;
21  
22  import javax.xml.namespace.QName;
23  
24  import org.apache.ws.security.WSConstants;
25  import org.apache.ws.security.WSSecurityException;
26  import org.apache.ws.security.util.DOM2Writer;
27  import org.apache.ws.security.util.WSSecurityUtil;
28  import org.w3c.dom.Document;
29  import org.w3c.dom.Element;
30  import org.w3c.dom.Node;
31  
32  /**
33   * Reference.
34   * 
35   * @author Davanum Srinivas (dims@yahoo.com).
36   */
37  public class Reference {
38      public static final QName TOKEN = new QName(WSConstants.WSSE_NS, "Reference");
39      protected Element element = null;
40      
41      /**
42       * Constructor.
43       * 
44       * @param elem The Reference element
45       * @throws WSSecurityException 
46       */
47      public Reference(Element elem) throws WSSecurityException {
48          if (elem == null) {
49              throw new WSSecurityException(WSSecurityException.INVALID_SECURITY, "noReference");
50          }
51          element = elem;
52          QName el = new QName(element.getNamespaceURI(), element.getLocalName());
53          if (!el.equals(TOKEN)) {
54              throw new WSSecurityException(
55                  WSSecurityException.FAILURE, "badElement", new Object[] {TOKEN, el}
56              );
57          }
58  
59          String uri = getURI();
60          // Reference URI cannot be null or empty
61          if (uri == null || "".equals(uri)) {
62              throw new WSSecurityException(
63                  WSSecurityException.INVALID_SECURITY, "badReferenceURI"
64              );
65          }
66      }
67  
68      /**
69       * Constructor.
70       * 
71       * @param doc 
72       */
73      public Reference(Document doc) {
74          element = doc.createElementNS(WSConstants.WSSE_NS, "wsse:Reference");
75      }
76      
77      /**
78       * Add the WSSE Namespace to this reference. The namespace is not added by default for
79       * efficiency purposes, as the reference is embedded in a wsse:SecurityTokenReference.
80       */
81      public void addWSSENamespace() {
82          WSSecurityUtil.setNamespace(this.element, WSConstants.WSSE_NS, WSConstants.WSSE_PREFIX);
83      }
84  
85      /**
86       * Get the DOM element.
87       * 
88       * @return the DOM element
89       */
90      public Element getElement() {
91          return element;
92      }
93  
94      /**
95       * Get the ValueType attribute.
96       * 
97       * @return the ValueType attribute
98       */
99      public String getValueType() {
100         return element.getAttribute("ValueType");
101     }
102 
103     /**
104      * Get the URI.
105      * 
106      * @return the URI
107      */
108     public String getURI() {
109         return element.getAttribute("URI");
110     }
111 
112     /**
113      * Set the Value type.
114      * 
115      * @param valueType the ValueType attribute to set
116      */
117     public void setValueType(String valueType) {
118         element.setAttributeNS(null, "ValueType", valueType);
119     }
120 
121     /**
122      * Set the URI.
123      * 
124      * @param uri the URI to set
125      */
126     public void setURI(String uri) {
127         element.setAttributeNS(null, "URI", uri);
128     }
129 
130     /**
131      * Return the string representation.
132      * 
133      * @return the string representation.
134      */
135     public String toString() {
136         return DOM2Writer.nodeToString((Node)element);
137     }
138     
139     @Override
140     public int hashCode() {
141         int result = 17;
142         String uri = getURI();
143         if (uri != null) {
144             result = 31 * result + uri.hashCode();
145         }
146         String valueType = getValueType();
147         if (valueType != null) {
148             result = 31 * result + valueType.hashCode();
149         }
150         return result;
151     }
152     
153     @Override
154     public boolean equals(Object object) {
155         if (!(object instanceof Reference)) {
156             return false;
157         }
158         Reference reference = (Reference)object;
159         if (!compare(getURI(), reference.getURI())) {
160             return false;
161         }
162         if (!compare(getValueType(), reference.getValueType())) {
163             return false;
164         }
165         return true;
166     }
167     
168     private boolean compare(String item1, String item2) {
169         if (item1 == null && item2 != null) { 
170             return false;
171         } else if (item1 != null && !item1.equals(item2)) {
172             return false;
173         }
174         return true;
175     }
176 }