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 2009-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2011-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.ResultCode;
033import org.opends.server.admin.std.server.
034        CollectiveAttributeSubentriesVirtualAttributeCfg;
035import org.opends.server.api.VirtualAttributeProvider;
036import org.opends.server.core.DirectoryServer;
037import org.opends.server.core.SearchOperation;
038import org.opends.server.types.*;
039
040import static org.opends.messages.ExtensionMessages.*;
041
042/**
043 * This class implements a virtual attribute provider to serve the
044 * collectiveAttributeSubentries operational attribute as described
045 * in RFC 3671.
046 */
047public class CollectiveAttributeSubentriesVirtualAttributeProvider
048        extends VirtualAttributeProvider<
049        CollectiveAttributeSubentriesVirtualAttributeCfg>
050{
051  /**
052   * Creates a new instance of this collectiveAttributeSubentries
053   * virtual attribute provider.
054   */
055  public CollectiveAttributeSubentriesVirtualAttributeProvider()
056  {
057    super();
058
059    // All initialization should be performed in the
060    // initializeVirtualAttributeProvider method.
061  }
062
063  /** {@inheritDoc} */
064  @Override
065  public boolean isMultiValued()
066  {
067    return true;
068  }
069
070  /** {@inheritDoc} */
071  @Override
072  public Attribute getValues(Entry entry, VirtualAttributeRule rule)
073  {
074
075    AttributeBuilder builder = new AttributeBuilder(rule.getAttributeType());
076    if (!entry.isSubentry() && !entry.isLDAPSubentry())
077    {
078      List<SubEntry> subentries = DirectoryServer.getSubentryManager()
079          .getCollectiveSubentries(entry);
080
081      for (SubEntry subentry : subentries)
082      {
083        if (subentry.isCollective() ||
084            subentry.isInheritedCollective())
085        {
086          builder.add(subentry.getDN().toString());
087        }
088      }
089    }
090    return builder.toAttribute();
091  }
092
093  /** {@inheritDoc} */
094  @Override
095  public boolean isSearchable(VirtualAttributeRule rule,
096                              SearchOperation searchOperation,
097                              boolean isPreIndexed)
098  {
099    return false;
100  }
101
102  /** {@inheritDoc} */
103  @Override
104  public void processSearch(VirtualAttributeRule rule,
105                            SearchOperation searchOperation)
106  {
107    searchOperation.setResultCode(ResultCode.UNWILLING_TO_PERFORM);
108
109    LocalizableMessage message =
110            ERR_COLLECTIVEATTRIBUTESUBENTRIES_VATTR_NOT_SEARCHABLE.get(
111            rule.getAttributeType().getNameOrOID());
112    searchOperation.appendErrorMessage(message);
113  }
114}