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-2015 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.opends.server.admin.server.ConfigurationChangeListener; 027import org.opends.server.admin.std.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 047 048 /** 049 * Creates a new instance of this member virtual attribute provider. 050 */ 051 public UserDefinedVirtualAttributeProvider() 052 { 053 super(); 054 055 // All initialization should be performed in the 056 // initializeVirtualAttributeProvider method. 057 } 058 059 060 061 /** {@inheritDoc} */ 062 @Override 063 public void initializeVirtualAttributeProvider( 064 UserDefinedVirtualAttributeCfg configuration) 065 throws ConfigException, InitializationException 066 { 067 this.currentConfig = configuration; 068 configuration.addUserDefinedChangeListener(this); 069 } 070 071 072 073 /** {@inheritDoc} */ 074 @Override 075 public void finalizeVirtualAttributeProvider() 076 { 077 currentConfig.removeUserDefinedChangeListener(this); 078 } 079 080 081 082 /** {@inheritDoc} */ 083 @Override 084 public boolean isMultiValued() 085 { 086 return currentConfig == null || currentConfig.getValue().size() > 1; 087 } 088 089 090 091 /** {@inheritDoc} */ 092 @Override 093 public Attribute getValues(Entry entry, VirtualAttributeRule rule) 094 { 095 Set<String> userDefinedValues = currentConfig.getValue(); 096 097 switch (userDefinedValues.size()) { 098 case 0: 099 return Attributes.empty(rule.getAttributeType()); 100 case 1: 101 String valueString = userDefinedValues.iterator().next(); 102 return Attributes.create(rule.getAttributeType(), valueString); 103 default: 104 AttributeBuilder builder = new AttributeBuilder(rule.getAttributeType()); 105 builder.addAllStrings(userDefinedValues); 106 return builder.toAttribute(); 107 } 108 } 109 110 111 112 /** {@inheritDoc} */ 113 @Override 114 public boolean isSearchable(VirtualAttributeRule rule, 115 SearchOperation searchOperation, 116 boolean isPreIndexed) 117 { 118 // We will not allow searches based only on user-defined virtual attributes. 119 return false; 120 } 121 122 123 124 /** {@inheritDoc} */ 125 @Override 126 public void processSearch(VirtualAttributeRule rule, 127 SearchOperation searchOperation) 128 { 129 searchOperation.setResultCode(ResultCode.UNWILLING_TO_PERFORM); 130 return; 131 } 132 133 134 135 /** {@inheritDoc} */ 136 @Override 137 public boolean isConfigurationChangeAcceptable( 138 UserDefinedVirtualAttributeCfg configuration, 139 List<LocalizableMessage> unacceptableReasons) 140 { 141 // The new configuration should always be acceptable. 142 return true; 143 } 144 145 146 147 /** {@inheritDoc} */ 148 @Override 149 public ConfigChangeResult applyConfigurationChange( 150 UserDefinedVirtualAttributeCfg configuration) 151 { 152 // Just accept the new configuration as-is. 153 currentConfig = configuration; 154 155 return new ConfigChangeResult(); 156 } 157} 158