1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
26  
27  import java.util.Enumeration;
28  
29  import javax.portlet.PortletException;
30  import javax.portlet.PortletMode;
31  import javax.portlet.PortletURL;
32  import javax.portlet.RenderRequest;
33  import javax.portlet.RenderResponse;
34  import javax.portlet.WindowState;
35  
36  /**
37   * <a href="PortletURLUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class PortletURLUtil {
43  
44      public static PortletURL getCurrent(RenderRequest req, RenderResponse res) {
45          PortletURL portletURL = res.createRenderURL();
46  
47          Enumeration enu = req.getParameterNames();
48  
49          while (enu.hasMoreElements()) {
50              String param = (String)enu.nextElement();
51              String[] values = req.getParameterValues(param);
52  
53              boolean addParam = true;
54  
55              // Don't set paramter values that are over 32 kb. See LEP-1755.
56  
57              for (int i = 0; i < values.length; i++) {
58                  if (values[i].length() > _CURRENT_URL_PARAMETER_THRESHOLD) {
59                      addParam = false;
60  
61                      break;
62                  }
63              }
64  
65              if (addParam) {
66                  portletURL.setParameter(param, values);
67              }
68          }
69  
70          return portletURL;
71      }
72  
73      public static PortletURL clone(PortletURL portletURL, RenderResponse res)
74          throws PortletException {
75  
76          LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
77  
78          return clone(liferayPortletURL, liferayPortletURL.isAction(), res);
79      }
80  
81      public static PortletURL clone(
82              PortletURL portletURL, boolean action, RenderResponse res)
83          throws PortletException {
84  
85          LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
86  
87          return clone(liferayPortletURL, action, res);
88      }
89  
90      public static PortletURL clone(
91              LiferayPortletURL liferayPortletURL, boolean action,
92              RenderResponse res)
93          throws PortletException {
94  
95          LiferayPortletURL newURLImpl = null;
96  
97          if (action) {
98              newURLImpl = (LiferayPortletURL)res.createActionURL();
99          }
100         else {
101             newURLImpl = (LiferayPortletURL)res.createRenderURL();
102         }
103 
104         newURLImpl.setPortletId(liferayPortletURL.getPortletId());
105 
106         WindowState windowState = liferayPortletURL.getWindowState();
107 
108         if (windowState != null) {
109             newURLImpl.setWindowState(windowState);
110         }
111 
112         PortletMode portletMode = liferayPortletURL.getPortletMode();
113 
114         if (portletMode != null) {
115             newURLImpl.setPortletMode(portletMode);
116         }
117 
118         newURLImpl.setParameters(liferayPortletURL.getParameterMap());
119 
120         return newURLImpl;
121     }
122 
123     private static final int _CURRENT_URL_PARAMETER_THRESHOLD = 32768;
124 
125 }