Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../../img/srcFileCovDistChart9.png 23% of files have more coverage
143   324   64   47,67
102   184   0,45   3
3     21,33  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  UriTransformer       Line # 45 143 0% 64 27 89,1% 0.891129
 
  (76)
 
1    /*
2    * Copyright (c) 2003-2006, Simon Brown
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * - Redistributions of source code must retain the above copyright
9    * notice, this list of conditions and the following disclaimer.
10    *
11    * - Redistributions in binary form must reproduce the above copyright
12    * notice, this list of conditions and the following disclaimer in
13    * the documentation and/or other materials provided with the
14    * distribution.
15    *
16    * - Neither the name of Pebble nor the names of its contributors may
17    * be used to endorse or promote products derived from this software
18    * without specific prior written permission.
19    *
20    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30    * POSSIBILITY OF SUCH DAMAGE.
31    */
32    package net.sourceforge.pebble.web.filter;
33   
34    import net.sourceforge.pebble.domain.*;
35    import net.sourceforge.pebble.permalink.DefaultPermalinkProvider;
36    import net.sourceforge.pebble.api.permalink.PermalinkProvider;
37    import org.apache.commons.logging.Log;
38    import org.apache.commons.logging.LogFactory;
39   
40    /**
41    * Responsible for converting an incoming URI to a real URI used by Pebble.
42    *
43    * @author Simon Brown
44    */
 
