KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > util > UrlHelper


1 package com.opensymphony.webwork.views.util;
2
3 import com.opensymphony.webwork.ServletActionContext;
4 import com.opensymphony.webwork.config.Configuration;
5 import com.opensymphony.xwork.util.OgnlValueStack;
6 import com.opensymphony.xwork.util.TextParseUtil;
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.http.HttpServletResponse JavaDoc;
12 import java.io.UnsupportedEncodingException JavaDoc;
13 import java.net.URLEncoder JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17
18 /**
19  * UrlHelper
20  *
21  * @author Jason Carreira
22  * Created Apr 19, 2003 9:32:19 PM
23  */

24 public class UrlHelper {
25     private static final Log LOG = LogFactory.getLog(UrlHelper.class);
26
27     /**
28      * Default HTTP port (80).
29      */

30     private static final int DEFAULT_HTTP_PORT = 80;
31
32     /**
33      * Default HTTPS port (443).
34      */

35     private static final int DEFAULT_HTTPS_PORT = 443;
36
37     private static final String JavaDoc AMP = "&";
38
39     public static String JavaDoc buildUrl(String JavaDoc action, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Map JavaDoc params) {
40         return buildUrl(action, request, response, params, null, true, true);
41     }
42
43     public static String JavaDoc buildUrl(String JavaDoc action, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Map JavaDoc params, String JavaDoc scheme, boolean includeContext, boolean encodeResult) {
44         StringBuffer JavaDoc link = new StringBuffer JavaDoc();
45
46         boolean changedScheme = false;
47
48         int httpPort = DEFAULT_HTTP_PORT;
49
50         try {
51             httpPort = Integer.parseInt((String JavaDoc) Configuration.get("webwork.url.http.port"));
52         } catch (Exception JavaDoc ex) {
53         }
54
55         int httpsPort = DEFAULT_HTTPS_PORT;
56
57         try {
58             httpsPort = Integer.parseInt((String JavaDoc) Configuration.get("webwork.url.https.port"));
59         } catch (Exception JavaDoc ex) {
60         }
61
62         // only append scheme if it is different to the current scheme
63
if ((scheme != null) && !scheme.equals(request.getScheme())) {
64             changedScheme = true;
65             link.append(scheme);
66             link.append("://");
67             link.append(request.getServerName());
68
69             if ((scheme.equals("http") && (httpPort != DEFAULT_HTTP_PORT)) || (scheme.equals("https") && httpsPort != DEFAULT_HTTPS_PORT)) {
70                 link.append(":");
71                 link.append(scheme.equals("http") ? httpPort : httpsPort);
72             }
73         }
74
75         if (action != null) {
76             // Check if context path needs to be added
77
// Add path to absolute links
78
if (action.startsWith("/") && includeContext) {
79                 link.append(request.getContextPath());
80             } else if (changedScheme) {
81                 String JavaDoc uri = request.getRequestURI();
82                 link.append(uri.substring(0, uri.lastIndexOf('/')));
83             }
84
85             // Add page
86
link.append(action);
87         } else {
88             // Go to "same page"
89
String JavaDoc requestURI = (String JavaDoc) request.getAttribute("webwork.request_uri");
90
91             if (requestURI == null) {
92                 requestURI = request.getRequestURI();
93             }
94
95             link.append(requestURI);
96         }
97
98         //if the action was not explicitly set grab the params from the request
99
if ((params != null) && (params.size() > 0)) {
100             if (link.toString().indexOf("?") == -1) {
101                 link.append("?");
102             } else {
103                 link.append(AMP);
104             }
105
106             // Set params
107
Iterator JavaDoc iter = params.entrySet().iterator();
108
109             String JavaDoc[] valueHolder = new String JavaDoc[1];
110
111             while (iter.hasNext()) {
112                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
113                 String JavaDoc name = (String JavaDoc) entry.getKey();
114                 Object JavaDoc value = entry.getValue();
115
116                 String JavaDoc[] values;
117
118                 if (value instanceof String JavaDoc[]) {
119                     values = (String JavaDoc[]) value;
120                 } else {
121                     valueHolder[0] = value.toString();
122                     values = valueHolder;
123                 }
124
125                 for (int i = 0; i < values.length; i++) {
126                     if (values[i] != null) {
127                         link.append(name);
128                         link.append('=');
129                         link.append(translateAndEncode(values[i]));
130                     }
131
132                     if (i < (values.length - 1)) {
133                         link.append("&amp;");
134                     }
135                 }
136
137                 if (iter.hasNext()) {
138                     link.append("&amp;");
139                 }
140             }
141         }
142
143         String JavaDoc result;
144
145         try {
146             result = encodeResult ? response.encodeURL(link.toString()) : link.toString();
147         } catch (Exception JavaDoc ex) {
148             // Could not encode the URL for some reason
149
// Use it unchanged
150
result = link.toString();
151         }
152
153         return result;
154     }
155
156     /**
157      * Translates any script expressions using {@link com.opensymphony.xwork.util.TextParseUtil#translateVariables} and
158      * encodes the URL using {@link java.net.URLEncoder#encode} with the encoding of UTF-8 (as recommended by the
159      * w3c and pointed out in issue WW-747).
160      *
161      * @param input
162      * @return the translated and encoded string
163      */

164     public static String JavaDoc translateAndEncode(String JavaDoc input) {
165         OgnlValueStack valueStack = ServletActionContext.getContext().getValueStack();
166         String JavaDoc output = TextParseUtil.translateVariables(input, valueStack);
167
168         try {
169             return URLEncoder.encode(output, "UTF-8");
170         } catch (UnsupportedEncodingException JavaDoc e) {
171             LOG.warn("Could not encode URL parameter '" + input + "', returning value un-encoded");
172             return output;
173         }
174     }
175 }
176
Popular Tags