001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    
032    import javax.portlet.MimeResponse;
033    import javax.portlet.PortletException;
034    import javax.portlet.PortletMode;
035    import javax.portlet.PortletRequest;
036    import javax.portlet.PortletURL;
037    import javax.portlet.WindowState;
038    
039    import javax.servlet.http.HttpServletRequest;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class PortletURLUtil {
045    
046            public static PortletURL getCurrent(
047                    PortletRequest portletRequest, MimeResponse mimeResponse) {
048    
049                    PortletURL portletURL = mimeResponse.createRenderURL();
050    
051                    Enumeration<String> enu = portletRequest.getParameterNames();
052    
053                    while (enu.hasMoreElements()) {
054                            String param = enu.nextElement();
055                            String[] values = portletRequest.getParameterValues(param);
056    
057                            boolean addParam = true;
058    
059                            // Don't set paramter values that are over 32 kb. See LEP-1755.
060    
061                            for (int i = 0; i < values.length; i++) {
062                                    if (values[i].length() > _CURRENT_URL_PARAMETER_THRESHOLD) {
063                                            addParam = false;
064    
065                                            break;
066                                    }
067                            }
068    
069                            if (addParam) {
070                                    portletURL.setParameter(param, values);
071                            }
072                    }
073    
074                    return portletURL;
075            }
076    
077            public static PortletURL clone(
078                            PortletURL portletURL, MimeResponse mimeResponse)
079                    throws PortletException {
080    
081                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
082    
083                    return clone(
084                            liferayPortletURL, liferayPortletURL.getLifecycle(), mimeResponse);
085            }
086    
087            public static PortletURL clone(
088                            PortletURL portletURL, String lifecycle, MimeResponse mimeResponse)
089                    throws PortletException {
090    
091                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
092    
093                    return clone(liferayPortletURL, lifecycle, mimeResponse);
094            }
095    
096            public static PortletURL clone(
097                            LiferayPortletURL liferayPortletURL, String lifecycle,
098                            MimeResponse mimeResponse)
099                    throws PortletException {
100    
101                    LiferayPortletURL newURLImpl = null;
102    
103                    if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
104                            newURLImpl = (LiferayPortletURL)mimeResponse.createActionURL();
105                    }
106                    else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
107                            newURLImpl = (LiferayPortletURL)mimeResponse.createRenderURL();
108                    }
109    
110                    newURLImpl.setPortletId(liferayPortletURL.getPortletId());
111    
112                    WindowState windowState = liferayPortletURL.getWindowState();
113    
114                    if (windowState != null) {
115                            newURLImpl.setWindowState(windowState);
116                    }
117    
118                    PortletMode portletMode = liferayPortletURL.getPortletMode();
119    
120                    if (portletMode != null) {
121                            newURLImpl.setPortletMode(portletMode);
122                    }
123    
124                    newURLImpl.setParameters(liferayPortletURL.getParameterMap());
125    
126                    return newURLImpl;
127            }
128    
129            public static String getRefreshURL(
130                    HttpServletRequest request, ThemeDisplay themeDisplay) {
131    
132                    StringBundler sb = new StringBundler();
133    
134                    sb.append(themeDisplay.getPathMain());
135                    sb.append("/portal/render_portlet?");
136    
137                    long plid = themeDisplay.getPlid();
138    
139                    sb.append("p_l_id=");
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");
151    
152                    sb.append("&p_t_lifecycle=");
153                    sb.append(themeDisplay.getLifecycle());
154    
155                    WindowState windowState = WindowState.NORMAL;
156    
157                    if (themeDisplay.isStatePopUp()) {
158                            windowState = LiferayWindowState.POP_UP;
159                    }
160                    else {
161                            LayoutTypePortlet layoutTypePortlet =
162                                    themeDisplay.getLayoutTypePortlet();
163    
164                            if (layoutTypePortlet.hasStateMaxPortletId(portletId)) {
165                                    windowState = WindowState.MAXIMIZED;
166                            }
167                            else if (layoutTypePortlet.hasStateMinPortletId(portletId)) {
168                                    windowState = WindowState.MINIMIZED;
169                            }
170                    }
171    
172                    sb.append("&p_p_state=");
173                    sb.append(windowState);
174    
175                    sb.append("&p_p_mode=view");
176    
177                    String columnId = (String)request.getAttribute(
178                            WebKeys.RENDER_PORTLET_COLUMN_ID);
179    
180                    sb.append("&p_p_col_id=");
181                    sb.append(columnId);
182    
183                    Integer columnPos = (Integer)request.getAttribute(
184                            WebKeys.RENDER_PORTLET_COLUMN_POS);
185    
186                    sb.append("&p_p_col_pos=");
187                    sb.append(columnPos);
188    
189                    Integer columnCount = (Integer)request.getAttribute(
190                            WebKeys.RENDER_PORTLET_COLUMN_COUNT);
191    
192                    sb.append("&p_p_col_count=");
193                    sb.append(columnCount);
194    
195                    if (portlet.isStatic()) {
196                            sb.append("&p_p_static=1");
197    
198                            if (portlet.isStaticStart()) {
199                                    sb.append("&p_p_static_start=1");
200                            }
201                    }
202    
203                    sb.append("&p_p_isolated=1");
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                                            !name.equals("currentURL")) {
227    
228                                            String[] values = request.getParameterValues(name);
229    
230                                            for (int i = 0; i < values.length; i++) {
231                                                    sb.append(StringPool.AMPERSAND);
232                                                    sb.append(name);
233                                                    sb.append(StringPool.EQUAL);
234                                                    sb.append(HttpUtil.encodeURL(values[i]));
235                                            }
236                                    }
237                            }
238                    }
239    
240                    String outerPortletId = PortalUtil.getOuterPortletId(request);
241    
242                    if (outerPortletId != null) {
243                            sb.append(StringPool.AMPERSAND);
244                            sb.append("p_o_p_id");
245                            sb.append(StringPool.EQUAL);
246                            sb.append(HttpUtil.encodeURL(outerPortletId));
247                    }
248    
249                    return sb.toString();
250            }
251    
252            private static final int _CURRENT_URL_PARAMETER_THRESHOLD = 32768;
253    
254    }