45    public class UriTransformer {
46   
47    /** literal used at the start of category URIs */
48    private static final String CATEGORIES = "/categories";
49   
50    /** literal used at the start of category URIs, in regex form */
51    private static final String CATEGORIES_REGEX = "\\/categories\\/";
52   
53    /** literal used at the start of tag URIs */
54    private static final String TAGS = "/tags/";
55   
56    /** literal used at the start of tag URIs, in regex form */
57    private static final String TAGS_REGEX = "\\/tags\\/";
58   
59    /** literal used at the start of author URIs */
60    private static final String AUTHORS = "/authors/";
61   
62    /** literal used at the start of tag URIs, in regex form */
63    private static final String AUTHORS_REGEX = "\\/authors\\/";
64   
65    /** the log used by this class */
66    private static Log log = LogFactory.getLog(UriTransformer.class);
67   
68    /**
69    * Checks for URI patterns and converts them to the appropriate action.
70    *
71    * @param uri the initial URI
72    * @param blog the current Blog instance
73    *
74    * @return the URI to used to service the original request (could be
75    * the same)
76    */
 
77  96 toggle public String getUri(String uri, Blog blog) {
78  96 PermalinkProvider permalinkProvider = blog.getPermalinkProvider();
79  96 DefaultPermalinkProvider defaultPermalinkProvider = new DefaultPermalinkProvider();
80  96 defaultPermalinkProvider.setBlog(permalinkProvider.getBlog());
81   
82  96 log.trace("URI before transformation : " + uri);
83   
84  96 if (uri == null || uri.trim().equals("")) {
85  2 uri = "/";
86    }
87   
88    // try to transform the URI with the permalink provider in use
89  96 String result = getUri(uri, permalinkProvider);
90  96 if (result == null) {
91    // for backwards compatibility, try the default permalink provider
92  86 result = getUri(uri, defaultPermalinkProvider);
93    }
94   
95    // if the result is still null, try the other URL patterns to transform the URI
96  96 if (result == null) {
97  84 if (uri.equals("/categories") || uri.equals("/categories/")) {
98    // URI of the form /categories/
99  4 result = "/viewCategories.action";
100  80 } else if (uri.matches(CATEGORIES_REGEX + ".*\\/.*xml")) {
101    // URI of the form /category[/subcategories]/[rss|rdf|atom].xml
102  6 int indexOfLastSlash = uri.lastIndexOf("/");
103  6 String categoryId = uri.substring(CATEGORIES.length(), indexOfLastSlash);
104   
105  6 if (uri.endsWith("rdf.xml")) {
106  2 result = "/feed.action?category=" + categoryId + "&flavor=rdf";
107  4 } else if (uri.endsWith("atom.xml")) {
108  2 result = "/feed.action?category=" + categoryId + "&flavor=atom";
109    } else {
110  2 result = "/feed.action?category=" + categoryId + "&flavor=rss20";
111    }
112  74 } else if (uri.startsWith(CATEGORIES)) {
113    // URI of the form /categories/category/
114  6 String category = uri.substring(CATEGORIES.length(), uri.length());
115  6 if (category.endsWith("/")) {
116  4 category = category.substring(0, category.length()-1);
117    }
118  6 result = "/viewCategory.action?category=" + category;
119  68 } else if (uri.equals("/tags") || uri.equals("/tags/")) {
120    // URI of the form /tags/
121  4 result = "/viewTags.action";
122  64 } else if (uri.matches(TAGS_REGEX + ".*\\/.*xml")) {
123    // URI of the form /tags/tag/[rss|rdf|atom].xml
124  6 int indexOfLastSlash = uri.lastIndexOf("/");
125  6 String tag = uri.substring(TAGS.length(), indexOfLastSlash);
126   
127  6 if (uri.endsWith("rdf.xml")) {
128  2 result = "/feed.action?tag=" + tag + "&flavor=rdf";
129  4 } else if (uri.endsWith("atom.xml")) {
130  2 result = "/feed.action?tag=" + tag + "&flavor=atom";
131    } else {
132  2 result = "/feed.action?tag=" + tag + "&flavor=rss20";
133    }
134  58 } else if (uri.matches(AUTHORS_REGEX + ".*\\/.*xml")) {
135    // URI of the form /authors/username/[rss|rdf|atom].xml
136  6 int indexOfLastSlash = uri.lastIndexOf("/");
137  6 String author = uri.substring(AUTHORS.length(), indexOfLastSlash);
138   
139  6 if (uri.endsWith("rdf.xml")) {
140  2 result = "/feed.action?author=" + author + "&flavor=rdf";
141  4 } else if (uri.endsWith("atom.xml")) {
142  2 result = "/feed.action?author=" + author + "&flavor=atom";
143    } else {
144  2 result = "/feed.action?author=" + author + "&flavor=rss20";
145    }
146  52 } else if (uri.startsWith(TAGS)) {
147    // URI of the form /tags/tag/
148  4 String tag = uri.substring(TAGS.length(), uri.length());
149  4 if (tag.endsWith("/")) {
150  2 tag = tag.substring(0, tag.length()-1);
151    }
152  4 result = "/viewTag.action?tag=" + Tag.encode(tag);
153  48 } else if (uri.startsWith(AUTHORS)) {
154    // URI of the form /authors/usename/
155  2 String author = uri.substring(AUTHORS.length(), uri.length());
156  2 if (author.endsWith("/")) {
157  2 author = author.substring(0, author.length()-1);
158    }
159  2 result = "/aboutAuthor.action?user=" + author;
160  46 } else if (uri.equals("/pages/") || uri.equals("/pages")) {
161  4 result = "/viewStaticPage.action?name=index";
162  42 } else if (uri.startsWith("/pages/")) {
163    // url matches /pages/xyz.html
164  2 String name = uri.substring(7, uri.length()-5);
165   
166  2 result = "/viewStaticPage.action?name=";
167  2 result += name;
168  40 } else if (uri.startsWith("/images/")) {
169    // url matches /images/xyz.xyz
170  4 String name = uri.substring(7, uri.length());
171   
172  4 result = "/file.action?type=" + FileMetaData.BLOG_IMAGE + "&name=";
173  4 result += name;
174  36 } else if (uri.startsWith("/files/")) {
175    // url matches /files/xyz.xyz
176  2 String name = uri.substring(6, uri.length());
177   
178  2 result = "/file.action?type=" + FileMetaData.BLOG_FILE + "&name=";
179  2 result += name;
180  34 } else if (uri.startsWith("/theme/")) {
181    // url matches /files/xyz.xyz
182  0 String name = uri.substring(6, uri.length());
183   
184  0 result = "/file.action?type=" + FileMetaData.THEME_FILE + "&name=";
185  0 result += name;
186  34 } else if (uri.matches("\\/help\\/\\w*\\.html")) {
187    // url matches /help/xyz.html
188  0 String name = uri.substring(6, uri.length());
189   
190  0 result = "/viewHelp.secureaction?name=";
191  0 result += name.substring(0, name.length()-5);
192  34 } else if (uri.equals("/help") || uri.equals("/help/")) {
193    // url matches /help/
194  0 result = "/viewHelp.secureaction?name=index";
195  34 } else if (uri.equals("/responses/rss.xml")) {
196    // url is for a response feed
197  2 result = "/responseFeed.action?flavor=rss20";
198  32 } else if (uri.startsWith("/responses/rss.xml?entry=")) {
199    // url is for a response feed
200  2 result = "/responseFeed.action?flavor=rss20&" + uri.substring("/responses/rss.xml?".length());
201  30 } else if (uri.startsWith("/rss.xml")) {
202    // url matches rss.xml
203  4 result = "/feed.action?flavor=rss20";
204  26 } else if (uri.startsWith("/feed.xml")) {
205    // url matches feed.xml
206  0 result = "/feed.action?flavor=rss20";
207  26 } else if (uri.startsWith("/rdf.xml")) {
208    // url matches rdf.xml
209  2 result = "/feed.action?flavor=rdf";
210  24 } else if (uri.startsWith("/responses/atom.xml")) {
211    // url is for a response feed
212  2 result = "/responseFeed.action?flavor=atom";
213  22 } else if (uri.startsWith("/atom.xml")) {
214    // url matches atom.xml
215  2 result = "/feed.action?flavor=atom";
216  20 } else if (uri.equals("/today.html")) {
217    // URI of the form /today.html
218  2 result = "/viewDay.action";
219  18 } else if (uri.equals("/about.html")) {
220    // URI of the form /about.html
221  2 result = "/about.action";
222  16 } else if (uri.startsWith("/blogentries/")) {
223    // view blog entries by page /blogentries/1.html
224  2 String page = uri.substring(13, uri.length()-5);
225  2 result = "/viewBlogEntriesByPage.action?page=" + page;
226  14 } else if (uri.equals("/") || uri.equals("/index.jsp") || uri.equals("/index.html")) {
227  8 result = "/viewHomePage.action";
228    } else {
229  6 result = uri;
230    }
231    }
232   
233  96 log.trace("URI after transformation : " + result);
234   
235  96 return result;
236    }
237   
238    /**
239    * Checks for URI patterns and converts them to the appropriate action.
240    *
241    * @param uri the initial URI
242    * @param blog the current Blog instance
243    *
244    * @return the URI to used to service the original request (could be
245    * the same)
246    */
 
247  8 toggle public String getUri(String uri, MultiBlog blog) {
248  8 String result;
249   
250  8 log.trace("URI before transformation : " + uri);
251   
252  8 if (uri == null || uri.trim().equals("")) {
253  0 uri = "/";
254    }
255   
256  8 if (uri.startsWith("/rss.xml")) {
257    // url matches rss.xml
258  4 result = "/feed.action?flavor=rss20";
259  4 } else if (uri.startsWith("/feed.xml")) {
260    // url matches feed.xml
261  0 result = "/feed.action?flavor=rss20";
262  4 } else if (uri.startsWith("/rdf.xml")) {
263    // url matches rdf.xml
264  0 result = "/feed.action?flavor=rdf";
265  4 } else if (uri.startsWith("/atom.xml")) {
266    // url matches atom.xml
267  0 result = "/feed.action?flavor=atom";
268  4 } else if (uri.equals("/") || uri.equals("/index.jsp") || uri.equals("/index.html")) {
269  2 result = "/viewHomePage.action";
270  2 } else if (uri.matches("\\/help\\/\\w*\\.html")) {
271    // url matches /help/xyz.html
272  0 String name = uri.substring(6, uri.length());
273   
274  0 result = "/viewHelp.secureaction?name=";
275  0 result += name.substring(0, name.length()-5);
276    } else {
277  2 result = uri;
278    }
279   
280  8 log.trace("URI after transformation : " + result);
281   
282  8 return result;
283    }
284   
285    /**
286    * Checks for URI patterns and converts them to the appropriate action, using
287    * the specified permalink provider.
288    *
289    * @param uri the initial URI
290    * @param permalinkProvider a PermalinkProvider instance to try and
291    * transform the URI with
292    *
293    * @return the URI to used to service the original request (could be
294    * the same)
295    */
 
296  182 toggle private String getUri(String uri, PermalinkProvider permalinkProvider) {
297  182 String result = null;
298   
299  182 if (permalinkProvider.isBlogEntryPermalink(uri)) {
300  10 BlogEntry blogEntry = permalinkProvider.getBlogEntry(uri);
301  10 if (blogEntry != null) {
302  8 result = "/viewBlogEntry.action?entry=" + blogEntry.getId();
303    }
304  172 } else if (permalinkProvider.isDayPermalink(uri)) {
305  2 Day day = permalinkProvider.getDay(uri);
306  2 if (day != null) {
307  2 result = "/viewDay.action";
308  2 result += "?year=" + day.getMonth().getYear().getYear();
309  2 result += "&month=" + day.getMonth().getMonth();
310  2 result += "&day=" + day.getDay();
311    }
312  170 } else if (permalinkProvider.isMonthPermalink(uri)) {
313  2 Month month = permalinkProvider.getMonth(uri);
314  2 if (month != null) {
315  2 result = "/viewMonth.action";
316  2 result += "?year=" + month.getYear().getYear();
317  2 result += "&month=" + month.getMonth();
318    }
319    }
320   
321  182 return result;
322    }
323   
324    }