1 18 19 package org.apache.roller.util; 20 21 import java.io.UnsupportedEncodingException ; 22 import java.net.URLDecoder ; 23 import java.net.URLEncoder ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import org.apache.roller.config.RollerRuntimeConfig; 28 import org.apache.roller.pojos.WebsiteData; 29 30 31 36 public final class URLUtilities { 37 38 private URLUtilities() {} 40 41 42 public static final String getXmlrpcURL(boolean absolute) { 43 44 StringBuffer url = new StringBuffer (); 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 getAtomProtocolURL(boolean absolute) { 58 59 StringBuffer url = new StringBuffer (); 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 76 public static final String getWeblogURL(WebsiteData weblog, 77 String locale, 78 boolean absolute) { 79 80 if(weblog == null) { 81 return null; 82 } 83 84 StringBuffer url = new StringBuffer (); 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 105 public static final String getWeblogEntryURL(WebsiteData weblog, 106 String locale, 107 String entryAnchor, 108 boolean absolute) { 109 110 if(weblog == null || entryAnchor == null) { 111 return null; 112 } 113 114 StringBuffer url = new StringBuffer (); 115 116 url.append(getWeblogURL(weblog, locale, absolute)); 117 url.append("entry/").append(encode(entryAnchor)); 118 119 return url.toString(); 120 } 121 122 123 126 public static final String getWeblogCommentsURL(WebsiteData weblog, 127 String locale, 128 String entryAnchor, 129 boolean absolute) { 130 131 return getWeblogEntryURL(weblog, locale, entryAnchor, absolute)+"#comments"; 132 } 133 134 135 138 public static final String getWeblogCommentURL(WebsiteData weblog, 139 String locale, 140 String entryAnchor, 141 String timeStamp, 142 boolean absolute) { 143 144 return getWeblogEntryURL(weblog, locale, entryAnchor, absolute)+"#comment-"+timeStamp; 145 } 146 147 148 151 public static final String getWeblogCollectionURL(WebsiteData weblog, 152 String locale, 153 String category, 154 String dateString, 155 int pageNum, 156 boolean absolute) { 157 158 if(weblog == null) { 159 return null; 160 } 161 162 StringBuffer pathinfo = new StringBuffer (); 163 Map params = new HashMap (); 164 165 pathinfo.append(getWeblogURL(weblog, locale, absolute)); 166 167 String 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 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 198 public static final String getWeblogPageURL(WebsiteData weblog, 199 String locale, 200 String pageLink, 201 String entryAnchor, 202 String category, 203 String dateString, 204 int pageNum, 205 boolean absolute) { 206 207 if(weblog == null) { 208 return null; 209 } 210 211 StringBuffer pathinfo = new StringBuffer (); 212 Map params = new HashMap (); 213 214 pathinfo.append(getWeblogURL(weblog, locale, absolute)); 215 216 if(pageLink != null) { 217 pathinfo.append("page/").append(pageLink); 218 219 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 return getWeblogCollectionURL(weblog, locale, category, dateString, pageNum, absolute); 232 } 233 234 return pathinfo.toString() + getQueryString(params); 235 } 236 237 238 241 public static final String getWeblogFeedURL(WebsiteData weblog, 242 String locale, 243 String type, 244 String format, 245 String category, 246 boolean excerpts, 247 boolean absolute) { 248 249 if(weblog == null) { 250 return null; 251 } 252 253 StringBuffer url = new StringBuffer (); 254 255 url.append(getWeblogURL(weblog, locale, absolute)); 256 url.append("feed/").append(type).append("/").append(format); 257 258 Map params = new HashMap (); 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 273 public static final String getWeblogSearchURL(WebsiteData weblog, 274 String locale, 275 String query, 276 String category, 277 int pageNum, 278 boolean absolute) { 279 280 if(weblog == null) { 281 return null; 282 } 283 284 StringBuffer url = new StringBuffer (); 285 286 url.append(getWeblogURL(weblog, locale, absolute)); 287 url.append("search"); 288 289 Map params = new HashMap (); 290 if(query != null) { 291 params.put("q", encode(query)); 292 293 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 309 public static final String getWeblogResourceURL(WebsiteData weblog, 310 String filePath, 311 boolean absolute) { 312 313 if(weblog == null) { 314 return null; 315 } 316 317 StringBuffer url = new StringBuffer (); 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 335 public static final String 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 349 public static final String getQueryString(Map params) { 350 351 if(params == null) { 352 return null; 353 } 354 355 StringBuffer queryString = new StringBuffer (); 356 357 for(Iterator keys = params.keySet().iterator(); keys.hasNext();) { 358 String key = (String ) keys.next(); 359 String value = (String ) 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 379 public static final String encode(String str) { 380 String encodedStr = str; 381 try { 382 encodedStr = URLEncoder.encode(str, "UTF-8"); 383 } catch (UnsupportedEncodingException ex) { 384 } 386 return encodedStr; 387 } 388 389 390 393 public static final String decode(String str) { 394 String decodedStr = str; 395 try { 396 decodedStr = URLDecoder.decode(str, "UTF-8"); 397 } catch (UnsupportedEncodingException ex) { 398 } 400 return decodedStr; 401 } 402 403 } 404 | Popular Tags |