Java tutorial
/** * * ?? * SCIMUtil * * ? * (?) * * * Copyright (c) 2015 OpenID Foundation Japan. * This is released under the MIT License, see LICENSE file. */ package jp.or.openid.eiwg.scim.util; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletResponse; import jp.or.openid.eiwg.constants.MessageConstants; import org.apache.commons.codec.binary.Base64; public class SCIMUtil { /** * BASE64 * * @param val ? * @return ? * @throws UnsupportedEncodingException * @throws Exception */ public static String decodeBase64(String val) throws IOException { // BASE64? val = val.replaceAll("[\n|\r]", ""); String decodeString = new String(Base64.decodeBase64(val), "UTF-8"); // ??????? String encodeString = encodeBase64(decodeString); if (encodeString.compareToIgnoreCase(val) != 0) { decodeString = ""; } // BASE64?? return decodeString; } /** * BASE64 * * @param val ? * @return ? * @throws UnsupportedEncodingException * @throws Exception */ public static String encodeBase64(String val) throws UnsupportedEncodingException { return new String(Base64.encodeBase64(val.getBytes("UTF-8"))); } /** * ?????? * * @param resourceInfo * @param attributeName ??? * @return */ public static Object getAttribute(Map<String, Object> resourceInfo, String attributeName) { Object result = null; Iterator<String> attributeNameIt = resourceInfo.keySet().iterator(); while (attributeNameIt.hasNext()) { String name = attributeNameIt.next(); if (name.equalsIgnoreCase(attributeName)) { result = resourceInfo.get(name); break; } } return result; } /** * ?????? * * @param context * @param attributeName * @param isCore * @return ? */ public static LinkedHashMap<String, Object> getUserAttributeInfo(ServletContext context, String attributeName, boolean isCore) { LinkedHashMap<String, Object> result = null; Set<String> schemaIdSet = new HashSet<>(); // ? @SuppressWarnings("unchecked") ArrayList<LinkedHashMap<String, Object>> resourceTypes = (ArrayList<LinkedHashMap<String, Object>>) context .getAttribute("ResourceTypes"); Iterator<LinkedHashMap<String, Object>> resourceTypesIt = resourceTypes.iterator(); while (resourceTypesIt.hasNext()) { LinkedHashMap<String, Object> resourceTypeInfo = resourceTypesIt.next(); Object endpoint = SCIMUtil.getAttribute(resourceTypeInfo, "endpoint"); if (endpoint != null && endpoint.toString().equalsIgnoreCase("/Users")) { Object schema = SCIMUtil.getAttribute(resourceTypeInfo, "schema"); if (schema != null) { schemaIdSet.add(schema.toString()); } Object schemaExtensions = SCIMUtil.getAttribute(resourceTypeInfo, "schemaExtensions"); if (schemaExtensions != null && schemaExtensions instanceof ArrayList) { @SuppressWarnings("unchecked") ArrayList<LinkedHashMap<String, Object>> schemaExtensionList = (ArrayList<LinkedHashMap<String, Object>>) schemaExtensions; Iterator<LinkedHashMap<String, Object>> schemaExtensionListIt = schemaExtensionList.iterator(); while (schemaExtensionListIt.hasNext()) { LinkedHashMap<String, Object> schemaExtensionInfo = schemaExtensionListIt.next(); schema = SCIMUtil.getAttribute(schemaExtensionInfo, "schema"); if (schema != null) { schemaIdSet.add(schema.toString()); } } } break; } } // ? @SuppressWarnings("unchecked") ArrayList<LinkedHashMap<String, Object>> schemas = (ArrayList<LinkedHashMap<String, Object>>) context .getAttribute("Schemas"); Iterator<LinkedHashMap<String, Object>> schemasIt = schemas.iterator(); while (schemasIt.hasNext()) { LinkedHashMap<String, Object> schemaInfo = schemasIt.next(); // id? Object id = SCIMUtil.getAttribute(schemaInfo, "id"); if (id != null && id instanceof String) { // ?? boolean isUserSchema = false; if (!schemaIdSet.isEmpty()) { Iterator<String> schemaIdSetIt = schemaIdSet.iterator(); while (schemaIdSetIt.hasNext()) { if (id.toString().equalsIgnoreCase(schemaIdSetIt.next())) { isUserSchema = true; } } } if (isUserSchema) { Object attributes = schemaInfo.get("attributes"); if (attributes != null && attributes instanceof ArrayList) { @SuppressWarnings("unchecked") ArrayList<LinkedHashMap<String, Object>> attributeList = (ArrayList<LinkedHashMap<String, Object>>) attributes; Iterator<LinkedHashMap<String, Object>> attributeListIt = attributeList.iterator(); while (attributeListIt.hasNext()) { LinkedHashMap<String, Object> attributeInfo = attributeListIt.next(); Object name = attributeInfo.get("name"); // ?? if (attributeName.equalsIgnoreCase(name.toString())) { result = attributeInfo; break; } } } } } } if (result == null && isCore) { if (attributeName.equalsIgnoreCase("schemas")) { result = new LinkedHashMap<String, Object>(); result.put("name", "schemas"); result.put("type", "string"); result.put("multiValued", true); result.put("mutability", "readWrite"); } else if (attributeName.equalsIgnoreCase("id")) { result = new LinkedHashMap<String, Object>(); result.put("name", "id"); result.put("type", "string"); result.put("multiValued", false); result.put("mutability", "readOnly"); } else if (attributeName.equalsIgnoreCase("meta")) { result = new LinkedHashMap<String, Object>(); result.put("name", "meta"); result.put("type", "complex"); result.put("multiValued", false); result.put("mutability", "readOnly"); } } return result; } /** * ????? * * ???(Simple? equal match)?? * ????DBLDAP?? * SQLLDAP?????? * * @param context * @param resourceInfo * @param filter * @return :true ?:false * @throws SCIMUtilException */ public static boolean checkUserSimpleFilter(ServletContext context, Map<String, Object> resourceInfo, String filter) throws SCIMUtilException { boolean result = false; // ? String work = filter; String filterAttribute = null; String operator = null; String filterValue = null; //????? int pos = work.indexOf(" "); pos = work.indexOf(" "); if (pos > 0) { // ??? filterAttribute = work.substring(0, pos); // ?? work = work.substring(pos + 1).trim(); pos = work.indexOf(" "); if (pos > 0) { operator = work.substring(0, pos); // // ???? eq ?? // if (operator.equals("eq")) { // ? filterValue = work.substring(2).trim(); // ?? if (filterValue.length() >= 2 && filterValue.charAt(0) == '\"' && filterValue.charAt(filterValue.length() - 1) == '\"') { filterValue = filterValue.substring(1, filterValue.length() - 1); } } else { String message = String.format(MessageConstants.ERROR_INVALID_FILTER_SYNTAX, filter); throw new SCIMUtilException(HttpServletResponse.SC_BAD_REQUEST, null, message); } } else { String message = String.format(MessageConstants.ERROR_INVALID_FILTER_SYNTAX, filter); throw new SCIMUtilException(HttpServletResponse.SC_BAD_REQUEST, null, message); } } else { String message = String.format(MessageConstants.ERROR_INVALID_FILTER_SYNTAX, filter); throw new SCIMUtilException(HttpServletResponse.SC_BAD_REQUEST, null, message); } // ??? String attributeName = null; //String subAttributeName = null; // ? if (filterAttribute.indexOf(".") > 0) { pos = filterAttribute.indexOf("."); attributeName = filterAttribute.substring(0, pos); //subAttributeName = filterAttribute.substring( pos + 1 ); } // ?? else { attributeName = filterAttribute; } // ? LinkedHashMap<String, Object> attributeSchema = SCIMUtil.getUserAttributeInfo(context, attributeName, true); if (attributeSchema == null) { String message = String.format(MessageConstants.ERROR_INVALID_FILTER_SYNTAX, filter); throw new SCIMUtilException(HttpServletResponse.SC_BAD_REQUEST, null, message); } // ? Iterator<String> attributeIt = resourceInfo.keySet().iterator(); while (attributeIt.hasNext()) { String name = attributeIt.next(); if (name.equalsIgnoreCase(attributeName)) { Object values = resourceInfo.get(name); if (values != null) { // // ???? Simple ???? // if (values instanceof String) { if (filterValue.equalsIgnoreCase(values.toString())) { result = true; } } else if (values instanceof Integer) { if (filterValue.equalsIgnoreCase(values.toString())) { result = true; } } else if (values instanceof Boolean) { if (filterValue.equalsIgnoreCase(values.toString())) { result = true; } } } break; } } return result; } }