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(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 String[] _PORTLET_URL_REFRESH_URL_RESERVED_PARAMETERS =
280 PropsUtil.getArray(
281 PropsKeys.PORTLET_URL_REFRESH_URL_RESERVED_PARAMETERS);
282
283 }