All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.unboundid.scim.ldap.Transformation Maven / Gradle / Ivy

/*
 * Copyright 2011-2019 Ping Identity Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (GPLv2 only)
 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see .
 */

package com.unboundid.scim.ldap;

import com.unboundid.asn1.ASN1OctetString;
import com.unboundid.scim.schema.AttributeDescriptor;
import com.unboundid.scim.sdk.Debug;
import com.unboundid.scim.sdk.SCIMAttributeValue;
import com.unboundid.util.ByteString;
import org.w3c.dom.Element;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Attribute mapping transformations may be used to alter the value of mapped
 * attributes. Transformations may be performed on an SCIM attribute value
 * when mapping SCIM resources to LDAP entries and vice versa. When no
 * transformation class is specified in the resources configuration file,
 * the DefaultTransformation implementation is used. It will simply return
 * the value as is without any alterations.
 * 

* Transformations are often useful when the syntax of an attribute is different * between SCIM and LDAP. The LDAP GeneralizedTime attribute syntax is * a good example where transformations are necessary when mapping those * attributes. *

* To use a custom transformation class, use the transform attribute to * specify the implementation class in any mapping or * subMapping configuration elements. For example: *

*
 * <subMapping name="formatted"
 *  ldapAttribute="postalAddress"
 *  transform="com.unboundid.scim.ldap.PostalAddressTransformation"
 * />
 * 
*

* This API is volatile and could change in future releases. */ public abstract class Transformation { /** * The map of arguments provided to this transformation. This maps the * element name to its value. See getArguments() for an example. */ private final Map arguments = new HashMap(); /** * Create a transformation from the name of a class that extends the * {@code Transformation} abstract class. * * @param className The name of a class that extends {@code Transformation}. * @param args The set of arguments that are configured for this * {@code Transformation}. * * @return A new instance of the transformation class. */ public static Transformation create(final String className, final List args) { if (className == null) { return new DefaultTransformation(); } Class clazz; try { clazz = Class.forName(className, true, Transformation.class.getClassLoader()); } catch (ClassNotFoundException e) { Debug.debugException(e); throw new IllegalArgumentException( "Class '" + className + "' not found", e); } final Object object; try { object = clazz.newInstance(); } catch (Exception e) { Debug.debugException(e); throw new IllegalArgumentException( "Cannot create instance of class '" + className + "'", e); } if (!(object instanceof Transformation)) { throw new IllegalArgumentException( "Class '" + className + "' is not a Transformation"); } Transformation t = (Transformation)object; if(args != null) { for(Object o : args) { Element e = (Element) o; String name = e.getTagName(); String value = e.getTextContent(); t.arguments.put(name, value); } } return t; } /** * Transform an LDAP value to a SCIM value. * * @param descriptor The SCIM attribute descriptor for the value. * @param byteString The LDAP value as a byte string. * * @return The SCIM value. */ public abstract SCIMAttributeValue toSCIMValue( final AttributeDescriptor descriptor, final ByteString byteString); /** * Transform a SCIM value to an LDAP value. * * @param descriptor The SCIM attribute descriptor for the value. * @param value The SCIM value. * * @return The LDAP value as an ASN1 octet string. */ public abstract ASN1OctetString toLDAPValue( final AttributeDescriptor descriptor, final SCIMAttributeValue value); /** * Transform a SCIM filter value to an LDAP filter value. * * @param scimFilterValue The SCIM filter value. * * @return The LDAP filter value as a string. */ public abstract String toLDAPFilterValue(final String scimFilterValue); /** * Returns the arguments map for this Transformation. The arguments map is * constructed from the child elements of the element. * For example, a derivation like the following: *
   *   <mapping ldapAttribute="title"
   *               transform="com.example.ExampleTransformation">
   *     <key1>value1</key1>
   *     <key2>value2</key2>
   *   </mapping>
   * 
* would have an arguments map containing 'key1' and 'key2' with values * 'value1' and 'value2'. * * @return a map of argument names to argument values. This is modifiable, * and will never be null. */ public final Map getArguments() { return arguments; } }