一、前言
关于flex-webtier源码包中flex.webtier.server.j2ee.URLEncoder、flex.webtier.server.j2ee.URLDecoder、flex.webtier.server.j2ee.URLHelper的URL资源及加解密工具类,这边主要URLHelper提供出获取getQueryString链接访问路径、将传入参数转换为Map处理getParameterMap、对入参进行加密编码处理encode、对url的伪码处理escapeSpace等
二、源码说明
1.URLHelper类
package flex.webtier.server.j2ee;@b@@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.Set;@b@import java.util.StringTokenizer;@b@@b@public class URLHelper@b@{@b@ public static String getQueryString(String url)@b@ {@b@ String queryString = null;@b@@b@ if (url != null) {@b@ int queryMark = url.indexOf(63);@b@ if (queryMark != -1)@b@ queryString = url.substring(queryMark + 1);@b@@b@ }@b@@b@ return queryString;@b@ }@b@@b@ public static Map getParameterMap(String queryString)@b@ {@b@ Map map;@b@ if (queryString != null) {@b@ StringTokenizer tokens = new StringTokenizer(queryString, "?&");@b@@b@ map = new HashMap(tokens.countTokens() * 2);@b@@b@ while (tokens.hasMoreElements()) {@b@ String nameValuePair = tokens.nextToken();@b@ String name = nameValuePair;@b@ String value = "";@b@ int equalsIndex = nameValuePair.indexOf(61);@b@ if (equalsIndex != -1) {@b@ name = nameValuePair.substring(0, equalsIndex);@b@ if (name.length() > 0)@b@ value = nameValuePair.substring(equalsIndex + 1);@b@ }@b@@b@ map.put(name, value);@b@ }@b@ } else {@b@ map = new HashMap();@b@ }@b@@b@ return map;@b@ }@b@@b@ public static String encode(Map parameterMap)@b@ {@b@ return encode(parameterMap, false);@b@ }@b@@b@ public static String encode(Map parameterMap, boolean quoteAmpersands)@b@ {@b@ if ((parameterMap != null) && (!(parameterMap.isEmpty()))) {@b@ StringBuffer queryString = new StringBuffer();@b@@b@ Iterator it = parameterMap.entrySet().iterator();@b@ while (it.hasNext()) {@b@ Map.Entry entry = (Map.Entry)it.next();@b@ String name = (String)entry.getKey();@b@ String value = String.valueOf(entry.getValue());@b@ queryString.append(URLEncoder.encode(name));@b@ if ((value != null) && (!(value.equals("")))) {@b@ queryString.append('=');@b@ queryString.append(URLEncoder.encode(value));@b@ }@b@ if (it.hasNext())@b@ queryString = (quoteAmpersands) ? queryString.append("\"&\"") : queryString.append('&');@b@@b@ }@b@@b@ return queryString.toString();@b@ }@b@ return null;@b@ }@b@@b@ public static String escapeSpace(String uri)@b@ {@b@ return escapeCharacter(uri, ' ', "%20");@b@ }@b@@b@ public static String escapeCharacter(String uri, char c, String to)@b@ {@b@ int i;@b@ StringBuffer sb = new StringBuffer();@b@@b@ int size = uri.length();@b@ int at = uri.indexOf(58);@b@ int lastAt = 0;@b@@b@ if (at > -1)@b@ {@b@ for (i = 0; i <= at; ++i)@b@ sb.append(uri.charAt(i));@b@ lastAt = ++at;@b@ }@b@@b@ while ((at = uri.indexOf(c, at)) > -1)@b@ {@b@ for (i = lastAt; i < at; ++i) {@b@ sb.append(uri.charAt(i));@b@ }@b@@b@ sb.append(to);@b@ lastAt = ++at;@b@ }@b@@b@ if (lastAt < size)@b@ {@b@ for (i = lastAt; i < size; ++i)@b@ sb.append(uri.charAt(i));@b@ }@b@ return sb.toString();@b@ }@b@}
2.URLEncoder、URLDecoder类
package flex.webtier.server.j2ee;@b@@b@import java.io.BufferedWriter;@b@import java.io.ByteArrayOutputStream;@b@import java.io.IOException;@b@import java.io.OutputStreamWriter;@b@import java.io.UnsupportedEncodingException;@b@@b@public final class URLEncoder@b@{@b@ public static final String charset = "UTF8";@b@@b@ public static String encode(String s)@b@ {@b@ try@b@ {@b@ return encode(s, "UTF8");@b@ }@b@ catch (UnsupportedEncodingException ex)@b@ {@b@ throw new IllegalArgumentException("UTF8");@b@ }@b@ }@b@@b@ public static String encode(String s, String enc) throws UnsupportedEncodingException@b@ {@b@ if (!(needsEncoding(s)))@b@ {@b@ return s;@b@ }@b@@b@ int length = s.length();@b@@b@ StringBuffer out = new StringBuffer(length);@b@@b@ ByteArrayOutputStream buf = new ByteArrayOutputStream(10);@b@@b@ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(buf, enc));@b@@b@ for (int i = 0; i < length; ++i)@b@ {@b@ int c = s.charAt(i);@b@ if (((c >= 97) && (c <= 122)) || ((c >= 65) && (c <= 90)) || ((c >= 48) && (c <= 57)) || (c == 32))@b@ {@b@ if (c == 32)@b@ {@b@ c = 43;@b@ }@b@@b@ toHex(out, buf.toByteArray());@b@ buf.reset();@b@@b@ out.append((char)c);@b@ }@b@ else@b@ {@b@ try@b@ {@b@ writer.write(c);@b@@b@ if ((c >= 55296) && (c <= 56319) && (i < length - 1))@b@ {@b@ int d = s.charAt(i + 1);@b@ if ((d >= 56320) && (d <= 57343))@b@ {@b@ writer.write(d);@b@ ++i;@b@ }@b@ }@b@@b@ writer.flush();@b@ }@b@ catch (IOException ex)@b@ {@b@ throw new IllegalArgumentException(s);@b@ }@b@ }@b@ }@b@@b@ toHex(out, buf.toByteArray());@b@@b@ return out.toString();@b@ }@b@@b@ private static void toHex(StringBuffer buffer, byte[] b)@b@ {@b@ for (int i = 0; i < b.length; ++i)@b@ {@b@ buffer.append('%');@b@@b@ char ch = Character.forDigit(b[i] >> 4 & 0xF, 16);@b@ if (Character.isLetter(ch))@b@ {@b@ ch = (char)(ch - ' ');@b@ }@b@ buffer.append(ch);@b@@b@ ch = Character.forDigit(b[i] & 0xF, 16);@b@ if (Character.isLetter(ch))@b@ {@b@ ch = (char)(ch - ' ');@b@ }@b@ buffer.append(ch);@b@ }@b@ }@b@@b@ private static boolean needsEncoding(String s)@b@ {@b@ if (s == null)@b@ {@b@ return false;@b@ }@b@@b@ int length = s.length();@b@@b@ for (int i = 0; i < length; ++i)@b@ {@b@ int c = s.charAt(i);@b@ if ((((c < 97) || (c > 122))) && (((c < 65) || (c > 90)))) { if ((c >= 48) && (c <= 57)) { break label65:@b@ }@b@@b@ return true;@b@ }@b@ }@b@@b@ label65: return false;@b@ }@b@}
package flex.webtier.server.j2ee;@b@@b@import java.io.UnsupportedEncodingException;@b@@b@public final class URLDecoder@b@{@b@ public static String decode(String s)@b@ {@b@ try@b@ {@b@ return decode(s, "UTF8");@b@ }@b@ catch (UnsupportedEncodingException ex)@b@ {@b@ throw new IllegalArgumentException("UTF8");@b@ }@b@ }@b@@b@ public static String decode(String s, String enc) throws UnsupportedEncodingException@b@ {@b@ if (!(needsDecoding(s)))@b@ {@b@ return s;@b@ }@b@@b@ int length = s.length();@b@ byte[] bytes = new byte[length];@b@@b@ s.getBytes(0, length, bytes, 0);@b@ int k = 0;@b@ length = bytes.length;@b@ for (int i = 0; i < length; ++i)@b@ {@b@ if (bytes[i] == 37)@b@ {@b@ while (bytes[(i + 1)] == 37)@b@ {@b@ ++i;@b@ }@b@ if (i < length - 2)@b@ {@b@ bytes[k] = x2c(bytes, i);@b@ i += 2; break label126:@b@ }@b@@b@ throw new IllegalArgumentException(s);@b@ }@b@@b@ if (bytes[i] == 43)@b@ {@b@ bytes[k] = 32;@b@ }@b@ else@b@ {@b@ bytes[k] = bytes[i];@b@ }@b@ ++k;@b@ }@b@@b@ label126: return new String(bytes, 0, k, enc);@b@ }@b@@b@ private static boolean needsDecoding(String s)@b@ {@b@ if (s == null)@b@ {@b@ return false;@b@ }@b@@b@ int length = s.length();@b@@b@ for (int i = 0; i < length; ++i)@b@ {@b@ int c = s.charAt(i);@b@ if ((c == 43) || (c == 37))@b@ {@b@ return true;@b@ }@b@ }@b@@b@ return false;@b@ }@b@@b@ private static byte x2c(byte[] b, int i)@b@ {@b@ byte b1 = b[(i + 1)];@b@ byte b2 = b[(i + 2)];@b@@b@ if ((b1 < 48) || ((b1 > 70) && (b1 < 97)) || (b1 > 102) || (b2 < 48) || ((b2 > 70) && (b2 < 97)) || (b2 > 102))@b@ {@b@ throw new IllegalArgumentException("%" + (char)b1 + (char)b2);@b@ }@b@@b@ int result = (b1 >= 65) ? (b1 & 0xDF) - 65 + 10 : b1 - 48;@b@ result *= 16;@b@ result += ((b2 >= 65) ? (b2 & 0xDF) - 65 + 10 : b2 - 48);@b@ return (byte)result;@b@ }@b@}