1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
18  import com.liferay.portal.kernel.util.HttpUtil;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.kernel.util.WebKeys;
24  import com.liferay.portal.model.LayoutTypePortlet;
25  import com.liferay.portal.model.Portlet;
26  import com.liferay.portal.theme.ThemeDisplay;
27  import com.liferay.portal.util.PortalUtil;
28  
29  import java.util.Enumeration;
30  
31  import javax.portlet.MimeResponse;
32  import javax.portlet.PortletException;
33  import javax.portlet.PortletMode;
34  import javax.portlet.PortletRequest;
35  import javax.portlet.PortletURL;
36  import javax.portlet.WindowState;
37  
38  import javax.servlet.http.HttpServletRequest;
39  
40  /**
41   * <a href="PortletURLUtil.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class PortletURLUtil {
46  
47      public static PortletURL getCurrent(
48          PortletRequest portletRequest, MimeResponse mimeResponse) {
49  
50          PortletURL portletURL = mimeResponse.createRenderURL();
51  
52          Enumeration<String> enu = portletRequest.getParameterNames();
53  
54          while (enu.hasMoreElements()) {
55              String param = enu.nextElement();
56              String[] values = portletRequest.getParameterValues(param);
57  
58              boolean addParam = true;
59  
60              // Don't set paramter values that are over 32 kb. See LEP-1755.
61  
62              for (int i = 0; i < values.length; i++) {
63                  if (values[i].length() > _CURRENT_URL_PARAMETER_THRESHOLD) {
64                      addParam = false;
65  
66                      break;
67                  }
68              }
69  
70              if (addParam) {
71                  portletURL.setParameter(param, values);
72              }
73          }
74  
75          return portletURL;
76      }
77  
78      public static PortletURL clone(
79              PortletURL portletURL, MimeResponse mimeResponse)
80          throws PortletException {
81  
82          LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
83  
84          return clone(
85              liferayPortletURL, liferayPortletURL.getLifecycle(), mimeResponse);
86      }
87  
88      public static PortletURL clone(
89              PortletURL portletURL, String lifecycle, MimeResponse mimeResponse)
90          throws PortletException {
91  
92          LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
93  
94          return clone(liferayPortletURL, lifecycle, mimeResponse);
95      }
96  
97      public static PortletURL clone(
98              LiferayPortletURL liferayPortletURL, String lifecycle,
99              MimeResponse mimeResponse)
100         throws PortletException {
101 
102         LiferayPortletURL newURLImpl = null;
103 
104         if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
105             newURLImpl = (LiferayPortletURL)mimeResponse.createActionURL();
106         }
107         else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
108             newURLImpl = (LiferayPortletURL)mimeResponse.createRenderURL();
109         }
110 
111         newURLImpl.setPortletId(liferayPortletURL.getPortletId());
112 
113         WindowState windowState = liferayPortletURL.getWindowState();
114 
115         if (windowState != null) {
116             newURLImpl.setWindowState(windowState);
117         }
118 
119         PortletMode portletMode = liferayPortletURL.getPortletMode();
120 
121         if (portletMode != null) {
122             newURLImpl.setPortletMode(portletMode);
123         }
124 
125         newURLImpl.setParameters(liferayPortletURL.getParameterMap());
126 
127         return newURLImpl;
128     }
129 
130     public static String getRefreshURL(
131         HttpServletRequest request, ThemeDisplay themeDisplay) {
132 
133         StringBundler sb = new StringBundler();
134 
135         sb.append(themeDisplay.getPathMain());
136         sb.append("/portal/render_portlet?");
137 
138         long plid = themeDisplay.getPlid();
139 
140         sb.append("p_l_id=");
141         sb.append(plid);
142 
143         Portlet portlet = (Portlet)request.getAttribute(
144             WebKeys.RENDER_PORTLET);
145 
146         String portletId = portlet.getPortletId();
147 
148         sb.append("&p_p_id=");
149         sb.append(portletId);
150 
151         sb.append("&p_p_lifecycle=0");
152 
153         WindowState windowState = WindowState.NORMAL;
154 
155         LayoutTypePortlet layoutTypePortlet =
156             themeDisplay.getLayoutTypePortlet();
157 
158         if (layoutTypePortlet.hasStateMaxPortletId(portletId)) {
159             windowState = WindowState.MAXIMIZED;
160         }
161         else if (layoutTypePortlet.hasStateMinPortletId(portletId)) {
162             windowState = WindowState.MINIMIZED;
163         }
164 
165         sb.append("&p_p_state=");
166         sb.append(windowState);
167 
168         sb.append("&p_p_mode=view");
169 
170         String columnId = (String)request.getAttribute(
171             WebKeys.RENDER_PORTLET_COLUMN_ID);
172 
173         sb.append("&p_p_col_id=");
174         sb.append(columnId);
175 
176         Integer columnPos = (Integer)request.getAttribute(
177             WebKeys.RENDER_PORTLET_COLUMN_POS);
178 
179         sb.append("&p_p_col_pos=");
180         sb.append(columnPos);
181 
182         Integer columnCount = (Integer)request.getAttribute(
183             WebKeys.RENDER_PORTLET_COLUMN_COUNT);
184 
185         sb.append("&p_p_col_count=");
186         sb.append(columnCount);
187 
188         if (portlet.isStatic()) {
189             sb.append("&p_p_static=1");
190 
191             if (portlet.isStaticStart()) {
192                 sb.append("&p_p_static_start=1");
193             }
194         }
195 
196         sb.append("&p_p_isolated=1");
197 
198         String doAsUserId = themeDisplay.getDoAsUserId();
199 
200         if (Validator.isNotNull(doAsUserId)) {
201             sb.append("&doAsUserId=");
202             sb.append(HttpUtil.encodeURL(doAsUserId));
203         }
204 
205         String currentURL = PortalUtil.getCurrentURL(request);
206 
207         sb.append("&currentURL=");
208         sb.append(HttpUtil.encodeURL(currentURL));
209 
210         String ppid = ParamUtil.getString(request, "p_p_id");
211 
212         if (ppid.equals(portletId)) {
213             Enumeration<String> enu = request.getParameterNames();
214 
215             while (enu.hasMoreElements()) {
216                 String name = enu.nextElement();
217 
218                 if (!PortalUtil.isReservedParameter(name) &&
219                     !name.equals("currentURL")) {
220 
221                     String[] values = request.getParameterValues(name);
222 
223                     for (int i = 0; i < values.length; i++) {
224                         sb.append(StringPool.AMPERSAND);
225                         sb.append(name);
226                         sb.append(StringPool.EQUAL);
227                         sb.append(HttpUtil.encodeURL(values[i]));
228                     }
229                 }
230             }
231         }
232 
233         return sb.toString();
234     }
235 
236     private static final int _CURRENT_URL_PARAMETER_THRESHOLD = 32768;
237 
238 }