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