KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > util > URLBean


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.util;
6
7 import com.opensymphony.webwork.views.util.UrlHelper;
8
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13
14
15 /**
16  * User: plightbo
17  * Date: Nov 16, 2003
18  * Time: 4:13:11 PM
19  */

20 public class URLBean {
21     //~ Instance fields ////////////////////////////////////////////////////////
22

23     HashMap JavaDoc params;
24     HttpServletRequest JavaDoc request;
25     HttpServletResponse JavaDoc response;
26     String JavaDoc page;
27
28     //~ Methods ////////////////////////////////////////////////////////////////
29

30     public void setPage(String JavaDoc page) {
31         this.page = page;
32     }
33
34     public void setRequest(HttpServletRequest JavaDoc request) {
35         this.request = request;
36     }
37
38     public void setResponse(HttpServletResponse JavaDoc response) {
39         this.response = response;
40     }
41
42     public String JavaDoc getURL() {
43         // all this trickier with maps is to reduce the number of objects created
44
Map JavaDoc fullParams = null;
45
46         if (params != null) {
47             fullParams = new HashMap JavaDoc();
48         }
49
50         if (page == null) {
51             // No particular page requested, so go to "same page"
52
// Add query params to parameters
53
if (fullParams != null) {
54                 fullParams.putAll(request.getParameterMap());
55             } else {
56                 fullParams = request.getParameterMap();
57             }
58         }
59
60         // added parameters override, just like in URLTag
61
if (params != null) {
62             fullParams.putAll(params);
63         }
64
65         return UrlHelper.buildUrl(page, request, response, fullParams);
66     }
67
68     public URLBean addParameter(String JavaDoc name, Object JavaDoc value) {
69         if (params == null) {
70             params = new HashMap JavaDoc();
71         }
72
73         if (value == null) {
74             params.remove(name);
75         } else {
76             params.put(name, value.toString());
77         }
78
79         return this;
80     }
81
82     public String JavaDoc toString() {
83         return getURL();
84     }
85 }
86
Popular Tags