From 6c7a6f8e222e4fb39a11c220b63e785f2037d428 Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Thu, 16 Jan 2014 08:55:06 +0100 Subject: fix added real source of stork modules --- .../peps/auth/commons/PersonalAttributeList.java | 305 +++++++++++++++++++++ 1 file changed, 305 insertions(+) create mode 100644 id/server/stork2-commons/src/main/java/eu/stork/peps/auth/commons/PersonalAttributeList.java (limited to 'id/server/stork2-commons/src/main/java/eu/stork/peps/auth/commons/PersonalAttributeList.java') diff --git a/id/server/stork2-commons/src/main/java/eu/stork/peps/auth/commons/PersonalAttributeList.java b/id/server/stork2-commons/src/main/java/eu/stork/peps/auth/commons/PersonalAttributeList.java new file mode 100644 index 000000000..d33ccfe18 --- /dev/null +++ b/id/server/stork2-commons/src/main/java/eu/stork/peps/auth/commons/PersonalAttributeList.java @@ -0,0 +1,305 @@ +/* + * This work is Open Source and licensed by the European Commission under the + * conditions of the European Public License v1.1 + * + * (http://www.osor.eu/eupl/european-union-public-licence-eupl-v.1.1); + * + * any use of this file implies acceptance of the conditions of this license. + * 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 eu.stork.peps.auth.commons; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.StringTokenizer; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +/** + * This class is a bean used to store the information relative to the + * PersonalAttributeList. + * + * @author ricardo.ferreira@multicert.com, renato.portela@multicert.com, + * luis.felix@multicert.com, hugo.magalhaes@multicert.com, + * paulo.ribeiro@multicert.com + * @version $Revision: 1.27 $, $Date: 2010-11-18 22:54:56 $ + * + * @see PersonalAttribute + */ +@SuppressWarnings("PMD") +public final class PersonalAttributeList extends + HashMap implements IPersonalAttributeList{ + + /** + * Logger object. + */ + private static final Logger LOG = Logger + .getLogger(PersonalAttributeList.class.getName()); + + /** + * Serial id. + */ + private static final long serialVersionUID = 7375127363889975062L; + + /** + * Hash with the latest fetched attribute name alias. + */ + private final transient Map latestAttrAlias = + new HashMap(); + + /** + * Hash with mapping number of alias or the attribute name. + */ + private final transient Map attrAliasNumber = + new HashMap(); + + /** + * Default constructor. + */ + public PersonalAttributeList() { + // The best practices recommend to call the super constructor. + super(); + } + + /** + * Constructor with initial capacity for the PersonalAttributeList size. + * + * @param capacity The initial capacity for the PersonalAttributeList. + */ + public PersonalAttributeList(final int capacity) { + super(capacity); + } + + /** + * {@inheritDoc} + */ + public Iterator iterator() { + return this.values().iterator(); + } + + /** + * {@inheritDoc} + */ + public PersonalAttribute get(final Object key) { + String attrName = (String) key; + + if (this.latestAttrAlias.containsKey(key)) { + attrName = attrName + this.latestAttrAlias.get(key); + } else { + if (this.attrAliasNumber.containsKey(key)) { + this.latestAttrAlias.put(attrName, this.attrAliasNumber.get(key)); + } + } + return super.get(attrName); + } + + /** + * {@inheritDoc} + */ + public void add(final PersonalAttribute value) { + if (value != null) { + this.put(value.getName(), value); + } + } + + /** + * {@inheritDoc} + */ + public PersonalAttribute put(final String key, final PersonalAttribute val) { + if (StringUtils.isNotEmpty(key) && val != null) { + // Validate if attribute name already exists! + String attrAlias = key; + if (this.containsKey(key)) { + if (!val.isEmptyValue() && StringUtils.isNumeric(val.getValue().get(0))) { + final String attrValue = val.getValue().get(0); + attrAlias = key + attrValue; + this.attrAliasNumber.put(key, Integer.valueOf(attrValue)); + } else { + final PersonalAttribute attr = super.get(key); + if (!attr.isEmptyValue() + && StringUtils.isNumeric(attr.getValue().get(0))) { + attrAlias = key + attr.getValue().get(0); + super.put(key, (PersonalAttribute) attr); + this.attrAliasNumber.put(key, null); + } + } + } + return super.put(attrAlias, val); + } else { + return null; + } + } + + /** + * {@inheritDoc} + */ + public void populate(final String attrList) { + final StringTokenizer strToken = + new StringTokenizer(attrList, PEPSValues.ATTRIBUTE_SEP.toString()); + + while (strToken.hasMoreTokens()) { + final PersonalAttribute persAttr = new PersonalAttribute(); + String[] tuples = + strToken.nextToken().split(PEPSValues.ATTRIBUTE_TUPLE_SEP.toString(), + AttributeConstants.NUMBER_TUPLES.intValue()); + + // Convert to the new format if needed! + tuples = convertFormat(tuples); + + if (AttributeUtil.hasValidTuples(tuples)) { + final int attrValueIndex = + AttributeConstants.ATTR_VALUE_INDEX.intValue(); + final String tmpAttrValue = + tuples[attrValueIndex].substring(1, + tuples[attrValueIndex].length() - 1); + final String[] vals = + tmpAttrValue.split(PEPSValues.ATTRIBUTE_VALUE_SEP.toString()); + + persAttr.setName(tuples[AttributeConstants.ATTR_NAME_INDEX.intValue()]); + persAttr.setIsRequired(Boolean + .valueOf(tuples[AttributeConstants.ATTR_TYPE_INDEX.intValue()])); + + // check if it is a complex value + if (tuples[AttributeConstants.ATTR_NAME_INDEX.intValue()] + .equals(PEPSParameters.COMPLEX_ADDRESS_VALUE.toString())) + { + persAttr.setComplexValue(createComplexValue(vals)); + } + else if (tuples[AttributeConstants.ATTR_NAME_INDEX.intValue()] + .equals(PEPSParameters.COMPLEX_NEWATTRIBUTE_VALUE.toString())) + { + persAttr.setComplexValue(createComplexValue(vals)); + } + + else if (tuples[AttributeConstants.ATTR_NAME_INDEX.intValue()] + .equals(PEPSParameters.COMPLEX_HASDEGREE_VALUE.toString())) + { + persAttr.setComplexValue(createComplexValue(vals)); + } + else if (tuples[AttributeConstants.ATTR_NAME_INDEX.intValue()] + .equals(PEPSParameters.COMPLEX_MANDATECONTENT_VALUE.toString())) + { + persAttr.setComplexValue(createComplexValue(vals)); + } + else + { + persAttr.setValue(createValues(vals)); + } + + if (tuples.length == AttributeConstants.NUMBER_TUPLES.intValue()) { + persAttr.setStatus(tuples[AttributeConstants.ATTR_STATUS_INDEX + .intValue()]); + } + this.put(tuples[AttributeConstants.ATTR_NAME_INDEX.intValue()], + persAttr); + + } else { + LOG.warn("Invalid personal attribute list tuples"); + } + + } + } + + /** + * Returns a copy of this IPersonalAttributeList instance. + * + * @return The copy of this IPersonalAttributeList. + */ + public Object clone() { + return (PersonalAttributeList) super.clone(); + } + + /** + * Creates a string in the following format. + * + * attrName:attrType:[attrValue1,attrValue2=attrComplexValue]:attrStatus; + * + * @return {@inheritDoc} + */ + @Override + public String toString() { + final Iterator> itAttrs = + this.entrySet().iterator(); + final StringBuilder strBuilder = new StringBuilder(); + + while (itAttrs.hasNext()) { + final Entry attrEntry = itAttrs.next(); + final PersonalAttribute attr = attrEntry.getValue(); + strBuilder.append(attr.toString()); + } + + return strBuilder.toString(); + } + + /** + * Validates and creates the attribute's complex values. + * + * @param values The complex values. + * + * @return The {@link Map} with the complex values. + * + * @see Map + */ + private Map createComplexValue(final String[] values) { + final Map complexValue = new HashMap(); + for (final String val : values) { + final String[] tVal = val.split("="); + if (StringUtils.isNotEmpty(val) && tVal.length == 2) { + complexValue.put(tVal[0], AttributeUtil.unescape(tVal[1])); + } + } + return complexValue; + } + + /** + * Validates and creates the attribute values. + * + * @param vals The attribute values. + * + * @return The {@link List} with the attribute values. + * + * @see List + */ + private List createValues(final String[] vals) { + final List values = new ArrayList(); + for (final String val : vals) { + if (StringUtils.isNotEmpty(val)) { + values.add(AttributeUtil.unescape(val)); + } + } + return values; + } + + /** + * Converts the attribute tuple (attrName:attrType...) to the new format. + * + * @param tuples The attribute tuples to convert. + * + * @return The attribute tuples in the new format. + */ + private String[] convertFormat(final String[] tuples) { + final String[] newFormatTuples = + new String[AttributeConstants.NUMBER_TUPLES.intValue()]; + if (tuples != null) { + System.arraycopy(tuples, 0, newFormatTuples, 0, tuples.length); + + for (int i = tuples.length; i < newFormatTuples.length; i++) { + if (i == AttributeConstants.ATTR_VALUE_INDEX.intValue()) { + newFormatTuples[i] = "[]"; + } else { + newFormatTuples[i] = ""; + } + } + } + return newFormatTuples; + } +} -- cgit v1.2.3 From 9cb68043945f53246928443cea723b58ee2b1c24 Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Tue, 21 Jan 2014 14:38:35 +0100 Subject: updated samlengine --- .../peps/auth/commons/PersonalAttributeList.java | 143 +++++++++++++++++---- 1 file changed, 117 insertions(+), 26 deletions(-) (limited to 'id/server/stork2-commons/src/main/java/eu/stork/peps/auth/commons/PersonalAttributeList.java') diff --git a/id/server/stork2-commons/src/main/java/eu/stork/peps/auth/commons/PersonalAttributeList.java b/id/server/stork2-commons/src/main/java/eu/stork/peps/auth/commons/PersonalAttributeList.java index d33ccfe18..642b249d4 100644 --- a/id/server/stork2-commons/src/main/java/eu/stork/peps/auth/commons/PersonalAttributeList.java +++ b/id/server/stork2-commons/src/main/java/eu/stork/peps/auth/commons/PersonalAttributeList.java @@ -18,7 +18,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; import java.util.StringTokenizer; import org.apache.commons.lang.StringUtils; @@ -37,7 +37,7 @@ import org.apache.log4j.Logger; */ @SuppressWarnings("PMD") public final class PersonalAttributeList extends - HashMap implements IPersonalAttributeList{ + ConcurrentHashMap implements IPersonalAttributeList{ /** * Logger object. @@ -118,8 +118,9 @@ public final class PersonalAttributeList extends if (StringUtils.isNotEmpty(key) && val != null) { // Validate if attribute name already exists! String attrAlias = key; - if (this.containsKey(key)) { - if (!val.isEmptyValue() && StringUtils.isNumeric(val.getValue().get(0))) { + if (this.containsKey(attrAlias)) { + //TODO isAgeOver should not be hardcoded, a better way of handling multipe isAgeOver requests should be implemented. + if (!val.isEmptyValue() && StringUtils.isNumeric(val.getValue().get(0)) && "isAgeOver".equals( val.getName() ) ) { final String attrValue = val.getValue().get(0); attrAlias = key + attrValue; this.attrAliasNumber.put(key, Integer.valueOf(attrValue)); @@ -169,27 +170,9 @@ public final class PersonalAttributeList extends .valueOf(tuples[AttributeConstants.ATTR_TYPE_INDEX.intValue()])); // check if it is a complex value - if (tuples[AttributeConstants.ATTR_NAME_INDEX.intValue()] - .equals(PEPSParameters.COMPLEX_ADDRESS_VALUE.toString())) - { + if (isComplexValue(vals)) { persAttr.setComplexValue(createComplexValue(vals)); } - else if (tuples[AttributeConstants.ATTR_NAME_INDEX.intValue()] - .equals(PEPSParameters.COMPLEX_NEWATTRIBUTE_VALUE.toString())) - { - persAttr.setComplexValue(createComplexValue(vals)); - } - - else if (tuples[AttributeConstants.ATTR_NAME_INDEX.intValue()] - .equals(PEPSParameters.COMPLEX_HASDEGREE_VALUE.toString())) - { - persAttr.setComplexValue(createComplexValue(vals)); - } - else if (tuples[AttributeConstants.ATTR_NAME_INDEX.intValue()] - .equals(PEPSParameters.COMPLEX_MANDATECONTENT_VALUE.toString())) - { - persAttr.setComplexValue(createComplexValue(vals)); - } else { persAttr.setValue(createValues(vals)); @@ -215,7 +198,11 @@ public final class PersonalAttributeList extends * @return The copy of this IPersonalAttributeList. */ public Object clone() { - return (PersonalAttributeList) super.clone(); + try { + return (PersonalAttributeList) super.clone(); + } catch (CloneNotSupportedException e) { + return null; + } } /** @@ -234,7 +221,8 @@ public final class PersonalAttributeList extends while (itAttrs.hasNext()) { final Entry attrEntry = itAttrs.next(); final PersonalAttribute attr = attrEntry.getValue(); - strBuilder.append(attr.toString()); + //strBuilder.append(attr.toString()); + strBuilder.insert(0, attr.toString()); } return strBuilder.toString(); @@ -260,6 +248,22 @@ public final class PersonalAttributeList extends return complexValue; } + /** + * Checks if value is complex or not + * @param values The values to check + * @return True if succesful + */ + private boolean isComplexValue(final String[] values) { + boolean isComplex = false; + if (values.length > 0) { + final String[] tVal = values[0].split("="); + if (StringUtils.isNotEmpty(values[0]) && tVal.length == 2) { + isComplex = true; + } + } + return isComplex; + } + /** * Validates and creates the attribute values. * @@ -302,4 +306,91 @@ public final class PersonalAttributeList extends } return newFormatTuples; } -} + + /** + * Returns a IPersonalAttributeList of the complex attributes in this map. + * + * @return an IPersonalAttributeList of the complex attributes contained in this map. + */ + public IPersonalAttributeList getComplexAttributes() { + LOG.info("get complex attributes"); + IPersonalAttributeList attrList = new PersonalAttributeList(); + for(PersonalAttribute attr: this) { + if(!attr.getComplexValue().isEmpty()) { + attrList.put(attr.getName(), attr); + LOG.info("adding complex attribute:"+attr.getName()); + } + } + return attrList; + } + + /** + * Returns a IPersonalAttributeList of the mandatory attributes in this map. + * + * @return an IPersonalAttributeList of the mandatory attributes contained in this map. + */ + public IPersonalAttributeList getSimpleValueAttributes() { + LOG.info("get simple attributes"); + IPersonalAttributeList attrList = new PersonalAttributeList(); + for(PersonalAttribute attr: this) { + if(attr.getComplexValue().isEmpty()) { + attrList.put(attr.getName(), attr); + LOG.info("adding simple attribute:"+attr.getName()); + } + } + return attrList; + } + + + + /** + * Returns a IPersonalAttributeList of the mandatory attributes in this map. + * + * @return an IPersonalAttributeList of the mandatory attributes contained in this map. + */ + public IPersonalAttributeList getMandatoryAttributes() { + return getAttributesByParam(true); + } + + + /** + * Returns a IPersonalAttributeList of the attributes in this map by parameter value. + * + * @param compareValue The boolean to get mandatory (true) or optional (false) attributes. + * + * @return an IPersonalAttributeList of the mandatory attributes contained in this map if compareValue is true or optional otherwise. + */ + private IPersonalAttributeList getAttributesByParam(final boolean compareValue) { + LOG.info("get attributes by param :"+compareValue); + IPersonalAttributeList attrList = new PersonalAttributeList(); + for(PersonalAttribute attr: this) { + if(attr.isRequired() == compareValue) { + attrList.put(attr.getName(), attr); + LOG.info("adding attribute:"+attr.getName()); + } + } + return attrList; + } + + + /** + * Returns a IPersonalAttributeList of the optional attributes in this map. + * + * @return an IPersonalAttributeList of the optional attributes contained in this map. + */ + public IPersonalAttributeList getOptionalAttributes() { + return getAttributesByParam(false); + } + + /** + * {@inheritDoc} + */ + public boolean hasMissingValues() { + for(PersonalAttribute attr: this) { + if(attr.isEmptyValue() && attr.isEmptyComplexValue()) { + return true; + } + } + return false; + } +} \ No newline at end of file -- cgit v1.2.3