001
014
015 package com.liferay.portlet;
016
017 import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
018 import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
019 import com.liferay.portal.kernel.portlet.LiferayPortletURL;
020 import com.liferay.portal.kernel.portlet.LiferayWindowState;
021 import com.liferay.portal.kernel.util.HttpUtil;
022 import com.liferay.portal.kernel.util.ParamUtil;
023 import com.liferay.portal.kernel.util.PropsKeys;
024 import com.liferay.portal.kernel.util.PropsUtil;
025 import com.liferay.portal.kernel.util.StringBundler;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.kernel.util.WebKeys;
029 import com.liferay.portal.model.LayoutTypePortlet;
030 import com.liferay.portal.model.Portlet;
031 import com.liferay.portal.theme.ThemeDisplay;
032 import com.liferay.portal.util.PortalUtil;
033
034 import java.util.Enumeration;
035 import java.util.Map;
036
037 import javax.portlet.MimeResponse;
038 import javax.portlet.PortletException;
039 import javax.portlet.PortletMode;
040 import javax.portlet.PortletRequest;
041 import javax.portlet.PortletURL;
042 import javax.portlet.WindowState;
043
044 import javax.servlet.http.HttpServletRequest;
045
046
050 public class PortletURLUtil {
051
052 public static PortletURL clone(
053 LiferayPortletURL liferayPortletURL, String lifecycle,
054 MimeResponse mimeResponse)
055 throws PortletException {
056
057 LiferayPortletURL newURLImpl = null;
058
059 if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
060 newURLImpl = (LiferayPortletURL)mimeResponse.createActionURL();
061 }
062 else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
063 newURLImpl = (LiferayPortletURL)mimeResponse.createRenderURL();
064 }
065
066 newURLImpl.setPortletId(liferayPortletURL.getPortletId());
067
068 WindowState windowState = liferayPortletURL.getWindowState();
069
070 if (windowState != null) {
071 newURLImpl.setWindowState(windowState);
072 }
073
074 PortletMode portletMode = liferayPortletURL.getPortletMode();
075
076 if (portletMode != null) {
077 newURLImpl.setPortletMode(portletMode);
078 }
079
080 newURLImpl.setParameters(liferayPortletURL.getParameterMap());
081
082 return newURLImpl;
083 }
084
085 public static PortletURL clone(
086 PortletURL portletURL, MimeResponse mimeResponse)
087 throws PortletException {
088
089 LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
090
091 return clone(
092 liferayPortletURL, liferayPortletURL.getLifecycle(), mimeResponse);
093 }
094
095 public static PortletURL clone(
096 PortletURL portletURL, String lifecycle, MimeResponse mimeResponse)
097 throws PortletException {
098
099 LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
100
101 return clone(liferayPortletURL, lifecycle, mimeResponse);
102 }
103
104 public static PortletURL getCurrent(
105 LiferayPortletRequest liferayPortletRequest,
106 LiferayPortletResponse liferayPortletResponse) {
107
108 PortletURL portletURL = liferayPortletResponse.createRenderURL();
109
110 Enumeration<String> enu = liferayPortletRequest.getParameterNames();
111
112 while (enu.hasMoreElements()) {
113 String param = enu.nextElement();
114 String[] values = liferayPortletRequest.getParameterValues(param);
115
116 boolean addParam = true;
117
118
119
120 for (int i = 0; i < values.length; i++) {
121 if (values[i].length() > _CURRENT_URL_PARAMETER_THRESHOLD) {
122 addParam = false;
123
124 break;
125 }
126 }
127
128 if (addParam) {
129 portletURL.setParameter(param, values);
130 }
131 }
132
133 return portletURL;
134 }
135
136 public static PortletURL getCurrent(
137 PortletRequest portletRequest, MimeResponse mimeResponse) {
138
139 return getCurrent(
140 (LiferayPortletRequest)portletRequest,
141 (LiferayPortletResponse)mimeResponse);
142 }
143
144 public static String getRefreshURL(
145 HttpServletRequest request, ThemeDisplay themeDisplay) {
146
147 StringBundler sb = new StringBundler(32);
148
149 sb.append(themeDisplay.getPathMain());
150 sb.append("/portal/render_portlet?p_l_id=");
151
152 long plid = themeDisplay.getPlid();
153
154 sb.append(plid);
155
156 Portlet portlet = (Portlet)request.getAttribute(WebKeys.RENDER_PORTLET);
157
158 String portletId = portlet.getPortletId();
159
160 sb.append("&p_p_id=");
161 sb.append(portletId);
162
163 sb.append("&p_p_lifecycle=0&p_t_lifecycle=");
164 sb.append(themeDisplay.getLifecycle());
165
166 WindowState windowState = WindowState.NORMAL;
167
168 if (themeDisplay.isStatePopUp()) {
169 windowState = LiferayWindowState.POP_UP;
170 }
171 else {
172 LayoutTypePortlet layoutTypePortlet =
173 themeDisplay.getLayoutTypePortlet();
174
175 if (layoutTypePortlet.hasStateMaxPortletId(portletId)) {
176 windowState = WindowState.MAXIMIZED;
177 }
178 else if (layoutTypePortlet.hasStateMinPortletId(portletId)) {
179 windowState = WindowState.MINIMIZED;
180 }
181 }
182
183 sb.append("&p_p_state=");
184 sb.append(windowState);
185
186 sb.append("&p_p_mode=view&p_p_col_id=");
187
188 String columnId = (String)request.getAttribute(
189 WebKeys.RENDER_PORTLET_COLUMN_ID);
190
191 sb.append(columnId);
192
193 Integer columnPos = (Integer)request.getAttribute(
194 WebKeys.RENDER_PORTLET_COLUMN_POS);
195
196 sb.append("&p_p_col_pos=");
197 sb.append(columnPos);
198
199 Integer columnCount = (Integer)request.getAttribute(
200 WebKeys.RENDER_PORTLET_COLUMN_COUNT);
201
202 sb.append("&p_p_col_count=");
203 sb.append(columnCount);
204
205 if (portlet.isStatic()) {
206 sb.append("&p_p_static=1");
207
208 if (portlet.isStaticStart()) {
209 sb.append("&p_p_static_start=1");
210 }
211 }
212
213 sb.append("&p_p_isolated=1");
214
215 long sourceGroupId = ParamUtil.getLong(request, "p_v_l_s_g_id");
216
217 if (sourceGroupId > 0) {
218 sb.append("&p_v_l_s_g_id=");
219 sb.append(sourceGroupId);
220 }
221
222 String doAsUserId = themeDisplay.getDoAsUserId();
223
224 if (Validator.isNotNull(doAsUserId)) {
225 sb.append("&doAsUserId=");
226 sb.append(HttpUtil.encodeURL(doAsUserId));
227 }
228
229 String currentURL = PortalUtil.getCurrentURL(request);
230
231 sb.append("¤tURL=");
232 sb.append(HttpUtil.encodeURL(currentURL));
233
234 String ppid = ParamUtil.getString(request, "p_p_id");
235
236 if (!ppid.equals(portletId)) {
237 return sb.toString();
238 }
239
240 String namespace = PortalUtil.getPortletNamespace(portletId);
241
242 Map<String, String[]> parameters = request.getParameterMap();
243
244 for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
245 String name = entry.getKey();
246
247 if (!PortalUtil.isReservedParameter(name) &&
248 !name.equals("currentURL") &&
249 !isRefreshURLReservedParameter(name, namespace)) {
250
251 String[] values = entry.getValue();
252
253 for (int i = 0; i < values.length; i++) {
254 sb.append(StringPool.AMPERSAND);
255 sb.append(name);
256 sb.append(StringPool.EQUAL);
257 sb.append(HttpUtil.encodeURL(values[i]));
258 }
259 }
260 }
261
262 return sb.toString();
263 }
264
265 protected static boolean isRefreshURLReservedParameter(
266 String parameter, String namespace) {
267
268 if ((_PORTLET_URL_REFRESH_URL_RESERVED_PARAMETERS == null) ||
269 (_PORTLET_URL_REFRESH_URL_RESERVED_PARAMETERS.length == 0)) {
270
271 return false;
272 }
273
274 for (int i = 0; i < _PORTLET_URL_REFRESH_URL_RESERVED_PARAMETERS.length;
275 i++) {
276
277 String reservedParameter = namespace.concat(
278 _PORTLET_URL_REFRESH_URL_RESERVED_PARAMETERS[i]);
279
280 if (parameter.equals(reservedParameter)) {
281 return true;
282 }
283 }
284
285 return false;
286 }
287
288 private static final int _CURRENT_URL_PARAMETER_THRESHOLD = 32768;
289
290 private static final String[] _PORTLET_URL_REFRESH_URL_RESERVED_PARAMETERS =
291 PropsUtil.getArray(
292 PropsKeys.PORTLET_URL_REFRESH_URL_RESERVED_PARAMETERS);
293
294 }