| | |
| | | import org.opends.messages.Message; |
| | | |
| | | |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.io.IOException; |
| | | |
| | | import org.opends.server.protocols.asn1.ASN1Element; |
| | | import org.opends.server.protocols.asn1.ASN1Enumerated; |
| | | import org.opends.server.protocols.asn1.ASN1OctetString; |
| | | import org.opends.server.protocols.asn1.ASN1Sequence; |
| | | import org.opends.server.types.DebugLogLevel; |
| | | import org.opends.server.protocols.asn1.*; |
| | | import org.opends.server.types.DN; |
| | | import org.opends.server.types.LDAPException; |
| | | import org.opends.server.types.ByteString; |
| | | |
| | | import static org.opends.server.loggers.debug.DebugLogger.*; |
| | | import org.opends.server.loggers.debug.DebugTracer; |
| | | import static org.opends.messages.ProtocolMessages.*; |
| | | import static org.opends.server.protocols.ldap.LDAPConstants.*; |
| | | import static org.opends.server.protocols.ldap.LDAPResultCode.*; |
| | | import static org.opends.server.util.ServerConstants.*; |
| | | |
| | | |
| | |
| | | private static final DebugTracer TRACER = getTracer(); |
| | | |
| | | // The value for this extended response. |
| | | private ASN1OctetString value; |
| | | private ByteString value; |
| | | |
| | | // The matched DN for this response. |
| | | private DN matchedDN; |
| | |
| | | */ |
| | | public ExtendedResponseProtocolOp(int resultCode, Message errorMessage, |
| | | DN matchedDN, List<String> referralURLs, |
| | | String oid, ASN1OctetString value) |
| | | String oid, ByteString value) |
| | | { |
| | | this.resultCode = resultCode; |
| | | this.errorMessage = errorMessage; |
| | |
| | | |
| | | |
| | | /** |
| | | * Specifies the result code for this response. |
| | | * |
| | | * @param resultCode The result code for this response. |
| | | */ |
| | | public void setResultCode(int resultCode) |
| | | { |
| | | this.resultCode = resultCode; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Retrieves the error message for this response. |
| | | * |
| | | * @return The error message for this response, or <CODE>null</CODE> if none |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Specifies the error message for this response. |
| | | * |
| | | * @param errorMessage The error message for this response. |
| | | */ |
| | | public void setErrorMessage(Message errorMessage) |
| | | { |
| | | this.errorMessage = errorMessage; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Retrieves the matched DN for this response. |
| | | * |
| | |
| | | |
| | | |
| | | /** |
| | | * Specifies the matched DN for this response. |
| | | * |
| | | * @param matchedDN The matched DN for this response. |
| | | */ |
| | | public void setMatchedDN(DN matchedDN) |
| | | { |
| | | this.matchedDN = matchedDN; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Retrieves the set of referral URLs for this response. |
| | | * |
| | | * @return The set of referral URLs for this response, or <CODE>null</CODE> |
| | |
| | | |
| | | |
| | | /** |
| | | * Specifies the set of referral URLs for this response. |
| | | * |
| | | * @param referralURLs The set of referral URLs for this response. |
| | | */ |
| | | public void setReferralURLs(List<String> referralURLs) |
| | | { |
| | | this.referralURLs = referralURLs; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Retrieves the OID for this extended response. |
| | | * |
| | | * @return The OID for this extended response, or <CODE>null</CODE> if none |
| | |
| | | |
| | | |
| | | /** |
| | | * Specifies the OID for this extended response. |
| | | * |
| | | * @param oid The OID for this extended response. |
| | | */ |
| | | public void setOID(String oid) |
| | | { |
| | | this.oid = oid; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Retrieves the value for this extended response. |
| | | * |
| | | * @return The value for this extended response, or <CODE>null</CODE> if none |
| | | * was provided. |
| | | */ |
| | | public ASN1OctetString getValue() |
| | | public ByteString getValue() |
| | | { |
| | | return value; |
| | | } |
| | |
| | | |
| | | |
| | | /** |
| | | * Specifies the value for this extended response. |
| | | * |
| | | * @param value The value for this extended response. |
| | | */ |
| | | public void setValue(ASN1OctetString value) |
| | | { |
| | | this.value = value; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Retrieves the BER type for this protocol op. |
| | | * |
| | | * @return The BER type for this protocol op. |
| | |
| | | return "Extended Response"; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Encodes this protocol op to an ASN.1 element suitable for including in an |
| | | * LDAP message. |
| | | * Writes this protocol op to an ASN.1 output stream. |
| | | * |
| | | * @return The ASN.1 element containing the encoded protocol op. |
| | | * @param stream The ASN.1 output stream to write to. |
| | | * @throws IOException If a problem occurs while writing to the stream. |
| | | */ |
| | | public ASN1Element encode() |
| | | public void write(ASN1Writer stream) throws IOException |
| | | { |
| | | ArrayList<ASN1Element> elements = new ArrayList<ASN1Element>(6); |
| | | elements.add(new ASN1Enumerated(resultCode)); |
| | | stream.writeStartSequence(OP_TYPE_EXTENDED_RESPONSE); |
| | | stream.writeEnumerated(resultCode); |
| | | |
| | | if (matchedDN == null) |
| | | if(matchedDN == null) |
| | | { |
| | | elements.add(new ASN1OctetString()); |
| | | stream.writeOctetString((String)null); |
| | | } |
| | | else |
| | | { |
| | | elements.add(new ASN1OctetString(matchedDN.toString())); |
| | | stream.writeOctetString(matchedDN.toString()); |
| | | } |
| | | |
| | | elements.add(new ASN1OctetString(errorMessage)); |
| | | if(errorMessage == null) |
| | | { |
| | | stream.writeOctetString((String)null); |
| | | } |
| | | else |
| | | { |
| | | stream.writeOctetString(errorMessage.toString()); |
| | | } |
| | | |
| | | if ((referralURLs != null) && (! referralURLs.isEmpty())) |
| | | { |
| | | ArrayList<ASN1Element> referralElements = |
| | | new ArrayList<ASN1Element>(referralURLs.size()); |
| | | |
| | | stream.writeStartSequence(TYPE_REFERRAL_SEQUENCE); |
| | | for (String s : referralURLs) |
| | | { |
| | | referralElements.add(new ASN1OctetString(s)); |
| | | stream.writeOctetString(s); |
| | | } |
| | | |
| | | elements.add(new ASN1Sequence(TYPE_REFERRAL_SEQUENCE, referralElements)); |
| | | stream.writeEndSequence(); |
| | | } |
| | | |
| | | if ((oid != null) && (oid.length() > 0)) |
| | | { |
| | | elements.add(new ASN1OctetString(TYPE_EXTENDED_RESPONSE_OID, oid)); |
| | | stream.writeOctetString(TYPE_EXTENDED_RESPONSE_OID, oid); |
| | | } |
| | | |
| | | if (value != null) |
| | | { |
| | | value.setType(TYPE_EXTENDED_RESPONSE_VALUE); |
| | | elements.add(value); |
| | | stream.writeOctetString(TYPE_EXTENDED_RESPONSE_VALUE, value); |
| | | } |
| | | |
| | | return new ASN1Sequence(OP_TYPE_EXTENDED_RESPONSE, elements); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Decodes the provided ASN.1 element as a extended response protocol op. |
| | | * |
| | | * @param element The ASN.1 element to decode. |
| | | * |
| | | * @return The decoded extended response protocol op. |
| | | * |
| | | * @throws LDAPException If a problem occurs while attempting to decode the |
| | | * ASN.1 element to a protocol op. |
| | | */ |
| | | public static ExtendedResponseProtocolOp decodeExtendedResponse(ASN1Element |
| | | element) |
| | | throws LDAPException |
| | | { |
| | | ArrayList<ASN1Element> elements; |
| | | try |
| | | { |
| | | elements = element.decodeAsSequence().elements(); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | if (debugEnabled()) |
| | | { |
| | | TRACER.debugCaught(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | Message message = ERR_LDAP_RESULT_DECODE_SEQUENCE.get(String.valueOf(e)); |
| | | throw new LDAPException(PROTOCOL_ERROR, message, e); |
| | | } |
| | | |
| | | |
| | | int numElements = elements.size(); |
| | | if ((numElements < 3) || (numElements > 6)) |
| | | { |
| | | Message message = ERR_LDAP_EXTENDED_RESULT_DECODE_INVALID_ELEMENT_COUNT. |
| | | get(numElements); |
| | | throw new LDAPException(PROTOCOL_ERROR, message); |
| | | } |
| | | |
| | | |
| | | int resultCode; |
| | | try |
| | | { |
| | | resultCode = elements.get(0).decodeAsInteger().intValue(); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | if (debugEnabled()) |
| | | { |
| | | TRACER.debugCaught(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | Message message = |
| | | ERR_LDAP_RESULT_DECODE_RESULT_CODE.get(String.valueOf(e)); |
| | | throw new LDAPException(PROTOCOL_ERROR, message, e); |
| | | } |
| | | |
| | | |
| | | DN matchedDN; |
| | | try |
| | | { |
| | | String dnString = elements.get(1).decodeAsOctetString().stringValue(); |
| | | if (dnString.length() == 0) |
| | | { |
| | | matchedDN = null; |
| | | } |
| | | else |
| | | { |
| | | matchedDN = DN.decode(dnString); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | if (debugEnabled()) |
| | | { |
| | | TRACER.debugCaught(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | Message message = |
| | | ERR_LDAP_RESULT_DECODE_MATCHED_DN.get(String.valueOf(e)); |
| | | throw new LDAPException(PROTOCOL_ERROR, message, e); |
| | | } |
| | | |
| | | |
| | | Message errorMessage; |
| | | try |
| | | { |
| | | errorMessage = Message.raw( |
| | | elements.get(2).decodeAsOctetString().stringValue()); |
| | | if (errorMessage.length() == 0) |
| | | { |
| | | errorMessage = null; |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | if (debugEnabled()) |
| | | { |
| | | TRACER.debugCaught(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | Message message = |
| | | ERR_LDAP_RESULT_DECODE_ERROR_MESSAGE.get(String.valueOf(e)); |
| | | throw new LDAPException(PROTOCOL_ERROR, message, e); |
| | | } |
| | | |
| | | |
| | | ArrayList<String> referralURLs = null; |
| | | String oid = null; |
| | | ASN1OctetString value = null; |
| | | for (int i=3; i < elements.size(); i++) |
| | | { |
| | | element = elements.get(i); |
| | | switch (element.getType()) |
| | | { |
| | | case TYPE_REFERRAL_SEQUENCE: |
| | | try |
| | | { |
| | | ArrayList<ASN1Element> referralElements = |
| | | element.decodeAsSequence().elements(); |
| | | referralURLs = new ArrayList<String>(referralElements.size()); |
| | | |
| | | for (ASN1Element e : referralElements) |
| | | { |
| | | referralURLs.add(e.decodeAsOctetString().stringValue()); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | if (debugEnabled()) |
| | | { |
| | | TRACER.debugCaught(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | Message message = ERR_LDAP_EXTENDED_RESULT_DECODE_REFERRALS.get( |
| | | String.valueOf(e)); |
| | | throw new LDAPException(PROTOCOL_ERROR, message, e); |
| | | } |
| | | |
| | | break; |
| | | case TYPE_EXTENDED_RESPONSE_OID: |
| | | try |
| | | { |
| | | oid = element.decodeAsOctetString().stringValue(); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | if (debugEnabled()) |
| | | { |
| | | TRACER.debugCaught(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | Message message = |
| | | ERR_LDAP_EXTENDED_RESULT_DECODE_OID.get(String.valueOf(e)); |
| | | throw new LDAPException(PROTOCOL_ERROR, message, e); |
| | | } |
| | | |
| | | break; |
| | | case TYPE_EXTENDED_RESPONSE_VALUE: |
| | | try |
| | | { |
| | | value = element.decodeAsOctetString(); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | if (debugEnabled()) |
| | | { |
| | | TRACER.debugCaught(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | Message message = |
| | | ERR_LDAP_EXTENDED_RESULT_DECODE_VALUE.get(String.valueOf(e)); |
| | | throw new LDAPException(PROTOCOL_ERROR, message, e); |
| | | } |
| | | |
| | | break; |
| | | default: |
| | | Message message = ERR_LDAP_EXTENDED_RESULT_DECODE_INVALID_TYPE.get( |
| | | element.getType()); |
| | | throw new LDAPException(PROTOCOL_ERROR, message); |
| | | } |
| | | } |
| | | |
| | | |
| | | return new ExtendedResponseProtocolOp(resultCode, errorMessage, matchedDN, |
| | | referralURLs, oid, value); |
| | | stream.writeEndSequence(); |
| | | } |
| | | |
| | | |
| | |
| | | if (value != null) |
| | | { |
| | | buffer.append(", value="); |
| | | value.toString(buffer); |
| | | buffer.append(value.toString()); |
| | | } |
| | | |
| | | buffer.append(")"); |
| | |
| | | { |
| | | buffer.append(indentBuf); |
| | | buffer.append(" Response Value: "); |
| | | value.toString(buffer); |
| | | buffer.append(value.toString()); |
| | | buffer.append(EOL); |
| | | } |
| | | } |