1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ldap.core;
18
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.springframework.ldap.BadLdapGrammarException;
29 import org.springframework.ldap.support.ListComparator;
30
31
32
33
34
35
36
37
38
39 public class LdapRdn implements Serializable, Comparable {
40 private static final long serialVersionUID = 5681397547245228750L;
41
42 private List components = new LinkedList();
43
44
45
46
47 public LdapRdn() {
48 }
49
50
51
52
53
54
55 public LdapRdn(String string) {
56 DnParser parser = DefaultDnParserFactory.createDnParser(string);
57 LdapRdn rdn;
58 try {
59 rdn = parser.rdn();
60 }
61 catch (ParseException e) {
62 throw new BadLdapGrammarException("Failed to parse Rdn", e);
63 }
64 catch (TokenMgrError e) {
65 throw new BadLdapGrammarException("Failed to parse Rdn", e);
66 }
67 this.components = rdn.components;
68 }
69
70
71
72
73
74
75
76 public LdapRdn(String key, String value) {
77 components.add(new LdapRdnComponent(key, value));
78 }
79
80
81
82
83
84
85 public void addComponent(LdapRdnComponent rdnComponent) {
86 components.add(rdnComponent);
87 }
88
89
90
91
92
93
94 public List getComponents() {
95 return components;
96 }
97
98
99
100
101
102
103
104 public LdapRdnComponent getComponent() {
105 return (LdapRdnComponent) components.get(0);
106 }
107
108
109
110
111
112
113
114
115 public LdapRdnComponent getComponent(int idx) {
116 return (LdapRdnComponent) components.get(idx);
117 }
118
119
120
121
122
123
124
125 public String getLdapEncoded() {
126 if (components.size() == 0) {
127 throw new IndexOutOfBoundsException("No components in Rdn.");
128 }
129 StringBuffer sb = new StringBuffer(100);
130 for (Iterator iter = components.iterator(); iter.hasNext();) {
131 LdapRdnComponent component = (LdapRdnComponent) iter.next();
132 sb.append(component.encodeLdap());
133 if (iter.hasNext()) {
134 sb.append("+");
135 }
136 }
137
138 return sb.toString();
139 }
140
141
142
143
144
145
146 public String encodeUrl() {
147 StringBuffer sb = new StringBuffer(100);
148 for (Iterator iter = components.iterator(); iter.hasNext();) {
149 LdapRdnComponent component = (LdapRdnComponent) iter.next();
150 sb.append(component.encodeUrl());
151 if (iter.hasNext()) {
152 sb.append("+");
153 }
154 }
155
156 return sb.toString();
157 }
158
159
160
161
162
163
164
165
166 public int compareTo(Object obj) {
167 LdapRdn that = (LdapRdn) obj;
168 Comparator comparator = new ListComparator();
169 return comparator.compare(this.components, that.components);
170 }
171
172
173
174
175
176
177 public boolean equals(Object obj) {
178 if (obj == null || obj.getClass() != this.getClass()) {
179 return false;
180 }
181
182 LdapRdn that = (LdapRdn) obj;
183 return this.getComponents().equals(that.getComponents());
184 }
185
186
187
188
189
190
191 public int hashCode() {
192 return this.getClass().hashCode() ^ getComponents().hashCode();
193 }
194
195
196
197
198
199
200 public String toString() {
201 return getLdapEncoded();
202 }
203
204
205
206
207
208
209
210
211
212
213 public String getValue() {
214 return getComponent().getValue();
215 }
216
217
218
219
220
221
222
223
224
225
226 public String getKey() {
227 return getComponent().getKey();
228 }
229
230
231
232
233
234
235
236
237
238
239 public String getValue(String key) {
240 for (Iterator iter = components.iterator(); iter.hasNext();) {
241 LdapRdnComponent component = (LdapRdnComponent) iter.next();
242 if (StringUtils.equals(component.getKey(), key)) {
243 return component.getValue();
244 }
245 }
246
247 throw new IllegalArgumentException("No RdnComponent with the key " + key);
248 }
249
250
251
252
253
254
255
256
257 public LdapRdn immutableLdapRdn() {
258 List listWithImmutableRdns = new ArrayList(components.size());
259 for (Iterator iterator = components.iterator(); iterator.hasNext();) {
260 LdapRdnComponent rdnComponent = (LdapRdnComponent) iterator.next();
261 listWithImmutableRdns.add(rdnComponent.immutableLdapRdnComponent());
262 }
263 List unmodifiableListOfImmutableRdns = Collections.unmodifiableList(listWithImmutableRdns);
264 LdapRdn immutableRdn = new LdapRdn();
265 immutableRdn.components = unmodifiableListOfImmutableRdns;
266 return immutableRdn;
267 }
268 }