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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.controls;
028import org.forgerock.i18n.LocalizableMessage;
029
030
031import org.forgerock.opendj.io.*;
032import org.opends.server.protocols.ldap.LDAPFilter;
033import org.opends.server.types.*;
034import org.forgerock.opendj.ldap.ResultCode;
035import org.forgerock.opendj.ldap.ByteString;
036import static org.opends.messages.ProtocolMessages.*;
037import static org.opends.server.util.ServerConstants.*;
038
039import java.io.IOException;
040
041
042/**
043 * This class implements the LDAP assertion request control as defined in RFC
044 * 4528.  This control makes it possible to conditionally perform an operation
045 * if a given assertion is true.  In particular, the associated operation should
046 * only be processed if the target entry matches the filter contained in this
047 * control.
048 */
049public class LDAPAssertionRequestControl
050    extends Control
051{
052  /**
053   * ControlDecoder implementation to decode this control from a ByteString.
054   */
055  private static final class Decoder
056      implements ControlDecoder<LDAPAssertionRequestControl>
057  {
058    /** {@inheritDoc} */
059    public LDAPAssertionRequestControl decode(boolean isCritical,
060                                              ByteString value)
061        throws DirectoryException
062    {
063      if (value == null)
064      {
065        LocalizableMessage message = ERR_LDAPASSERT_NO_CONTROL_VALUE.get();
066        throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message);
067      }
068
069      ASN1Reader reader = ASN1.getReader(value);
070      LDAPFilter filter;
071      try
072      {
073        filter = LDAPFilter.decode(reader);
074      }
075      catch (LDAPException e)
076      {
077        throw new DirectoryException(ResultCode.valueOf(e.getResultCode()), e
078            .getMessageObject(), e.getCause());
079      }
080
081      return new LDAPAssertionRequestControl(isCritical, filter);
082    }
083
084    public String getOID()
085    {
086      return OID_LDAP_ASSERTION;
087    }
088
089  }
090
091  /**
092   * The Control Decoder that can be used to decode this control.
093   */
094  public static final ControlDecoder<LDAPAssertionRequestControl> DECODER =
095    new Decoder();
096
097
098
099  /** The unparsed LDAP search filter contained in the request from the client. */
100  private LDAPFilter rawFilter;
101
102  /** The processed search filter. */
103  private SearchFilter filter;
104
105
106
107  /**
108   * Creates a new instance of this LDAP assertion request control with the
109   * provided information.
110   *
111   * @param  isCritical  Indicates whether support for this control should be
112   *                     considered a critical part of the server processing.
113   * @param  rawFilter   The unparsed LDAP search filter contained in the
114   *                     request from the client.
115   */
116  public LDAPAssertionRequestControl(boolean isCritical, LDAPFilter rawFilter)
117  {
118    super(OID_LDAP_ASSERTION, isCritical);
119
120
121    this.rawFilter = rawFilter;
122
123    filter = null;
124  }
125
126
127
128  /**
129   * Writes this control's value to an ASN.1 writer. The value (if any) must be
130   * written as an ASN1OctetString.
131   *
132   * @param writer The ASN.1 output stream to write to.
133   * @throws IOException If a problem occurs while writing to the stream.
134   */
135  @Override
136  public void writeValue(ASN1Writer writer) throws IOException {
137    writer.writeStartSequence(ASN1.UNIVERSAL_OCTET_STRING_TYPE);
138    rawFilter.write(writer);
139    writer.writeEndSequence();
140  }
141
142
143
144  /**
145   * Retrieves the raw, unparsed filter from the request control.
146   *
147   * @return  The raw, unparsed filter from the request control.
148   */
149  public LDAPFilter getRawFilter()
150  {
151    return rawFilter;
152  }
153
154
155  /**
156   * Retrieves the processed search filter for this control.
157   *
158   * @return  The processed search filter for this control.
159   *
160   * @throws  DirectoryException  If a problem occurs while attempting to
161   *                              process the search filter.
162   */
163  public SearchFilter getSearchFilter()
164         throws DirectoryException
165  {
166    if (filter == null)
167    {
168      filter = rawFilter.toSearchFilter();
169    }
170
171    return filter;
172  }
173
174
175
176  /**
177   * Appends a string representation of this LDAP assertion request control to
178   * the provided buffer.
179   *
180   * @param  buffer  The buffer to which the information should be appended.
181   */
182  @Override
183  public void toString(StringBuilder buffer)
184  {
185    buffer.append("LDAPAssertionRequestControl(criticality=");
186    buffer.append(isCritical());
187    buffer.append(",filter=\"");
188    rawFilter.toString(buffer);
189    buffer.append("\")");
190  }
191}
192