se.litsec.opensaml.saml2.attribute.AttributeUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opensaml3-ext Show documentation
Show all versions of opensaml3-ext Show documentation
OpenSAML 3.X utility extension library
/*
* The opensaml-ext project is an open-source package that extends OpenSAML
* with useful extensions and utilities.
*
* More details on
* Copyright (C) 2017 Litsec AB
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 se.litsec.opensaml.saml2.attribute;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.opensaml.core.xml.XMLObject;
import org.opensaml.core.xml.schema.XSString;
import org.opensaml.saml.saml2.core.Attribute;
/**
* 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()
.filter(type::isInstance)
.map(type::cast)
.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()
.filter(type::isInstance)
.map(type::cast)
.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();
}
// Hidden
private AttributeUtils() {
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy