001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
018    import com.liferay.portal.kernel.portlet.LiferayWindowState;
019    import com.liferay.portal.kernel.util.HttpUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.util.WebKeys;
025    import com.liferay.portal.model.LayoutTypePortlet;
026    import com.liferay.portal.model.Portlet;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    
030    import java.util.Enumeration;
031    import java.util.Map;
032    
033    import javax.portlet.MimeResponse;
034    import javax.portlet.PortletException;
035    import javax.portlet.PortletMode;
036    import javax.portlet.PortletRequest;
037    import javax.portlet.PortletURL;
038    import javax.portlet.WindowState;
039    
040    import javax.servlet.http.HttpServletRequest;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class PortletURLUtil {
046    
047            public static PortletURL getCurrent(
048                    PortletRequest portletRequest, MimeResponse mimeResponse) {
049    
050                    PortletURL portletURL = mimeResponse.createRenderURL();
051    
052                    Enumeration<String> enu = portletRequest.getParameterNames();
053    
054                    while (enu.hasMoreElements()) {
055                            String param = enu.nextElement();
056                            String[] values = portletRequest.getParameterValues(param);
057    
058                            boolean addParam = true;
059    
060                            // Don't set paramter values that are over 32 kb. See LEP-1755.
061    
062                            for (int i = 0; i < values.length; i++) {
063                                    if (values[i].length() > _CURRENT_URL_PARAMETER_THRESHOLD) {
064                                            addParam = false;
065    
066                                            break;
067                                    }
068                            }
069    
070                            if (addParam) {
071                                    portletURL.setParameter(param, values);
072                            }
073                    }
074    
075                    return portletURL;
076            }
077    
078            public static PortletURL clone(
079                            PortletURL portletURL, MimeResponse mimeResponse)
080                    throws PortletException {
081    
082                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
083    
084                    return clone(
085                            liferayPortletURL, liferayPortletURL.getLifecycle(), mimeResponse);
086            }
087    
088            public static PortletURL clone(
089                            PortletURL portletURL, String lifecycle, MimeResponse mimeResponse)
090                    throws PortletException {
091    
092                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
093    
094                    return clone(liferayPortletURL, lifecycle, mimeResponse);
095            }
096    
097            public static PortletURL clone(
098                            LiferayPortletURL liferayPortletURL, String lifecycle,
099                            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(32);
134    
135                    sb.append(themeDisplay.getPathMain());
136                    sb.append("/portal/render_portlet?p_l_id=");
137    
138                    long plid = themeDisplay.getPlid();
139    
140                    sb.append(plid);
141    
142                    Portlet portlet = (Portlet)request.getAttribute(
143                            WebKeys.RENDER_PORTLET);
144    
145                    String portletId = portlet.getPortletId();
146    
147                    sb.append("&p_p_id=");
148                    sb.append(portletId);
149    
150                    sb.append("&p_p_lifecycle=0&p_t_lifecycle=");
151                    sb.append(themeDisplay.getLifecycle());
152    
153                    WindowState windowState = WindowState.NORMAL;
154    
155                    if (themeDisplay.isStatePopUp()) {
156                            windowState = LiferayWindowState.POP_UP;
157                    }
158                    else {
159                            LayoutTypePortlet layoutTypePortlet =
160                                    themeDisplay.getLayoutTypePortlet();
161    
162                            if (layoutTypePortlet.hasStateMaxPortletId(portletId)) {
163                                    windowState = WindowState.MAXIMIZED;
164                            }
165                            else if (layoutTypePortlet.hasStateMinPortletId(portletId)) {
166                                    windowState = WindowState.MINIMIZED;
167                            }
168                    }
169    
170                    sb.append("&p_p_state=");
171                    sb.append(windowState);
172    
173                    sb.append("&p_p_mode=view&p_p_col_id=");
174    
175                    String columnId = (String)request.getAttribute(
176                            WebKeys.RENDER_PORTLET_COLUMN_ID);
177    
178                    sb.append(columnId);
179    
180                    Integer columnPos = (Integer)request.getAttribute(
181                            WebKeys.RENDER_PORTLET_COLUMN_POS);
182    
183                    sb.append("&p_p_col_pos=");
184                    sb.append(columnPos);
185    
186                    Integer columnCount = (Integer)request.getAttribute(
187                            WebKeys.RENDER_PORTLET_COLUMN_COUNT);
188    
189                    sb.append("&p_p_col_count=");
190                    sb.append(columnCount);
191    
192                    if (portlet.isStatic()) {
193                            sb.append("&p_p_static=1");
194    
195                            if (portlet.isStaticStart()) {
196                                    sb.append("&p_p_static_start=1");
197                            }
198                    }
199    
200                    sb.append("&p_p_isolated=1");
201    
202                    String doAsUserId = themeDisplay.getDoAsUserId();
203    
204                    if (Validator.isNotNull(doAsUserId)) {
205                            sb.append("&doAsUserId=");
206                            sb.append(HttpUtil.encodeURL(doAsUserId));
207                    }
208    
209                    String currentURL = PortalUtil.getCurrentURL(request);
210    
211                    sb.append("&currentURL=");
212                    sb.append(HttpUtil.encodeURL(currentURL));
213    
214                    String ppid = ParamUtil.getString(request, "p_p_id");
215    
216                    if (ppid.equals(portletId)) {
217                            Map<String, String[]> parameters = request.getParameterMap();
218    
219                            for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
220                                    String name = entry.getKey();
221    
222                                    if (!PortalUtil.isReservedParameter(name) &&
223                                            !name.equals("currentURL")) {
224    
225                                            String[] values = entry.getValue();
226    
227                                            for (int i = 0; i < values.length; i++) {
228                                                    sb.append(StringPool.AMPERSAND);
229                                                    sb.append(name);
230                                                    sb.append(StringPool.EQUAL);
231                                                    sb.append(HttpUtil.encodeURL(values[i]));
232                                            }
233                                    }
234                            }
235                    }
236    
237                    String outerPortletId = PortalUtil.getOuterPortletId(request);
238    
239                    if (outerPortletId != null) {
240                            sb.append(StringPool.AMPERSAND);
241                            sb.append("p_o_p_id");
242                            sb.append(StringPool.EQUAL);
243                            sb.append(HttpUtil.encodeURL(outerPortletId));
244                    }
245    
246                    return sb.toString();
247            }
248    
249            private static final int _CURRENT_URL_PARAMETER_THRESHOLD = 32768;
250    
251    }