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