| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 108 |
|
| 109 |
private int _msgid; |
| 110 |
private JDAPProtocolOp _protocolOp = null; |
| 111 |
private LDAPControl _controls[] = null; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 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; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 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 |
|
| 159 |
|
| 160 |
|
| 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 |
|
| 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 |
|
| 222 |
|
| 223 |
|
| 224 |
public LDAPControl[] getControls() { |
| 225 |
return (_controls != null) ? _controls : new LDAPControl[0]; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public int getMessageID(){ |
| 233 |
return _msgid; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public int getType(){ |
| 241 |
return _protocolOp.getType(); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
JDAPProtocolOp getProtocolOp() { |
| 249 |
return _protocolOp; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 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) { |
| 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 |
|
| 278 |
|
| 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 |
|
| 295 |
|
| 296 |
|
| 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 |
} |