| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 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 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 172 | public class UriTransformer { |
| 46 | |
|
| 47 | |
|
| 48 | |
private static final String CATEGORIES = "/categories"; |
| 49 | |
|
| 50 | |
|
| 51 | |
private static final String CATEGORIES_REGEX = "\\/categories\\/"; |
| 52 | |
|
| 53 | |
|
| 54 | |
private static final String TAGS = "/tags/"; |
| 55 | |
|
| 56 | |
|
| 57 | |
private static final String TAGS_REGEX = "\\/tags\\/"; |
| 58 | |
|
| 59 | |
|
| 60 | |
private static final String AUTHORS = "/authors/"; |
| 61 | |
|
| 62 | |
|
| 63 | |
private static final String AUTHORS_REGEX = "\\/authors\\/"; |
| 64 | |
|
| 65 | |
|
| 66 | 4 | private static Log log = LogFactory.getLog(UriTransformer.class); |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
public String getUri(String uri, Blog blog) { |
| 78 | 192 | PermalinkProvider permalinkProvider = blog.getPermalinkProvider(); |
| 79 | 192 | DefaultPermalinkProvider defaultPermalinkProvider = new DefaultPermalinkProvider(); |
| 80 | 192 | defaultPermalinkProvider.setBlog(permalinkProvider.getBlog()); |
| 81 | |
|
| 82 | 192 | log.trace("URI before transformation : " + uri); |
| 83 | |
|
| 84 | 192 | if (uri == null || uri.trim().equals("")) { |
| 85 | 4 | uri = "/"; |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | 192 | String result = getUri(uri, permalinkProvider); |
| 90 | 192 | if (result == null) { |
| 91 | |
|
| 92 | 172 | result = getUri(uri, defaultPermalinkProvider); |
| 93 | |
} |
| 94 | |
|
| 95 | |
|
| 96 | 192 | if (result == null) { |
| 97 | 168 | if (uri.equals("/categories") || uri.equals("/categories/")) { |
| 98 | |
|
| 99 | 8 | result = "/viewCategories.action"; |
| 100 | 160 | } else if (uri.matches(CATEGORIES_REGEX + ".*\\/.*xml")) { |
| 101 | |
|
| 102 | 12 | int indexOfLastSlash = uri.lastIndexOf("/"); |
| 103 | 12 | String categoryId = uri.substring(CATEGORIES.length(), indexOfLastSlash); |
| 104 | |
|
| 105 | 12 | if (uri.endsWith("rdf.xml")) { |
| 106 | 4 | result = "/feed.action?category=" + categoryId + "&flavor=rdf"; |
| 107 | 8 | } else if (uri.endsWith("atom.xml")) { |
| 108 | 4 | result = "/feed.action?category=" + categoryId + "&flavor=atom"; |
| 109 | |
} else { |
| 110 | 4 | result = "/feed.action?category=" + categoryId + "&flavor=rss20"; |
| 111 | |
} |
| 112 | 12 | } else if (uri.startsWith(CATEGORIES)) { |
| 113 | |
|
| 114 | 12 | String category = uri.substring(CATEGORIES.length(), uri.length()); |
| 115 | 12 | if (category.endsWith("/")) { |
| 116 | 8 | category = category.substring(0, category.length()-1); |
| 117 | |
} |
| 118 | 12 | result = "/viewCategory.action?category=" + category; |
| 119 | 12 | } else if (uri.equals("/tags") || uri.equals("/tags/")) { |
| 120 | |
|
| 121 | 8 | result = "/viewTags.action"; |
| 122 | 128 | } else if (uri.matches(TAGS_REGEX + ".*\\/.*xml")) { |
| 123 | |
|
| 124 | 12 | int indexOfLastSlash = uri.lastIndexOf("/"); |
| 125 | 12 | String tag = uri.substring(TAGS.length(), indexOfLastSlash); |
| 126 | |
|
| 127 | 12 | if (uri.endsWith("rdf.xml")) { |
| 128 | 4 | result = "/feed.action?tag=" + tag + "&flavor=rdf"; |
| 129 | 8 | } else if (uri.endsWith("atom.xml")) { |
| 130 | 4 | result = "/feed.action?tag=" + tag + "&flavor=atom"; |
| 131 | |
} else { |
| 132 | 4 | result = "/feed.action?tag=" + tag + "&flavor=rss20"; |
| 133 | |
} |
| 134 | 12 | } else if (uri.matches(AUTHORS_REGEX + ".*\\/.*xml")) { |
| 135 | |
|
| 136 | 12 | int indexOfLastSlash = uri.lastIndexOf("/"); |
| 137 | 12 | String author = uri.substring(AUTHORS.length(), indexOfLastSlash); |
| 138 | |
|
| 139 | 12 | if (uri.endsWith("rdf.xml")) { |
| 140 | 4 | result = "/feed.action?author=" + author + "&flavor=rdf"; |
| 141 | 8 | } else if (uri.endsWith("atom.xml")) { |
| 142 | 4 | result = "/feed.action?author=" + author + "&flavor=atom"; |
| 143 | |
} else { |
| 144 | 4 | result = "/feed.action?author=" + author + "&flavor=rss20"; |
| 145 | |
} |
| 146 | 12 | } else if (uri.startsWith(TAGS)) { |
| 147 | |
|
| 148 | 8 | String tag = uri.substring(TAGS.length(), uri.length()); |
| 149 | 8 | if (tag.endsWith("/")) { |
| 150 | 4 | tag = tag.substring(0, tag.length()-1); |
| 151 | |
} |
| 152 | 8 | result = "/viewTag.action?tag=" + Tag.encode(tag); |
| 153 | 8 | } else if (uri.startsWith(AUTHORS)) { |
| 154 | |
|
| 155 | 4 | String author = uri.substring(AUTHORS.length(), uri.length()); |
| 156 | 4 | if (author.endsWith("/")) { |
| 157 | 4 | author = author.substring(0, author.length()-1); |
| 158 | |
} |
| 159 | 4 | result = "/aboutAuthor.action?user=" + author; |
| 160 | 4 | } else if (uri.equals("/pages/") || uri.equals("/pages")) { |
| 161 | 8 | result = "/viewStaticPage.action?name=index"; |
| 162 | 84 | } else if (uri.startsWith("/pages/")) { |
| 163 | |
|
| 164 | 4 | String name = uri.substring(7, uri.length()-5); |
| 165 | |
|
| 166 | 4 | result = "/viewStaticPage.action?name="; |
| 167 | 4 | result += name; |
| 168 | 4 | } else if (uri.startsWith("/images/")) { |
| 169 | |
|
| 170 | 8 | String name = uri.substring(7, uri.length()); |
| 171 | |
|
| 172 | 8 | result = "/file.action?type=" + FileMetaData.BLOG_IMAGE + "&name="; |
| 173 | 8 | result += name; |
| 174 | 8 | } else if (uri.startsWith("/files/")) { |
| 175 | |
|
| 176 | 4 | String name = uri.substring(6, uri.length()); |
| 177 | |
|
| 178 | 4 | result = "/file.action?type=" + FileMetaData.BLOG_FILE + "&name="; |
| 179 | 4 | result += name; |
| 180 | 4 | } else if (uri.startsWith("/theme/")) { |
| 181 | |
|
| 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 | 0 | } else if (uri.matches("\\/help\\/\\w*\\.html")) { |
| 187 | |
|
| 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 | 0 | } else if (uri.equals("/help") || uri.equals("/help/")) { |
| 193 | |
|
| 194 | 0 | result = "/viewHelp.secureaction?name=index"; |
| 195 | 68 | } else if (uri.equals("/responses/rss.xml")) { |
| 196 | |
|
| 197 | 4 | result = "/responseFeed.action?flavor=rss20"; |
| 198 | 64 | } else if (uri.startsWith("/responses/rss.xml?entry=")) { |
| 199 | |
|
| 200 | 4 | result = "/responseFeed.action?flavor=rss20&" + uri.substring("/responses/rss.xml?".length()); |
| 201 | 60 | } else if (uri.startsWith("/rss.xml")) { |
| 202 | |
|
| 203 | 8 | result = "/feed.action?flavor=rss20"; |
| 204 | 52 | } else if (uri.startsWith("/feed.xml")) { |
| 205 | |
|
| 206 | 0 | result = "/feed.action?flavor=rss20"; |
| 207 | 52 | } else if (uri.startsWith("/rdf.xml")) { |
| 208 | |
|
| 209 | 4 | result = "/feed.action?flavor=rdf"; |
| 210 | 48 | } else if (uri.startsWith("/responses/atom.xml")) { |
| 211 | |
|
| 212 | 4 | result = "/responseFeed.action?flavor=atom"; |
| 213 | 44 | } else if (uri.startsWith("/atom.xml")) { |
| 214 | |
|
| 215 | 4 | result = "/feed.action?flavor=atom"; |
| 216 | 40 | } else if (uri.equals("/today.html")) { |
| 217 | |
|
| 218 | 4 | result = "/viewDay.action"; |
| 219 | 36 | } else if (uri.equals("/about.html")) { |
| 220 | |
|
| 221 | 4 | result = "/about.action"; |
| 222 | 32 | } else if (uri.startsWith("/blogentries/")) { |
| 223 | |
|
| 224 | 4 | String page = uri.substring(13, uri.length()-5); |
| 225 | 4 | result = "/viewBlogEntriesByPage.action?page=" + page; |
| 226 | 4 | } else if (uri.equals("/") || uri.equals("/index.jsp") || uri.equals("/index.html")) { |
| 227 | 16 | result = "/viewHomePage.action"; |
| 228 | |
} else { |
| 229 | 12 | result = uri; |
| 230 | |
} |
| 231 | |
} |
| 232 | |
|
| 233 | 192 | log.trace("URI after transformation : " + result); |
| 234 | |
|
| 235 | 192 | return result; |
| 236 | |
} |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
public String getUri(String uri, MultiBlog blog) { |
| 248 | |
String result; |
| 249 | |
|
| 250 | 16 | log.trace("URI before transformation : " + uri); |
| 251 | |
|
| 252 | 16 | if (uri == null || uri.trim().equals("")) { |
| 253 | 0 | uri = "/"; |
| 254 | |
} |
| 255 | |
|
| 256 | 16 | if (uri.startsWith("/rss.xml")) { |
| 257 | |
|
| 258 | 8 | result = "/feed.action?flavor=rss20"; |
| 259 | 8 | } else if (uri.startsWith("/feed.xml")) { |
| 260 | |
|
| 261 | 0 | result = "/feed.action?flavor=rss20"; |
| 262 | 8 | } else if (uri.startsWith("/rdf.xml")) { |
| 263 | |
|
| 264 | 0 | result = "/feed.action?flavor=rdf"; |
| 265 | 8 | } else if (uri.startsWith("/atom.xml")) { |
| 266 | |
|
| 267 | 0 | result = "/feed.action?flavor=atom"; |
| 268 | 8 | } else if (uri.equals("/") || uri.equals("/index.jsp") || uri.equals("/index.html")) { |
| 269 | 4 | result = "/viewHomePage.action"; |
| 270 | 4 | } else if (uri.matches("\\/help\\/\\w*\\.html")) { |
| 271 | |
|
| 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 | 0 | } else { |
| 277 | 4 | result = uri; |
| 278 | |
} |
| 279 | |
|
| 280 | 16 | log.trace("URI after transformation : " + result); |
| 281 | |
|
| 282 | 16 | return result; |
| 283 | |
} |
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
private String getUri(String uri, PermalinkProvider permalinkProvider) { |
| 297 | 364 | String result = null; |
| 298 | |
|
| 299 | 364 | if (permalinkProvider.isBlogEntryPermalink(uri)) { |
| 300 | 20 | BlogEntry blogEntry = permalinkProvider.getBlogEntry(uri); |
| 301 | 20 | if (blogEntry != null) { |
| 302 | 16 | result = "/viewBlogEntry.action?entry=" + blogEntry.getId(); |
| 303 | |
} |
| 304 | 20 | } else if (permalinkProvider.isDayPermalink(uri)) { |
| 305 | 4 | Day day = permalinkProvider.getDay(uri); |
| 306 | 4 | if (day != null) { |
| 307 | 4 | result = "/viewDay.action"; |
| 308 | 4 | result += "?year=" + day.getMonth().getYear().getYear(); |
| 309 | 4 | result += "&month=" + day.getMonth().getMonth(); |
| 310 | 4 | result += "&day=" + day.getDay(); |
| 311 | |
} |
| 312 | 4 | } else if (permalinkProvider.isMonthPermalink(uri)) { |
| 313 | 4 | Month month = permalinkProvider.getMonth(uri); |
| 314 | 4 | if (month != null) { |
| 315 | 4 | result = "/viewMonth.action"; |
| 316 | 4 | result += "?year=" + month.getYear().getYear(); |
| 317 | 4 | result += "&month=" + month.getMonth(); |
| 318 | |
} |
| 319 | |
} |
| 320 | |
|
| 321 | 364 | return result; |
| 322 | |
} |
| 323 | |
|
| 324 | |
} |