View Javadoc

1   /*
2    * $Id: URLBean.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.util;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.struts2.views.util.UrlHelper;
31  
32  
33  /***
34   * A bean that can generate a URL.
35   *
36   */
37  public class URLBean {
38  
39      HashMap params;
40      HttpServletRequest request;
41      HttpServletResponse response;
42      String page;
43  
44  
45      public URLBean setPage(String page) {
46          this.page = page;
47          return this;
48      }
49  
50      public void setRequest(HttpServletRequest request) {
51          this.request = request;
52      }
53  
54      public void setResponse(HttpServletResponse response) {
55          this.response = response;
56      }
57  
58      public String getURL() {
59          // all this trickier with maps is to reduce the number of objects created
60          Map fullParams = null;
61  
62          if (params != null) {
63              fullParams = new HashMap();
64          }
65  
66          if (page == null) {
67              // No particular page requested, so go to "same page"
68              // Add query params to parameters
69              if (fullParams != null) {
70                  fullParams.putAll(request.getParameterMap());
71              } else {
72                  fullParams = request.getParameterMap();
73              }
74          }
75  
76          // added parameters override, just like in URLTag
77          if (params != null) {
78              fullParams.putAll(params);
79          }
80  
81          return UrlHelper.buildUrl(page, request, response, fullParams);
82      }
83  
84      public URLBean addParameter(String name, Object value) {
85          if (params == null) {
86              params = new HashMap();
87          }
88  
89          if (value == null) {
90              params.remove(name);
91          } else {
92              params.put(name, value.toString());
93          }
94  
95          return this;
96      }
97  
98      public String toString() {
99          return getURL();
100     }
101 }