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