View Javadoc

1   /* ------------------------------------
2    * © Kaamelot - 2006
3    * ------------------------------------
4    * Projet  : KaamelotAddOn
5    * Fichier : VelocityPluginRessourceResolver.java
6    * $Id$ 
7    * $Date$ 
8    * $Log$
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   * @author Kaamelot - 2006
36   * <b>Description :</b>
37   * Provide a Resolver for Plugin Ressource
38   * @version $Id$
39   * @history <ul>
40   * </ul>
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  		// do nothing here
83  	}
84  
85  	/** The use of RequestDispatcher.dispatch() to resolve the ressources identified by [_path] is possible due to a HTTP Client connection is close. The Servlet and DownloadStrategy Class are used
86  	 * directly ! 
87  	 * @param _path
88  	 * @param _request
89  	 * @param _response
90  	 * @return
91  	 * @throws IOException
92  	 * @throws ServletException
93  	 */
94  	protected String getFileContent(String _path, HttpServletRequest _request, HttpServletResponse _response) throws IOException, ServletException {
95  		// Implementing the DownloadStrategy Class
96  		FileServerServlet s = new FileServerServlet();
97  		CharResponseWrapper charResponseWrapper = new CharResponseWrapper(_response);
98  		PluginResourceDownload downloadStrategy = new PluginResourceDownload(pluginManager);
99  		
100 		// An MockRequest is build just to provide the correct URI , the downloadable ressource [_path]
101 		MockRequest mockRequest = new MockRequest();
102 		mockRequest.setRequestURI(_path);
103 
104 		// Retrieve Downloadable file ...
105 		downloadStrategy.serveFile(s, mockRequest, charResponseWrapper);
106 
107 		return charResponseWrapper.toString();
108 	}
109 
110 	// Mock HttpServletRequest Class to provide RequestURI
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 }