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

com.unboundid.scim.data.AttributeValueResolver Maven / Gradle / Ivy

Go to download

The UnboundID SCIM SDK is a library that may be used to interact with various types of SCIM-enabled endpoints (such as the UnboundID server products) to perform lightweight, cloud-based identity management via the SCIM Protocol. See http://www.simplecloud.info for more information.

There is a newer version: 1.8.26
Show newest version
/*
 * Copyright 2011-2016 UnboundID Corp.
 *
 * 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.data;

import com.unboundid.scim.sdk.ComplexValue;
import com.unboundid.scim.schema.AttributeDescriptor;
import com.unboundid.scim.sdk.InvalidResourceException;
import com.unboundid.scim.sdk.SCIMAttribute;
import com.unboundid.scim.sdk.SCIMAttributeValue;
import com.unboundid.scim.sdk.SimpleValue;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Used to resolve SCIM attribute values to Java instances.
 *
 * @param  The Java class to resolve.
 */
public abstract class AttributeValueResolver
{
  /**
   * Create an instance from the given attribute value.
   *
   * @param value The value to create an instance from.
   * @return The instance created from the attribute value.
   */
  public abstract T toInstance(final SCIMAttributeValue value);

  /**
   * Create a SCIM attribute value from the given instance.
   *
   * @param attributeDescriptor The descriptor for the attribute to create.
   * @param value The instance.
   * @return The SCIM attribute value created from the instance.
   * @throws InvalidResourceException if the value violates the schema.
   */
  public abstract SCIMAttributeValue fromInstance(
      final AttributeDescriptor attributeDescriptor, final T value)
      throws InvalidResourceException;

  /**
   * The AttributeValueResolver that resolves SCIM attribute values
   * to/from String instances.
   */
  public static final AttributeValueResolver STRING_RESOLVER =
      new AttributeValueResolver() {

        /**
         * {@inheritDoc}
         */
        public String toInstance(final SCIMAttributeValue value) {
          return value.getStringValue();
        }

        /**
         * {@inheritDoc}
         */
        public SCIMAttributeValue fromInstance(
            final AttributeDescriptor attributeDescriptor, final String value) {
          return SCIMAttributeValue.createStringValue(value);
        }
      };


  /**
   * The AttributeValueResolver that resolves SCIM attribute values
   * to/from String instances.
   */
  public static final AttributeValueResolver DATE_RESOLVER =
      new AttributeValueResolver() {
        /**
         * {@inheritDoc}
         */
        public Date toInstance(final SCIMAttributeValue value) {
          return value.getDateValue();
        }

        /**
         * {@inheritDoc}
         */
        public SCIMAttributeValue fromInstance(
            final AttributeDescriptor attributeDescriptor, final Date value) {
          return SCIMAttributeValue.createDateValue(value);
        }
      };

  /**
   * The AttributeValueResolver that resolves SCIM attribute values
   * to/from Boolean instances.
   */
  public static final AttributeValueResolver BOOLEAN_RESOLVER =
      new AttributeValueResolver() {
        /**
         * {@inheritDoc}
         */
        public Boolean toInstance(final SCIMAttributeValue value) {
          return value.getBooleanValue();
        }

        /**
         * {@inheritDoc}
         */
        public SCIMAttributeValue fromInstance(
            final AttributeDescriptor attributeDescriptor,
            final Boolean value) {
          return SCIMAttributeValue.createBooleanValue(value);
        }
      };

  /**
   * The AttributeValueResolver that resolves SCIM attribute values
   * to/from byte[] instances.
   */
  public static final AttributeValueResolver BINARY_RESOLVER =
      new AttributeValueResolver() {

        /**
         * {@inheritDoc}
         */
        @Override
        public byte[] toInstance(final SCIMAttributeValue value) {
          return value.getBinaryValue();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public SCIMAttributeValue fromInstance(
            final AttributeDescriptor attributeDescriptor,
            final byte[] value) {
          return SCIMAttributeValue.createBinaryValue(value);
        }
      };

  /**
   * The AttributeValueResolver that resolves SCIM attribute values
   * to/from Decimal instances.
   */
  public static final AttributeValueResolver DECIMAL_RESOLVER =
      new AttributeValueResolver() {
        /**
         * {@inheritDoc}
         */
        public Double toInstance(final SCIMAttributeValue value) {
          return value.getDecimalValue();
        }

        /**
         * {@inheritDoc}
         */
        public SCIMAttributeValue fromInstance(
            final AttributeDescriptor attributeDescriptor,
            final Double value) {
          return SCIMAttributeValue.createStringValue(String.valueOf(value));
        }
      };

  /**
   * The AttributeValueResolver that resolves SCIM attribute values
   * to/from Integer instances.
   */
  public static final AttributeValueResolver INTEGER_RESOLVER =
      new AttributeValueResolver() {
        /**
         * {@inheritDoc}
         */
        public Long toInstance(final SCIMAttributeValue value) {
          return value.getIntegerValue();
        }

        /**
         * {@inheritDoc}
         */
        public SCIMAttributeValue fromInstance(
            final AttributeDescriptor attributeDescriptor,
            final Long value) {
          return SCIMAttributeValue.createStringValue(String.valueOf(value));
        }
      };


  /**
   * A default AttributeValueResolver for complex SCIM attribute
   * values. Resolves to/from a ComplexValue instance.
   */
  public static final AttributeValueResolver
      COMPLEX_RESOLVER = new AttributeValueResolver() {

        /**
         * {@inheritDoc}
         */
        @Override
        public ComplexValue toInstance(
            final SCIMAttributeValue value) {

          ComplexValue complex = new ComplexValue();
          for (Map.Entry subEntry :
              value.getAttributes().entrySet())
          {
            String subAttrName = subEntry.getKey();
            SCIMAttribute subAttribute = subEntry.getValue();
            complex.put(subAttrName, subAttribute.getValue().getValue());
          }
          return complex;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public SCIMAttributeValue fromInstance(
            final AttributeDescriptor attributeDescriptor,
            final ComplexValue complex)
            throws InvalidResourceException {

          List subAttributes = new ArrayList();

          for (Map.Entry subEntry :
              complex.entrySet())
          {
            String subAttrName = subEntry.getKey();
            SimpleValue subAttrValue = subEntry.getValue();

            AttributeDescriptor subDescriptor =
                attributeDescriptor.getSubAttribute(subAttrName);

            subAttributes.add(SCIMAttribute.create(subDescriptor,
                SCIMAttributeValue.createSimpleValue(subAttrValue)));
          }

          return SCIMAttributeValue.createComplexValue(subAttributes);
        }
      };
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy