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 2009-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2011-2015 ForgeRock AS.
016 */
017package org.opends.server.extensions;
018
019import java.util.List;
020
021import org.forgerock.i18n.LocalizableMessage;
022import org.forgerock.opendj.ldap.ResultCode;
023import org.opends.server.admin.std.server.
024        CollectiveAttributeSubentriesVirtualAttributeCfg;
025import org.opends.server.api.VirtualAttributeProvider;
026import org.opends.server.core.DirectoryServer;
027import org.opends.server.core.SearchOperation;
028import org.opends.server.types.*;
029
030import static org.opends.messages.ExtensionMessages.*;
031
032/**
033 * This class implements a virtual attribute provider to serve the
034 * collectiveAttributeSubentries operational attribute as described
035 * in RFC 3671.
036 */
037public class CollectiveAttributeSubentriesVirtualAttributeProvider
038        extends VirtualAttributeProvider<
039        CollectiveAttributeSubentriesVirtualAttributeCfg>
040{
041  /**
042   * Creates a new instance of this collectiveAttributeSubentries
043   * virtual attribute provider.
044   */
045  public CollectiveAttributeSubentriesVirtualAttributeProvider()
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 true;
058  }
059
060  /** {@inheritDoc} */
061  @Override
062  public Attribute getValues(Entry entry, VirtualAttributeRule rule)
063  {
064
065    AttributeBuilder builder = new AttributeBuilder(rule.getAttributeType());
066    if (!entry.isSubentry() && !entry.isLDAPSubentry())
067    {
068      List<SubEntry> subentries = DirectoryServer.getSubentryManager()
069          .getCollectiveSubentries(entry);
070
071      for (SubEntry subentry : subentries)
072      {
073        if (subentry.isCollective() ||
074            subentry.isInheritedCollective())
075        {
076          builder.add(subentry.getDN().toString());
077        }
078      }
079    }
080    return builder.toAttribute();
081  }
082
083  /** {@inheritDoc} */
084  @Override
085  public boolean isSearchable(VirtualAttributeRule rule,
086                              SearchOperation searchOperation,
087                              boolean isPreIndexed)
088  {
089    return false;
090  }
091
092  /** {@inheritDoc} */
093  @Override
094  public void processSearch(VirtualAttributeRule rule,
095                            SearchOperation searchOperation)
096  {
097    searchOperation.setResultCode(ResultCode.UNWILLING_TO_PERFORM);
098
099    LocalizableMessage message =
100            ERR_COLLECTIVEATTRIBUTESUBENTRIES_VATTR_NOT_SEARCHABLE.get(
101            rule.getAttributeType().getNameOrOID());
102    searchOperation.appendErrorMessage(message);
103  }
104}