KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portal > portlet > struts > StrutsURLEncoder


1 package org.exoplatform.portal.portlet.struts;
2
3 import java.util.HashMap JavaDoc;
4 import org.exoplatform.services.portletcontainer.helper.URLEncoder;
5
6
7 public class StrutsURLEncoder implements URLEncoder {
8   private static String JavaDoc STATIC_RESOURCE = "static";
9   private static HashMap JavaDoc staticResource_;
10
11   static {
12     staticResource_ = new HashMap JavaDoc();
13     staticResource_.put("gif", STATIC_RESOURCE);
14     staticResource_.put("jpg", STATIC_RESOURCE);
15     staticResource_.put("png", STATIC_RESOURCE);
16     staticResource_.put("bmp", STATIC_RESOURCE);
17     staticResource_.put("txt", STATIC_RESOURCE);
18     staticResource_.put("html", STATIC_RESOURCE);
19     staticResource_.put("xml", STATIC_RESOURCE);
20     staticResource_.put("xsl", STATIC_RESOURCE);
21   }
22
23   private String JavaDoc contextPath_;
24   private String JavaDoc portalRequestUrl_;
25
26   public StrutsURLEncoder(String JavaDoc contextPath, String JavaDoc portalUrl) {
27     contextPath_ = contextPath;
28     portalRequestUrl_ = portalUrl;
29   }
30
31   public String JavaDoc encodeURL(String JavaDoc url) {
32     if (url.startsWith("http://")) return url;
33         
34     int ml_index = url.indexOf("/", 1);
35     String JavaDoc ml_ext = url.substring(ml_index);
36     url = contextPath_ + ml_ext;
37
38     int index = url.lastIndexOf(".");
39     String JavaDoc ext = null;
40     if (index > 0) {
41       ext = url.substring(index + 1, url.length());
42     }
43     if (isStaticResource(ext)) {
44       return url;
45     } else {
46       url = url.replace('?', '&');
47       return portalRequestUrl_ + "&requestUri=" + url;
48     }
49
50
51   }
52
53   private boolean isStaticResource(String JavaDoc ext) {
54     if (ext == null) return false;
55     Object JavaDoc o = staticResource_.get(ext);
56     return o != null;
57   }
58 }
59
Popular Tags