You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultDeploymentConfiguration.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import java.util.Locale;
  18. import java.util.Properties;
  19. import java.util.logging.Logger;
  20. import com.vaadin.shared.communication.PushMode;
  21. /**
  22. * The default implementation of {@link DeploymentConfiguration} based on a base
  23. * class for resolving system properties and a set of init parameters.
  24. *
  25. * @author Vaadin Ltd
  26. * @since 7.0.0
  27. */
  28. public class DefaultDeploymentConfiguration
  29. extends AbstractDeploymentConfiguration {
  30. /**
  31. * Default value for {@link #getResourceCacheTime()} = {@value} .
  32. */
  33. public static final int DEFAULT_RESOURCE_CACHE_TIME = 3600;
  34. /**
  35. * Default value for {@link #getHeartbeatInterval()} = {@value} .
  36. */
  37. public static final int DEFAULT_HEARTBEAT_INTERVAL = 300;
  38. /**
  39. * Default value for {@link #isCloseIdleSessions()} = {@value} .
  40. */
  41. public static final boolean DEFAULT_CLOSE_IDLE_SESSIONS = false;
  42. /**
  43. * Default value for {@link #isSyncIdCheckEnabled()} = {@value} .
  44. *
  45. * @since 7.3
  46. */
  47. public static final boolean DEFAULT_SYNC_ID_CHECK = true;
  48. public static final boolean DEFAULT_SEND_URLS_AS_PARAMETERS = true;
  49. private final Properties initParameters;
  50. private boolean productionMode;
  51. private boolean xsrfProtectionEnabled;
  52. private int resourceCacheTime;
  53. private int heartbeatInterval;
  54. private boolean closeIdleSessions;
  55. private PushMode pushMode;
  56. private final Class<?> systemPropertyBaseClass;
  57. private boolean syncIdCheck;
  58. private boolean sendUrlsAsParameters;
  59. /**
  60. * Create a new deployment configuration instance.
  61. *
  62. * @param systemPropertyBaseClass
  63. * the class that should be used as a basis when reading system
  64. * properties
  65. * @param initParameters
  66. * the init parameters that should make up the foundation for
  67. * this configuration
  68. */
  69. public DefaultDeploymentConfiguration(Class<?> systemPropertyBaseClass,
  70. Properties initParameters) {
  71. this.initParameters = initParameters;
  72. this.systemPropertyBaseClass = systemPropertyBaseClass;
  73. checkProductionMode();
  74. checkXsrfProtection();
  75. checkResourceCacheTime();
  76. checkHeartbeatInterval();
  77. checkCloseIdleSessions();
  78. checkPushMode();
  79. checkSyncIdCheck();
  80. checkSendUrlsAsParameters();
  81. }
  82. @Override
  83. public String getApplicationOrSystemProperty(String propertyName,
  84. String defaultValue) {
  85. String val = null;
  86. // Try system properties
  87. val = getSystemProperty(propertyName);
  88. if (val != null) {
  89. return val;
  90. }
  91. // Try application properties
  92. val = getApplicationProperty(propertyName);
  93. if (val != null) {
  94. return val;
  95. }
  96. return defaultValue;
  97. }
  98. /**
  99. * Gets an system property value.
  100. *
  101. * @param parameterName
  102. * the Name or the parameter.
  103. * @return String value or null if not found
  104. */
  105. protected String getSystemProperty(String parameterName) {
  106. String val = null;
  107. String pkgName;
  108. final Package pkg = systemPropertyBaseClass.getPackage();
  109. if (pkg != null) {
  110. pkgName = pkg.getName();
  111. } else {
  112. final String className = systemPropertyBaseClass.getName();
  113. int index = className.lastIndexOf('.');
  114. if (index >= 0) {
  115. pkgName = className.substring(0, index);
  116. } else {
  117. pkgName = null;
  118. }
  119. }
  120. if (pkgName == null) {
  121. pkgName = "";
  122. } else {
  123. pkgName += '.';
  124. }
  125. val = System.getProperty(pkgName + parameterName);
  126. if (val != null) {
  127. return val;
  128. }
  129. // Try lowercased system properties
  130. val = System
  131. .getProperty(pkgName + parameterName.toLowerCase(Locale.ROOT));
  132. if (val != null) {
  133. return val;
  134. }
  135. // version prefixed with just "vaadin."
  136. val = System.getProperty("vaadin." + parameterName);
  137. return val;
  138. }
  139. /**
  140. * Gets an application property value.
  141. *
  142. * @param parameterName
  143. * the Name or the parameter.
  144. * @return String value or null if not found
  145. */
  146. public String getApplicationProperty(String parameterName) {
  147. String val = initParameters.getProperty(parameterName);
  148. if (val != null) {
  149. return val;
  150. }
  151. // Try lower case application properties for backward compatibility with
  152. // 3.0.2 and earlier
  153. val = initParameters
  154. .getProperty(parameterName.toLowerCase(Locale.ROOT));
  155. return val;
  156. }
  157. /**
  158. * {@inheritDoc}
  159. *
  160. * The default is false.
  161. */
  162. @Override
  163. public boolean isProductionMode() {
  164. return productionMode;
  165. }
  166. /**
  167. * {@inheritDoc}
  168. * <p>
  169. * The default is true.
  170. */
  171. @Override
  172. public boolean isXsrfProtectionEnabled() {
  173. return xsrfProtectionEnabled;
  174. }
  175. /**
  176. * {@inheritDoc}
  177. * <p>
  178. * The default interval is 3600 seconds (1 hour).
  179. */
  180. @Override
  181. public int getResourceCacheTime() {
  182. return resourceCacheTime;
  183. }
  184. /**
  185. * {@inheritDoc}
  186. * <p>
  187. * The default interval is 300 seconds (5 minutes).
  188. */
  189. @Override
  190. public int getHeartbeatInterval() {
  191. return heartbeatInterval;
  192. }
  193. /**
  194. * {@inheritDoc}
  195. * <p>
  196. * The default value is false.
  197. */
  198. @Override
  199. public boolean isCloseIdleSessions() {
  200. return closeIdleSessions;
  201. }
  202. /**
  203. * {@inheritDoc}
  204. * <p>
  205. * The default value is <code>true</code>.
  206. */
  207. @Override
  208. public boolean isSyncIdCheckEnabled() {
  209. return syncIdCheck;
  210. }
  211. /**
  212. * {@inheritDoc}
  213. * <p>
  214. * The default value is <code>true</code>.
  215. */
  216. @Override
  217. public boolean isSendUrlsAsParameters() {
  218. return sendUrlsAsParameters;
  219. }
  220. /**
  221. * {@inheritDoc}
  222. * <p>
  223. * The default mode is {@link PushMode#DISABLED}.
  224. */
  225. @Override
  226. public PushMode getPushMode() {
  227. return pushMode;
  228. }
  229. @Override
  230. public Properties getInitParameters() {
  231. return initParameters;
  232. }
  233. /**
  234. * Log a warning if Vaadin is not running in production mode.
  235. */
  236. private void checkProductionMode() {
  237. productionMode = getApplicationOrSystemProperty(
  238. Constants.SERVLET_PARAMETER_PRODUCTION_MODE, "false")
  239. .equals("true");
  240. if (!productionMode) {
  241. getLogger().warning(Constants.NOT_PRODUCTION_MODE_INFO);
  242. }
  243. }
  244. /**
  245. * Log a warning if cross-site request forgery protection is disabled.
  246. */
  247. private void checkXsrfProtection() {
  248. xsrfProtectionEnabled = !getApplicationOrSystemProperty(
  249. Constants.SERVLET_PARAMETER_DISABLE_XSRF_PROTECTION, "false")
  250. .equals("true");
  251. if (!xsrfProtectionEnabled) {
  252. getLogger().warning(Constants.WARNING_XSRF_PROTECTION_DISABLED);
  253. }
  254. }
  255. /**
  256. * Log a warning if resource cache time is set but is not an integer.
  257. */
  258. private void checkResourceCacheTime() {
  259. try {
  260. resourceCacheTime = Integer.parseInt(getApplicationOrSystemProperty(
  261. Constants.SERVLET_PARAMETER_RESOURCE_CACHE_TIME,
  262. Integer.toString(DEFAULT_RESOURCE_CACHE_TIME)));
  263. } catch (NumberFormatException e) {
  264. getLogger().warning(
  265. Constants.WARNING_RESOURCE_CACHING_TIME_NOT_NUMERIC);
  266. resourceCacheTime = DEFAULT_RESOURCE_CACHE_TIME;
  267. }
  268. }
  269. private void checkHeartbeatInterval() {
  270. try {
  271. heartbeatInterval = Integer.parseInt(getApplicationOrSystemProperty(
  272. Constants.SERVLET_PARAMETER_HEARTBEAT_INTERVAL,
  273. Integer.toString(DEFAULT_HEARTBEAT_INTERVAL)));
  274. } catch (NumberFormatException e) {
  275. getLogger()
  276. .warning(Constants.WARNING_HEARTBEAT_INTERVAL_NOT_NUMERIC);
  277. heartbeatInterval = DEFAULT_HEARTBEAT_INTERVAL;
  278. }
  279. }
  280. private void checkCloseIdleSessions() {
  281. closeIdleSessions = getApplicationOrSystemProperty(
  282. Constants.SERVLET_PARAMETER_CLOSE_IDLE_SESSIONS,
  283. Boolean.toString(DEFAULT_CLOSE_IDLE_SESSIONS)).equals("true");
  284. }
  285. private void checkPushMode() {
  286. String mode = getApplicationOrSystemProperty(
  287. Constants.SERVLET_PARAMETER_PUSH_MODE,
  288. PushMode.DISABLED.toString());
  289. try {
  290. pushMode = Enum.valueOf(PushMode.class,
  291. mode.toUpperCase(Locale.ROOT));
  292. } catch (IllegalArgumentException e) {
  293. getLogger().warning(Constants.WARNING_PUSH_MODE_NOT_RECOGNIZED);
  294. pushMode = PushMode.DISABLED;
  295. }
  296. }
  297. private void checkSyncIdCheck() {
  298. syncIdCheck = getApplicationOrSystemProperty(
  299. Constants.SERVLET_PARAMETER_SYNC_ID_CHECK,
  300. Boolean.toString(DEFAULT_SYNC_ID_CHECK)).equals("true");
  301. }
  302. private void checkSendUrlsAsParameters() {
  303. sendUrlsAsParameters = getApplicationOrSystemProperty(
  304. Constants.SERVLET_PARAMETER_SENDURLSASPARAMETERS,
  305. Boolean.toString(DEFAULT_SEND_URLS_AS_PARAMETERS))
  306. .equals("true");
  307. }
  308. private Logger getLogger() {
  309. return Logger.getLogger(getClass().getName());
  310. }
  311. }