KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > components > URL


1 package com.opensymphony.webwork.components;
2
3 import com.opensymphony.webwork.views.util.UrlHelper;
4 import com.opensymphony.xwork.util.OgnlValueStack;
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10 import javax.servlet.http.HttpUtils JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.Writer JavaDoc;
13
14 /**
15  * User: plightbo
16  * Date: Aug 7, 2005
17  * Time: 11:29:02 AM
18  */

19 public class URL extends Component {
20     private static final Log LOG = LogFactory.getLog(URL.class);
21
22     /**
23      * The includeParams attribute may have the value 'none', 'get' or 'all'.
24      * It is used when the url tag is used without a value or page attribute.
25      * Its value is looked up on the ValueStack
26      * If no includeParams is specified then 'get' is used.
27      * none - include no parameters in the URL
28      * get - include only GET parameters in the URL (default)
29      * all - include both GET and POST parameters in the URL
30      */

31     public static final String JavaDoc NONE = "none";
32     public static final String JavaDoc GET = "get";
33     public static final String JavaDoc ALL = "all";
34
35     private HttpServletRequest JavaDoc req;
36     private HttpServletResponse JavaDoc res;
37
38     protected String JavaDoc includeParams;
39     protected String JavaDoc scheme;
40     protected String JavaDoc value;
41     protected boolean encode = true;
42     protected boolean includeContext = true;
43
44     public URL(OgnlValueStack stack, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
45         super(stack);
46         this.req = req;
47         this.res = res;
48     }
49
50     public void start(Writer JavaDoc writer) {
51         if (value != null) {
52             value = findString(value);
53         }
54
55         // no explicit url set so attach params from current url, do
56
// this at start so body params can override any of these they wish.
57
try {
58             String JavaDoc includeParams = null;
59
60             if (this.includeParams != null) {
61                 includeParams = findString(this.includeParams);
62             }
63
64             if ((includeParams == null && value == null) || GET.equalsIgnoreCase(includeParams)) {
65                 // Parse the query string to make sure that the parameters come from the query, and not some posted data
66
String JavaDoc query = req.getQueryString();
67
68                 if (query != null) {
69                     // Remove possible #foobar suffix
70
int idx = query.lastIndexOf('#');
71
72                     if (idx != -1) {
73                         query = query.substring(0, idx - 1);
74                     }
75
76                     parameters.putAll(HttpUtils.parseQueryString(query));
77                 }
78             } else if (ALL.equalsIgnoreCase(includeParams)) {
79                 parameters.putAll(req.getParameterMap());
80             } else if (value == null && !NONE.equalsIgnoreCase(includeParams)) {
81                 LOG.warn("Unknown value for includeParams parameter to URL tag: " + includeParams);
82             }
83         } catch (Exception JavaDoc e) {
84             LOG.warn("Unable to put request parameters (" + req.getQueryString() + ") into parameter map.", e);
85         }
86
87     }
88
89     public void end(Writer JavaDoc writer) {
90         String JavaDoc scheme = req.getScheme();
91
92         if (this.scheme != null) {
93             scheme = this.scheme;
94         }
95
96         String JavaDoc result = UrlHelper.buildUrl(value, req, res, parameters, scheme, includeContext, encode);
97
98         String JavaDoc id = getId();
99
100         if (id != null) {
101             getStack().getContext().put(id, result);
102
103             // add to the request and page scopes as well
104
req.setAttribute(id, result);
105         } else {
106             try {
107                 writer.write(result);
108                 writer.flush();
109             } catch (IOException JavaDoc e) {
110                 e.printStackTrace();
111                 throw new RuntimeException JavaDoc("IOError: " + e.getMessage(), e);
112             }
113         }
114
115         super.end(writer);
116     }
117
118     public void setIncludeParams(String JavaDoc includeParams) {
119         this.includeParams = includeParams;
120     }
121
122     public void setScheme(String JavaDoc scheme) {
123         this.scheme = scheme;
124     }
125
126     public void setValue(String JavaDoc value) {
127         this.value = value;
128     }
129
130     public void setEncode(boolean encode) {
131         this.encode = encode;
132     }
133
134     public void setIncludeContext(boolean includeContext) {
135         this.includeContext = includeContext;
136     }
137 }
138
Popular Tags