1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PortletURLUtil {
42  
43      public static PortletURL getCurrent(
44          PortletRequest portletRequest, MimeResponse mimeResponse) {
45  
46          PortletURL portletURL = mimeResponse.createRenderURL();
47  
48          Enumeration<String> enu = portletRequest.getParameterNames();
49  
50          while (enu.hasMoreElements()) {
51              String param = enu.nextElement();
52              String[] values = portletRequest.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(
75              PortletURL portletURL, MimeResponse mimeResponse)
76          throws PortletException {
77  
78          LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
79  
80          return clone(
81              liferayPortletURL, liferayPortletURL.getLifecycle(), mimeResponse);
82      }
83  
84      public static PortletURL clone(
85              PortletURL portletURL, String lifecycle, MimeResponse mimeResponse)
86          throws PortletException {
87  
88          LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
89  
90          return clone(liferayPortletURL, lifecycle, mimeResponse);
91      }
92  
93      public static PortletURL clone(
94              LiferayPortletURL liferayPortletURL, String lifecycle,
95              MimeResponse mimeResponse)
96          throws PortletException {
97  
98          LiferayPortletURL newURLImpl = null;
99  
100         if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
101             newURLImpl = (LiferayPortletURL)mimeResponse.createActionURL();
102         }
103         else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
104             newURLImpl = (LiferayPortletURL)mimeResponse.createRenderURL();
105         }
106 
107         newURLImpl.setPortletId(liferayPortletURL.getPortletId());
108 
109         WindowState windowState = liferayPortletURL.getWindowState();
110 
111         if (windowState != null) {
112             newURLImpl.setWindowState(windowState);
113         }
114 
115         PortletMode portletMode = liferayPortletURL.getPortletMode();
116 
117         if (portletMode != null) {
118             newURLImpl.setPortletMode(portletMode);
119         }
120 
121         newURLImpl.setParameters(liferayPortletURL.getParameterMap());
122 
123         return newURLImpl;
124     }
125 
126     private static final int _CURRENT_URL_PARAMETER_THRESHOLD = 32768;
127 
128 }