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

se.litsec.opensaml.saml2.attribute.AttributeUtils Maven / Gradle / Ivy

There is a newer version: 1.4.5
Show newest version
/*
 * Copyright 2016-2018 Litsec AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package se.litsec.opensaml.saml2.attribute;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
import org.opensaml.core.xml.XMLObject;
import org.opensaml.core.xml.schema.XSAny;
import org.opensaml.core.xml.schema.XSBoolean;
import org.opensaml.core.xml.schema.XSBooleanValue;
import org.opensaml.core.xml.schema.XSDateTime;
import org.opensaml.core.xml.schema.XSInteger;
import org.opensaml.core.xml.schema.XSString;
import org.opensaml.saml.saml2.core.Attribute;

import se.litsec.opensaml.utils.ObjectUtils;

/**
 * Helper methods for accessing attribute values. See also {@link AttributeBuilder}.
 * 
 * @author Martin Lindström ([email protected])
 * @see AttributeBuilder
 */
public class AttributeUtils {

  /**
   * Given an attribute holding string values this method will return a list of these values.
   * 
   * @param attribute
   *          the attribute
   * @return a (possibly empty) list of string values
   */
  public static List getAttributeStringValues(Attribute attribute) {
    return getAttributeValues(attribute, XSString.class)
      .stream()
      .map(XSString::getValue)
      .collect(Collectors.toList());
  }

  /**
   * Given a single-valued string attribute, this method returns its string value.
   * 
   * @param attribute
   *          the attribute
   * @return the value, or {@code null} if no value is stored
   */
  public static String getAttributeStringValue(Attribute attribute) {
    XSString v = getAttributeValue(attribute, XSString.class);
    return v != null ? v.getValue() : null;
  }

  /**
   * Returns the attribute values of the given type.
   * 
   * @param attribute
   *          the attribute
   * @param type
   *          the type to match
   * @param 
   *          the value type
   * @return a (possibly empty) list of values.
   */
  public static  List getAttributeValues(Attribute attribute, Class type) {
    return attribute.getAttributeValues()
      .stream()
      .flatMap(o -> mapAttribute(o, type))
      .collect(Collectors.toList());
  }

  /**
   * Given a single-valued attribute, this method returns its value (of the given type).
   * 
   * @param attribute
   *          the attribute
   * @param type
   *          the type to match
   * @param 
   *          the value type
   * @return the value, or {@code null}
   */
  public static  T getAttributeValue(Attribute attribute, Class type) {
    return attribute.getAttributeValues()
      .stream()
      .flatMap(o -> mapAttribute(o, type))
      .findFirst()
      .orElse(null);
  }

  /**
   * Returns an attribute with a given name from an attribute list.
   * 
   * @param name
   *          the attribute name
   * @param attributes
   *          the list of attributes
   * @return the attribute or {@link Optional#empty()}
   */
  public static Optional getAttribute(String name, List attributes) {
    if (attributes == null) {
      return Optional.empty();
    }
    return attributes.stream().filter(a -> a.getName().equals(name)).findFirst();
  }

  /**
   * Helper method that filters attribute values based on the requested type.
   * 
   * 

* The SAML core specification section 2.7.3.1.1 states: "If the data content of an {@code } element * is of an XML Schema simple type (such as xs:integer or xs:string), the datatype MAY be declared explicitly by means * of an xsi:type declaration in the {@code } element." *

*

* Therefore this method handles attribute values with no explicit type given for {@code XSString}, {@code XSInteger}, * {@code XSBoolean} and {@code XSDateTime}. *

* * @param obj * the object being streamed * @param type * the requested type * @return a stream holding one or zero elements */ private static Stream mapAttribute(R obj, Class type) { if (type.isInstance(obj)) { return Stream.of(type.cast(obj)); } else if (XSAny.class.isInstance(obj)) { if (type.isAssignableFrom(XSString.class)) { XSString newObject = ObjectUtils.createXMLObject(XSString.class, XSString.TYPE_NAME); newObject.setValue(((XSAny) obj).getTextContent()); return Stream.of(type.cast(newObject)); } else if (type.isAssignableFrom(XSInteger.class)) { try { Integer v = Integer.parseInt(((XSAny) obj).getTextContent()); XSInteger newObject = ObjectUtils.createXMLObject(XSInteger.class, XSInteger.TYPE_NAME); newObject.setValue(v); return Stream.of(type.cast(newObject)); } catch (NumberFormatException e) { return Stream.empty(); } } else if (type.isAssignableFrom(XSBoolean.class)) { String text = ((XSAny) obj).getTextContent(); if (text == null) { return Stream.empty(); } Boolean b = ("true".equalsIgnoreCase(text) || "1".equals(text)) ? Boolean.TRUE : ("false".equalsIgnoreCase(text) || "0".equals(text)) ? Boolean.FALSE : null; if (b != null) { XSBoolean newObject = ObjectUtils.createXMLObject(XSBoolean.class, XSBoolean.TYPE_NAME); XSBooleanValue newValue = XSBooleanValue.valueOf(text); newObject.setValue(newValue); return Stream.of(type.cast(newObject)); } } else if (type.isAssignableFrom(XSDateTime.class)) { String text = ((XSAny) obj).getTextContent(); if (text == null) { return Stream.empty(); } try { DateTime date = DateTime.parse(text, ISODateTimeFormat.dateTime() .withChronology(org.joda.time.chrono.ISOChronology.getInstanceUTC())); if (date != null) { XSDateTime newObject = ObjectUtils.createXMLObject(XSDateTime.class, XSDateTime.TYPE_NAME); newObject.setValue(date); return Stream.of(type.cast(newObject)); } } catch (Exception e) { } } } return Stream.empty(); } // Hidden private AttributeUtils() { } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy