View Javadoc

1   /*
2    * $Id: URLBean.java 495509 2007-01-12 07:50:25Z mrdon $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "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  /***
33   * A bean that can generate a URL.
34   *
35   */
36  public class URLBean {
37  
38      HashMap params;
39      HttpServletRequest request;
40      HttpServletResponse response;
41      String page;
42  
43  
44      public URLBean setPage(String page) {
45          this.page = page;
46          return this;
47      }
48  
49      public void setRequest(HttpServletRequest request) {
50          this.request = request;
51      }
52  
53      public void setResponse(HttpServletResponse response) {
54          this.response = response;
55      }
56  
57      public String getURL() {
58          // all this trickier with maps is to reduce the number of objects created
59          Map fullParams = null;
60  
61          if (params != null) {
62              fullParams = new HashMap();
63          }
64  
65          if (page == null) {
66              // No particular page requested, so go to "same page"
67              // Add query params to parameters
68              if (fullParams != null) {
69                  fullParams.putAll(request.getParameterMap());
70              } else {
71                  fullParams = request.getParameterMap();
72              }
73          }
74  
75          // added parameters override, just like in URLTag
76          if (params != null) {
77              fullParams.putAll(params);
78          }
79  
80          return UrlHelper.buildUrl(page, request, response, fullParams);
81      }
82  
83      public URLBean addParameter(String name, Object value) {
84          if (params == null) {
85              params = new HashMap();
86          }
87  
88          if (value == null) {
89              params.remove(name);
90          } else {
91              params.put(name, value.toString());
92          }
93  
94          return this;
95      }
96  
97      public String toString() {
98          return getURL();
99      }
100 }