// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov. // Jad home page: http://www.geocities.com/kpdus/jad.html // Decompiler options: braces fieldsfirst space lnc package org.apache.oltu.oauth2.common.utils; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import org.apache.commons.codec.binary.Base64; import org.apache.oltu.oauth2.common.exception.OAuthProblemException; import org.apache.oltu.oauth2.common.exception.OAuthSystemException; public final class OAuthUtils { public static final String AUTH_SCHEME = "Bearer"; private static final String DEFAULT_CONTENT_CHARSET = "UTF-8"; private static final String ENCODING = "UTF-8"; public static final String MULTIPART = "multipart/"; private static final String NAME_VALUE_SEPARATOR = "="; private static final Pattern NVP = Pattern.compile("(\\S*)\\s*\\=\\s*\"([^\"]*)\""); private static final Pattern OAUTH_HEADER = Pattern.compile("\\s*(\\w*)\\s+(.*)"); private static final String PARAMETER_SEPARATOR = "&"; public OAuthUtils() { } public static String[] decodeClientAuthenticationHeader(String s) { String as[]; if (s != null && !"".equals(s)) { if ((s = s.split(" ")) != null && (s[0] == null || "".equals(s[0]) || s[0].equalsIgnoreCase("basic")) && s[1] != null && !"".equals(s[1]) && ((s = new String(Base64.decodeBase64(s[1]))).contains(":") && s.split(":").length == 2) && (!isEmpty((as = s.split(":"))[0]) && !isEmpty(as[1]))) { return s.split(":"); } } return null; } public static Map decodeForm(String s) { HashMap hashmap = new HashMap(); if (!isEmpty(s)) { String as[] = s.split("\\&"); int j = as.length; int i = 0; while (i < j) { String s1 = as[i]; int k = s1.indexOf('='); if (k < 0) { s = decodePercent(s1); s1 = null; } else { s = decodePercent(s1.substring(0, k)); s1 = decodePercent(s1.substring(k + 1)); } hashmap.put(s, s1); i++; } } return hashmap; } public static Map decodeOAuthHeader(String s) { HashMap hashmap = new HashMap(); if (s != null) { s = OAUTH_HEADER.matcher(s); if (s.matches() && "Bearer".equalsIgnoreCase(s.group(1))) { s = s.group(2).split("\\s*,\\s*"); int j = s.length; for (int i = 0; i < j; i++) { Object obj = s[i]; obj = NVP.matcher(((CharSequence) (obj))); if (((Matcher) (obj)).matches()) { hashmap.put(decodePercent(((Matcher) (obj)).group(1)), decodePercent(((Matcher) (obj)).group(2))); } } } } return hashmap; } public static String decodePercent(String s) { try { s = URLDecoder.decode(s, "UTF-8"); } // Misplaced declaration of an exception variable catch (String s) { throw new RuntimeException(s.getMessage(), s); } return s; } public static Set decodeScopes(String s) { HashSet hashset = new HashSet(); if (!isEmpty(s)) { for (s = new StringTokenizer(s, " "); s.hasMoreElements(); hashset.add(s.nextToken())) { } } return hashset; } private static String encode(String s, String s1) { if (s1 == null) { s1 = "UTF-8"; } try { return URLEncoder.encode(s, s1); } // Misplaced declaration of an exception variable catch (String s) { throw new IllegalArgumentException(s); } } public static String encodeAuthorizationBearerHeader(Map map) { StringBuffer stringbuffer = new StringBuffer(); stringbuffer.append("Bearer").append(" "); Iterator iterator = map.entrySet().iterator(); do { if (!iterator.hasNext()) { break; } java.util.Map.Entry entry = (java.util.Map.Entry)iterator.next(); if (entry.getValue() == null) { map = null; } else { map = String.valueOf(entry.getValue()); } if (!isEmpty((String)entry.getKey()) && !isEmpty(map)) { stringbuffer.append(map); } } while (true); return stringbuffer.toString(); } public static String encodeOAuthHeader(Map map) { StringBuffer stringbuffer = new StringBuffer(); stringbuffer.append("Bearer").append(" "); Iterator iterator = map.entrySet().iterator(); do { if (!iterator.hasNext()) { break; } java.util.Map.Entry entry = (java.util.Map.Entry)iterator.next(); if (entry.getValue() == null) { map = null; } else { map = String.valueOf(entry.getValue()); } if (!isEmpty((String)entry.getKey()) && !isEmpty(map)) { stringbuffer.append((String)entry.getKey()); stringbuffer.append("=\""); stringbuffer.append(map); stringbuffer.append("\","); } } while (true); return stringbuffer.substring(0, stringbuffer.length() - 1); } public static String encodeScopes(Set set) { StringBuffer stringbuffer = new StringBuffer(); for (set = set.iterator(); set.hasNext(); stringbuffer.append((String)set.next()).append(" ")) { } return stringbuffer.toString().trim(); } public static String format(Collection collection, String s) { StringBuilder stringbuilder = new StringBuilder(); Iterator iterator = collection.iterator(); do { if (!iterator.hasNext()) { break; } Object obj = (java.util.Map.Entry)iterator.next(); if (((java.util.Map.Entry) (obj)).getValue() == null) { collection = null; } else { collection = String.valueOf(((java.util.Map.Entry) (obj)).getValue()); } if (!isEmpty((String)((java.util.Map.Entry) (obj)).getKey()) && !isEmpty(collection)) { obj = encode((String)((java.util.Map.Entry) (obj)).getKey(), s); if (collection != null) { collection = encode(collection, s); } else { collection = ""; } if (stringbuilder.length() > 0) { stringbuilder.append("&"); } stringbuilder.append(((String) (obj))); stringbuilder.append("="); stringbuilder.append(collection); } } while (true); return stringbuilder.toString(); } public static String getAuthHeaderField(String s) { if (s != null) { s = OAUTH_HEADER.matcher(s); if (s.matches() && "Bearer".equalsIgnoreCase(s.group(1))) { return s.group(2); } } return null; } public static String getAuthzMethod(String s) { if (s != null) { s = OAUTH_HEADER.matcher(s); if (s.matches()) { return s.group(1); } } return null; } public static OAuthProblemException handleBadContentTypeException(String s) { return handleOAuthProblemException((new StringBuilder("Bad request content type. Expecting: ")).append(s).toString()); } public static OAuthProblemException handleMissingParameters(Set set) { StringBuffer stringbuffer = new StringBuffer("Missing parameters: "); if (!isEmpty(set)) { for (set = set.iterator(); set.hasNext(); stringbuffer.append((String)set.next()).append(" ")) { } } return handleOAuthProblemException(stringbuffer.toString().trim()); } public static OAuthProblemException handleNotAllowedParametersOAuthException(List list) { StringBuffer stringbuffer = new StringBuffer("Not allowed parameters: "); if (list != null) { for (list = list.iterator(); list.hasNext(); stringbuffer.append((String)list.next()).append(" ")) { } } return handleOAuthProblemException(stringbuffer.toString().trim()); } public static OAuthProblemException handleOAuthProblemException(String s) { return OAuthProblemException.error("invalid_request").description(s); } public static boolean hasContentType(String s, String s1) { if (!isEmpty(s1) && !isEmpty(s)) { s = new StringTokenizer(s, ";"); while (s.hasMoreTokens()) { if (s1.equals(s.nextToken())) { return true; } } } return false; } public static boolean hasEmptyValues(String as[]) { if (as != null && as.length != 0) goto _L2; else goto _L1 _L1: return true; _L2: int j = as.length; int i = 0; label0: do { label1: { if (i >= j) { break label1; } if (isEmpty(as[i])) { break label0; } i++; } } while (true); if (true) goto _L1; else goto _L3 _L3: return false; } public static Object instantiateClass(Class class1) throws OAuthSystemException { return instantiateClassWithParameters(class1, null, null); } public static Object instantiateClassWithParameters(Class class1, Class aclass[], Object aobj[]) throws OAuthSystemException { if (aclass == null || aobj == null) { break MISSING_BLOCK_LABEL_61; } try { if (aclass.length != aobj.length) { throw new IllegalArgumentException("Number of types and values must be equal"); } } // Misplaced declaration of an exception variable catch (Class class1) { throw new OAuthSystemException(class1); } // Misplaced declaration of an exception variable catch (Class class1) { throw new OAuthSystemException(class1); } // Misplaced declaration of an exception variable catch (Class class1) { throw new OAuthSystemException(class1); } // Misplaced declaration of an exception variable catch (Class class1) { throw new OAuthSystemException(class1); } if (aclass.length == 0 && aobj.length == 0) { return class1.newInstance(); } else { return class1.getConstructor(aclass).newInstance(aobj); } class1 = ((Class) (class1.newInstance())); return class1; } public static boolean isEmpty(String s) { return s == null || "".equals(s); } private static boolean isEmpty(Set set) { return set == null || set.size() == 0; } public static boolean isFormEncoded(String s) { if (s == null) { return false; } int i = s.indexOf(";"); String s1 = s; if (i >= 0) { s1 = s.substring(0, i); } return "application/x-www-form-urlencoded".equalsIgnoreCase(s1.trim()); } public static boolean isMultipart(HttpServletRequest httpservletrequest) { if ("post".equals(httpservletrequest.getMethod().toLowerCase())) { if ((httpservletrequest = httpservletrequest.getContentType()) != null && httpservletrequest.toLowerCase().startsWith("multipart/")) { return true; } } return false; } public static String percentEncode(Iterable iterable) { StringBuilder stringbuilder = new StringBuilder(); iterable = iterable.iterator(); do { if (!iterable.hasNext()) { break; } Object obj = iterable.next(); if (!isEmpty(toString(obj))) { if (stringbuilder.length() > 0) { stringbuilder.append("&"); } stringbuilder.append(percentEncode(toString(obj))); } } while (true); return stringbuilder.toString(); } public static String percentEncode(String s) { if (s == null) { return ""; } try { s = URLEncoder.encode(s, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~"); } // Misplaced declaration of an exception variable catch (String s) { throw new RuntimeException(s.getMessage(), s); } return s; } public static String saveStreamAsString(InputStream inputstream) throws IOException { return toString(inputstream, "UTF-8"); } public static String toString(InputStream inputstream, String s) throws IOException { if (inputstream == null) { throw new IllegalArgumentException("InputStream may not be null"); } String s1 = s; if (s == null) { s1 = "UTF-8"; } inputstream = new InputStreamReader(inputstream, s1); s = new StringBuilder(); char ac[] = new char[4096]; _L1: int i = inputstream.read(ac); if (i == -1) { break MISSING_BLOCK_LABEL_77; } s.append(ac, 0, i); goto _L1 s; inputstream.close(); throw s; inputstream.close(); return s.toString(); } private static final String toString(Object obj) { if (obj == null) { return null; } else { return obj.toString(); } } }