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 clone(
051 LiferayPortletURL liferayPortletURL, String lifecycle,
052 MimeResponse mimeResponse)
053 throws PortletException {
054
055 LiferayPortletURL newURLImpl = null;
056
057 if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
058 newURLImpl = (LiferayPortletURL)mimeResponse.createActionURL();
059 }
060 else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
061 newURLImpl = (LiferayPortletURL)mimeResponse.createRenderURL();
062 }
063
064 newURLImpl.setPortletId(liferayPortletURL.getPortletId());
065
066 WindowState windowState = liferayPortletURL.getWindowState();
067
068 if (windowState != null) {
069 newURLImpl.setWindowState(windowState);
070 }
071
072 PortletMode portletMode = liferayPortletURL.getPortletMode();
073
074 if (portletMode != null) {
075 newURLImpl.setPortletMode(portletMode);
076 }
077
078 newURLImpl.setParameters(liferayPortletURL.getParameterMap());
079
080 return newURLImpl;
081 }
082
083 public static PortletURL clone(
084 PortletURL portletURL, MimeResponse mimeResponse)
085 throws PortletException {
086
087 LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
088
089 return clone(
090 liferayPortletURL, liferayPortletURL.getLifecycle(), mimeResponse);
091 }
092
093 public static PortletURL clone(
094 PortletURL portletURL, String lifecycle, MimeResponse mimeResponse)
095 throws PortletException {
096
097 LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
098
099 return clone(liferayPortletURL, lifecycle, mimeResponse);
100 }
101
102 public static PortletURL getCurrent(
103 PortletRequest portletRequest, MimeResponse mimeResponse) {
104
105 PortletURL portletURL = mimeResponse.createRenderURL();
106
107 Enumeration<String> enu = portletRequest.getParameterNames();
108
109 while (enu.hasMoreElements()) {
110 String param = enu.nextElement();
111 String[] values = portletRequest.getParameterValues(param);
112
113 boolean addParam = true;
114
115
116
117 for (int i = 0; i < values.length; i++) {
118 if (values[i].length() > _CURRENT_URL_PARAMETER_THRESHOLD) {
119 addParam = false;
120
121 break;
122 }
123 }
124
125 if (addParam) {
126 portletURL.setParameter(param, values);
127 }
128 }
129
130 return portletURL;
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(WebKeys.RENDER_PORTLET);
146
147 String portletId = portlet.getPortletId();
148
149 sb.append("&p_p_id=");
150 sb.append(portletId);
151
152 sb.append("&p_p_lifecycle=0&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&p_p_col_id=");
176
177 String columnId = (String)request.getAttribute(
178 WebKeys.RENDER_PORTLET_COLUMN_ID);
179
180 sb.append(columnId);
181
182 Integer columnPos = (Integer)request.getAttribute(
183 WebKeys.RENDER_PORTLET_COLUMN_POS);
184
185 sb.append("&p_p_col_pos=");
186 sb.append(columnPos);
187
188 Integer columnCount = (Integer)request.getAttribute(
189 WebKeys.RENDER_PORTLET_COLUMN_COUNT);
190
191 sb.append("&p_p_col_count=");
192 sb.append(columnCount);
193
194 if (portlet.isStatic()) {
195 sb.append("&p_p_static=1");
196
197 if (portlet.isStaticStart()) {
198 sb.append("&p_p_static_start=1");
199 }
200 }
201
202 sb.append("&p_p_isolated=1");
203
204 String doAsUserId = themeDisplay.getDoAsUserId();
205
206 if (Validator.isNotNull(doAsUserId)) {
207 sb.append("&doAsUserId=");
208 sb.append(HttpUtil.encodeURL(doAsUserId));
209 }
210
211 String currentURL = PortalUtil.getCurrentURL(request);
212
213 sb.append("¤tURL=");
214 sb.append(HttpUtil.encodeURL(currentURL));
215
216 String ppid = ParamUtil.getString(request, "p_p_id");
217
218 if (ppid.equals(portletId)) {
219 String namespace = PortalUtil.getPortletNamespace(portletId);
220
221 Map<String, String[]> parameters = request.getParameterMap();
222
223 for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
224 String name = entry.getKey();
225
226 if (!PortalUtil.isReservedParameter(name) &&
227 !name.equals("currentURL") &&
228 !isRefreshURLReservedParameter(name, namespace)) {
229
230 String[] values = entry.getValue();
231
232 for (int i = 0; i < values.length; i++) {
233 sb.append(StringPool.AMPERSAND);
234 sb.append(name);
235 sb.append(StringPool.EQUAL);
236 sb.append(HttpUtil.encodeURL(values[i]));
237 }
238 }
239 }
240 }
241
242 String outerPortletId = PortalUtil.getOuterPortletId(request);
243
244 if (outerPortletId != null) {
245 sb.append(StringPool.AMPERSAND);
246 sb.append("p_o_p_id");
247 sb.append(StringPool.EQUAL);
248 sb.append(HttpUtil.encodeURL(outerPortletId));
249 }
250
251 return sb.toString();
252 }
253
254 protected static boolean isRefreshURLReservedParameter(
255 String parameter, String namespace) {
256
257 if ((_PORTLET_URL_REFRESH_URL_RESERVED_PARAMETERS == null) ||
258 (_PORTLET_URL_REFRESH_URL_RESERVED_PARAMETERS.length == 0)) {
259
260 return false;
261 }
262
263 for (int i = 0; i < _PORTLET_URL_REFRESH_URL_RESERVED_PARAMETERS.length;
264 i++) {
265
266 String reservedParameter = namespace.concat(
267 _PORTLET_URL_REFRESH_URL_RESERVED_PARAMETERS[i]);
268
269 if (parameter.equals(reservedParameter)) {
270 return true;
271 }
272 }
273
274 return false;
275 }
276
277 private static final int _CURRENT_URL_PARAMETER_THRESHOLD = 32768;
278
279 private static final String[] _PORTLET_URL_REFRESH_URL_RESERVED_PARAMETERS =
280 PropsUtil.getArray(
281 PropsKeys.PORTLET_URL_REFRESH_URL_RESERVED_PARAMETERS);
282
283 }