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