📄 ldapconnection.java
字号:
} attrs.put(type, acc); } handler.searchResultEntry(objectName, attrs); break; case SEARCH_REFERENCE: List acc = new ArrayList(); BERDecoder urls = response.parseSequence(code); while (urls.available()) { acc.add(urls.parseString()); } handler.searchResultReference(acc); break; case SEARCH_RESULT_DONE: return parseResult(response.parseSequence(code)); default: throw new ProtocolException("Unexpected response: " + code); } } while (true); } /** * Issues a modify request. * @param name the LDAP DN of the object to be modified(alias * dereferencing will not be performed) * @param modifications a sequence of modifications to be executed * to be executed * @see Modification */ public LDAPResult modify(String name, final Modification[] modifications) throws IOException { int id = messageId++; boolean utf8 = (version == 3); BEREncoder modify = new BEREncoder(utf8); modify.append(name); BEREncoder modSeq = new BEREncoder(utf8); for (int i = 0; i < modifications.length; i++) { BEREncoder mod = new BEREncoder(utf8); mod.append(modifications[i].operation); BEREncoder typeAndValues = new BEREncoder(utf8); typeAndValues.append(modifications[i].type); BEREncoder values = new BEREncoder(utf8); appendValues(values, modifications[i].values); typeAndValues.append(values.toByteArray(), BERConstants.SET); mod.append(typeAndValues.toByteArray(), BERConstants.SEQUENCE); modSeq.append(mod.toByteArray(), BERConstants.SEQUENCE); } modify.append(modSeq.toByteArray(), BERConstants.SEQUENCE); // Write request write(id, MODIFY_REQUEST, modify.toByteArray()); // Read response BERDecoder response = read(id); BERDecoder resultSequence = response.parseSequence(MODIFY_RESPONSE); LDAPResult result = parseResult(resultSequence); return result; } /** * Requests the addition of a new entry into the directory. * @param name the LDAP DN of the new entry * @param attributes a sequence of attributes to assign to the new entry */ public LDAPResult add(String name, AttributeValues[] attributes) throws IOException { int id = messageId++; boolean utf8 = (version == 3); BEREncoder add = new BEREncoder(utf8); add.append(name); BEREncoder attrSeq = new BEREncoder(utf8); for (int i = 0; i < attributes.length; i++) { BEREncoder attr = new BEREncoder(utf8); attr.append(attributes[i].type); BEREncoder values = new BEREncoder(utf8); appendValues(values, attributes[i].values); attr.append(values.toByteArray(), BERConstants.SET); attrSeq.append(attr.toByteArray(), BERConstants.SEQUENCE); } add.append(attrSeq.toByteArray(), BERConstants.SEQUENCE); // Write request write(id, ADD_REQUEST, add.toByteArray()); // Read response BERDecoder response = read(id); BERDecoder resultSequence = response.parseSequence(ADD_RESPONSE); LDAPResult result = parseResult(resultSequence); return result; } /** * Requests the removal of an entry from the directory. * @param name the LDAP DN of the entry to remove */ public LDAPResult delete(String name) throws IOException { int id = messageId++; boolean utf8 = (version == 3); BEREncoder del = new BEREncoder(utf8); del.append(name); // Write request write(id, DELETE_REQUEST, del.toByteArray()); // Read response BERDecoder response = read(id); int code = response.parseType(); if (code != DELETE_RESPONSE) { throw new ProtocolException("Unexpected response type: " + code); } BERDecoder resultSequence = response.parseSequence(); LDAPResult result = parseResult(resultSequence); return result; } /** * Changes the leftmost(least significant) component of the name of an * entry in the directory, or move a subtree of entries to a new location * in the directory. * @param name the LDAP DN of the entry to be changed * @param newRDN the RDN that will form the leftmost component of the new * name of the entry * @param deleteOldRDN if false, the old RDN values will be retained as * attributes of the entry, otherwise they are deleted from the entry * @param newSuperior if non-null, the DN of the entry to become the * immediate superior of the existing entry */ public LDAPResult modifyDN(String name, String newRDN, boolean deleteOldRDN, String newSuperior) throws IOException { int id = messageId++; boolean utf8 = (version == 3); BEREncoder modifyDN = new BEREncoder(utf8); modifyDN.append(name); modifyDN.append(newRDN); modifyDN.append(deleteOldRDN); if (newSuperior != null) { modifyDN.append(newSuperior); } // Write request write(id, MODIFY_DN_REQUEST, modifyDN.toByteArray()); // Read response BERDecoder response = read(id); BERDecoder resultSequence = response.parseSequence(MODIFY_DN_RESPONSE); LDAPResult result = parseResult(resultSequence); return result; } /* TODO Compare Operation */ /* TODO Abandon Operation */ /* TODO Extended Operation */ /** * Appends the specified set of values to the given encoder. */ void appendValues(BEREncoder encoder, Set values) throws BERException { if (values != null) { for (Iterator i = values.iterator(); i.hasNext(); ) { Object value = i.next(); if (value == null) { encoder.appendNull(); } else if (value instanceof String) { encoder.append((String) value); } else if (value instanceof Integer) { encoder.append(((Integer) value).intValue()); } else if (value instanceof Boolean) { encoder.append(((Boolean) value).booleanValue()); } else if (value instanceof byte[]) { encoder.append((byte[]) value); } // TODO float else { throw new ClassCastException(value.getClass().getName()); } } } } /** * Encode a control. */ byte[] controlSequence(final Control control, boolean utf8) throws IOException { BEREncoder encoder = new BEREncoder(utf8); encoder.append(control.getID()); if (control.isCritical()) { encoder.append(true); } return encoder.toByteArray(); } /** * Parse a response into an LDAP result object. */ LDAPResult parseResult(BERDecoder response) throws IOException { int status = response.parseInt(); String matchingDN = response.parseString(); String errorMessage = response.parseString(); String[] referrals = null; if (response.available()) { int type = response.parseType(); if (type == BERConstants.SEQUENCE) { ArrayList list = new ArrayList(); BERDecoder sequence = response.parseSequence(); type = sequence.parseType(); while (type != -1) { list.add(sequence.parseString()); } referrals = new String[list.size()]; list.toArray(referrals); } } return new LDAPResult(status, matchingDN, errorMessage, referrals); } /** * Write a request. * @param id the message ID * @param code the operation code * @param request the request body */ void write(int id, int code, byte[] request) throws IOException { boolean utf8 = (version == 3); BEREncoder envelope = new BEREncoder(utf8); envelope.append(id); envelope.append(request, code); BEREncoder message = new BEREncoder(utf8); message.append(envelope.toByteArray(), MESSAGE); byte[] toSend = message.toByteArray(); // Write to socket out.write(toSend); out.flush(); } /** * Read a response associated with the given message ID. * @param id the message ID * @return a BERDecoder for the content of the message */ BERDecoder read(int id) throws IOException { // Check for an already received async response Integer key = new Integer(id); List responses = (List) asyncResponses.get(key); if (responses != null) { BERDecoder response = (BERDecoder) responses.remove(0); if (responses.size() == 0) { asyncResponses.remove(key); } return response; } do { // Read LDAP message byte[] bytes = readMessage(); boolean utf8 = (version == 3); BERDecoder message = new BERDecoder(bytes, utf8); message = message.parseSequence(MESSAGE); // Check message ID int msgId = message.parseInt(); if (msgId == id) { return message; } else { // Store this message for later processing key = new Integer(msgId); responses = (List) asyncResponses.get(key); if (responses == null) { responses = new ArrayList(); asyncResponses.put(key, responses); } responses.add(message); } } while (true); } /** * Read an LDAP message. */ byte[] readMessage() throws IOException { // Peek at the length part of the BER encoding to determine the length // of the message // TODO normalize this with functionality in BERDecoder byte[] header = new byte[6]; int offset = 0; header[offset++] = (byte) readByte(); // type int len = readByte(); // length 0 header[offset++] = (byte) len; if ((len & 0x80) != 0) { int lsize = len - 0x80; if (lsize > 4) { throw new BERException("Data too long: " + lsize); } len = 0; for (int i = 0; i < lsize; i++) { int c = readByte(); header[offset++] = (byte) c; len = (len << 8) + c; } } // Allocate message array byte[] message = new byte[offset + len]; System.arraycopy(header, 0, message, 0, offset); if (len == 0) { return message; } header = null; // Read message content do { int l = in.read(message, offset, len); if (l == -1) { throw new IOException("EOF"); } offset += l; len -= l; } while (len > 0); return message; } /** * Read a single byte. */ int readByte() throws IOException { int ret = in.read(); if (ret == -1) { throw new IOException("EOF"); } return ret & 0xff; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -