001    /**
002     * Copyright (c) 2000-2010 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.util.HttpUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.model.LayoutTypePortlet;
025    import com.liferay.portal.model.Portlet;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    
029    import java.util.Enumeration;
030    
031    import javax.portlet.MimeResponse;
032    import javax.portlet.PortletException;
033    import javax.portlet.PortletMode;
034    import javax.portlet.PortletRequest;
035    import javax.portlet.PortletURL;
036    import javax.portlet.WindowState;
037    
038    import javax.servlet.http.HttpServletRequest;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class PortletURLUtil {
044    
045            public static PortletURL getCurrent(
046                    PortletRequest portletRequest, MimeResponse mimeResponse) {
047    
048                    PortletURL portletURL = mimeResponse.createRenderURL();
049    
050                    Enumeration<String> enu = portletRequest.getParameterNames();
051    
052                    while (enu.hasMoreElements()) {
053                            String param = enu.nextElement();
054                            String[] values = portletRequest.getParameterValues(param);
055    
056                            boolean addParam = true;
057    
058                            // Don't set paramter values that are over 32 kb. See LEP-1755.
059    
060                            for (int i = 0; i < values.length; i++) {
061                                    if (values[i].length() > _CURRENT_URL_PARAMETER_THRESHOLD) {
062                                            addParam = false;
063    
064                                            break;
065                                    }
066                            }
067    
068                            if (addParam) {
069                                    portletURL.setParameter(param, values);
070                            }
071                    }
072    
073                    return portletURL;
074            }
075    
076            public static PortletURL clone(
077                            PortletURL portletURL, MimeResponse mimeResponse)
078                    throws PortletException {
079    
080                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
081    
082                    return clone(
083                            liferayPortletURL, liferayPortletURL.getLifecycle(), mimeResponse);
084            }
085    
086            public static PortletURL clone(
087                            PortletURL portletURL, String lifecycle, MimeResponse mimeResponse)
088                    throws PortletException {
089    
090                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
091    
092                    return clone(liferayPortletURL, lifecycle, mimeResponse);
093            }
094    
095            public static PortletURL clone(
096                            LiferayPortletURL liferayPortletURL, String lifecycle,
097                            MimeResponse mimeResponse)
098                    throws PortletException {
099    
100                    LiferayPortletURL newURLImpl = null;
101    
102                    if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
103                            newURLImpl = (LiferayPortletURL)mimeResponse.createActionURL();
104                    }
105                    else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
106                            newURLImpl = (LiferayPortletURL)mimeResponse.createRenderURL();
107                    }
108    
109                    newURLImpl.setPortletId(liferayPortletURL.getPortletId());
110    
111                    WindowState windowState = liferayPortletURL.getWindowState();
112    
113                    if (windowState != null) {
114                            newURLImpl.setWindowState(windowState);
115                    }
116    
117                    PortletMode portletMode = liferayPortletURL.getPortletMode();
118    
119                    if (portletMode != null) {
120                            newURLImpl.setPortletMode(portletMode);
121                    }
122    
123                    newURLImpl.setParameters(liferayPortletURL.getParameterMap());
124    
125                    return newURLImpl;
126            }
127    
128            public static String getRefreshURL(
129                    HttpServletRequest request, ThemeDisplay themeDisplay) {
130    
131                    StringBundler sb = new StringBundler();
132    
133                    sb.append(themeDisplay.getPathMain());
134                    sb.append("/portal/render_portlet?");
135    
136                    long plid = themeDisplay.getPlid();
137    
138                    sb.append("p_l_id=");
139                    sb.append(plid);
140    
141                    Portlet portlet = (Portlet)request.getAttribute(
142                            WebKeys.RENDER_PORTLET);
143    
144                    String portletId = portlet.getPortletId();
145    
146                    sb.append("&p_p_id=");
147                    sb.append(portletId);
148    
149                    sb.append("&p_p_lifecycle=0");
150    
151                    WindowState windowState = WindowState.NORMAL;
152    
153                    LayoutTypePortlet layoutTypePortlet =
154                            themeDisplay.getLayoutTypePortlet();
155    
156                    if (layoutTypePortlet.hasStateMaxPortletId(portletId)) {
157                            windowState = WindowState.MAXIMIZED;
158                    }
159                    else if (layoutTypePortlet.hasStateMinPortletId(portletId)) {
160                            windowState = WindowState.MINIMIZED;
161                    }
162    
163                    sb.append("&p_p_state=");
164                    sb.append(windowState);
165    
166                    sb.append("&p_p_mode=view");
167    
168                    String columnId = (String)request.getAttribute(
169                            WebKeys.RENDER_PORTLET_COLUMN_ID);
170    
171                    sb.append("&p_p_col_id=");
172                    sb.append(columnId);
173    
174                    Integer columnPos = (Integer)request.getAttribute(
175                            WebKeys.RENDER_PORTLET_COLUMN_POS);
176    
177                    sb.append("&p_p_col_pos=");
178                    sb.append(columnPos);
179    
180                    Integer columnCount = (Integer)request.getAttribute(
181                            WebKeys.RENDER_PORTLET_COLUMN_COUNT);
182    
183                    sb.append("&p_p_col_count=");
184                    sb.append(columnCount);
185    
186                    if (portlet.isStatic()) {
187                            sb.append("&p_p_static=1");
188    
189                            if (portlet.isStaticStart()) {
190                                    sb.append("&p_p_static_start=1");
191                            }
192                    }
193    
194                    sb.append("&p_p_isolated=1");
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                    return sb.toString();
232            }
233    
234            private static final int _CURRENT_URL_PARAMETER_THRESHOLD = 32768;
235    
236    }