1
2
3
4
5
6
7
8
9
10 package com.atlassian.jira.servlet;
11
12 import java.io.IOException;
13 import java.util.Map;
14
15 import javax.servlet.ServletConfig;
16 import javax.servlet.ServletException;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.apache.log4j.Category;
22 import org.apache.velocity.exception.VelocityException;
23
24 import com.atlassian.cache.servlet.resolver.ContentResolver;
25 import com.atlassian.core.util.map.EasyMap;
26 import com.atlassian.jira.ComponentManager;
27 import com.atlassian.jira.config.properties.APKeys;
28 import com.atlassian.jira.config.properties.ApplicationProperties;
29 import com.atlassian.jira.web.servlet.FileServerServlet;
30 import com.atlassian.plugin.PluginManager;
31 import com.atlassian.plugin.servlet.PluginResourceDownload;
32 import com.atlassian.velocity.VelocityManager;
33
34
35
36
37
38
39
40
41
42 public class VelocityPluginRessourceResolver implements ContentResolver {
43
44 protected final Category log = Category.getInstance(getClass());
45
46 protected PluginManager pluginManager = ComponentManager.getInstance().getPluginManager();
47
48 private static final String PATH_SEPARATOR = "/";
49
50 public String getContent(String _path, HttpServletRequest _request, HttpServletResponse _response) throws IOException, ServletException {
51 StringBuffer data = new StringBuffer();
52
53 if (_request != null) {
54 if (log.isDebugEnabled())
55 log.debug("Rendering content for '" + _path + "'.");
56
57 String velocityTemplateBody = getFileContent(_path, _request, _response);
58
59 VelocityManager velocityManager = ComponentManager.getInstance().getVelocityManager();
60 ApplicationProperties applicationProperties = ComponentManager.getInstance().getApplicationProperties();
61
62 try {
63 Map params = getVelocityParameters(_path, _request, _response);
64 String renderedTemplate = velocityManager.getEncodedBodyForContent(velocityTemplateBody, applicationProperties.getString(APKeys.JIRA_BASEURL), params);
65 data.append(renderedTemplate);
66 } catch (VelocityException e) {
67 log.error("Error occurred while rendering content for '" + _path + "'.", e);
68 }
69
70 } else {
71 log.warn("Request is null. Cannot serve content for '" + _path + "'");
72 }
73
74 return data.toString();
75 }
76
77 public Map getVelocityParameters(String _path, HttpServletRequest _request, HttpServletResponse _response) throws ServletException {
78 return EasyMap.build("req", _request.getContextPath(), "absolutePath", _request.getContextPath() + StringUtils.substringBeforeLast(_path, PATH_SEPARATOR));
79 }
80
81 public void init(ServletConfig servletConfig) throws ServletException {
82
83 }
84
85
86
87
88
89
90
91
92
93
94 protected String getFileContent(String _path, HttpServletRequest _request, HttpServletResponse _response) throws IOException, ServletException {
95
96 FileServerServlet s = new FileServerServlet();
97 CharResponseWrapper charResponseWrapper = new CharResponseWrapper(_response);
98 PluginResourceDownload downloadStrategy = new PluginResourceDownload(pluginManager);
99
100
101 MockRequest mockRequest = new MockRequest();
102 mockRequest.setRequestURI(_path);
103
104
105 downloadStrategy.serveFile(s, mockRequest, charResponseWrapper);
106
107 return charResponseWrapper.toString();
108 }
109
110
111 class MockRequest extends EmptyRequest implements HttpServletRequest {
112 String requestURI;
113
114 public String getRequestURI() {
115 return requestURI;
116 }
117
118 public void setRequestURI(String _requestURI) {
119 requestURI = _requestURI;
120 }
121
122 }
123 }