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