1   /**
2    * Copyright (c) 2000-2008 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.PortletRequest;
32  import javax.portlet.PortletURL;
33  import javax.portlet.RenderRequest;
34  import javax.portlet.RenderResponse;
35  import javax.portlet.WindowState;
36  
37  /**
38   * <a href="PortletURLUtil.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class PortletURLUtil {
44  
45      public static PortletURL getCurrent(RenderRequest req, RenderResponse res) {
46          PortletURL portletURL = res.createRenderURL();
47  
48          Enumeration<String> enu = req.getParameterNames();
49  
50          while (enu.hasMoreElements()) {
51              String param = enu.nextElement();
52              String[] values = req.getParameterValues(param);
53  
54              boolean addParam = true;
55  
56              // Don't set paramter values that are over 32 kb. See LEP-1755.
57  
58              for (int i = 0; i < values.length; i++) {
59                  if (values[i].length() > _CURRENT_URL_PARAMETER_THRESHOLD) {
60                      addParam = false;
61  
62                      break;
63                  }
64              }
65  
66              if (addParam) {
67                  portletURL.setParameter(param, values);
68              }
69          }
70  
71          return portletURL;
72      }
73  
74      public static PortletURL clone(PortletURL portletURL, RenderResponse res)
75          throws PortletException {
76  
77          LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
78  
79          return clone(liferayPortletURL, liferayPortletURL.getLifecycle(), res);
80      }
81  
82      public static PortletURL clone(
83              PortletURL portletURL, String lifecycle, RenderResponse res)
84          throws PortletException {
85  
86          LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
87  
88          return clone(liferayPortletURL, lifecycle, res);
89      }
90  
91      public static PortletURL clone(
92              LiferayPortletURL liferayPortletURL, String lifecycle,
93              RenderResponse res)
94          throws PortletException {
95  
96          LiferayPortletURL newURLImpl = null;
97  
98          if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
99              newURLImpl = (LiferayPortletURL)res.createActionURL();
100         }
101         else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
102             newURLImpl = (LiferayPortletURL)res.createRenderURL();
103         }
104 
105         newURLImpl.setPortletId(liferayPortletURL.getPortletId());
106 
107         WindowState windowState = liferayPortletURL.getWindowState();
108 
109         if (windowState != null) {
110             newURLImpl.setWindowState(windowState);
111         }
112 
113         PortletMode portletMode = liferayPortletURL.getPortletMode();
114 
115         if (portletMode != null) {
116             newURLImpl.setPortletMode(portletMode);
117         }
118 
119         newURLImpl.setParameters(liferayPortletURL.getParameterMap());
120 
121         return newURLImpl;
122     }
123 
124     private static final int _CURRENT_URL_PARAMETER_THRESHOLD = 32768;
125 
126 }