1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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         String doAsUserId = themeDisplay.getDoAsUserId();
197 
198         if (Validator.isNotNull(doAsUserId)) {
199             sb.append("&doAsUserId=");
200             sb.append(HttpUtil.encodeURL(doAsUserId));
201         }
202 
203         String currentURL = PortalUtil.getCurrentURL(request);
204 
205         sb.append("&currentURL=");
206         sb.append(HttpUtil.encodeURL(currentURL));
207 
208         String ppid = ParamUtil.getString(request, "p_p_id");
209 
210         if (ppid.equals(portletId)) {
211             Enumeration<String> enu = request.getParameterNames();
212 
213             while (enu.hasMoreElements()) {
214                 String name = enu.nextElement();
215 
216                 if (!PortalUtil.isReservedParameter(name) &&
217                     !name.equals("currentURL")) {
218 
219                     String[] values = request.getParameterValues(name);
220 
221                     for (int i = 0; i < values.length; i++) {
222                         sb.append(StringPool.AMPERSAND);
223                         sb.append(name);
224                         sb.append(StringPool.EQUAL);
225                         sb.append(HttpUtil.encodeURL(values[i]));
226                     }
227                 }
228             }
229         }
230 
231         String outerPortletId = PortalUtil.getOuterPortletId(request);
232 
233         if (outerPortletId != null) {
234             sb.append(StringPool.AMPERSAND);
235             sb.append("p_o_p_id");
236             sb.append(StringPool.EQUAL);
237             sb.append(HttpUtil.encodeURL(outerPortletId));
238         }
239 
240         return sb.toString();
241     }
242 
243     private static final int _CURRENT_URL_PARAMETER_THRESHOLD = 32768;
244 
245 }