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.i18n.slf4j.LocalizedLogger;
036import org.forgerock.opendj.ldap.ByteString;
037import org.forgerock.opendj.ldap.ConditionResult;
038import org.forgerock.opendj.ldap.ResultCode;
039import org.forgerock.opendj.ldap.schema.MatchingRule;
040import org.opends.server.admin.std.server.EntryUUIDVirtualAttributeCfg;
041import org.opends.server.api.VirtualAttributeProvider;
042import org.opends.server.core.SearchOperation;
043import org.opends.server.types.Attribute;
044import org.opends.server.types.Attributes;
045import org.opends.server.types.Entry;
046import org.opends.server.types.VirtualAttributeRule;
047
048/**
049 * This class implements a virtual attribute provider that is meant to serve the
050 * entryUUID operational attribute as described in RFC 4530.  Note that this
051 * should only be used for entries used in conjunction with data in private
052 * backends (e.g., those holding the configuration, schema, monitor, and root
053 * DSE entries).  Real user data should have entry UUID values generated at the
054 * time the entries are added or imported.
055 */
056public class EntryUUIDVirtualAttributeProvider
057       extends VirtualAttributeProvider<EntryUUIDVirtualAttributeCfg>
058{
059  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
060
061  /**
062   * Creates a new instance of this entryUUID virtual attribute provider.
063   */
064  public EntryUUIDVirtualAttributeProvider()
065  {
066    super();
067
068    // All initialization should be performed in the
069    // initializeVirtualAttributeProvider method.
070  }
071
072  /** {@inheritDoc} */
073  @Override
074  public boolean isMultiValued()
075  {
076    return false;
077  }
078
079  /** {@inheritDoc} */
080  @Override
081  public Attribute getValues(Entry entry, VirtualAttributeRule rule)
082  {
083    return Attributes.create(rule.getAttributeType(), getUUIDString(entry));
084  }
085
086  private String getUUIDString(Entry entry)
087  {
088    ByteString normDN = entry.getName().toNormalizedByteString();
089    return UUID.nameUUIDFromBytes(normDN.toByteArray()).toString();
090  }
091
092  /** {@inheritDoc} */
093  @Override
094  public boolean hasValue(Entry entry, VirtualAttributeRule rule)
095  {
096    // This virtual attribute provider will always generate a value.
097    return true;
098  }
099
100  /** {@inheritDoc} */
101  @Override
102  public boolean hasValue(Entry entry, VirtualAttributeRule rule, ByteString value)
103  {
104    MatchingRule matchingRule = rule.getAttributeType().getEqualityMatchingRule();
105    try
106    {
107      String uuidString = getUUIDString(entry);
108      ByteString normValue = matchingRule.normalizeAttributeValue(value);
109      return uuidString.equals(normValue.toString());
110    }
111    catch (Exception e)
112    {
113      logger.traceException(e);
114      return false;
115    }
116  }
117
118  /** {@inheritDoc} */
119  @Override
120  public ConditionResult matchesSubstring(Entry entry,
121                                          VirtualAttributeRule rule,
122                                          ByteString subInitial,
123                                          List<ByteString> subAny,
124                                          ByteString subFinal)
125  {
126    // DNs cannot be used in substring matching.
127    return ConditionResult.UNDEFINED;
128  }
129
130  /** {@inheritDoc} */
131  @Override
132  public ConditionResult greaterThanOrEqualTo(Entry entry,
133                              VirtualAttributeRule rule,
134                              ByteString value)
135  {
136    // DNs cannot be used in ordering matching.
137    return ConditionResult.UNDEFINED;
138  }
139
140  /** {@inheritDoc} */
141  @Override
142  public ConditionResult lessThanOrEqualTo(Entry entry,
143                              VirtualAttributeRule rule,
144                              ByteString value)
145  {
146    // DNs cannot be used in ordering matching.
147    return ConditionResult.UNDEFINED;
148  }
149
150  /** {@inheritDoc} */
151  @Override
152  public ConditionResult approximatelyEqualTo(Entry entry,
153                              VirtualAttributeRule rule,
154                              ByteString value)
155  {
156    // DNs cannot be used in approximate matching.
157    return ConditionResult.UNDEFINED;
158  }
159
160  /** {@inheritDoc} */
161  @Override
162  public boolean isSearchable(VirtualAttributeRule rule,
163                              SearchOperation searchOperation,
164                              boolean isPreIndexed)
165  {
166    return false;
167  }
168
169  /** {@inheritDoc} */
170  @Override
171  public void processSearch(VirtualAttributeRule rule,
172                            SearchOperation searchOperation)
173  {
174    searchOperation.setResultCode(ResultCode.UNWILLING_TO_PERFORM);
175
176    LocalizableMessage message = ERR_ENTRYUUID_VATTR_NOT_SEARCHABLE.get(
177            rule.getAttributeType().getNameOrOID());
178    searchOperation.appendErrorMessage(message);
179  }
180}
181