01: /*
02: * $Id: URLBean.java 495509 2007-01-12 07:50:25Z mrdon $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21: package org.apache.struts2.util;
22:
23: import java.util.HashMap;
24: import java.util.Map;
25:
26: import javax.servlet.http.HttpServletRequest;
27: import javax.servlet.http.HttpServletResponse;
28:
29: import org.apache.struts2.views.util.UrlHelper;
30:
31: /**
32: * A bean that can generate a URL.
33: *
34: */
35: public class URLBean {
36:
37: HashMap params;
38: HttpServletRequest request;
39: HttpServletResponse response;
40: String page;
41:
42: public URLBean setPage(String page) {
43: this .page = page;
44: return this ;
45: }
46:
47: public void setRequest(HttpServletRequest request) {
48: this .request = request;
49: }
50:
51: public void setResponse(HttpServletResponse response) {
52: this .response = response;
53: }
54:
55: public String getURL() {
56: // all this trickier with maps is to reduce the number of objects created
57: Map fullParams = null;
58:
59: if (params != null) {
60: fullParams = new HashMap();
61: }
62:
63: if (page == null) {
64: // No particular page requested, so go to "same page"
65: // Add query params to parameters
66: if (fullParams != null) {
67: fullParams.putAll(request.getParameterMap());
68: } else {
69: fullParams = request.getParameterMap();
70: }
71: }
72:
73: // added parameters override, just like in URLTag
74: if (params != null) {
75: fullParams.putAll(params);
76: }
77:
78: return UrlHelper.buildUrl(page, request, response, fullParams);
79: }
80:
81: public URLBean addParameter(String name, Object value) {
82: if (params == null) {
83: params = new HashMap();
84: }
85:
86: if (value == null) {
87: params.remove(name);
88: } else {
89: params.put(name, value.toString());
90: }
91:
92: return this ;
93: }
94:
95: public String toString() {
96: return getURL();
97: }
98: }
|