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.forgerock.opendj.server.config.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  /** Creates a new instance of this subschemaSubentry virtual attribute provider. */
042  public SubschemaSubentryVirtualAttributeProvider()
043  {
044    super();
045
046    // All initialization should be performed in the
047    // initializeVirtualAttributeProvider method.
048  }
049
050  @Override
051  public boolean isMultiValued()
052  {
053    return false;
054  }
055
056  @Override
057  public Attribute getValues(Entry entry, VirtualAttributeRule rule)
058  {
059    DN schemaDN = DirectoryServer.getSchemaDN();
060    if (schemaDN == null)
061    {
062      return Attributes.empty(rule.getAttributeType());
063    }
064    return Attributes.create(rule.getAttributeType(), schemaDN.toString());
065  }
066
067  @Override
068  public ConditionResult matchesSubstring(Entry entry,
069                                          VirtualAttributeRule rule,
070                                          ByteString subInitial,
071                                          List<ByteString> subAny,
072                                          ByteString subFinal)
073  {
074    // DNs cannot be used in substring matching.
075    return ConditionResult.UNDEFINED;
076  }
077
078  @Override
079  public ConditionResult greaterThanOrEqualTo(Entry entry,
080                              VirtualAttributeRule rule,
081                              ByteString value)
082  {
083    // DNs cannot be used in ordering matching.
084    return ConditionResult.UNDEFINED;
085  }
086
087  @Override
088  public ConditionResult lessThanOrEqualTo(Entry entry,
089                              VirtualAttributeRule rule,
090                              ByteString value)
091  {
092    // DNs cannot be used in ordering matching.
093    return ConditionResult.UNDEFINED;
094  }
095
096  @Override
097  public ConditionResult approximatelyEqualTo(Entry entry,
098                              VirtualAttributeRule rule,
099                              ByteString value)
100  {
101    // DNs cannot be used in approximate matching.
102    return ConditionResult.UNDEFINED;
103  }
104
105  @Override
106  public boolean isSearchable(VirtualAttributeRule rule,
107                              SearchOperation searchOperation,
108                              boolean isPreIndexed)
109  {
110    // This attribute is not searchable, since it will have the same value in
111    // tons of entries.
112    return false;
113  }
114
115  @Override
116  public void processSearch(VirtualAttributeRule rule,
117                            SearchOperation searchOperation)
118  {
119    searchOperation.setResultCode(ResultCode.UNWILLING_TO_PERFORM);
120
121    LocalizableMessage message = ERR_VATTR_NOT_SEARCHABLE.get(
122            rule.getAttributeType().getNameOrOID());
123    searchOperation.appendErrorMessage(message);
124  }
125}