/* * 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; } }