From a9fab8fc750f9f31f1354e6d212eed7305f666b1 Mon Sep 17 00:00:00 2001 From: Bojan Suzic Date: Mon, 17 Mar 2014 18:25:09 +0100 Subject: adding removed modules - backup purpose only - remove it after successful integration --- .../eu/stork/peps/auth/commons/AttributeUtil.java | 231 +++++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 id/server/legacy-backup/stork2-commons/src/main/java/eu/stork/peps/auth/commons/AttributeUtil.java (limited to 'id/server/legacy-backup/stork2-commons/src/main/java/eu/stork/peps/auth/commons/AttributeUtil.java') diff --git a/id/server/legacy-backup/stork2-commons/src/main/java/eu/stork/peps/auth/commons/AttributeUtil.java b/id/server/legacy-backup/stork2-commons/src/main/java/eu/stork/peps/auth/commons/AttributeUtil.java new file mode 100644 index 000000000..af7ab6cf3 --- /dev/null +++ b/id/server/legacy-backup/stork2-commons/src/main/java/eu/stork/peps/auth/commons/AttributeUtil.java @@ -0,0 +1,231 @@ +/* + * 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.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.commons.lang.StringUtils; + +/** + * This class holds static helper methods. + * + * @author ricardo.ferreira@multicert.com, renato.portela@multicert.com, + * luis.felix@multicert.com, hugo.magalhaes@multicert.com, + * paulo.ribeiro@multicert.com + * @version $Revision: 1.5 $, $Date: 2010-12-15 23:19:59 $ + */ +public final class AttributeUtil { + + /** + * Private constructor. Prevents the class from being instantiated. + */ + private AttributeUtil() { + // empty constructor + } + + /** + * Safe escape any given string. + * + * @param value The HTTP Value to escaped. + * + * @return The value escaped value. + */ + public static String escape(final String value) { + + final String attrSep = PEPSValues.ATTRIBUTE_SEP.toString(); + final String attrTupleSep = PEPSValues.ATTRIBUTE_TUPLE_SEP.toString(); + final String attrValueSep = PEPSValues.ATTRIBUTE_VALUE_SEP.toString(); + + final String escAttrSep = "%" + (int) attrSep.charAt(0); + final String escAttrTupleSep = "%" + (int) attrTupleSep.charAt(0); + final String escAttrValueSep = "%" + (int) attrValueSep.charAt(0); + + return value.replace(attrSep, escAttrSep) + .replace(attrTupleSep, escAttrTupleSep) + .replace(attrValueSep, escAttrValueSep); + } + + /** + * Unescape any given string. + * + * @param value The HTTP Value to be unescaped. + * + * @return The value unescaped value. + */ + public static String unescape(final String value) { + final String attrSep = PEPSValues.ATTRIBUTE_SEP.toString(); + final String attrTupleSep = PEPSValues.ATTRIBUTE_TUPLE_SEP.toString(); + final String attrValueSep = PEPSValues.ATTRIBUTE_VALUE_SEP.toString(); + + final String escAttrSep = "%" + (int) attrSep.charAt(0); + final String escAttrTupleSep = "%" + (int) attrTupleSep.charAt(0); + final String escAttrValueSep = "%" + (int) attrValueSep.charAt(0); + + return value.replace(escAttrSep, attrSep) + .replace(escAttrTupleSep, attrTupleSep) + .replace(escAttrValueSep, attrValueSep); + } + + /** + * Appends the string representation of an object to a StringBuilder. + * + * @param strBuilder The StringBuilder to append to. + * @param val The string representation of an object. + */ + public static void appendIfNotNull(final StringBuilder strBuilder, + final Object val) { + + if (val != null) { + strBuilder.append(val); + } + } + + /** + * Given a separator and a list of strings, joins the list, as a string, + * separated by the separator string. + * + * @param list The list of strings to join. + * @param separator The separator string. + * @return the list, as a string, separated by the separator string. + */ + public static String listToString(final List list, + final String separator) { + + final StringBuilder strBuilder = new StringBuilder(); + for (final String s : list) { + if (!StringUtils.isEmpty(s)) { + strBuilder.append(AttributeUtil.escape(s) + separator); + } + } + return strBuilder.toString(); + } + + /** + * Given a separator and a map of strings to strings, joins the map, as a + * string, separated by the separator string with the pair key/value + * concatenated with a '='. + * + * @param map The map of strings to join. + * @param separator The separator string. + * + * @return the map of strings, as a string, separated by the separator string + * with the pair key/value concatenated with a '='. + */ + public static String mapToString(final Map map, + final String separator) { + + final StringBuilder strBuilder = new StringBuilder(); + final Iterator> valuesIt = map.entrySet().iterator(); + while (valuesIt.hasNext()) { + final Entry entry = valuesIt.next(); + strBuilder.append(entry.getKey()); + strBuilder.append('='); + strBuilder.append(AttributeUtil.escape(entry.getValue())); + strBuilder.append(separator); + } + return strBuilder.toString(); + } + + /** + * Validates the attribute value format. + * + * @param value The attribute value to validate. + * + * @return true if value has a valid format. + */ + public static boolean isValidValue(final String value) { + boolean retVal = false; + if (value != null && value.charAt(0) == '[' && value.endsWith("]")) { + final String tmpAttrValue = value.substring(1, value.length() - 1); + final String[] vals = + tmpAttrValue.split(PEPSValues.ATTRIBUTE_VALUE_SEP.toString()); + + if (tmpAttrValue.length() >= 0 + || (vals.length > 0 && vals[0].length() > 0)) { + retVal = true; + } + } + return retVal; + } + + /** + * Validates the attribute type value. It's case insensitive. E.g. return true + * value to: a) "true", "TRUE", "True", ... b) "false", "FALSE", "False", ... + * + * @param type The attribute type value. + * + * @return true if type has a true or false (case insensitive) value. + */ + public static boolean isValidType(final String type) { + return StringUtils.isNotEmpty(type) && (PEPSValues.TRUE.toString().equalsIgnoreCase(type) || PEPSValues.FALSE.toString().equalsIgnoreCase(type)) ; + } + + /** + * Validates the Personal attribute tuple. E.g. name:type:[value]:status + * + * @param tuples The Personal attribute's tuple. + * + * @return true if the tuples' format is valid. + * + * @see PEPSUtil#validateParameter(String, String, String) + * @see String#equalsIgnoreCase(String) + */ + public static boolean hasValidTuples(final String[] tuples) { + boolean retVal = false; + + final int numberTuples = AttributeConstants.NUMBER_TUPLES.intValue(); + if (tuples != null && tuples.length == numberTuples) { + // validate attrName + final int attrNameIndex = AttributeConstants.ATTR_NAME_INDEX.intValue(); + final int attrTypeIndex = AttributeConstants.ATTR_TYPE_INDEX.intValue(); + final int attrValueIndex = AttributeConstants.ATTR_VALUE_INDEX.intValue(); + + retVal = + StringUtils.isNotEmpty(tuples[attrNameIndex]) + && StringUtils.isNotEmpty(tuples[attrTypeIndex]) + && StringUtils.isNotEmpty(tuples[attrValueIndex]) + && AttributeUtil.isValidType(tuples[attrTypeIndex]) + && AttributeUtil.isValidValue(tuples[attrValueIndex]); + } + return retVal; + } + + /** + * Check if all mandatory attributes have values. + * + * @param personalAttrList The Personal Attributes List. + * + * @return true if all mandatory attributes have values, false if at least one + * attribute doesn't have value. + */ + public static boolean checkMandatoryAttributes( + final IPersonalAttributeList personalAttrList) { + + final Iterator itAttributes = + personalAttrList.values().iterator(); + boolean retVal = true; + while (itAttributes.hasNext() && retVal) { + final PersonalAttribute attr = itAttributes.next(); + if (attr.isRequired() + && !STORKStatusCode.STATUS_AVAILABLE.toString() + .equals(attr.getStatus())) { + retVal = false; + } + } + return retVal; + } +} -- cgit v1.2.3