1 13 package com.tonbeller.wcf.utils; 14 15 import java.util.StringTokenizer ; 16 17 import javax.servlet.http.HttpServletRequest ; 18 19 import com.tonbeller.wcf.charset.CharsetFilter; 20 21 25 public class UrlUtils { 26 27 31 public static final String URLENCODING = "ISO-8859-1"; 32 33 private UrlUtils() { 34 } 35 36 43 public static String forceExtension(String url, String ext) { 44 return forceExtension(url, ".jsp", ext); 45 } 46 47 54 public static boolean matchPattern(HttpServletRequest req, String [] urlPattern) { 55 String contextPath = req.getContextPath(); 56 String requestUri = req.getRequestURI(); 57 requestUri = requestUri.substring(contextPath.length()); 58 return matchPattern(requestUri, urlPattern); 59 } 60 61 66 public static boolean matchPattern(String uri, String [] pattern) { 67 for (int i = 0; i < pattern.length; i++) { 68 if (matchPattern(uri, pattern[i])) 69 return true; 70 } 71 return false; 72 } 73 74 79 public static boolean matchPattern(String uri, String pattern) { 80 if (pattern.equals("/*")) 81 return true; 82 83 if (pattern.equals(uri)) 84 return true; 85 86 if (pattern.startsWith("*.")) { 87 if (uri.endsWith(pattern.substring(2))) 88 return true; 89 return false; 90 } 91 92 if (pattern.endsWith("/*")) { 93 String prefix = pattern.substring(0, pattern.length() - 2); 94 if (!uri.startsWith(prefix)) 95 return false; 96 if (uri.equals(prefix)) 98 return true; 99 100 char c = uri.charAt(prefix.length()); 104 return c == '/'; 105 } 106 107 return false; 108 } 109 110 114 public static String [] parseUrlPatternList(String urlPatternList) { 115 if (urlPatternList != null) { 116 StringTokenizer st = new StringTokenizer (urlPatternList); 117 int N = st.countTokens(); 118 String [] passThru = new String [N]; 119 for (int i = 0; i < N; i++) 120 passThru[i] = st.nextToken(); 121 return passThru; 122 } 123 return null; 124 } 125 126 public static String forceExtension(String url, String old, String ext) { 127 if (url != null && ext != null) { 128 int dot = url.lastIndexOf(old); 129 if (dot >= 0) { 130 int qmk = url.indexOf('?', dot); 131 String queryParam; 132 if (qmk > 0) 133 queryParam = url.substring(qmk); 134 else 135 queryParam = ""; 136 url = url.substring(0, dot) + ext + queryParam; 137 } 138 } 139 return url; 140 } 141 142 public static String redirectURI(HttpServletRequest request, String uri) { 143 if (uri.startsWith("/")) { 144 StringBuffer sb = new StringBuffer (); 145 sb.append(request.getContextPath()); 146 sb.append(uri); 147 uri = sb.toString(); 148 } 149 return uri; 150 } 151 152 } | Popular Tags |