1 34 package net.myvietnam.mvncore.security; 35 36 import java.lang.reflect.Method ; 37 import java.net.URLDecoder ; 38 import java.net.URLEncoder ; 39 import java.security.MessageDigest ; 40 41 import net.myvietnam.mvncore.misc.Base64; 42 import net.myvietnam.mvncore.util.MailUtil; 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 46 public class Encoder { 47 48 private static Log log = LogFactory.getLog(Encoder.class); 49 50 private static MessageDigest digest = null; 53 private static boolean isInited = false; 54 55 private static Method encodeMethod1_4 = null; 56 private static Method decodeMethod1_4 = null; 57 58 static { 62 try { 63 Class urlEncoderClass = Class.forName("java.net.URLEncoder"); 64 encodeMethod1_4 = urlEncoderClass.getMethod("encode", 65 new Class [] {String .class, String .class}); 66 } catch (Exception ex) {} 68 try { 69 Class urlDecoderClass = Class.forName("java.net.URLDecoder"); 70 decodeMethod1_4 = urlDecoderClass.getMethod("decode", 71 new Class [] {String .class, String .class}); 72 } catch (Exception ex) {} } 74 75 private Encoder() { 76 } 77 78 84 public static synchronized String getMD5_Base64(String input) { 85 if (isInited == false) { 89 isInited = true; 90 try { 91 digest = MessageDigest.getInstance("MD5"); 92 } catch (Exception ex) { 93 log.fatal("Cannot get MessageDigest. Application may fail to run correctly.", ex); 94 } 95 } 96 if (digest == null) return input; 97 98 try { 100 digest.update(input.getBytes("UTF-8")); 101 } catch (java.io.UnsupportedEncodingException ex) { 102 log.error("Assertion: This should never occur."); 103 } 104 byte[] rawData = digest.digest(); 105 byte[] encoded = Base64.encode(rawData); 106 String retValue = new String (encoded); 107 return retValue; 108 } 109 110 116 public static String encodeURL(String input) { 117 if (encodeMethod1_4 != null) { 118 Object [] methodArgsName = new Object [2]; 119 methodArgsName[0] = input; 120 methodArgsName[1] = "UTF-8"; 121 122 try { 123 return (String )encodeMethod1_4.invoke(null, methodArgsName); 124 } catch (Exception ex) { 125 throw new RuntimeException ("System error invoking URLEncoder.encode() by reflection."); 126 } 127 } else { 128 130 return URLEncoder.encode(input); 135 } 136 } 137 138 144 public static String decodeURL(String input) { 145 if (decodeMethod1_4 != null) { 146 Object [] methodArgsName = new Object [2]; 147 methodArgsName[0] = input; 148 methodArgsName[1] = "UTF-8"; 149 150 try { 151 return (String )decodeMethod1_4.invoke(null, methodArgsName); 152 } catch (Exception ex) { 153 throw new RuntimeException ("System error invoking URLDecoder.decode() by reflection."); 154 } 155 } else { 156 158 return URLDecoder.decode(input); 163 } 164 } 165 166 171 public static String filterUrl(String url) { 172 String lowerUrl = url.toLowerCase(); 173 if ( (lowerUrl.indexOf("javascript:") >= 0) || 174 lowerUrl.indexOf("file:") >= 0) { 175 return ""; 176 } 177 178 String protocol = "http://"; String name = null; 180 if (url.startsWith("http://")) { 181 protocol = "http://"; 182 name = url.substring(protocol.length()); } else if (url.startsWith("https://")) { 184 protocol = "https://"; 185 name = url.substring(protocol.length()); } else if (url.startsWith("ftp://")) { 187 protocol = "ftp://"; 188 name = url.substring(protocol.length()); } else if (url.startsWith("mailto:")) { 190 protocol = "mailto:"; 191 name = url.substring(protocol.length()); } else { 193 name = url; 194 } 195 String ret; 196 if (protocol.equals("mailto:")) { 197 try { 198 MailUtil.checkGoodEmail(name); 199 ret = protocol + name; 200 } catch (Exception ex) { 201 ret = ""; 202 } 203 } else { 204 ret = protocol + encodePath(name); 205 } 206 return ret; 207 } 208 209 214 public static String encodePath(String path) { 215 path = removeInvalidUserInURL(path); 216 return path; 217 238 } 239 240 248 private static String removeInvalidUserInURL(String path) { 249 int atIndex = path.lastIndexOf('@'); 251 if (atIndex != -1) { 252 int pecentIndex = path.indexOf('%'); 255 if ((pecentIndex != -1) && (pecentIndex < atIndex)) { 256 return path.substring(atIndex + 1); } 259 } 260 return path; 261 } 262 282 } 283 | Popular Tags |