PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-core/src/main/java/com/atlassian/jira/util/velocity/DefaultVelocityRequestContextFactory.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 136 lines | 92 code | 18 blank | 26 comment | 8 complexity | 04c0ad7f379d953622370cfd88f75ccc MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.util.velocity;
  2. import com.atlassian.jira.component.ComponentAccessor;
  3. import com.atlassian.jira.config.properties.APKeys;
  4. import com.atlassian.jira.config.properties.ApplicationProperties;
  5. import com.atlassian.jira.security.JiraAuthenticationContext;
  6. import com.atlassian.jira.security.JiraAuthenticationContextImpl;
  7. import com.atlassian.jira.security.RequestCacheKeys;
  8. import com.atlassian.jira.util.JiraVelocityUtils;
  9. import com.atlassian.jira.util.http.JiraUrl;
  10. import com.google.common.annotations.VisibleForTesting;
  11. import com.google.common.base.Function;
  12. import javax.annotation.Nonnull;
  13. import javax.servlet.http.HttpServletRequest;
  14. import java.util.Map;
  15. /**
  16. * Return an instance of {@link VelocityRequestContext}, depending on whether we are called from a web or non-web context.
  17. * <p>
  18. * The original intention of this class is to get around bugs such as JRA-11038, where velocity fragments are called from both
  19. * web and non-web contexts. Originally we tried to proxy HttpServletRequest, but it makes more sense to have a specific interface
  20. * {@link VelocityRequestContext}.
  21. */
  22. public class DefaultVelocityRequestContextFactory implements VelocityRequestContextFactory {
  23. private final ApplicationProperties applicationProperties;
  24. public DefaultVelocityRequestContextFactory(ApplicationProperties applicationProperties) {
  25. this.applicationProperties = applicationProperties;
  26. }
  27. /**
  28. * @deprecated Please use {@link #DefaultVelocityRequestContextFactory(com.atlassian.jira.config.properties.ApplicationProperties)} instead
  29. */
  30. public DefaultVelocityRequestContextFactory() {
  31. this(ComponentAccessor.getApplicationProperties());
  32. }
  33. public VelocityRequestContext getJiraVelocityRequestContext() {
  34. VelocityRequestContext cachedRequestContext = getCachedContext();
  35. if (cachedRequestContext != null) {
  36. return cachedRequestContext;
  37. } else {
  38. return createStaticContext();
  39. }
  40. }
  41. @Override
  42. public Map<String, Object> getDefaultVelocityParams(Map<String, Object> startingParams, JiraAuthenticationContext authenticationContext) {
  43. return JiraVelocityUtils.getDefaultVelocityParams(startingParams, authenticationContext);
  44. }
  45. /**
  46. * Called from a servlet filter. Passes the {@link javax.servlet.http.HttpServletRequest#getContextPath()} along as the baseUrl.
  47. *
  48. * @param request The HttpServletRequest used to construct the {@link com.atlassian.jira.util.velocity.RequestContextParameterHolder}
  49. * @deprecated Use {@link VelocityRequestContextFactory#setVelocityRequestContext(javax.servlet.http.HttpServletRequest)} instead. Since v5.0.
  50. */
  51. public static void cacheVelocityRequestContext(HttpServletRequest request) {
  52. cacheVelocityRequestContext(request.getContextPath(), request);
  53. }
  54. @Override
  55. public void setVelocityRequestContext(HttpServletRequest request) {
  56. setVelocityRequestContext(request.getContextPath(), request);
  57. }
  58. /**
  59. * Should be called from a servlet filter before the request gets a chance to run
  60. *
  61. * @param baseUrl Should pass in {@link javax.servlet.http.HttpServletRequest#getContextPath()}
  62. * @param request The HttpServletRequest used to construct the {@link com.atlassian.jira.util.velocity.RequestContextParameterHolder}
  63. * @deprecated Use {@link VelocityRequestContextFactory#setVelocityRequestContext(String, javax.servlet.http.HttpServletRequest)} instead. Since v5.0.
  64. */
  65. public static void cacheVelocityRequestContext(String baseUrl, HttpServletRequest request) {
  66. RequestContextParameterHolder requestContextParameterHolder = null;
  67. String canonicalBaseURL = baseUrl;
  68. VelocityRequestSession session = null;
  69. if (request != null) {
  70. requestContextParameterHolder = new RequestContextParameterHolderImpl(request);
  71. canonicalBaseURL = JiraUrl.constructBaseUrl(request);
  72. session = new HttpSessionBackedVelocityRequestSession(request);
  73. }
  74. final VelocityRequestContext velocityRequestContext = new SimpleVelocityRequestContext(baseUrl, canonicalBaseURL, requestContextParameterHolder, session);
  75. JiraAuthenticationContextImpl.getRequestCache().put(RequestCacheKeys.VELOCITY_REQUEST_CONTEXT, velocityRequestContext);
  76. }
  77. @Override
  78. public void setVelocityRequestContext(String baseUrl, HttpServletRequest request) {
  79. cacheVelocityRequestContext(baseUrl, request);
  80. }
  81. /**
  82. * @deprecated Use {@link VelocityRequestContextFactory#setVelocityRequestContext(VelocityRequestContext)} instead. Since v5.0.
  83. */
  84. public void cacheVelocityRequestContext(final VelocityRequestContext velocityRequestContext) {
  85. setVelocityRequestContext(velocityRequestContext);
  86. }
  87. @Override
  88. public void setVelocityRequestContext(final VelocityRequestContext velocityRequestContext) {
  89. getRequestCache().put(RequestCacheKeys.VELOCITY_REQUEST_CONTEXT, velocityRequestContext);
  90. }
  91. @Override
  92. public <I, O> O runWithStaticBaseUrl(final I input, @Nonnull final Function<I, O> runnable) {
  93. final VelocityRequestContext cachedContext = getCachedContext();
  94. setVelocityRequestContext(createStaticContext());
  95. try {
  96. return runnable.apply(input);
  97. } finally {
  98. if (cachedContext == null) {
  99. clearVelocityRequestContext();
  100. } else {
  101. setVelocityRequestContext(cachedContext);
  102. }
  103. }
  104. }
  105. public void clearVelocityRequestContext() {
  106. getRequestCache().remove(RequestCacheKeys.VELOCITY_REQUEST_CONTEXT);
  107. }
  108. @VisibleForTesting
  109. Map<String, Object> getRequestCache() {
  110. return JiraAuthenticationContextImpl.getRequestCache();
  111. }
  112. private VelocityRequestContext createStaticContext() {
  113. return new SimpleVelocityRequestContext(applicationProperties.getString(APKeys.JIRA_BASEURL));
  114. }
  115. private VelocityRequestContext getCachedContext() {
  116. return (VelocityRequestContext) getRequestCache().get(RequestCacheKeys.VELOCITY_REQUEST_CONTEXT);
  117. }
  118. }