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-2016 ForgeRock AS.
016 */
017package org.opends.server.controls;
018import static org.opends.messages.ProtocolMessages.*;
019import static org.opends.server.util.ServerConstants.*;
020
021import java.io.IOException;
022
023import org.forgerock.i18n.LocalizableMessage;
024import org.forgerock.opendj.io.ASN1;
025import org.forgerock.opendj.io.ASN1Reader;
026import org.forgerock.opendj.io.ASN1Writer;
027import org.forgerock.opendj.ldap.ByteString;
028import org.forgerock.opendj.ldap.ResultCode;
029import org.opends.server.protocols.ldap.LDAPFilter;
030import org.opends.server.types.Control;
031import org.opends.server.types.DirectoryException;
032import org.opends.server.types.LDAPException;
033import org.opends.server.types.SearchFilter;
034
035
036/**
037 * This class implements the LDAP assertion request control as defined in RFC
038 * 4528.  This control makes it possible to conditionally perform an operation
039 * if a given assertion is true.  In particular, the associated operation should
040 * only be processed if the target entry matches the filter contained in this
041 * control.
042 */
043public class LDAPAssertionRequestControl
044    extends Control
045{
046  /** ControlDecoder implementation to decode this control from a ByteString. */
047  private static final class Decoder
048      implements ControlDecoder<LDAPAssertionRequestControl>
049  {
050    @Override
051    public LDAPAssertionRequestControl decode(boolean isCritical,
052                                              ByteString value)
053        throws DirectoryException
054    {
055      if (value == null)
056      {
057        LocalizableMessage message = ERR_LDAPASSERT_NO_CONTROL_VALUE.get();
058        throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message);
059      }
060
061      ASN1Reader reader = ASN1.getReader(value);
062      LDAPFilter filter;
063      try
064      {
065        filter = LDAPFilter.decode(reader);
066      }
067      catch (LDAPException e)
068      {
069        throw new DirectoryException(ResultCode.valueOf(e.getResultCode()), e
070            .getMessageObject(), e.getCause());
071      }
072
073      return new LDAPAssertionRequestControl(isCritical, filter);
074    }
075
076    @Override
077    public String getOID()
078    {
079      return OID_LDAP_ASSERTION;
080    }
081
082  }
083
084  /** The Control Decoder that can be used to decode this control. */
085  public static final ControlDecoder<LDAPAssertionRequestControl> DECODER =
086    new Decoder();
087
088
089
090  /** The unparsed LDAP search filter contained in the request from the client. */
091  private LDAPFilter rawFilter;
092
093  /** The processed search filter. */
094  private SearchFilter filter;
095
096
097
098  /**
099   * Creates a new instance of this LDAP assertion request control with the
100   * provided information.
101   *
102   * @param  isCritical  Indicates whether support for this control should be
103   *                     considered a critical part of the server processing.
104   * @param  rawFilter   The unparsed LDAP search filter contained in the
105   *                     request from the client.
106   */
107  public LDAPAssertionRequestControl(boolean isCritical, LDAPFilter rawFilter)
108  {
109    super(OID_LDAP_ASSERTION, isCritical);
110
111
112    this.rawFilter = rawFilter;
113
114    filter = null;
115  }
116
117  @Override
118  public void writeValue(ASN1Writer writer) throws IOException {
119    writer.writeStartSequence(ASN1.UNIVERSAL_OCTET_STRING_TYPE);
120    rawFilter.write(writer);
121    writer.writeEndSequence();
122  }
123
124
125
126  /**
127   * Retrieves the raw, unparsed filter from the request control.
128   *
129   * @return  The raw, unparsed filter from the request control.
130   */
131  public LDAPFilter getRawFilter()
132  {
133    return rawFilter;
134  }
135
136
137  /**
138   * Retrieves the processed search filter for this control.
139   *
140   * @return  The processed search filter for this control.
141   *
142   * @throws  DirectoryException  If a problem occurs while attempting to
143   *                              process the search filter.
144   */
145  public SearchFilter getSearchFilter()
146         throws DirectoryException
147  {
148    if (filter == null)
149    {
150      filter = rawFilter.toSearchFilter();
151    }
152    return filter;
153  }
154
155  @Override
156  public void toString(StringBuilder buffer)
157  {
158    buffer.append("LDAPAssertionRequestControl(criticality=");
159    buffer.append(isCritical());
160    buffer.append(",filter=\"");
161    rawFilter.toString(buffer);
162    buffer.append("\")");
163  }
164}