1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.ldap.core;
17
18 import java.io.Serializable;
19 import java.net.URI;
20 import java.net.URISyntaxException;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.commons.lang.Validate;
24
25
26
27
28
29
30
31
32
33 public class LdapRdnComponent implements Comparable, Serializable {
34 private static final long serialVersionUID = -3296747972616243038L;
35
36 public static final boolean DONT_DECODE_VALUE = false;
37
38 private String key;
39
40 private String value;
41
42
43
44
45
46
47
48 public LdapRdnComponent(String key, String value) {
49 this(key, value, DONT_DECODE_VALUE);
50 }
51
52
53
54
55
56
57
58
59
60
61 public LdapRdnComponent(String key, String value, boolean decodeValue) {
62 Validate.notEmpty(key, "Key must not be empty");
63 Validate.notEmpty(value, "Value must not be empty");
64
65 this.key = StringUtils.lowerCase(key);
66 if (decodeValue) {
67 this.value = LdapEncoder.nameDecode(value);
68 }
69 else {
70 this.value = value;
71 }
72 }
73
74
75
76
77
78
79 public String getKey() {
80 return key;
81 }
82
83
84
85
86
87
88
89
90 public void setKey(String key) {
91 this.key = key;
92 }
93
94
95
96
97
98
99 public String getValue() {
100 return value;
101 }
102
103
104
105
106
107
108
109
110 public void setValue(String value) {
111 this.value = value;
112 }
113
114
115
116
117
118
119 protected String encodeLdap() {
120 StringBuffer buff = new StringBuffer(key.length() + value.length() * 2);
121
122 buff.append(key);
123 buff.append('=');
124 buff.append(LdapEncoder.nameEncode(value));
125
126 return buff.toString();
127 }
128
129
130
131
132
133
134 public String toString() {
135 return getLdapEncoded();
136 }
137
138
139
140
141 public String getLdapEncoded() {
142 return encodeLdap();
143 }
144
145
146
147
148
149
150 public String encodeUrl() {
151
152 try {
153 URI valueUri = new URI(null, null, value, null);
154 return key + "=" + valueUri.toString();
155 }
156 catch (URISyntaxException e) {
157
158 return key + "=" + "value";
159 }
160 }
161
162
163
164
165
166
167 public int hashCode() {
168 return key.toUpperCase().hashCode() ^ value.toUpperCase().hashCode();
169 }
170
171
172
173
174
175
176 public boolean equals(Object obj) {
177
178
179 if (obj != null && obj instanceof LdapRdnComponent) {
180 LdapRdnComponent that = (LdapRdnComponent) obj;
181 return StringUtils.equalsIgnoreCase(this.key, that.key)
182 && StringUtils.equalsIgnoreCase(this.value, that.value);
183
184 }
185 else {
186 return false;
187 }
188 }
189
190
191
192
193
194
195
196
197 public int compareTo(Object obj) {
198 LdapRdnComponent that = (LdapRdnComponent) obj;
199 return this.toString().compareTo(that.toString());
200 }
201
202
203
204
205
206
207
208
209 public LdapRdnComponent immutableLdapRdnComponent() {
210 return new ImmutableLdapRdnComponent(key, value);
211 }
212
213 private static class ImmutableLdapRdnComponent extends LdapRdnComponent {
214 private static final long serialVersionUID = -7099970046426346567L;
215
216 public ImmutableLdapRdnComponent(String key, String value) {
217 super(key, value);
218 }
219
220 public void setKey(String key) {
221 throw new UnsupportedOperationException("SetValue not supported for this immutable LdapRdnComponent");
222 }
223
224 public void setValue(String value) {
225 throw new UnsupportedOperationException("SetKey not supported for this immutable LdapRdnComponent");
226 }
227 }
228 }