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-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.ResultCode;
023import org.forgerock.opendj.server.config.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  /** Creates a new instance of this collectiveAttributeSubentries virtual attribute provider. */
042  public CollectiveAttributeSubentriesVirtualAttributeProvider()
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 true;
054  }
055
056  @Override
057  public Attribute getValues(Entry entry, VirtualAttributeRule rule)
058  {
059    AttributeBuilder builder = new AttributeBuilder(rule.getAttributeType());
060    if (!entry.isSubentry() && !entry.isLDAPSubentry())
061    {
062      List<SubEntry> subentries = DirectoryServer.getSubentryManager()
063          .getCollectiveSubentries(entry);
064
065      for (SubEntry subentry : subentries)
066      {
067        if (subentry.isCollective() ||
068            subentry.isInheritedCollective())
069        {
070          builder.add(subentry.getDN().toString());
071        }
072      }
073    }
074    return builder.toAttribute();
075  }
076
077  @Override
078  public boolean isSearchable(VirtualAttributeRule rule,
079                              SearchOperation searchOperation,
080                              boolean isPreIndexed)
081  {
082    return false;
083  }
084
085  @Override
086  public void processSearch(VirtualAttributeRule rule,
087                            SearchOperation searchOperation)
088  {
089    searchOperation.setResultCode(ResultCode.UNWILLING_TO_PERFORM);
090
091    LocalizableMessage message =
092            ERR_COLLECTIVEATTRIBUTESUBENTRIES_VATTR_NOT_SEARCHABLE.get(
093            rule.getAttributeType().getNameOrOID());
094    searchOperation.appendErrorMessage(message);
095  }
096}