1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35
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
43
44
45
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
61 if (uri == null || "".equals(uri)) {
62 throw new WSSecurityException(
63 WSSecurityException.INVALID_SECURITY, "badReferenceURI"
64 );
65 }
66 }
67
68
69
70
71
72
73 public Reference(Document doc) {
74 element = doc.createElementNS(WSConstants.WSSE_NS, "wsse:Reference");
75 }
76
77
78
79
80
81 public void addWSSENamespace() {
82 WSSecurityUtil.setNamespace(this.element, WSConstants.WSSE_NS, WSConstants.WSSE_PREFIX);
83 }
84
85
86
87
88
89
90 public Element getElement() {
91 return element;
92 }
93
94
95
96
97
98
99 public String getValueType() {
100 return element.getAttribute("ValueType");
101 }
102
103
104
105
106
107
108 public String getURI() {
109 return element.getAttribute("URI");
110 }
111
112
113
114
115
116
117 public void setValueType(String valueType) {
118 element.setAttributeNS(null, "ValueType", valueType);
119 }
120
121
122
123
124
125
126 public void setURI(String uri) {
127 element.setAttributeNS(null, "URI", uri);
128 }
129
130
131
132
133
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 }