01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.webwork.util;
06:
07: import com.opensymphony.webwork.views.util.UrlHelper;
08:
09: import javax.servlet.http.HttpServletRequest;
10: import javax.servlet.http.HttpServletResponse;
11: import java.util.HashMap;
12: import java.util.Map;
13:
14: /**
15: * A bean that can generate a URL.
16: *
17: * @author plightbo
18: * @version $Date: 2007-01-10 16:21:00 +0100 (Wed, 10 Jan 2007) $ $Id: URLBean.java 2802 2007-01-10 15:21:00Z tmjee $
19: */
20: public class URLBean {
21:
22: HashMap params;
23: HttpServletRequest request;
24: HttpServletResponse response;
25: String page;
26:
27: /**
28: * Set the <code>page</code> and return itself, such that one could :-
29: * <pre>
30: * (new URLBean()).setPage(...).addParameter(...).addParameter(...) ...
31: * </pre>
32: *
33: * @param page
34: * @return
35: */
36: public URLBean setPage(String page) {
37: this .page = page;
38: return this ;
39: }
40:
41: public void setRequest(HttpServletRequest request) {
42: this .request = request;
43: }
44:
45: public void setResponse(HttpServletResponse response) {
46: this .response = response;
47: }
48:
49: public String getURL() {
50: // all this trickier with maps is to reduce the number of objects created
51: Map fullParams = null;
52:
53: if (params != null) {
54: fullParams = new HashMap();
55: }
56:
57: if (page == null) {
58: // No particular page requested, so go to "same page"
59: // Add query params to parameters
60: if (fullParams != null) {
61: fullParams.putAll(request.getParameterMap());
62: } else {
63: fullParams = request.getParameterMap();
64: }
65: }
66:
67: // added parameters override, just like in URLTag
68: if (params != null) {
69: fullParams.putAll(params);
70: }
71:
72: return UrlHelper.buildUrl(page, request, response, fullParams);
73: }
74:
75: public URLBean addParameter(String name, Object value) {
76: if (params == null) {
77: params = new HashMap();
78: }
79:
80: if (value == null) {
81: params.remove(name);
82: } else {
83: params.put(name, value.toString());
84: }
85:
86: return this ;
87: }
88:
89: public String toString() {
90: return getURL();
91: }
92: }
|