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

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

Go to download

The UnboundID SCIM-LDAP module builds on the UnboundID SCIM-SDK to provide classes that map SCIM resources to LDAP entries and vice versa. It also contains several APIs that may be used to implement custom behaviors for the mapping configuration file to extend its capabilities above and beyond those provided out of the box. Each extension type varies in the amount of control the implementation has over the mapping process and the amount of effort required for implementation.

There is a newer version: 1.8.26
Show newest version
/*
 * Copyright 2011-2012 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.ldap;

import com.unboundid.asn1.ASN1OctetString;
import com.unboundid.scim.schema.AttributeDescriptor;
import com.unboundid.scim.sdk.Debug;
import com.unboundid.scim.sdk.SimpleValue;
import com.unboundid.util.ByteString;
import com.unboundid.util.StaticUtils;

import java.text.ParseException;
import java.util.Date;



/**
 * A transformation for LDAP GeneralizedTime syntax. SCIM XML DateTime values
 * are converted to GeneralizedTime and vice-versa.
 */
public class GeneralizedTimeTransformation extends Transformation
{
  /**
   * {@inheritDoc}
   */
  @Override
  public SimpleValue toSCIMValue(final AttributeDescriptor descriptor,
                                 final ByteString byteString)
  {
    switch (descriptor.getDataType())
    {
      case DATETIME:
        final String generalizedTime = byteString.stringValue();
        try
        {
          final Date date = StaticUtils.decodeGeneralizedTime(generalizedTime);
          return new SimpleValue(date);
        }
        catch (ParseException e)
        {
          Debug.debugException(e);
          throw new RuntimeException(
              "Error in transformation from LDAP generalized time value: " +
              e.getMessage());
        }

      case STRING:
      case BOOLEAN:
      case DECIMAL:
      case INTEGER:
      case BINARY:
      default:
        throw new IllegalArgumentException(
            "The generalized time transformation can not be used on " +
            descriptor.getDataType() + " data");
    }
  }



  /**
   * {@inheritDoc}
   */
  @Override
  public ASN1OctetString toLDAPValue(final AttributeDescriptor descriptor,
                                     final SimpleValue simpleValue)
  {
    switch (descriptor.getDataType())
    {
      case DATETIME:
        final Date date = simpleValue.getDateValue();
        return new ASN1OctetString(StaticUtils.encodeGeneralizedTime(date));

      case STRING:
      case BOOLEAN:
      case DECIMAL:
      case INTEGER:
      case BINARY:
      default:
        throw new IllegalArgumentException(
            "The generalized time transformation can not be used on " +
            descriptor.getDataType() + " data");
    }
  }



  /**
   * {@inheritDoc}
   */
  @Override
  public String toLDAPFilterValue(final String scimFilterValue)
  {
    SimpleValue simpleValue = new SimpleValue(scimFilterValue);
    return StaticUtils.encodeGeneralizedTime(simpleValue.getDateValue());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy