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