001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.controls;
028    import org.opends.messages.Message;
029    
030    
031    
032    import org.opends.server.protocols.asn1.ASN1Element;
033    import org.opends.server.protocols.asn1.ASN1Exception;
034    import org.opends.server.protocols.asn1.ASN1OctetString;
035    import org.opends.server.protocols.ldap.LDAPFilter;
036    import org.opends.server.protocols.ldap.LDAPResultCode;
037    import org.opends.server.types.Control;
038    import org.opends.server.types.DirectoryException;
039    import org.opends.server.types.DebugLogLevel;
040    import org.opends.server.types.LDAPException;
041    import org.opends.server.types.SearchFilter;
042    
043    import static org.opends.server.loggers.debug.DebugLogger.*;
044    import org.opends.server.loggers.debug.DebugTracer;
045    import static org.opends.messages.ProtocolMessages.*;
046    import static org.opends.server.util.ServerConstants.*;
047    
048    
049    
050    /**
051     * This class implements the LDAP assertion request control as defined in RFC
052     * 4528.  This control makes it possible to conditionally perform an operation
053     * if a given assertion is true.  In particular, the associated operation should
054     * only be processed if the target entry matches the filter contained in this
055     * control.
056     */
057    public class LDAPAssertionRequestControl
058           extends Control
059    {
060      /**
061       * The tracer object for the debug logger.
062       */
063      private static final DebugTracer TRACER = getTracer();
064    
065    
066    
067    
068      // The unparsed LDAP search filter contained in the request from the client.
069      private LDAPFilter rawFilter;
070    
071      // The processed search filter
072      private SearchFilter filter;
073    
074    
075    
076      /**
077       * Creates a new instance of this LDAP assertion request control with the
078       * provided information.
079       *
080       * @param  isCritical  Indicates whether support for this control should be
081       *                     considered a critical part of the server processing.
082       * @param  rawFilter   The unparsed LDAP search filter contained in the
083       *                     request from the client.
084       */
085      public LDAPAssertionRequestControl(boolean isCritical, LDAPFilter rawFilter)
086      {
087        super(OID_LDAP_ASSERTION, isCritical,
088              new ASN1OctetString(rawFilter.encode().encode()));
089    
090    
091        this.rawFilter = rawFilter;
092    
093        filter = null;
094      }
095    
096    
097    
098      /**
099       * Creates a new instance of this LDAP assertion request control with the
100       * provided information.
101       *
102       * @param  oid         The OID to use for this control.
103       * @param  isCritical  Indicates whether support for this control should be
104       *                     considered a critical part of the server processing.
105       * @param  rawFilter   The unparsed LDAP search filter contained in the
106       *                     request from the client.
107       */
108      public LDAPAssertionRequestControl(String oid, boolean isCritical,
109                                         LDAPFilter rawFilter)
110      {
111        super(oid, isCritical, new ASN1OctetString(rawFilter.encode().encode()));
112    
113    
114        this.rawFilter = rawFilter;
115    
116        filter = null;
117      }
118    
119    
120    
121      /**
122       * Creates a new instance of this LDAP assertion request control with the
123       * provided information.
124       *
125       * @param  oid           The OID to use for this control.
126       * @param  isCritical    Indicates whether support for this control should be
127       *                       considered a critical part of the server processing.
128       * @param  rawFilter     The unparsed LDAP search filter contained in the
129       *                       request from the client.
130       * @param  encodedValue  The pre-encoded value for this control.
131       */
132      private LDAPAssertionRequestControl(String oid, boolean isCritical,
133                                          LDAPFilter rawFilter,
134                                          ASN1OctetString encodedValue)
135      {
136        super(oid, isCritical, encodedValue);
137    
138    
139        this.rawFilter = rawFilter;
140    
141        filter = null;
142      }
143    
144    
145    
146      /**
147       * Creates a new LDAP assertion request control from the contents of the
148       * provided control.
149       *
150       * @param  control  The generic control containing the information to use to
151       *                  create this LDAP assertion request control.
152       *
153       * @return  The LDAP assertion control decoded from the provided control.
154       *
155       * @throws  LDAPException  If this control cannot be decoded as a valid LDAP
156       *                         assertion control.
157       */
158      public static LDAPAssertionRequestControl decodeControl(Control control)
159             throws LDAPException
160      {
161        if (! control.hasValue())
162        {
163          Message message = ERR_LDAPASSERT_NO_CONTROL_VALUE.get();
164          throw new LDAPException(LDAPResultCode.PROTOCOL_ERROR, message);
165        }
166    
167    
168        ASN1Element valueElement;
169        try
170        {
171          valueElement = ASN1Element.decode(control.getValue().value());
172        }
173        catch (ASN1Exception ae)
174        {
175          if (debugEnabled())
176          {
177            TRACER.debugCaught(DebugLogLevel.ERROR, ae);
178          }
179    
180          Message message =
181              ERR_LDAPASSERT_INVALID_CONTROL_VALUE.get(ae.getMessage());
182          throw new LDAPException(LDAPResultCode.PROTOCOL_ERROR, message,
183                                  ae);
184        }
185    
186    
187        return new LDAPAssertionRequestControl(control.getOID(),
188                                               control.isCritical(),
189                                               LDAPFilter.decode(valueElement),
190                                               control.getValue());
191      }
192    
193    
194    
195      /**
196       * Retrieves the raw, unparsed filter from the request control.
197       *
198       * @return  The raw, unparsed filter from the request control.
199       */
200      public LDAPFilter getRawFilter()
201      {
202        return rawFilter;
203      }
204    
205    
206    
207      /**
208       * Sets the raw, unparsed filter from the request control.  This method should
209       * only be called by pre-parse plugins.
210       *
211       * @param  rawFilter  The raw, unparsed filter from the request control.
212       */
213      public void setRawFilter(LDAPFilter rawFilter)
214      {
215        this.rawFilter = rawFilter;
216        this.filter    = null;
217    
218        setValue(new ASN1OctetString(rawFilter.encode().encode()));
219      }
220    
221    
222    
223      /**
224       * Retrieves the processed search filter for this control.
225       *
226       * @return  The processed search filter for this control.
227       *
228       * @throws  DirectoryException  If a problem occurs while attempting to
229       *                              process the search filter.
230       */
231      public SearchFilter getSearchFilter()
232             throws DirectoryException
233      {
234        if (filter == null)
235        {
236          filter = rawFilter.toSearchFilter();
237        }
238    
239        return filter;
240      }
241    
242    
243    
244      /**
245       * Retrieves a string representation of this LDAP assertion request control.
246       *
247       * @return  A string representation of this LDAP assertion request control.
248       */
249      public String toString()
250      {
251        StringBuilder buffer = new StringBuilder();
252        toString(buffer);
253        return buffer.toString();
254      }
255    
256    
257    
258      /**
259       * Appends a string representation of this LDAP assertion request control to
260       * the provided buffer.
261       *
262       * @param  buffer  The buffer to which the information should be appended.
263       */
264      public void toString(StringBuilder buffer)
265      {
266        buffer.append("LDAPAssertionRequestControl(criticality=");
267        buffer.append(isCritical());
268        buffer.append(",filter=\"");
269        rawFilter.toString(buffer);
270        buffer.append("\")");
271      }
272    }
273