001 /*
002 * Copyright 2009-2012 UnboundID Corp.
003 * All Rights Reserved.
004 */
005 /*
006 * Copyright (C) 2009-2012 UnboundID Corp.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021 package com.unboundid.ldap.protocol;
022
023
024
025 import com.unboundid.asn1.ASN1Buffer;
026 import com.unboundid.asn1.ASN1BufferSequence;
027 import com.unboundid.asn1.ASN1OctetString;
028 import com.unboundid.asn1.ASN1StreamReader;
029 import com.unboundid.asn1.ASN1StreamReaderSequence;
030 import com.unboundid.ldap.sdk.LDAPException;
031 import com.unboundid.ldap.sdk.ResultCode;
032 import com.unboundid.util.NotMutable;
033 import com.unboundid.util.InternalUseOnly;
034 import com.unboundid.util.ThreadSafety;
035 import com.unboundid.util.ThreadSafetyLevel;
036
037 import static com.unboundid.ldap.protocol.ProtocolMessages.*;
038 import static com.unboundid.util.Debug.*;
039 import static com.unboundid.util.StaticUtils.*;
040 import static com.unboundid.util.Validator.*;
041
042
043
044 /**
045 * This class provides an implementation of an LDAP extended request protocol
046 * op.
047 */
048 @InternalUseOnly()
049 @NotMutable()
050 @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
051 public final class ExtendedRequestProtocolOp
052 implements ProtocolOp
053 {
054 /**
055 * The BER type for the OID element.
056 */
057 public static final byte TYPE_OID = (byte) 0x80;
058
059
060
061 /**
062 * The BER type for the value element.
063 */
064 public static final byte TYPE_VALUE = (byte) 0x81;
065
066
067
068 /**
069 * The serial version UID for this serializable class.
070 */
071 private static final long serialVersionUID = -5343424210200494377L;
072
073
074
075 // The value for this extended request.
076 private final ASN1OctetString value;
077
078 // The OID for this extended request.
079 private final String oid;
080
081
082
083 /**
084 * Creates a new extended request protocol op with the provided information.
085 *
086 * @param oid The OID for this extended request.
087 * @param value The value for this extended request, or {@code null} if
088 * there should not be a value.
089 */
090 public ExtendedRequestProtocolOp(final String oid,
091 final ASN1OctetString value)
092 {
093 this.oid = oid;
094
095 if (value == null)
096 {
097 this.value = null;
098 }
099 else
100 {
101 this.value = new ASN1OctetString(TYPE_VALUE, value.getValue());
102 }
103 }
104
105
106
107 /**
108 * Creates a new extended request protocol op read from the provided ASN.1
109 * stream reader.
110 *
111 * @param reader The ASN.1 stream reader from which to read the extended
112 * request protocol op.
113 *
114 * @throws LDAPException If a problem occurs while reading or parsing the
115 * extended request.
116 */
117 ExtendedRequestProtocolOp(final ASN1StreamReader reader)
118 throws LDAPException
119 {
120 try
121 {
122 final ASN1StreamReaderSequence opSequence = reader.beginSequence();
123 oid = reader.readString();
124 ensureNotNull(oid);
125
126 if (opSequence.hasMoreElements())
127 {
128 value = new ASN1OctetString(TYPE_VALUE, reader.readBytes());
129 }
130 else
131 {
132 value = null;
133 }
134 }
135 catch (Exception e)
136 {
137 debugException(e);
138
139 throw new LDAPException(ResultCode.DECODING_ERROR,
140 ERR_EXTENDED_REQUEST_CANNOT_DECODE.get(getExceptionMessage(e)), e);
141 }
142 }
143
144
145
146 /**
147 * Retrieves the OID for this extended request.
148 *
149 * @return The OID for this extended request.
150 */
151 public String getOID()
152 {
153 return oid;
154 }
155
156
157
158 /**
159 * Retrieves the value for this extended request, if any.
160 *
161 * @return The value for this extended request, or {@code null} if there is
162 * no value.
163 */
164 public ASN1OctetString getValue()
165 {
166 return value;
167 }
168
169
170
171 /**
172 * {@inheritDoc}
173 */
174 public byte getProtocolOpType()
175 {
176 return LDAPMessage.PROTOCOL_OP_TYPE_EXTENDED_REQUEST;
177 }
178
179
180
181 /**
182 * {@inheritDoc}
183 */
184 public void writeTo(final ASN1Buffer buffer)
185 {
186 final ASN1BufferSequence opSequence =
187 buffer.beginSequence(LDAPMessage.PROTOCOL_OP_TYPE_EXTENDED_REQUEST);
188 buffer.addOctetString(TYPE_OID, oid);
189
190 if (value != null)
191 {
192 buffer.addOctetString(TYPE_VALUE, value.getValue());
193 }
194 opSequence.end();
195 }
196
197
198
199 /**
200 * Retrieves a string representation of this protocol op.
201 *
202 * @return A string representation of this protocol op.
203 */
204 @Override()
205 public String toString()
206 {
207 final StringBuilder buffer = new StringBuilder();
208 toString(buffer);
209 return buffer.toString();
210 }
211
212
213
214 /**
215 * {@inheritDoc}
216 */
217 public void toString(final StringBuilder buffer)
218 {
219 buffer.append("ExtendedRequestProtocolOp(oid='");
220 buffer.append(oid);
221 buffer.append("')");
222 }
223 }