/portal-impl/src/com/liferay/portal/struts/StrutsURLEncoder.java

https://github.com/spreddy/liferay-portal · Java · 223 lines · 150 code · 49 blank · 24 comment · 26 complexity · 19fce1fd5f0da99ba6a82c5bc52024eb MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portal.struts;
  15. import com.liferay.portal.kernel.log.Log;
  16. import com.liferay.portal.kernel.log.LogFactoryUtil;
  17. import com.liferay.portal.kernel.portlet.LiferayPortletURL;
  18. import com.liferay.portal.kernel.portlet.PortletModeFactory;
  19. import com.liferay.portal.kernel.portlet.WindowStateFactory;
  20. import com.liferay.portal.kernel.servlet.URLEncoder;
  21. import com.liferay.portal.kernel.util.CharPool;
  22. import com.liferay.portal.kernel.util.GetterUtil;
  23. import com.liferay.portal.kernel.util.HttpUtil;
  24. import com.liferay.portal.kernel.util.StringPool;
  25. import com.liferay.portal.kernel.util.StringUtil;
  26. import com.liferay.portal.kernel.util.Validator;
  27. import java.util.HashMap;
  28. import javax.portlet.PortletMode;
  29. import javax.portlet.PortletModeException;
  30. import javax.portlet.PortletRequest;
  31. import javax.portlet.WindowState;
  32. import javax.portlet.WindowStateException;
  33. import javax.servlet.http.HttpServletResponse;
  34. /**
  35. * @author Brian Wing Shun Chan
  36. */
  37. public class StrutsURLEncoder implements URLEncoder {
  38. public static void setParameters(
  39. LiferayPortletURL liferayPortletURL, String queryString) {
  40. String[] params = StringUtil.split(queryString, '&');
  41. for (int i = 0; i < params.length; i++) {
  42. int pos = params[i].indexOf("=");
  43. if (pos != -1) {
  44. String param = params[i].substring(0, pos);
  45. String value = params[i].substring(pos + 1, params[i].length());
  46. if (param.equals("windowState")) {
  47. try {
  48. liferayPortletURL.setWindowState(
  49. WindowStateFactory.getWindowState(value));
  50. }
  51. catch (WindowStateException wse) {
  52. _log.error(wse.getMessage());
  53. }
  54. }
  55. else if (param.equals("portletMode")) {
  56. try {
  57. liferayPortletURL.setPortletMode(
  58. PortletModeFactory.getPortletMode(value));
  59. }
  60. catch (PortletModeException pme) {
  61. _log.error(pme.getMessage());
  62. }
  63. }
  64. else if (param.equals("actionURL")) {
  65. String lifecycle = PortletRequest.RENDER_PHASE;
  66. if (GetterUtil.getBoolean(value)) {
  67. lifecycle = PortletRequest.ACTION_PHASE;
  68. }
  69. liferayPortletURL.setLifecycle(lifecycle);
  70. }
  71. else {
  72. liferayPortletURL.setParameter(
  73. param, HttpUtil.decodeURL(value), true);
  74. }
  75. }
  76. }
  77. }
  78. public StrutsURLEncoder(
  79. String contextPath, String mainPath, String servletMapping,
  80. LiferayPortletURL liferayPortletURL) {
  81. _contextPath = contextPath;
  82. _mainPath = mainPath;
  83. _setServletMapping(servletMapping);
  84. _liferayPortletURL = liferayPortletURL;
  85. _windowState = liferayPortletURL.getWindowState();
  86. _portletMode = liferayPortletURL.getPortletMode();
  87. }
  88. public String encodeURL(HttpServletResponse response, String path) {
  89. if (_log.isDebugEnabled()) {
  90. _log.debug("Path " + path);
  91. _log.debug("Context path " + _contextPath);
  92. _log.debug("Servlet mapping " + _servletMapping);
  93. }
  94. String encodedURL = path;
  95. if (path.startsWith("//") ||
  96. path.startsWith(_contextPath) ||
  97. path.startsWith(_servletMapping)) {
  98. // Struts uses &amp; instead of & to delimit parameter key value
  99. // pairs when you set the "name" attribute for html:link.
  100. path = StringUtil.replace(path, "&amp;", "&");
  101. // Reset portlet URL settings so it can be reused
  102. _liferayPortletURL.setLifecycle(PortletRequest.RENDER_PHASE);
  103. _liferayPortletURL.setParameters(new HashMap<String, String[]>());
  104. try {
  105. _liferayPortletURL.setWindowState(_windowState);
  106. }
  107. catch (WindowStateException wse) {
  108. }
  109. try {
  110. _liferayPortletURL.setPortletMode(_portletMode);
  111. }
  112. catch (PortletModeException pme) {
  113. }
  114. // Separate the Struts action from the query string
  115. String strutsAction = path;
  116. String queryString = StringPool.BLANK;
  117. int pos = strutsAction.indexOf(CharPool.QUESTION);
  118. if (pos != -1) {
  119. strutsAction = path.substring(0, pos);
  120. queryString = path.substring(pos + 1, path.length());
  121. }
  122. // Set the Struts action
  123. if (strutsAction.startsWith("c/")) {
  124. strutsAction = strutsAction.substring(1);
  125. }
  126. else if (strutsAction.startsWith("/c/")) {
  127. strutsAction = strutsAction.substring(2);
  128. }
  129. if (Validator.isNotNull(_contextPath)) {
  130. strutsAction = strutsAction.substring(
  131. _contextPath.length(), strutsAction.length());
  132. }
  133. if (strutsAction.startsWith(_servletMapping)) {
  134. strutsAction = strutsAction.substring(
  135. _servletMapping.length(), strutsAction.length());
  136. }
  137. if (!strutsAction.startsWith(StringPool.SLASH)) {
  138. strutsAction = StringPool.SLASH + strutsAction;
  139. }
  140. if (_log.isDebugEnabled()) {
  141. _log.debug("Struts action " + strutsAction);
  142. }
  143. _liferayPortletURL.setParameter("struts_action", strutsAction);
  144. // Set the query string
  145. setParameters(_liferayPortletURL, queryString);
  146. // Return the portlet URL
  147. encodedURL = _liferayPortletURL.toString();
  148. if (_log.isDebugEnabled()) {
  149. _log.debug("Encoded portlet URL " + encodedURL);
  150. }
  151. }
  152. return encodedURL;
  153. }
  154. private void _setServletMapping(String servletMapping) {
  155. if (servletMapping != null) {
  156. // See org.apache.struts.util.RequestUtils.getActionMappingURL
  157. if (servletMapping.endsWith("/*")) {
  158. int pos = 0;
  159. if (servletMapping.startsWith(_mainPath)) {
  160. pos = _mainPath.length() - 2;
  161. }
  162. _servletMapping = servletMapping.substring(
  163. pos, servletMapping.length() - 1);
  164. }
  165. }
  166. }
  167. private static Log _log = LogFactoryUtil.getLog(StrutsURLEncoder.class);
  168. private String _contextPath;
  169. private LiferayPortletURL _liferayPortletURL;
  170. private String _mainPath;
  171. private PortletMode _portletMode;
  172. private String _servletMapping = StringPool.BLANK;
  173. private WindowState _windowState;
  174. }