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 Sun Microsystems, Inc.
025 *      Portions Copyright 2012-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.ByteString;
033import org.forgerock.opendj.ldap.ConditionResult;
034import org.forgerock.opendj.ldap.ResultCode;
035import org.opends.server.admin.std.server.StructuralObjectClassVirtualAttributeCfg;
036import org.opends.server.api.VirtualAttributeProvider;
037import org.opends.server.core.SearchOperation;
038import org.opends.server.types.Attribute;
039import org.opends.server.types.Attributes;
040import org.opends.server.types.Entry;
041import org.opends.server.types.VirtualAttributeRule;
042
043import static org.opends.messages.ExtensionMessages.*;
044
045/**
046 * This class implements a virtual attribute provider that is meant to serve
047 * the structuralObjectClass operational attribute as described in RFC 4512.
048 */
049public class StructuralObjectClassVirtualAttributeProvider
050     extends VirtualAttributeProvider<StructuralObjectClassVirtualAttributeCfg>
051{
052  /**
053   * Creates a new instance of this structuralObjectClass virtual attribute
054   * provider.
055   */
056  public StructuralObjectClassVirtualAttributeProvider()
057  {
058    super();
059
060    // All initialization should be performed in the
061    // initializeVirtualAttributeProvider method.
062  }
063
064  /** {@inheritDoc} */
065  @Override
066  public boolean isMultiValued()
067  {
068    return false;
069  }
070
071  /** {@inheritDoc} */
072  @Override
073  public Attribute getValues(Entry entry, VirtualAttributeRule rule)
074  {
075    return Attributes.create(rule.getAttributeType(), entry
076        .getStructuralObjectClass().getNameOrOID());
077  }
078
079  /** {@inheritDoc} */
080  @Override
081  public boolean hasValue(Entry entry, VirtualAttributeRule rule)
082  {
083    return entry.getStructuralObjectClass() != null;
084  }
085
086  /** {@inheritDoc} */
087  @Override
088  public ConditionResult matchesSubstring(Entry entry,
089                                          VirtualAttributeRule rule,
090                                          ByteString subInitial,
091                                          List<ByteString> subAny,
092                                          ByteString subFinal)
093  {
094    //Substring matching is not supported.
095    return ConditionResult.UNDEFINED;
096  }
097
098  /** {@inheritDoc} */
099  @Override
100  public ConditionResult greaterThanOrEqualTo(Entry entry,
101                              VirtualAttributeRule rule,
102                              ByteString value)
103  {
104    // An object class can not be used for ordering.
105    return ConditionResult.UNDEFINED;
106  }
107
108  /** {@inheritDoc} */
109  @Override
110  public ConditionResult lessThanOrEqualTo(Entry entry,
111                              VirtualAttributeRule rule,
112                              ByteString value)
113  {
114    // An object class can not be used for ordering.
115    return ConditionResult.UNDEFINED;
116  }
117
118  /** {@inheritDoc} */
119  @Override
120  public ConditionResult approximatelyEqualTo(Entry entry,
121                              VirtualAttributeRule rule,
122                              ByteString value)
123  {
124    // An object class can not be used in approximate matching.
125    return ConditionResult.UNDEFINED;
126  }
127
128  /** {@inheritDoc} */
129  @Override
130  public boolean isSearchable(VirtualAttributeRule rule,
131                              SearchOperation searchOperation,
132                              boolean isPreIndexed)
133  {
134    // This attribute is not searchable, since it will have the same value in
135    // tons of entries.
136    return false;
137  }
138
139  /** {@inheritDoc} */
140  @Override
141  public void processSearch(VirtualAttributeRule rule,
142                            SearchOperation searchOperation)
143  {
144    searchOperation.setResultCode(ResultCode.UNWILLING_TO_PERFORM);
145
146    LocalizableMessage message = ERR_VATTR_NOT_SEARCHABLE.get(
147            rule.getAttributeType().getNameOrOID());
148    searchOperation.appendErrorMessage(message);
149  }
150}
151