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 2008-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2012-2015 ForgeRock AS
026 */
027package org.opends.server.extensions;
028
029import static org.opends.messages.ExtensionMessages.*;
030
031import java.util.List;
032import java.util.UUID;
033
034import org.forgerock.i18n.LocalizableMessage;
035import org.forgerock.opendj.ldap.ByteString;
036import org.forgerock.opendj.ldap.ConditionResult;
037import org.forgerock.opendj.ldap.ResultCode;
038import org.opends.server.admin.std.server.EntryUUIDVirtualAttributeCfg;
039import org.opends.server.api.VirtualAttributeProvider;
040import org.opends.server.core.SearchOperation;
041import org.opends.server.types.Attribute;
042import org.opends.server.types.Attributes;
043import org.opends.server.types.Entry;
044import org.opends.server.types.VirtualAttributeRule;
045
046/**
047 * This class implements a virtual attribute provider that is meant to serve the
048 * entryUUID operational attribute as described in RFC 4530.  Note that this
049 * should only be used for entries used in conjunction with data in private
050 * backends (e.g., those holding the configuration, schema, monitor, and root
051 * DSE entries).  Real user data should have entry UUID values generated at the
052 * time the entries are added or imported.
053 */
054public class EntryUUIDVirtualAttributeProvider
055       extends VirtualAttributeProvider<EntryUUIDVirtualAttributeCfg>
056{
057  /**
058   * Creates a new instance of this entryUUID virtual attribute provider.
059   */
060  public EntryUUIDVirtualAttributeProvider()
061  {
062    super();
063
064    // All initialization should be performed in the
065    // initializeVirtualAttributeProvider method.
066  }
067
068  /** {@inheritDoc} */
069  @Override
070  public boolean isMultiValued()
071  {
072    return false;
073  }
074
075  /** {@inheritDoc} */
076  @Override
077  public Attribute getValues(Entry entry, VirtualAttributeRule rule)
078  {
079    ByteString normDN = entry.getName().toNormalizedByteString();
080    String uuid = UUID.nameUUIDFromBytes(normDN.toByteArray()).toString();
081    return Attributes.create(rule.getAttributeType(), uuid);
082  }
083
084  /** {@inheritDoc} */
085  @Override
086  public boolean hasValue(Entry entry, VirtualAttributeRule rule)
087  {
088    // This virtual attribute provider will always generate a value.
089    return true;
090  }
091
092  /** {@inheritDoc} */
093  @Override
094  public ConditionResult matchesSubstring(Entry entry,
095                                          VirtualAttributeRule rule,
096                                          ByteString subInitial,
097                                          List<ByteString> subAny,
098                                          ByteString subFinal)
099  {
100    // DNs cannot be used in substring matching.
101    return ConditionResult.UNDEFINED;
102  }
103
104  /** {@inheritDoc} */
105  @Override
106  public ConditionResult greaterThanOrEqualTo(Entry entry,
107                              VirtualAttributeRule rule,
108                              ByteString value)
109  {
110    // DNs cannot be used in ordering matching.
111    return ConditionResult.UNDEFINED;
112  }
113
114  /** {@inheritDoc} */
115  @Override
116  public ConditionResult lessThanOrEqualTo(Entry entry,
117                              VirtualAttributeRule rule,
118                              ByteString value)
119  {
120    // DNs cannot be used in ordering matching.
121    return ConditionResult.UNDEFINED;
122  }
123
124  /** {@inheritDoc} */
125  @Override
126  public ConditionResult approximatelyEqualTo(Entry entry,
127                              VirtualAttributeRule rule,
128                              ByteString value)
129  {
130    // DNs cannot be used in approximate matching.
131    return ConditionResult.UNDEFINED;
132  }
133
134  /** {@inheritDoc} */
135  @Override
136  public boolean isSearchable(VirtualAttributeRule rule,
137                              SearchOperation searchOperation,
138                              boolean isPreIndexed)
139  {
140    return false;
141  }
142
143  /** {@inheritDoc} */
144  @Override
145  public void processSearch(VirtualAttributeRule rule,
146                            SearchOperation searchOperation)
147  {
148    searchOperation.setResultCode(ResultCode.UNWILLING_TO_PERFORM);
149
150    LocalizableMessage message = ERR_ENTRYUUID_VATTR_NOT_SEARCHABLE.get(
151            rule.getAttributeType().getNameOrOID());
152    searchOperation.appendErrorMessage(message);
153  }
154}
155