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 * docs/licenses/cddl.txt
011 * or http://www.opensource.org/licenses/cddl1.php.
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 * docs/licenses/cddl.txt.  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 2013-2016 UnboundID Corp.
026 */
027package com.unboundid.directory.sdk.ds.api;
028
029
030
031import java.util.Collections;
032import java.util.List;
033import java.util.Map;
034
035import com.unboundid.directory.sdk.broker.internal.BrokerExtension;
036import com.unboundid.directory.sdk.common.internal.ExampleUsageProvider;
037import com.unboundid.directory.sdk.common.internal.Reconfigurable;
038import com.unboundid.directory.sdk.common.internal.UnboundIDExtension;
039import com.unboundid.directory.sdk.common.types.OperationContext;
040import com.unboundid.directory.sdk.ds.config.RequestCriteriaConfig;
041import com.unboundid.directory.sdk.ds.types.DirectoryServerContext;
042import com.unboundid.directory.sdk.ds.internal.DirectoryServerExtension;
043import com.unboundid.directory.sdk.metrics.internal.MetricsEngineExtension;
044import com.unboundid.directory.sdk.proxy.internal.DirectoryProxyServerExtension;
045import com.unboundid.directory.sdk.sync.internal.SynchronizationServerExtension;
046import com.unboundid.ldap.sdk.LDAPException;
047import com.unboundid.ldap.sdk.ResultCode;
048import com.unboundid.util.Extensible;
049import com.unboundid.util.ThreadSafety;
050import com.unboundid.util.ThreadSafetyLevel;
051import com.unboundid.util.args.ArgumentException;
052import com.unboundid.util.args.ArgumentParser;
053
054
055
056/**
057 * This class defines an API that must be implemented by extensions which can
058 * be used to classify client requests.
059 * <BR>
060 * <H2>Configuring Request Criteria</H2>
061 * In order to configure a request criteria object created using this API, use a
062 * command like:
063 * <PRE>
064 *      dsconfig create-request-criteria \
065 *           --criteria-name "<I>{criteria-name}</I>" \
066 *           --type third-party \
067 *           --set enabled:true \
068 *           --set "extension-class:<I>{class-name}</I>" \
069 *           --set "extension-argument:<I>{name=value}</I>"
070 * </PRE>
071 * where "<I>{criteria-name}</I>" is the name to use for the request criteria
072 * instance, "<I>{class-name}</I>" is the fully-qualified name of the Java class
073 * that extends {@code com.unboundid.directory.sdk.ds.api.RequestCriteria}, and
074 * "<I>{name=value}</I>" represents name-value pairs for any arguments to
075 * provide to the request criteria.  If multiple arguments should be provided to
076 * the request criteria instance, then the
077 * "<CODE>--set extension-argument:<I>{name=value}</I></CODE>" option should be
078 * provided multiple times.
079 */
080@Extensible()
081@DirectoryServerExtension()
082@DirectoryProxyServerExtension(appliesToLocalContent=true,
083     appliesToRemoteContent=true)
084@SynchronizationServerExtension(appliesToLocalContent=true,
085     appliesToSynchronizedContent=false)
086@MetricsEngineExtension()
087@BrokerExtension()
088@ThreadSafety(level=ThreadSafetyLevel.INTERFACE_THREADSAFE)
089public abstract class RequestCriteria
090       implements UnboundIDExtension,
091                  Reconfigurable<RequestCriteriaConfig>,
092                  ExampleUsageProvider
093{
094  /**
095   * Creates a new instance of this request criteria.  All request criteria
096   * implementations must include a default constructor, but any initialization
097   * should generally be done in the {@code initializeRequestCriteria} method.
098   */
099  public RequestCriteria()
100  {
101    // No implementation is required.
102  }
103
104
105
106  /**
107   * {@inheritDoc}
108   */
109  public abstract String getExtensionName();
110
111
112
113  /**
114   * {@inheritDoc}
115   */
116  public abstract String[] getExtensionDescription();
117
118
119
120  /**
121   * {@inheritDoc}
122   */
123  public void defineConfigArguments(final ArgumentParser parser)
124         throws ArgumentException
125  {
126    // No arguments will be allowed by default.
127  }
128
129
130
131  /**
132   * Initializes this request criteria.
133   *
134   * @param  serverContext  A handle to the server context for the server in
135   *                        which this extension is running.
136   * @param  config         The general configuration for this request criteria.
137   * @param  parser         The argument parser which has been initialized from
138   *                        the configuration for this request criteria.
139   *
140   * @throws  LDAPException  If a problem occurs while initializing this request
141   *                         criteria.
142   */
143  public void initializeRequestCriteria(
144                   final DirectoryServerContext serverContext,
145                   final RequestCriteriaConfig config,
146                   final ArgumentParser parser)
147         throws LDAPException
148  {
149    // No initialization will be performed by default.
150  }
151
152
153
154  /**
155   * {@inheritDoc}
156   */
157  public boolean isConfigurationAcceptable(
158                      final RequestCriteriaConfig config,
159                      final ArgumentParser parser,
160                      final List<String> unacceptableReasons)
161  {
162    // No extended validation will be performed by default.
163    return true;
164  }
165
166
167
168  /**
169   * {@inheritDoc}
170   */
171  public ResultCode applyConfiguration(final RequestCriteriaConfig config,
172                                       final ArgumentParser parser,
173                                       final List<String> adminActionsRequired,
174                                       final List<String> messages)
175  {
176    // By default, no configuration changes will be applied.  If there are any
177    // arguments, then add an admin action message indicating that the extension
178    // needs to be restarted for any changes to take effect.
179    if (! parser.getNamedArguments().isEmpty())
180    {
181      adminActionsRequired.add(
182           "No configuration change has actually been applied.  The new " +
183                "configuration will not take effect until this request " +
184                "criteria is disabled and re-enabled or until the server is " +
185                "restarted.");
186    }
187
188    return ResultCode.SUCCESS;
189  }
190
191
192
193  /**
194   * Performs any cleanup which may be necessary when this request criteria
195   * is to be taken out of service.
196   */
197  public void finalizeRequestCriteria()
198  {
199    // No implementation is required.
200  }
201
202
203
204  /**
205   * Indicates whether the provided operation matches the criteria for this
206   * request criteria object.
207   *
208   * @param  o  The operation for which to make the determination.
209   *
210   * @return  {@code true} if the provided operation matches the criteria for
211   *          this request criteria object, or {@code false} if not.
212   */
213  public abstract boolean matches(final OperationContext o);
214
215
216
217  /**
218   * {@inheritDoc}
219   */
220  public Map<List<String>,String> getExamplesArgumentSets()
221  {
222    return Collections.emptyMap();
223  }
224}