1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
23  
24  import java.util.Enumeration;
25  
26  import javax.portlet.MimeResponse;
27  import javax.portlet.PortletException;
28  import javax.portlet.PortletMode;
29  import javax.portlet.PortletRequest;
30  import javax.portlet.PortletURL;
31  import javax.portlet.WindowState;
32  
33  /**
34   * <a href="PortletURLUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class PortletURLUtil {
40  
41      public static PortletURL getCurrent(
42          PortletRequest portletRequest, MimeResponse mimeResponse) {
43  
44          PortletURL portletURL = mimeResponse.createRenderURL();
45  
46          Enumeration<String> enu = portletRequest.getParameterNames();
47  
48          while (enu.hasMoreElements()) {
49              String param = enu.nextElement();
50              String[] values = portletRequest.getParameterValues(param);
51  
52              boolean addParam = true;
53  
54              // Don't set paramter values that are over 32 kb. See LEP-1755.
55  
56              for (int i = 0; i < values.length; i++) {
57                  if (values[i].length() > _CURRENT_URL_PARAMETER_THRESHOLD) {
58                      addParam = false;
59  
60                      break;
61                  }
62              }
63  
64              if (addParam) {
65                  portletURL.setParameter(param, values);
66              }
67          }
68  
69          return portletURL;
70      }
71  
72      public static PortletURL clone(
73              PortletURL portletURL, MimeResponse mimeResponse)
74          throws PortletException {
75  
76          LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
77  
78          return clone(
79              liferayPortletURL, liferayPortletURL.getLifecycle(), mimeResponse);
80      }
81  
82      public static PortletURL clone(
83              PortletURL portletURL, String lifecycle, MimeResponse mimeResponse)
84          throws PortletException {
85  
86          LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
87  
88          return clone(liferayPortletURL, lifecycle, mimeResponse);
89      }
90  
91      public static PortletURL clone(
92              LiferayPortletURL liferayPortletURL, String lifecycle,
93              MimeResponse mimeResponse)
94          throws PortletException {
95  
96          LiferayPortletURL newURLImpl = null;
97  
98          if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
99              newURLImpl = (LiferayPortletURL)mimeResponse.createActionURL();
100         }
101         else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
102             newURLImpl = (LiferayPortletURL)mimeResponse.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 }