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  import com.liferay.portal.kernel.util.HttpUtil;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.LayoutTypePortlet;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.WebKeys;
35  
36  import java.util.Enumeration;
37  import java.util.Iterator;
38  import java.util.Map;
39  
40  import javax.portlet.MimeResponse;
41  import javax.portlet.PortletException;
42  import javax.portlet.PortletMode;
43  import javax.portlet.PortletRequest;
44  import javax.portlet.PortletURL;
45  import javax.portlet.WindowState;
46  
47  import javax.servlet.http.HttpServletRequest;
48  
49  /**
50   * <a href="PortletURLUtil.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   */
54  public class PortletURLUtil {
55  
56      public static PortletURL getCurrent(
57          PortletRequest portletRequest, MimeResponse mimeResponse) {
58  
59          PortletURL portletURL = mimeResponse.createRenderURL();
60  
61          Enumeration<String> enu = portletRequest.getParameterNames();
62  
63          while (enu.hasMoreElements()) {
64              String param = enu.nextElement();
65              String[] values = portletRequest.getParameterValues(param);
66  
67              boolean addParam = true;
68  
69              // Don't set paramter values that are over 32 kb. See LEP-1755.
70  
71              for (int i = 0; i < values.length; i++) {
72                  if (values[i].length() > _CURRENT_URL_PARAMETER_THRESHOLD) {
73                      addParam = false;
74  
75                      break;
76                  }
77              }
78  
79              if (addParam) {
80                  portletURL.setParameter(param, values);
81              }
82          }
83  
84          return portletURL;
85      }
86  
87      public static PortletURL clone(
88              PortletURL portletURL, MimeResponse mimeResponse)
89          throws PortletException {
90  
91          LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
92  
93          return clone(
94              liferayPortletURL, liferayPortletURL.getLifecycle(), mimeResponse);
95      }
96  
97      public static PortletURL clone(
98              PortletURL portletURL, String lifecycle, MimeResponse mimeResponse)
99          throws PortletException {
100 
101         LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
102 
103         return clone(liferayPortletURL, lifecycle, mimeResponse);
104     }
105 
106     public static PortletURL clone(
107             LiferayPortletURL liferayPortletURL, String lifecycle,
108             MimeResponse mimeResponse)
109         throws PortletException {
110 
111         LiferayPortletURL newURLImpl = null;
112 
113         if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
114             newURLImpl = (LiferayPortletURL)mimeResponse.createActionURL();
115         }
116         else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
117             newURLImpl = (LiferayPortletURL)mimeResponse.createRenderURL();
118         }
119 
120         newURLImpl.setPortletId(liferayPortletURL.getPortletId());
121 
122         WindowState windowState = liferayPortletURL.getWindowState();
123 
124         if (windowState != null) {
125             newURLImpl.setWindowState(windowState);
126         }
127 
128         PortletMode portletMode = liferayPortletURL.getPortletMode();
129 
130         if (portletMode != null) {
131             newURLImpl.setPortletMode(portletMode);
132         }
133 
134         newURLImpl.setParameters(liferayPortletURL.getParameterMap());
135 
136         return newURLImpl;
137     }
138 
139     public static String getRefreshURL(
140         HttpServletRequest request, ThemeDisplay themeDisplay) {
141 
142         StringBuilder sb = new StringBuilder();
143 
144         sb.append(themeDisplay.getPathMain());
145         sb.append("/portal/render_portlet?");
146 
147         long plid = themeDisplay.getPlid();
148 
149         sb.append("p_l_id=");
150         sb.append(plid);
151 
152         Portlet portlet = (Portlet)request.getAttribute(
153             WebKeys.RENDER_PORTLET);
154 
155         String portletId = portlet.getPortletId();
156 
157         sb.append("&p_p_id=");
158         sb.append(portletId);
159 
160         sb.append("&p_p_lifecycle=0");
161 
162         WindowState windowState = WindowState.NORMAL;
163 
164         LayoutTypePortlet layoutTypePortlet =
165             themeDisplay.getLayoutTypePortlet();
166 
167         if (layoutTypePortlet.hasStateMaxPortletId(portletId)) {
168             windowState = WindowState.MAXIMIZED;
169         }
170         else if (layoutTypePortlet.hasStateMinPortletId(portletId)) {
171             windowState = WindowState.MINIMIZED;
172         }
173 
174         sb.append("&p_p_state=");
175         sb.append(windowState);
176 
177         sb.append("&p_p_mode=view");
178 
179         String columnId = (String)request.getAttribute(
180             WebKeys.RENDER_PORTLET_COLUMN_ID);
181 
182         sb.append("&p_p_col_id=");
183         sb.append(columnId);
184 
185         Integer columnPos = (Integer)request.getAttribute(
186             WebKeys.RENDER_PORTLET_COLUMN_POS);
187 
188         sb.append("&p_p_col_pos=");
189         sb.append(columnPos);
190 
191         Integer columnCount = (Integer)request.getAttribute(
192             WebKeys.RENDER_PORTLET_COLUMN_COUNT);
193 
194         sb.append("&p_p_col_count=");
195         sb.append(columnCount);
196 
197         if (portlet.isStatic()) {
198             sb.append("&p_p_static=1");
199 
200             if (portlet.isStaticStart()) {
201                 sb.append("&p_p_static_start=1");
202             }
203         }
204 
205         String doAsUserId = themeDisplay.getDoAsUserId();
206 
207         if (Validator.isNotNull(doAsUserId)) {
208             sb.append("&doAsUserId=");
209             sb.append(HttpUtil.encodeURL(doAsUserId));
210         }
211 
212         String currentURL = PortalUtil.getCurrentURL(request);
213 
214         sb.append("&currentURL=");
215         sb.append(HttpUtil.encodeURL(currentURL));
216 
217         String ppid = ParamUtil.getString(request, "p_p_id");
218 
219         if (ppid.equals(portletId)) {
220             Enumeration<String> enu = request.getParameterNames();
221 
222             while (enu.hasMoreElements()) {
223                 String name = enu.nextElement();
224 
225                 if (!PortalUtil.isReservedParameter(name)) {
226                     String[] values = request.getParameterValues(name);
227 
228                     for (int i = 0; i < values.length; i++) {
229                         sb.append(StringPool.AMPERSAND);
230                         sb.append(name);
231                         sb.append(StringPool.EQUAL);
232                         sb.append(HttpUtil.encodeURL(values[i]));
233                     }
234                 }
235             }
236 
237             Map<String, String[]> renderParameters = RenderParametersPool.get(
238                 request, plid, ppid);
239 
240             Iterator<String> itr = renderParameters.keySet().iterator();
241 
242             while (itr.hasNext()) {
243                 String name = itr.next();
244 
245                 String[] values = renderParameters.get(name);
246 
247                 for (int i = 0; i < values.length; i++) {
248                     sb.append(StringPool.AMPERSAND);
249                     sb.append(name);
250                     sb.append(StringPool.EQUAL);
251                     sb.append(HttpUtil.encodeURL(values[i]));
252                 }
253             }
254         }
255 
256         return sb.toString();
257     }
258 
259     private static final int _CURRENT_URL_PARAMETER_THRESHOLD = 32768;
260 
261 }