1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.util;
20
21 import java.io.File;
22 import java.io.IOException;
23
24 import java.io.UnsupportedEncodingException;
25
26 import java.net.JarURLConnection;
27 import java.net.URL;
28 import java.net.URLConnection;
29
30 import java.net.URLDecoder;
31
32 import java.net.URLEncoder;
33
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38
39 import javax.servlet.http.HttpServletResponse;
40
41 public final class URLUtils
42 {
43 private URLUtils()
44 {
45 }
46
47 public static long getLastModified(URL url) throws IOException
48 {
49 if ("file".equals(url.getProtocol()))
50 {
51 String externalForm = url.toExternalForm();
52
53 File file = new File(externalForm.substring(5));
54
55 return file.lastModified();
56 }
57 else
58 {
59 return getLastModified(url.openConnection());
60 }
61 }
62
63 public static long getLastModified(URLConnection connection) throws IOException
64 {
65 long modified;
66 if (connection instanceof JarURLConnection)
67 {
68
69
70
71
72
73
74
75
76
77
78 URL jarFileUrl = ((JarURLConnection) connection).getJarFileURL();
79 URLConnection jarFileConnection = jarFileUrl.openConnection();
80
81 try
82 {
83 modified = jarFileConnection.getLastModified();
84 }
85 finally
86 {
87 try
88 {
89 jarFileConnection.getInputStream().close();
90 }
91 catch (Exception exception)
92 {
93
94 }
95 }
96 }
97 else
98 {
99 modified = connection.getLastModified();
100 }
101
102 return modified;
103 }
104
105
106
107
108 public static String jsEncodeURL(String url, String charset)
109 throws UnsupportedEncodingException
110 {
111 StringBuilder sb = new StringBuilder(url.length() * 2);
112 for(char c: url.toCharArray())
113 {
114 if ((c >= 'A' && c <= 'Z') ||
115 (c >= 'a' && c <= 'z') ||
116 (c >= '0' && c <= '9') ||
117 _JS_IMMUNE_CHARS.indexOf(c) > -1
118 )
119 {
120
121 sb.append(c);
122 }
123 else
124 {
125
126 for(byte b: Character.toString(c).getBytes(charset))
127 {
128 sb.append("\\x")
129 .append(String.format("%02X", b));
130 }
131 }
132 }
133 return sb.toString();
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147 public static String encodeURL(String url, Map<String, List<String>> params, String characterResponseEncoding)
148 {
149 String fragment = null;
150 String queryString = null;
151 Map<String, List<String>> paramMap = null;
152
153
154 int index = url.indexOf(_URL_FRAGMENT_SEPERATOR);
155 if (index != -1)
156 {
157 fragment = url.substring(index+1);
158 url = url.substring(0,index);
159 }
160
161
162 index = url.indexOf(_URL_QUERY_SEPERATOR);
163 if (index != -1)
164 {
165 queryString = url.substring(index + 1);
166 url = url.substring(0, index);
167 String[] nameValuePairs = queryString.split(_URL_PARAM_SEPERATOR);
168 for (int i = 0; i < nameValuePairs.length; i++)
169 {
170 String[] currentPair = nameValuePairs[i].split(_URL_NAME_VALUE_PAIR_SEPERATOR);
171
172 ArrayList<String> value = new ArrayList<String>(1);
173 try
174 {
175 value.add(currentPair.length > 1
176 ? URLDecoder.decode(currentPair[1], characterResponseEncoding)
177 : "");
178 }
179 catch (UnsupportedEncodingException e)
180 {
181
182 throw new UnsupportedOperationException("Encoding type=" + characterResponseEncoding
183 + " not supported", e);
184 }
185 if (paramMap == null)
186 {
187 paramMap = new HashMap<String, List<String>>();
188 }
189 paramMap.put(currentPair[0], value);
190 }
191 }
192
193
194 if (params != null && params.size() > 0)
195 {
196 for (Map.Entry<String, List<String>> pair : params.entrySet())
197 {
198 if (pair.getKey() != null && pair.getKey().trim().length() != 0)
199 {
200 if (paramMap == null)
201 {
202 paramMap = new HashMap<String, List<String>>();
203 }
204 paramMap.put(pair.getKey(), pair.getValue());
205 }
206 }
207 }
208
209
210 StringBuilder newUrl = new StringBuilder(url);
211
212
213 if (paramMap != null && paramMap.size()>0)
214 {
215 boolean isFirstPair = true;
216 for (Map.Entry<String, List<String>> pair : paramMap.entrySet())
217 {
218 for (String value : pair.getValue())
219 {
220 if (!isFirstPair)
221 {
222 newUrl.append(_URL_PARAM_SEPERATOR);
223 }
224 else
225 {
226 newUrl.append(_URL_QUERY_SEPERATOR);
227 isFirstPair = false;
228 }
229
230 newUrl.append(pair.getKey());
231 newUrl.append(_URL_NAME_VALUE_PAIR_SEPERATOR);
232 try
233 {
234 newUrl.append(URLEncoder.encode(value,characterResponseEncoding));
235 }
236 catch (UnsupportedEncodingException e)
237 {
238
239 throw new UnsupportedOperationException("Encoding type=" + characterResponseEncoding
240 + " not supported", e);
241 }
242 }
243 }
244 }
245
246
247 if (fragment != null)
248 {
249 newUrl.append(_URL_FRAGMENT_SEPERATOR + fragment);
250 }
251
252 return newUrl.toString();
253 }
254
255 private static final String _URL_PARAM_SEPERATOR="&";
256 private static final String _URL_QUERY_SEPERATOR="?";
257 private static final String _URL_FRAGMENT_SEPERATOR="#";
258 private static final String _URL_NAME_VALUE_PAIR_SEPERATOR="=";
259 private static final String _JS_IMMUNE_CHARS=",._";
260 }