Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/LDAPMessage.java

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38 package org.ietf.ldap;
39
40 import java.io.IOException;
41 import java.io.OutputStream;
42 import java.io.Serializable;
43
44 import org.ietf.ldap.ber.stream.BERElement;
45 import org.ietf.ldap.ber.stream.BERInteger;
46 import org.ietf.ldap.ber.stream.BERSequence;
47 import org.ietf.ldap.ber.stream.BERTag;
48 import org.ietf.ldap.client.opers.JDAPAddResponse;
49 import org.ietf.ldap.client.opers.JDAPBindResponse;
50 import org.ietf.ldap.client.opers.JDAPCompareResponse;
51 import org.ietf.ldap.client.opers.JDAPDeleteResponse;
52 import org.ietf.ldap.client.opers.JDAPExtendedResponse;
53 import org.ietf.ldap.client.opers.JDAPModifyRDNResponse;
54 import org.ietf.ldap.client.opers.JDAPModifyResponse;
55 import org.ietf.ldap.client.opers.JDAPProtocolOp;
56 import org.ietf.ldap.client.opers.JDAPSearchResponse;
57 import org.ietf.ldap.client.opers.JDAPSearchResult;
58 import org.ietf.ldap.client.opers.JDAPSearchResultReference;
59
60
61 /**
62 * Base class for LDAP request and response messages.
63 * This class represents the LDAPMessage in RFC2251. The
64 * message is the entity that got transferred back and
65 * fro between the server and the client interface. Each
66 * message has a protocol operation. The protocol operation
67 * indicates if it is a request or response.
68 * <pre>
69 * LDAPMessage ::= SEQUENCE {
70 * messageID MessageID,
71 * protocolOp CHOICE {
72 * bindRequest BindRequest,
73 * ...
74 * }
75 * controls [0] Controls OPTIONAL
76 * }
77 * </pre>
78 *
79 * @version 1.0
80 */
81 public class LDAPMessage implements Serializable {
82
83 static final long serialVersionUID = -1364094245850026720L;
84
85 public final static int BIND_REQUEST = 0;
86 public final static int BIND_RESPONSE = 1;
87 public final static int UNBIND_REQUEST = 2;
88 public final static int SEARCH_REQUEST = 3;
89 public final static int SEARCH_RESPONSE = 4;
90 public final static int SEARCH_RESULT = 5;
91 public final static int MODIFY_REQUEST = 6;
92 public final static int MODIFY_RESPONSE = 7;
93 public final static int ADD_REQUEST = 8;
94 public final static int ADD_RESPONSE = 9;
95 public final static int DEL_REQUEST = 10;
96 public final static int DEL_RESPONSE = 11;
97 public final static int MODIFY_RDN_REQUEST = 12;
98 public final static int MODIFY_RDN_RESPONSE = 13;
99 public final static int COMPARE_REQUEST = 14;
100 public final static int COMPARE_RESPONSE = 15;
101 public final static int ABANDON_REQUEST = 16;
102 public final static int SEARCH_RESULT_REFERENCE = 19;
103 public final static int EXTENDED_REQUEST = 23;
104 public final static int EXTENDED_RESPONSE = 24;
105
106 /**
107 * Internal variables
108 */
109 private int _msgid;
110 private JDAPProtocolOp _protocolOp = null;
111 private LDAPControl _controls[] = null;
112
113 /**
114 * Constructs a ldap message.
115 * @param msgid message identifier
116 * @param op operation protocol
117 */
118 LDAPMessage(int msgid, JDAPProtocolOp op) {
119 _msgid = msgid;
120 _protocolOp = op;
121 }
122
123 LDAPMessage(int msgid, JDAPProtocolOp op, LDAPControl controls[]) {
124 _msgid = msgid;
125 _protocolOp = op;
126 _controls = controls; /* LDAPv3 additions */
127 }
128
129 /**
130 * Creates a ldap message from a BERElement. This method is used
131 * to parse LDAP response messages
132 *
133 * @param element ber element constructed from incoming byte stream
134 */
135 static LDAPMessage parseMessage(BERElement element) throws IOException {
136 int l_msgid;
137 JDAPProtocolOp l_protocolOp = null;
138 LDAPControl l_controls[] = null;
139
140 if (element.getType() != BERElement.SEQUENCE)
141 throw new IOException("SEQUENCE in jdap message expected");
142 BERSequence seq = (BERSequence)element;
143 BERInteger msgid = (BERInteger)seq.elementAt(0);
144 l_msgid = msgid.getValue();
145 BERElement protocolOp = seq.elementAt(1);
146 if (protocolOp.getType() != BERElement.TAG) {
147 throw new IOException("TAG in protocol operation is expected");
148 }
149 BERTag tag = (BERTag)protocolOp;
150 switch (tag.getTag()&0x1f) {
151 case JDAPProtocolOp.BIND_RESPONSE:
152 l_protocolOp = new JDAPBindResponse(protocolOp);
153 break;
154 case JDAPProtocolOp.SEARCH_RESPONSE:
155 l_protocolOp = new JDAPSearchResponse(protocolOp);
156 break;
157 /*
158 * If doing search without bind,
159 * x500.arc.nasa.gov returns tag SEARCH_REQUEST tag
160 * in SEARCH_RESULT.
161 */
162 case JDAPProtocolOp.SEARCH_REQUEST:
163 case JDAPProtocolOp.SEARCH_RESULT:
164 l_protocolOp = new JDAPSearchResult(protocolOp);
165 break;
166 case JDAPProtocolOp.MODIFY_RESPONSE:
167 l_protocolOp = new JDAPModifyResponse(protocolOp);
168 break;
169 case JDAPProtocolOp.ADD_RESPONSE:
170 l_protocolOp = new JDAPAddResponse(protocolOp);
171 break;
172 case JDAPProtocolOp.DEL_RESPONSE:
173 l_protocolOp = new JDAPDeleteResponse(protocolOp);
174 break;
175 case JDAPProtocolOp.MODIFY_RDN_RESPONSE:
176 l_protocolOp = new JDAPModifyRDNResponse(protocolOp);
177 break;
178 case JDAPProtocolOp.COMPARE_RESPONSE:
179 l_protocolOp = new JDAPCompareResponse(protocolOp);
180 break;
181 case JDAPProtocolOp.SEARCH_RESULT_REFERENCE:
182 l_protocolOp = new JDAPSearchResultReference(protocolOp);
183 break;
184 case JDAPProtocolOp.EXTENDED_RESPONSE:
185 l_protocolOp = new JDAPExtendedResponse(protocolOp);
186 break;
187 default:
188 throw new IOException("Unknown protocol operation");
189 }
190
191 /* parse control */
192 if (seq.size() >= 3) {
193 tag = (BERTag)seq.elementAt(2);
194 if ( tag.getTag() == (BERTag.CONSTRUCTED|BERTag.CONTEXT|0) ) {
195 BERSequence controls = (BERSequence)tag.getValue();
196 l_controls = new LDAPControl[controls.size()];
197 for (int i = 0; i < controls.size(); i++) {
198 l_controls[i] = LDAPControl.parseControl(controls.elementAt(i));
199 }
200 }
201 }
202
203 if (l_protocolOp instanceof JDAPSearchResponse) {
204 return new LDAPSearchResult(l_msgid,
205 (JDAPSearchResponse) l_protocolOp, l_controls);
206 }
207 else if (l_protocolOp instanceof JDAPSearchResultReference) {
208 return new LDAPSearchResultReference(l_msgid,
209 (JDAPSearchResultReference) l_protocolOp, l_controls);
210 }
211 else if (l_protocolOp instanceof JDAPExtendedResponse) {
212 return new LDAPExtendedResponse(l_msgid,
213 (JDAPExtendedResponse) l_protocolOp, l_controls);
214 }
215 else {
216 return new LDAPResponse(l_msgid, l_protocolOp, l_controls);
217 }
218 }
219
220 /**
221 * Retrieves list of controls.
222 * @return controls.
223 */
224 public LDAPControl[] getControls() {
225 return (_controls != null) ? _controls : new LDAPControl[0];
226 }
227
228 /**
229 * Returns the message identifer.
230 * @return message identifer.
231 */
232 public int getMessageID(){
233 return _msgid;
234 }
235
236 /**
237 * Returns the LDAP operation type of the message
238 * @return message type.
239 */
240 public int getType(){
241 return _protocolOp.getType();
242 }
243
244 /**
245 * Retrieves the protocol operation.
246 * @return protocol operation.
247 */
248 JDAPProtocolOp getProtocolOp() {
249 return _protocolOp;
250 }
251
252 /**
253 * Writes the ber encoding to stream.
254 * @param s output stream
255 */
256 void write(OutputStream s) throws IOException {
257 BERSequence seq = new BERSequence();
258 BERInteger i = new BERInteger(_msgid);
259 seq.addElement(i);
260 BERElement e = _protocolOp.getBERElement();
261 if (e == null) {
262 throw new IOException("Bad BER element");
263 }
264 seq.addElement(e);
265 if (_controls != null) { /* LDAPv3 additions */
266 BERSequence c = new BERSequence();
267 for (int j = 0; j < _controls.length; j++) {
268 c.addElement(_controls[j].getBERElement());
269 }
270 BERTag t = new BERTag(BERTag.CONTEXT|BERTag.CONSTRUCTED|0, c, true);
271 seq.addElement(t);
272 }
273 seq.write(s);
274 }
275
276 /**
277 * Returns string representation of an LDAP message.
278 * @return LDAP message.
279 */
280 public String toString() {
281 StringBuffer sb = new StringBuffer("[LDAPMessage] ");
282 sb.append(_msgid);
283 sb.append(" ");
284 sb.append(_protocolOp.toString());
285
286 for (int i =0; _controls != null && i < _controls.length; i++) {
287 sb.append(" ");
288 sb.append(_controls[i].toString());
289 }
290 return sb.toString();
291 }
292
293 /**
294 * Returns string representation of a ldap message with
295 * the time stamp. Used for message trace
296 * @return ldap message with the time stamp
297 */
298 StringBuffer toTraceString() {
299 StringBuffer sb = new StringBuffer(" op=");
300 sb.append(_msgid);
301 sb.append(" ");
302 sb.append(_protocolOp.toString());
303
304 for (int i =0; _controls != null && i < _controls.length; i++) {
305 sb.append(" ");
306 sb.append(_controls[i].toString());
307 }
308 return sb;
309 }
310 }