PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/plugin/src/main/java/com/atlassian/plugin/remotable/plugin/loader/universalbinary/UBServletContextWrapper.java

https://bitbucket.org/rodogu/remotable-plugins
Java | 379 lines | 140 code | 41 blank | 198 comment | 0 complexity | 481b6eef300bf02d1fd34edc5f1b2cde MD5 | raw file
  1. package com.atlassian.plugin.remotable.plugin.loader.universalbinary;
  2. import com.atlassian.plugin.Plugin;
  3. import javax.servlet.*;
  4. import java.io.InputStream;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.util.*;
  8. import java.util.concurrent.ConcurrentMap;
  9. /**
  10. * A wrapper around servlet context that allows plugin servlets to add
  11. * attributes which will not be shared/clobbered by other plugins.
  12. */
  13. public class UBServletContextWrapper implements ServletContext
  14. {
  15. private final Plugin plugin;
  16. private final ServletContext context;
  17. private final ConcurrentMap<String, Object> attributes;
  18. private final Map<String, String> initParams;
  19. private final String contextPath;
  20. public UBServletContextWrapper(String contextPath,
  21. Plugin plugin,
  22. ServletContext context,
  23. ConcurrentMap<String, Object> attributes,
  24. Map<String, String> initParams
  25. )
  26. {
  27. this.contextPath = contextPath;
  28. this.plugin = plugin;
  29. this.context = context;
  30. this.attributes = attributes;
  31. this.initParams = initParams;
  32. }
  33. /**
  34. * <p>Gets the named attribute. The attribute is first looked for in the local
  35. * attribute map, if it is not found there it is looked for in the wrapped
  36. * contexts attribute map. If it is not there, null is returned.</p>
  37. *
  38. * <p>A consequence of this ordering is that servlets may, in their own
  39. * context, override but not overwrite attributes from the wrapped context.</p>
  40. */
  41. public Object getAttribute(String name)
  42. {
  43. return attributes.get(name);
  44. }
  45. /**
  46. * @return an enumeration of all the attributes from the wrapped
  47. * context as well as the local attributes.
  48. */
  49. public Enumeration getAttributeNames()
  50. {
  51. Collection<String> names = new HashSet<String>();
  52. names.addAll(attributes.keySet());
  53. return Collections.enumeration(names);
  54. }
  55. /**
  56. * Removes an attribute from the local context. Leaves the wrapped context
  57. * completely untouched.
  58. */
  59. public void removeAttribute(String name)
  60. {
  61. attributes.remove(name);
  62. }
  63. @Override
  64. public String getServletContextName()
  65. {
  66. return context.getServletContextName();
  67. }
  68. // @Override
  69. // public ServletRegistration.Dynamic addServlet(String s, String s1)
  70. // {
  71. // throw new UnsupportedOperationException("Not implemented");
  72. // }
  73. //
  74. // @Override
  75. // public ServletRegistration.Dynamic addServlet(String s, Servlet servlet)
  76. // {
  77. // throw new UnsupportedOperationException("Not implemented");
  78. // }
  79. //
  80. // @Override
  81. // public ServletRegistration.Dynamic addServlet(String s, Class<? extends Servlet> aClass)
  82. // {
  83. // throw new UnsupportedOperationException("Not implemented");
  84. // }
  85. //
  86. // @Override
  87. // public <T extends Servlet> T createServlet(Class<T> tClass) throws ServletException
  88. // {
  89. // throw new UnsupportedOperationException("Not implemented");
  90. // }
  91. //
  92. // @Override
  93. // public ServletRegistration getServletRegistration(String s)
  94. // {
  95. // throw new UnsupportedOperationException("Not implemented");
  96. // }
  97. //
  98. // @Override
  99. // public Map<String, ? extends ServletRegistration> getServletRegistrations()
  100. // {
  101. // throw new UnsupportedOperationException("Not implemented");
  102. // }
  103. //
  104. // @Override
  105. // public FilterRegistration.Dynamic addFilter(String s, String s1)
  106. // {
  107. // throw new UnsupportedOperationException("Not implemented");
  108. // }
  109. //
  110. // @Override
  111. // public FilterRegistration.Dynamic addFilter(String s, Filter filter)
  112. // {
  113. // throw new UnsupportedOperationException("Not implemented");
  114. // }
  115. //
  116. // @Override
  117. // public FilterRegistration.Dynamic addFilter(String s, Class<? extends Filter> aClass)
  118. // {
  119. // throw new UnsupportedOperationException("Not implemented");
  120. // }
  121. //
  122. // @Override
  123. // public <T extends Filter> T createFilter(Class<T> tClass) throws ServletException
  124. // {
  125. // throw new UnsupportedOperationException("Not implemented");
  126. // }
  127. //
  128. // @Override
  129. // public FilterRegistration getFilterRegistration(String s)
  130. // {
  131. // throw new UnsupportedOperationException("Not implemented");
  132. // }
  133. //
  134. // @Override
  135. // public Map<String, ? extends FilterRegistration> getFilterRegistrations()
  136. // {
  137. // throw new UnsupportedOperationException("Not implemented");
  138. // }
  139. //
  140. // @Override
  141. // public SessionCookieConfig getSessionCookieConfig()
  142. // {
  143. // throw new UnsupportedOperationException("Not implemented");
  144. // }
  145. //
  146. // @Override
  147. // public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes)
  148. // {
  149. // throw new UnsupportedOperationException("Not implemented");
  150. // }
  151. //
  152. // @Override
  153. // public Set<SessionTrackingMode> getDefaultSessionTrackingModes()
  154. // {
  155. // return context.getDefaultSessionTrackingModes();
  156. // }
  157. //
  158. // @Override
  159. // public Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
  160. // {
  161. // return context.getEffectiveSessionTrackingModes();
  162. // }
  163. // @Override
  164. // public void addListener(String s)
  165. // {
  166. // throw new UnsupportedOperationException("Not implemented");
  167. // }
  168. //
  169. // @Override
  170. // public <T extends EventListener> void addListener(T t)
  171. // {
  172. // throw new UnsupportedOperationException("Not implemented");
  173. // }
  174. //
  175. // @Override
  176. // public void addListener(Class<? extends EventListener> aClass)
  177. // {
  178. // throw new UnsupportedOperationException("Not implemented");
  179. // }
  180. //
  181. // @Override
  182. // public <T extends EventListener> T createListener(Class<T> tClass) throws ServletException
  183. // {
  184. // throw new UnsupportedOperationException("Not implemented");
  185. // }
  186. // @Override
  187. // public JspConfigDescriptor getJspConfigDescriptor()
  188. // {
  189. // return context.getJspConfigDescriptor();
  190. // }
  191. //
  192. // @Override
  193. // public ClassLoader getClassLoader()
  194. // {
  195. // return context.getClassLoader();
  196. // }
  197. // @Override
  198. // public void declareRoles(String... strings)
  199. // {
  200. // throw new UnsupportedOperationException("Not implemented");
  201. // }
  202. /**
  203. * <p>Sets an attribute in the local attribute map, leaving the wrapped
  204. * context untouched.</p>
  205. *
  206. * <p>Servlets may use this and the lookup ordering of the
  207. * <code>getAttribute()</code> method to effectively override the value
  208. * of an attribute in the wrapped servlet context with their own value and
  209. * this overridden value will only be seen in the plugins own scope.</p>
  210. */
  211. public void setAttribute(String name, Object object)
  212. {
  213. attributes.put(name, object);
  214. }
  215. /**
  216. * @return the init parameter from the servlet module descriptor.
  217. */
  218. public String getInitParameter(String name)
  219. {
  220. return initParams.get(name);
  221. }
  222. /**
  223. * @return an enumeration of the init parameters from the servlet module
  224. * descriptor.
  225. */
  226. public Enumeration getInitParameterNames()
  227. {
  228. return Collections.enumeration(initParams.keySet());
  229. }
  230. // @Override
  231. // public boolean setInitParameter(String s, String s1)
  232. // {
  233. // throw new UnsupportedOperationException("Not implemented");
  234. // }
  235. /**
  236. * @return the resource from the plugin classloader if it exists, otherwise the
  237. * resource is looked up from the wrapped context and returned
  238. */
  239. public URL getResource(String path) throws MalformedURLException
  240. {
  241. URL url = plugin.getResource(path);
  242. return url;
  243. }
  244. /**
  245. * @return the resource stream from the plugin classloader if it exists, otherwise
  246. * the resource stream is attempted to be retrieved from the wrapped context
  247. */
  248. public InputStream getResourceAsStream(String path)
  249. {
  250. InputStream in = plugin.getResourceAsStream(path);
  251. return in;
  252. }
  253. /**
  254. * @return null so that servlet plugins can't escape their box
  255. */
  256. public ServletContext getContext(String uripath)
  257. {
  258. return null;
  259. }
  260. @Override
  261. public int getMajorVersion()
  262. {
  263. return context.getMajorVersion();
  264. }
  265. @Override
  266. public int getMinorVersion()
  267. {
  268. return context.getMinorVersion();
  269. }
  270. // @Override
  271. // public int getEffectiveMajorVersion()
  272. // {
  273. // return context.getEffectiveMajorVersion();
  274. // }
  275. //
  276. // @Override
  277. // public int getEffectiveMinorVersion()
  278. // {
  279. // return context.getEffectiveMinorVersion();
  280. // }
  281. @Override
  282. public String getMimeType(String s)
  283. {
  284. return context.getMimeType(s);
  285. }
  286. public String getContextPath() {
  287. return contextPath;
  288. }
  289. public RequestDispatcher getNamedDispatcher(String name)
  290. {
  291. throw new UnsupportedOperationException("Not implemented");
  292. }
  293. public String getRealPath(String path)
  294. {
  295. throw new UnsupportedOperationException("Not implemented");
  296. }
  297. @Override
  298. public String getServerInfo()
  299. {
  300. return context.getServerInfo();
  301. }
  302. public RequestDispatcher getRequestDispatcher(String path)
  303. {
  304. throw new UnsupportedOperationException("Not implemented");
  305. }
  306. public Set getResourcePaths(String arg0)
  307. {
  308. throw new UnsupportedOperationException("Not implemented");
  309. }
  310. public Servlet getServlet(String name) throws ServletException
  311. {
  312. return null;
  313. }
  314. public Enumeration getServletNames()
  315. {
  316. throw new UnsupportedOperationException("Not implemented");
  317. }
  318. @Override
  319. public void log(String s)
  320. {
  321. context.log(s);
  322. }
  323. @Override
  324. public void log(Exception e, String s)
  325. {
  326. context.log(e, s);
  327. }
  328. @Override
  329. public void log(String s, Throwable throwable)
  330. {
  331. context.log(s, throwable);
  332. }
  333. public Enumeration getServlets()
  334. {
  335. throw new UnsupportedOperationException("Not implemented");
  336. }
  337. //---- All methods below simply delegate to the wrapped servlet context ----
  338. }