/modules/apps/iframe/iframe-web/src/main/java/com/liferay/iframe/web/internal/display/context/IFrameDisplayContext.java

https://github.com/kiyoshilee/liferay-portal · Java · 300 lines · 208 code · 76 blank · 16 comment · 44 complexity · 15f68a5d27b6d222946a10f684fc223d MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present 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.iframe.web.internal.display.context;
  15. import com.liferay.iframe.web.internal.configuration.IFramePortletInstanceConfiguration;
  16. import com.liferay.iframe.web.internal.constants.IFrameWebKeys;
  17. import com.liferay.iframe.web.internal.util.IFrameUtil;
  18. import com.liferay.petra.string.CharPool;
  19. import com.liferay.petra.string.StringPool;
  20. import com.liferay.portal.kernel.exception.PortalException;
  21. import com.liferay.portal.kernel.module.configuration.ConfigurationException;
  22. import com.liferay.portal.kernel.theme.PortletDisplay;
  23. import com.liferay.portal.kernel.theme.ThemeDisplay;
  24. import com.liferay.portal.kernel.util.KeyValuePair;
  25. import com.liferay.portal.kernel.util.ListUtil;
  26. import com.liferay.portal.kernel.util.StringUtil;
  27. import com.liferay.portal.kernel.util.Validator;
  28. import com.liferay.portal.kernel.util.WebKeys;
  29. import java.util.ArrayList;
  30. import java.util.Enumeration;
  31. import java.util.List;
  32. import javax.portlet.PortletRequest;
  33. import javax.portlet.WindowState;
  34. /**
  35. * @author Juergen Kappler
  36. */
  37. public class IFrameDisplayContext {
  38. public IFrameDisplayContext(PortletRequest request)
  39. throws ConfigurationException {
  40. _request = request;
  41. _themeDisplay = (ThemeDisplay)request.getAttribute(
  42. WebKeys.THEME_DISPLAY);
  43. PortletDisplay portletDisplay = _themeDisplay.getPortletDisplay();
  44. _iFramePortletInstanceConfiguration =
  45. portletDisplay.getPortletInstanceConfiguration(
  46. IFramePortletInstanceConfiguration.class);
  47. }
  48. public String getAuthType() {
  49. if (_authType != null) {
  50. return _authType;
  51. }
  52. _authType = _iFramePortletInstanceConfiguration.authType();
  53. return _authType;
  54. }
  55. public String getFormMethod() {
  56. if (_formMethod != null) {
  57. return _formMethod;
  58. }
  59. _formMethod = _iFramePortletInstanceConfiguration.formMethod();
  60. return _formMethod;
  61. }
  62. public String getHeight() {
  63. if (_height != null) {
  64. return _height;
  65. }
  66. String windowState = String.valueOf(_request.getWindowState());
  67. if (windowState.equals(WindowState.MAXIMIZED)) {
  68. _height = _iFramePortletInstanceConfiguration.heightMaximized();
  69. }
  70. else {
  71. _height = _iFramePortletInstanceConfiguration.heightNormal();
  72. }
  73. return _height;
  74. }
  75. public List<KeyValuePair> getHiddenVariableKVPs() {
  76. List<KeyValuePair> hiddenVariableKVPs = new ArrayList<>();
  77. List<String> hiddenVariables = ListUtil.fromArray(
  78. StringUtil.split(getHiddenVariables(), CharPool.SEMICOLON));
  79. hiddenVariables.addAll(getIFrameVariables());
  80. for (String hiddenVariable : hiddenVariables) {
  81. String key = StringPool.BLANK;
  82. String value = StringPool.BLANK;
  83. int pos = hiddenVariable.indexOf(StringPool.EQUAL);
  84. if (pos != -1) {
  85. key = hiddenVariable.substring(0, pos);
  86. value = hiddenVariable.substring(pos + 1);
  87. }
  88. hiddenVariableKVPs.add(new KeyValuePair(key, value));
  89. }
  90. return hiddenVariableKVPs;
  91. }
  92. public String getHiddenVariables() {
  93. if (_hiddenVariables != null) {
  94. return _hiddenVariables;
  95. }
  96. _hiddenVariables = StringUtil.merge(
  97. _iFramePortletInstanceConfiguration.hiddenVariables(),
  98. StringPool.PIPE);
  99. return _hiddenVariables;
  100. }
  101. public String getIframeBaseSrc() {
  102. if (_iFrameBaseSrc != null) {
  103. return _iFrameBaseSrc;
  104. }
  105. _iFrameBaseSrc = getIframeSrc();
  106. if (_iFrameBaseSrc.length() > 6) {
  107. String s = _iFrameBaseSrc.substring(7);
  108. int index = s.lastIndexOf(StringPool.SLASH);
  109. if (index != -1) {
  110. _iFrameBaseSrc = _iFrameBaseSrc.substring(0, index + 8);
  111. }
  112. }
  113. return _iFrameBaseSrc;
  114. }
  115. public IFramePortletInstanceConfiguration
  116. getIFramePortletInstanceConfiguration() {
  117. return _iFramePortletInstanceConfiguration;
  118. }
  119. public String getIframeSrc() {
  120. if (_iFrameSrc != null) {
  121. return _iFrameSrc;
  122. }
  123. _iFrameSrc = StringPool.BLANK;
  124. if (_iFramePortletInstanceConfiguration.relative()) {
  125. _iFrameSrc = _themeDisplay.getPathContext();
  126. }
  127. _iFrameSrc += (String)_request.getAttribute(IFrameWebKeys.IFRAME_SRC);
  128. if (!ListUtil.isEmpty(getIFrameVariables())) {
  129. if (_iFrameSrc.contains(StringPool.QUESTION)) {
  130. _iFrameSrc += StringPool.AMPERSAND;
  131. }
  132. else {
  133. _iFrameSrc += StringPool.QUESTION;
  134. }
  135. _iFrameSrc += StringUtil.merge(
  136. getIFrameVariables(), StringPool.AMPERSAND);
  137. }
  138. return _iFrameSrc;
  139. }
  140. public List<String> getIFrameVariables() {
  141. List<String> iFrameVariables = new ArrayList<>();
  142. Enumeration<String> enu = _request.getParameterNames();
  143. while (enu.hasMoreElements()) {
  144. String name = enu.nextElement();
  145. if (name.startsWith(_IFRAME_PREFIX)) {
  146. iFrameVariables.add(
  147. name.substring(_IFRAME_PREFIX.length()) + StringPool.EQUAL +
  148. _request.getParameter(name));
  149. }
  150. }
  151. return iFrameVariables;
  152. }
  153. public String getPassword() throws PortalException {
  154. if (_password != null) {
  155. return _password;
  156. }
  157. String authType = getAuthType();
  158. if (authType.equals("basic")) {
  159. _password = _iFramePortletInstanceConfiguration.basicPassword();
  160. }
  161. else {
  162. _password = _iFramePortletInstanceConfiguration.formPassword();
  163. }
  164. if (Validator.isNull(_password)) {
  165. return StringPool.BLANK;
  166. }
  167. String passwordField =
  168. _iFramePortletInstanceConfiguration.passwordField();
  169. if (Validator.isNull(passwordField)) {
  170. int pos = _password.indexOf(StringPool.EQUAL);
  171. if (pos != -1) {
  172. String fieldValuePair = _password;
  173. passwordField = fieldValuePair.substring(0, pos);
  174. _password = fieldValuePair.substring(pos + 1);
  175. }
  176. }
  177. if (Validator.isNotNull(passwordField)) {
  178. _password = IFrameUtil.getPassword(_request, _password);
  179. }
  180. return _password;
  181. }
  182. public String getUserName() throws PortalException {
  183. if (_userName != null) {
  184. return _userName;
  185. }
  186. String authType = getAuthType();
  187. if (authType.equals("basic")) {
  188. _userName = _iFramePortletInstanceConfiguration.basicUserName();
  189. }
  190. else {
  191. _userName = _iFramePortletInstanceConfiguration.formUserName();
  192. }
  193. if (Validator.isNull(_userName)) {
  194. return StringPool.BLANK;
  195. }
  196. String userNameField =
  197. _iFramePortletInstanceConfiguration.userNameField();
  198. if (Validator.isNull(userNameField)) {
  199. int pos = _userName.indexOf(StringPool.EQUAL);
  200. if (pos != -1) {
  201. String fieldValuePair = _userName;
  202. userNameField = fieldValuePair.substring(0, pos);
  203. _userName = fieldValuePair.substring(pos + 1);
  204. }
  205. }
  206. if (Validator.isNotNull(userNameField)) {
  207. _userName = IFrameUtil.getUserName(_request, _userName);
  208. }
  209. return _userName;
  210. }
  211. private static final String _IFRAME_PREFIX = "iframe_";
  212. private String _authType;
  213. private String _formMethod;
  214. private String _height;
  215. private String _hiddenVariables;
  216. private String _iFrameBaseSrc;
  217. private final IFramePortletInstanceConfiguration
  218. _iFramePortletInstanceConfiguration;
  219. private String _iFrameSrc;
  220. private String _password;
  221. private final PortletRequest _request;
  222. private final ThemeDisplay _themeDisplay;
  223. private String _userName;
  224. }