KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > util > URLUtilities


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.util;
20
21 import java.io.UnsupportedEncodingException JavaDoc;
22 import java.net.URLDecoder JavaDoc;
23 import java.net.URLEncoder JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import org.apache.roller.config.RollerRuntimeConfig;
28 import org.apache.roller.pojos.WebsiteData;
29
30
31 /**
32  * Utilities class for building urls. This class is meant to centralize the
33  * logic behind building urls so that logic isn't duplicated throughout the
34  * code.
35  */

36 public final class URLUtilities {
37     
38     // non-intantiable
39
private URLUtilities() {}
40     
41     
42     public static final String JavaDoc getXmlrpcURL(boolean absolute) {
43         
44         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
45         
46         if(absolute) {
47             url.append(RollerRuntimeConfig.getAbsoluteContextURL());
48         } else {
49             url.append(RollerRuntimeConfig.getRelativeContextURL());
50         }
51         
52         url.append("/roller-services/xmlrpc");
53         
54         return url.toString();
55     }
56     
57     public static final String JavaDoc getAtomProtocolURL(boolean absolute) {
58         
59         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
60         
61         if(absolute) {
62             url.append(RollerRuntimeConfig.getAbsoluteContextURL());
63         } else {
64             url.append(RollerRuntimeConfig.getRelativeContextURL());
65         }
66         
67         url.append("/roller-services/app");
68         
69         return url.toString();
70     }
71     
72     
73     /**
74      * Get root url for a given weblog. Optionally for a certain locale.
75      */

76     public static final String JavaDoc getWeblogURL(WebsiteData weblog,
77                                             String JavaDoc locale,
78                                             boolean absolute) {
79         
80         if(weblog == null) {
81             return null;
82         }
83         
84         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
85         
86         if(absolute) {
87             url.append(RollerRuntimeConfig.getAbsoluteContextURL());
88         } else {
89             url.append(RollerRuntimeConfig.getRelativeContextURL());
90         }
91         
92         url.append("/").append(weblog.getHandle()).append("/");
93         
94         if(locale != null) {
95             url.append(locale).append("/");
96         }
97         
98         return url.toString();
99     }
100     
101     
102     /**
103      * Get url for a single weblog entry on a given weblog.
104      */

105     public static final String JavaDoc getWeblogEntryURL(WebsiteData weblog,
106                                                  String JavaDoc locale,
107                                                  String JavaDoc entryAnchor,
108                                                  boolean absolute) {
109         
110         if(weblog == null || entryAnchor == null) {
111             return null;
112         }
113         
114         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
115         
116         url.append(getWeblogURL(weblog, locale, absolute));
117         url.append("entry/").append(encode(entryAnchor));
118         
119         return url.toString();
120     }
121     
122     
123     /**
124      * Get url for a single weblog entry comments on a given weblog.
125      */

126     public static final String JavaDoc getWeblogCommentsURL(WebsiteData weblog,
127                                                     String JavaDoc locale,
128                                                     String JavaDoc entryAnchor,
129                                                     boolean absolute) {
130         
131         return getWeblogEntryURL(weblog, locale, entryAnchor, absolute)+"#comments";
132     }
133     
134     
135     /**
136      * Get url for a single weblog entry comment on a given weblog.
137      */

138     public static final String JavaDoc getWeblogCommentURL(WebsiteData weblog,
139                                                    String JavaDoc locale,
140                                                    String JavaDoc entryAnchor,
141                                                    String JavaDoc timeStamp,
142                                                    boolean absolute) {
143         
144         return getWeblogEntryURL(weblog, locale, entryAnchor, absolute)+"#comment-"+timeStamp;
145     }
146     
147     
148     /**
149      * Get url for a collection of entries on a given weblog.
150      */

151     public static final String JavaDoc getWeblogCollectionURL(WebsiteData weblog,
152                                                       String JavaDoc locale,
153                                                       String JavaDoc category,
154                                                       String JavaDoc dateString,
155                                                       int pageNum,
156                                                       boolean absolute) {
157         
158         if(weblog == null) {
159             return null;
160         }
161         
162         StringBuffer JavaDoc pathinfo = new StringBuffer JavaDoc();
163         Map JavaDoc params = new HashMap JavaDoc();
164         
165         pathinfo.append(getWeblogURL(weblog, locale, absolute));
166         
167         String JavaDoc cat = null;
168         if(category != null && "/".equals(category)) {
169             cat = null;
170         } else if(category != null && category.startsWith("/")) {
171             cat = category.substring(1);
172         }
173         
174         if(cat != null && dateString == null) {
175             // yes, for the path based versions we double encode the category
176
// because otherwise some characters cause problems, like "?"
177
pathinfo.append("category/").append(encode(encode(cat)));
178             
179         } else if(dateString != null && cat == null) {
180             pathinfo.append("date/").append(dateString);
181             
182         } else {
183             if(dateString != null) params.put("date", dateString);
184             if(cat != null) params.put("cat", encode(cat));
185         }
186
187         if(pageNum > 0) {
188             params.put("page", Integer.toString(pageNum));
189         }
190         
191         return pathinfo.toString() + getQueryString(params);
192     }
193     
194     
195     /**
196      * Get url for a custom page on a given weblog.
197      */

198     public static final String JavaDoc getWeblogPageURL(WebsiteData weblog,
199                                                 String JavaDoc locale,
200                                                 String JavaDoc pageLink,
201                                                 String JavaDoc entryAnchor,
202                                                 String JavaDoc category,
203                                                 String JavaDoc dateString,
204                                                 int pageNum,
205                                                 boolean absolute) {
206         
207         if(weblog == null) {
208             return null;
209         }
210         
211         StringBuffer JavaDoc pathinfo = new StringBuffer JavaDoc();
212         Map JavaDoc params = new HashMap JavaDoc();
213         
214         pathinfo.append(getWeblogURL(weblog, locale, absolute));
215         
216         if(pageLink != null) {
217             pathinfo.append("page/").append(pageLink);
218             
219             // for custom pages we only allow query params
220
if(dateString != null) {
221                 params.put("date", dateString);
222             }
223             if(category != null) {
224                 params.put("cat", encode(category));
225             }
226             if(pageNum > 0) {
227                 params.put("page", Integer.toString(pageNum));
228             }
229         } else {
230             // if there is no page link then this is just a typical collection url
231
return getWeblogCollectionURL(weblog, locale, category, dateString, pageNum, absolute);
232         }
233         
234         return pathinfo.toString() + getQueryString(params);
235     }
236     
237     
238     /**
239      * Get url for a feed on a given weblog.
240      */

241     public static final String JavaDoc getWeblogFeedURL(WebsiteData weblog,
242                                                 String JavaDoc locale,
243                                                 String JavaDoc type,
244                                                 String JavaDoc format,
245                                                 String JavaDoc category,
246                                                 boolean excerpts,
247                                                 boolean absolute) {
248         
249         if(weblog == null) {
250             return null;
251         }
252         
253         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
254         
255         url.append(getWeblogURL(weblog, locale, absolute));
256         url.append("feed/").append(type).append("/").append(format);
257         
258         Map JavaDoc params = new HashMap JavaDoc();
259         if(category != null && category.trim().length() > 0) {
260             params.put("cat", encode(category));
261         }
262         if(excerpts) {
263             params.put("excerpts", "true");
264         }
265         
266         return url.toString() + getQueryString(params);
267     }
268     
269     
270     /**
271      * Get url to search endpoint on a given weblog.
272      */

273     public static final String JavaDoc getWeblogSearchURL(WebsiteData weblog,
274                                                   String JavaDoc locale,
275                                                   String JavaDoc query,
276                                                   String JavaDoc category,
277                                                   int pageNum,
278                                                   boolean absolute) {
279         
280         if(weblog == null) {
281             return null;
282         }
283         
284         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
285         
286         url.append(getWeblogURL(weblog, locale, absolute));
287         url.append("search");
288         
289         Map JavaDoc params = new HashMap JavaDoc();
290         if(query != null) {
291             params.put("q", encode(query));
292             
293             // other stuff only makes sense if there is a query
294
if(category != null) {
295                 params.put("cat", encode(category));
296             }
297             if(pageNum > 0) {
298                 params.put("page", Integer.toString(pageNum));
299             }
300         }
301         
302         return url.toString() + getQueryString(params);
303     }
304     
305     
306     /**
307      * Get url to a resource on a given weblog.
308      */

309     public static final String JavaDoc getWeblogResourceURL(WebsiteData weblog,
310                                                     String JavaDoc filePath,
311                                                     boolean absolute) {
312         
313         if(weblog == null) {
314             return null;
315         }
316         
317         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
318         
319         url.append(getWeblogURL(weblog, null, absolute));
320         url.append("resource/");
321         
322         if(filePath.startsWith("/")) {
323             url.append(filePath.substring(1));
324         } else {
325             url.append(filePath);
326         }
327         
328         return url.toString();
329     }
330     
331     
332     /**
333      * Get url to rsd file on a given weblog.
334      */

335     public static final String JavaDoc getWeblogRsdURL(WebsiteData weblog,
336                                                boolean absolute) {
337         
338         if(weblog == null) {
339             return null;
340         }
341         
342         return getWeblogURL(weblog, null, absolute)+"rsd";
343     }
344     
345     
346     /**
347      * Compose a map of key=value params into a query string.
348      */

349     public static final String JavaDoc getQueryString(Map JavaDoc params) {
350         
351         if(params == null) {
352             return null;
353         }
354         
355         StringBuffer JavaDoc queryString = new StringBuffer JavaDoc();
356         
357         for(Iterator JavaDoc keys = params.keySet().iterator(); keys.hasNext();) {
358             String JavaDoc key = (String JavaDoc) keys.next();
359             String JavaDoc value = (String JavaDoc) params.get(key);
360             
361             if (queryString.length() == 0) {
362                 queryString.append("?");
363             } else {
364                 queryString.append("&");
365             }
366             
367             queryString.append(key);
368             queryString.append("=");
369             queryString.append(value);
370         }
371         
372         return queryString.toString();
373     }
374     
375     
376     /**
377      * URL encode a string using UTF-8.
378      */

379     public static final String JavaDoc encode(String JavaDoc str) {
380         String JavaDoc encodedStr = str;
381         try {
382             encodedStr = URLEncoder.encode(str, "UTF-8");
383         } catch (UnsupportedEncodingException JavaDoc ex) {
384             // ignored
385
}
386         return encodedStr;
387     }
388     
389     
390     /**
391      * URL decode a string using UTF-8.
392      */

393     public static final String JavaDoc decode(String JavaDoc str) {
394         String JavaDoc decodedStr = str;
395         try {
396             decodedStr = URLDecoder.decode(str, "UTF-8");
397         } catch (UnsupportedEncodingException JavaDoc ex) {
398             // ignored
399
}
400         return decodedStr;
401     }
402     
403 }
404
Popular Tags