KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > utils > UrlUtils


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.utils;
14
15 import java.util.StringTokenizer JavaDoc;
16
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18
19 import com.tonbeller.wcf.charset.CharsetFilter;
20
21 /**
22  * @author av
23  * @since 12.07.2004
24  */

25 public class UrlUtils {
26   
27   /**
28    * URL encoding to be used with
29    * @deprecated - use {@link CharsetFilter#getEncoding()} instead
30    */

31   public static final String JavaDoc URLENCODING = "ISO-8859-1";
32
33   private UrlUtils() {
34   }
35
36   /**
37    * ensures that the extension of the file portion matches ext. E.g.
38    * forceExtension("/a/b.jsp?x=y", ".jspx") will return "/a/b.jspx?x=y".
39    *
40    * @param url the url to modify
41    * @param ext the required extension
42    */

43   public static String JavaDoc forceExtension(String JavaDoc url, String JavaDoc ext) {
44     return forceExtension(url, ".jsp", ext);
45   }
46
47   /**
48    * true, if the request uri matches one of the url patterns.
49    * @param req the request
50    * @param urlPattern an url pattern may start with "*.ext" which is an extension
51    * mapping or end with "/*" which is a prefix mapping, or "/" which matches
52    * everything.
53    */

54   public static boolean matchPattern(HttpServletRequest JavaDoc req, String JavaDoc[] urlPattern) {
55     String JavaDoc contextPath = req.getContextPath();
56     String JavaDoc requestUri = req.getRequestURI();
57     requestUri = requestUri.substring(contextPath.length());
58     return matchPattern(requestUri, urlPattern);
59   }
60
61   /**
62    * true if requestUri matches one of the urlPatterns
63    * @param uri w/o contextPath
64    * @param pattern
65    */

66   public static boolean matchPattern(String JavaDoc uri, String JavaDoc[] 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   /**
75    * true if requestUri matches one of the urlPatterns
76    * @param uri w/o contextPath
77    * @param pattern
78    */

79   public static boolean matchPattern(String JavaDoc uri, String JavaDoc 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 JavaDoc prefix = pattern.substring(0, pattern.length() - 2);
94       if (!uri.startsWith(prefix))
95         return false;
96       // "/a/b" matches "/a/b/*"
97
if (uri.equals(prefix))
98         return true;
99
100       // "/a/bc" does not match "/a/b/*"
101
// "/a/b.jsp" does not match "/a/b/*"
102
// "/a/b/b" does match "/a/b/*"
103
char c = uri.charAt(prefix.length());
104       return c == '/';
105     }
106     
107     return false;
108   }
109
110   /**
111    * parses urlPatterns from a whitespace separated list.
112    * @return null if urlPatternList is null
113    */

114   public static String JavaDoc[] parseUrlPatternList(String JavaDoc urlPatternList) {
115     if (urlPatternList != null) {
116       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(urlPatternList);
117       int N = st.countTokens();
118       String JavaDoc[] passThru = new String JavaDoc[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 JavaDoc forceExtension(String JavaDoc url, String JavaDoc old, String JavaDoc 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 JavaDoc 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 JavaDoc redirectURI(HttpServletRequest JavaDoc request, String JavaDoc uri) {
143     if (uri.startsWith("/")) {
144       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
145       sb.append(request.getContextPath());
146       sb.append(uri);
147       uri = sb.toString();
148     }
149     return uri;
150   }
151
152 }
Popular Tags