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 java.util.List; 020import java.util.Set; 021 022import org.forgerock.i18n.LocalizableMessage; 023import org.forgerock.opendj.config.server.ConfigChangeResult; 024import org.forgerock.opendj.config.server.ConfigException; 025import org.forgerock.opendj.ldap.ResultCode; 026import org.forgerock.opendj.config.server.ConfigurationChangeListener; 027import org.forgerock.opendj.server.config.server.UserDefinedVirtualAttributeCfg; 028import org.opends.server.api.VirtualAttributeProvider; 029import org.opends.server.core.SearchOperation; 030import org.opends.server.types.*; 031 032/** 033 * This class implements a virtual attribute provider that allows administrators 034 * to define their own values that will be inserted into any entry that matches 035 * the criteria defined in the virtual attribute rule. This can be used to 036 * provide functionality like Class of Service (CoS) in the Sun Java System 037 * Directory Server. 038 */ 039public class UserDefinedVirtualAttributeProvider 040 extends VirtualAttributeProvider<UserDefinedVirtualAttributeCfg> 041 implements ConfigurationChangeListener<UserDefinedVirtualAttributeCfg> 042{ 043 /** The current configuration for this virtual attribute provider. */ 044 private UserDefinedVirtualAttributeCfg currentConfig; 045 046 /** Creates a new instance of this member virtual attribute provider. */ 047 public UserDefinedVirtualAttributeProvider() 048 { 049 super(); 050 051 // All initialization should be performed in the 052 // initializeVirtualAttributeProvider method. 053 } 054 055 @Override 056 public void initializeVirtualAttributeProvider( 057 UserDefinedVirtualAttributeCfg configuration) 058 throws ConfigException, InitializationException 059 { 060 this.currentConfig = configuration; 061 configuration.addUserDefinedChangeListener(this); 062 } 063 064 @Override 065 public void finalizeVirtualAttributeProvider() 066 { 067 currentConfig.removeUserDefinedChangeListener(this); 068 } 069 070 @Override 071 public boolean isMultiValued() 072 { 073 return currentConfig == null || currentConfig.getValue().size() > 1; 074 } 075 076 @Override 077 public Attribute getValues(Entry entry, VirtualAttributeRule rule) 078 { 079 Set<String> userDefinedValues = currentConfig.getValue(); 080 081 switch (userDefinedValues.size()) { 082 case 0: 083 return Attributes.empty(rule.getAttributeType()); 084 case 1: 085 String valueString = userDefinedValues.iterator().next(); 086 return Attributes.create(rule.getAttributeType(), valueString); 087 default: 088 AttributeBuilder builder = new AttributeBuilder(rule.getAttributeType()); 089 builder.addAllStrings(userDefinedValues); 090 return builder.toAttribute(); 091 } 092 } 093 094 @Override 095 public boolean isSearchable(VirtualAttributeRule rule, 096 SearchOperation searchOperation, 097 boolean isPreIndexed) 098 { 099 // We will not allow searches based only on user-defined virtual attributes. 100 return false; 101 } 102 103 @Override 104 public void processSearch(VirtualAttributeRule rule, 105 SearchOperation searchOperation) 106 { 107 searchOperation.setResultCode(ResultCode.UNWILLING_TO_PERFORM); 108 return; 109 } 110 111 @Override 112 public boolean isConfigurationChangeAcceptable( 113 UserDefinedVirtualAttributeCfg configuration, 114 List<LocalizableMessage> unacceptableReasons) 115 { 116 // The new configuration should always be acceptable. 117 return true; 118 } 119 120 @Override 121 public ConfigChangeResult applyConfigurationChange( 122 UserDefinedVirtualAttributeCfg configuration) 123 { 124 // Just accept the new configuration as-is. 125 currentConfig = configuration; 126 127 return new ConfigChangeResult(); 128 } 129}