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-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2012-2015 ForgeRock AS
026 */
027package org.opends.server.extensions;
028
029import java.util.List;
030
031import org.forgerock.i18n.LocalizableMessage;
032import org.forgerock.opendj.ldap.ByteString;
033import org.forgerock.opendj.ldap.ConditionResult;
034import org.forgerock.opendj.ldap.ResultCode;
035import org.opends.server.admin.std.server.SubschemaSubentryVirtualAttributeCfg;
036import org.opends.server.api.VirtualAttributeProvider;
037import org.opends.server.core.DirectoryServer;
038import org.opends.server.core.SearchOperation;
039import org.opends.server.types.*;
040
041import static org.opends.messages.ExtensionMessages.*;
042
043/**
044 * This class implements a virtual attribute provider that is meant to serve the
045 * subschemaSubentry operational attribute as described in RFC 4512.
046 */
047public class SubschemaSubentryVirtualAttributeProvider
048       extends VirtualAttributeProvider<SubschemaSubentryVirtualAttributeCfg>
049{
050  /**
051   * Creates a new instance of this subschemaSubentry virtual attribute
052   * provider.
053   */
054  public SubschemaSubentryVirtualAttributeProvider()
055  {
056    super();
057
058    // All initialization should be performed in the
059    // initializeVirtualAttributeProvider method.
060  }
061
062  /** {@inheritDoc} */
063  @Override
064  public boolean isMultiValued()
065  {
066    return false;
067  }
068
069  /** {@inheritDoc} */
070  @Override
071  public Attribute getValues(Entry entry, VirtualAttributeRule rule)
072  {
073    DN schemaDN = DirectoryServer.getSchemaDN();
074    if (schemaDN == null)
075    {
076      return Attributes.empty(rule.getAttributeType());
077    }
078    return Attributes.create(rule.getAttributeType(), schemaDN.toString());
079  }
080
081  /** {@inheritDoc} */
082  @Override
083  public ConditionResult matchesSubstring(Entry entry,
084                                          VirtualAttributeRule rule,
085                                          ByteString subInitial,
086                                          List<ByteString> subAny,
087                                          ByteString subFinal)
088  {
089    // DNs cannot be used in substring matching.
090    return ConditionResult.UNDEFINED;
091  }
092
093  /** {@inheritDoc} */
094  @Override
095  public ConditionResult greaterThanOrEqualTo(Entry entry,
096                              VirtualAttributeRule rule,
097                              ByteString value)
098  {
099    // DNs cannot be used in ordering matching.
100    return ConditionResult.UNDEFINED;
101  }
102
103  /** {@inheritDoc} */
104  @Override
105  public ConditionResult lessThanOrEqualTo(Entry entry,
106                              VirtualAttributeRule rule,
107                              ByteString value)
108  {
109    // DNs cannot be used in ordering matching.
110    return ConditionResult.UNDEFINED;
111  }
112
113  /** {@inheritDoc} */
114  @Override
115  public ConditionResult approximatelyEqualTo(Entry entry,
116                              VirtualAttributeRule rule,
117                              ByteString value)
118  {
119    // DNs cannot be used in approximate matching.
120    return ConditionResult.UNDEFINED;
121  }
122
123  /** {@inheritDoc} */
124  @Override
125  public boolean isSearchable(VirtualAttributeRule rule,
126                              SearchOperation searchOperation,
127                              boolean isPreIndexed)
128  {
129    // This attribute is not searchable, since it will have the same value in
130    // tons of entries.
131    return false;
132  }
133
134  /** {@inheritDoc} */
135  @Override
136  public void processSearch(VirtualAttributeRule rule,
137                            SearchOperation searchOperation)
138  {
139    searchOperation.setResultCode(ResultCode.UNWILLING_TO_PERFORM);
140
141    LocalizableMessage message = ERR_VATTR_NOT_SEARCHABLE.get(
142            rule.getAttributeType().getNameOrOID());
143    searchOperation.appendErrorMessage(message);
144  }
145}
146