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.

PortletApplicationContext2.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.server;
  5. import java.io.File;
  6. import java.io.Serializable;
  7. import java.net.URL;
  8. import java.util.HashMap;
  9. import java.util.LinkedHashSet;
  10. import java.util.Map;
  11. import java.util.Set;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.portlet.ActionRequest;
  15. import javax.portlet.ActionResponse;
  16. import javax.portlet.EventRequest;
  17. import javax.portlet.EventResponse;
  18. import javax.portlet.MimeResponse;
  19. import javax.portlet.PortletConfig;
  20. import javax.portlet.PortletMode;
  21. import javax.portlet.PortletModeException;
  22. import javax.portlet.PortletResponse;
  23. import javax.portlet.PortletSession;
  24. import javax.portlet.PortletURL;
  25. import javax.portlet.RenderRequest;
  26. import javax.portlet.RenderResponse;
  27. import javax.portlet.ResourceRequest;
  28. import javax.portlet.ResourceResponse;
  29. import javax.portlet.ResourceURL;
  30. import javax.portlet.StateAwareResponse;
  31. import javax.servlet.http.HttpSessionBindingListener;
  32. import javax.xml.namespace.QName;
  33. import com.vaadin.Application;
  34. import com.vaadin.terminal.ApplicationResource;
  35. import com.vaadin.terminal.ExternalResource;
  36. import com.vaadin.ui.Window;
  37. /**
  38. * TODO Write documentation, fix JavaDoc tags.
  39. *
  40. * This is automatically registered as a {@link HttpSessionBindingListener} when
  41. * {@link PortletSession#setAttribute()} is called with the context as value.
  42. *
  43. * @author peholmst
  44. */
  45. @SuppressWarnings("serial")
  46. public class PortletApplicationContext2 extends AbstractWebApplicationContext {
  47. private static final Logger logger = Logger
  48. .getLogger(PortletApplicationContext2.class.getName());
  49. protected Map<Application, Set<PortletListener>> portletListeners = new HashMap<Application, Set<PortletListener>>();
  50. protected transient PortletSession session;
  51. protected transient PortletConfig portletConfig;
  52. protected HashMap<String, Application> portletWindowIdToApplicationMap = new HashMap<String, Application>();
  53. private PortletResponse response;
  54. private final Map<String, QName> eventActionDestinationMap = new HashMap<String, QName>();
  55. private final Map<String, Serializable> eventActionValueMap = new HashMap<String, Serializable>();
  56. private final Map<String, String> sharedParameterActionNameMap = new HashMap<String, String>();
  57. private final Map<String, String> sharedParameterActionValueMap = new HashMap<String, String>();
  58. public File getBaseDirectory() {
  59. String resultPath = session.getPortletContext().getRealPath("/");
  60. if (resultPath != null) {
  61. return new File(resultPath);
  62. } else {
  63. try {
  64. final URL url = session.getPortletContext().getResource("/");
  65. return new File(url.getFile());
  66. } catch (final Exception e) {
  67. // FIXME: Handle exception
  68. logger.log(
  69. Level.FINE,
  70. "Cannot access base directory, possible security issue "
  71. + "with Application Server or Servlet Container",
  72. e);
  73. }
  74. }
  75. return null;
  76. }
  77. protected PortletCommunicationManager getApplicationManager(
  78. Application application) {
  79. PortletCommunicationManager mgr = (PortletCommunicationManager) applicationToAjaxAppMgrMap
  80. .get(application);
  81. if (mgr == null) {
  82. // Creates a new manager
  83. mgr = new PortletCommunicationManager(application);
  84. applicationToAjaxAppMgrMap.put(application, mgr);
  85. }
  86. return mgr;
  87. }
  88. public static PortletApplicationContext2 getApplicationContext(
  89. PortletSession session) {
  90. PortletApplicationContext2 cx = (PortletApplicationContext2) session
  91. .getAttribute(PortletApplicationContext2.class.getName());
  92. if (cx == null) {
  93. cx = new PortletApplicationContext2();
  94. session.setAttribute(PortletApplicationContext2.class.getName(), cx);
  95. }
  96. if (cx.session == null) {
  97. cx.session = session;
  98. }
  99. return cx;
  100. }
  101. @Override
  102. protected void removeApplication(Application application) {
  103. super.removeApplication(application);
  104. // values() is backed by map, removes the key-value pair from the map
  105. portletWindowIdToApplicationMap.values().remove(application);
  106. }
  107. protected void addApplication(Application application,
  108. String portletWindowId) {
  109. applications.add(application);
  110. portletWindowIdToApplicationMap.put(portletWindowId, application);
  111. }
  112. public Application getApplicationForWindowId(String portletWindowId) {
  113. return portletWindowIdToApplicationMap.get(portletWindowId);
  114. }
  115. public PortletSession getPortletSession() {
  116. return session;
  117. }
  118. public PortletConfig getPortletConfig() {
  119. return portletConfig;
  120. }
  121. public void setPortletConfig(PortletConfig config) {
  122. portletConfig = config;
  123. }
  124. public void addPortletListener(Application app, PortletListener listener) {
  125. Set<PortletListener> l = portletListeners.get(app);
  126. if (l == null) {
  127. l = new LinkedHashSet<PortletListener>();
  128. portletListeners.put(app, l);
  129. }
  130. l.add(listener);
  131. }
  132. public void removePortletListener(Application app, PortletListener listener) {
  133. Set<PortletListener> l = portletListeners.get(app);
  134. if (l != null) {
  135. l.remove(listener);
  136. }
  137. }
  138. public void firePortletRenderRequest(Application app, Window window,
  139. RenderRequest request, RenderResponse response) {
  140. Set<PortletListener> listeners = portletListeners.get(app);
  141. if (listeners != null) {
  142. for (PortletListener l : listeners) {
  143. l.handleRenderRequest(request, new RestrictedRenderResponse(
  144. response), window);
  145. }
  146. }
  147. }
  148. public void firePortletActionRequest(Application app, Window window,
  149. ActionRequest request, ActionResponse response) {
  150. String key = request.getParameter(ActionRequest.ACTION_NAME);
  151. if (eventActionDestinationMap.containsKey(key)) {
  152. // this action request is only to send queued portlet events
  153. response.setEvent(eventActionDestinationMap.get(key),
  154. eventActionValueMap.get(key));
  155. // cleanup
  156. eventActionDestinationMap.remove(key);
  157. eventActionValueMap.remove(key);
  158. } else if (sharedParameterActionNameMap.containsKey(key)) {
  159. // this action request is only to set shared render parameters
  160. response.setRenderParameter(sharedParameterActionNameMap.get(key),
  161. sharedParameterActionValueMap.get(key));
  162. // cleanup
  163. sharedParameterActionNameMap.remove(key);
  164. sharedParameterActionValueMap.remove(key);
  165. } else {
  166. // normal action request, notify listeners
  167. Set<PortletListener> listeners = portletListeners.get(app);
  168. if (listeners != null) {
  169. for (PortletListener l : listeners) {
  170. l.handleActionRequest(request, response, window);
  171. }
  172. }
  173. }
  174. }
  175. public void firePortletEventRequest(Application app, Window window,
  176. EventRequest request, EventResponse response) {
  177. Set<PortletListener> listeners = portletListeners.get(app);
  178. if (listeners != null) {
  179. for (PortletListener l : listeners) {
  180. l.handleEventRequest(request, response, window);
  181. }
  182. }
  183. }
  184. public void firePortletResourceRequest(Application app, Window window,
  185. ResourceRequest request, ResourceResponse response) {
  186. Set<PortletListener> listeners = portletListeners.get(app);
  187. if (listeners != null) {
  188. for (PortletListener l : listeners) {
  189. l.handleResourceRequest(request, response, window);
  190. }
  191. }
  192. }
  193. public interface PortletListener extends Serializable {
  194. public void handleRenderRequest(RenderRequest request,
  195. RenderResponse response, Window window);
  196. public void handleActionRequest(ActionRequest request,
  197. ActionResponse response, Window window);
  198. public void handleEventRequest(EventRequest request,
  199. EventResponse response, Window window);
  200. public void handleResourceRequest(ResourceRequest request,
  201. ResourceResponse response, Window window);
  202. }
  203. /**
  204. * This is for use by {@link AbstractApplicationPortlet} only.
  205. *
  206. * TODO cleaner implementation, now "semi-static"!
  207. *
  208. * @param mimeResponse
  209. */
  210. void setResponse(PortletResponse response) {
  211. this.response = response;
  212. }
  213. @Override
  214. public String generateApplicationResourceURL(ApplicationResource resource,
  215. String mapKey) {
  216. if (response instanceof MimeResponse) {
  217. ResourceURL resourceURL = ((MimeResponse) response)
  218. .createResourceURL();
  219. final String filename = resource.getFilename();
  220. if (filename == null) {
  221. resourceURL.setResourceID("APP/" + mapKey + "/");
  222. } else {
  223. resourceURL.setResourceID("APP/" + mapKey + "/" + filename);
  224. }
  225. return resourceURL.toString();
  226. } else {
  227. // in a background thread or otherwise outside a request
  228. // TODO exception ??
  229. return null;
  230. }
  231. }
  232. /**
  233. * Creates a new action URL.
  234. *
  235. * @param action
  236. * @return action URL or null if called outside a MimeRequest (outside a
  237. * UIDL request or similar)
  238. */
  239. public PortletURL generateActionURL(String action) {
  240. PortletURL url = null;
  241. if (response instanceof MimeResponse) {
  242. url = ((MimeResponse) response).createActionURL();
  243. url.setParameter("javax.portlet.action", action);
  244. } else {
  245. return null;
  246. }
  247. return url;
  248. }
  249. /**
  250. * Sends a portlet event to the indicated destination.
  251. *
  252. * Internally, an action may be created and opened, as an event cannot be
  253. * sent directly from all types of requests.
  254. *
  255. * The event destinations and values need to be kept in the context until
  256. * sent. Any memory leaks if the action fails are limited to the session.
  257. *
  258. * Event names for events sent and received by a portlet need to be declared
  259. * in portlet.xml .
  260. *
  261. * @param window
  262. * a window in which a temporary action URL can be opened if
  263. * necessary
  264. * @param name
  265. * event name
  266. * @param value
  267. * event value object that is Serializable and, if appropriate,
  268. * has a valid JAXB annotation
  269. */
  270. public void sendPortletEvent(Window window, QName name, Serializable value)
  271. throws IllegalStateException {
  272. if (response instanceof MimeResponse) {
  273. String actionKey = "" + System.currentTimeMillis();
  274. while (eventActionDestinationMap.containsKey(actionKey)) {
  275. actionKey = actionKey + ".";
  276. }
  277. PortletURL actionUrl = generateActionURL(actionKey);
  278. if (actionUrl != null) {
  279. eventActionDestinationMap.put(actionKey, name);
  280. eventActionValueMap.put(actionKey, value);
  281. window.open(new ExternalResource(actionUrl.toString()));
  282. } else {
  283. // this should never happen as we already know the response is a
  284. // MimeResponse
  285. throw new IllegalStateException(
  286. "Portlet events can only be sent from a portlet request");
  287. }
  288. } else if (response instanceof StateAwareResponse) {
  289. ((StateAwareResponse) response).setEvent(name, value);
  290. } else {
  291. throw new IllegalStateException(
  292. "Portlet events can only be sent from a portlet request");
  293. }
  294. }
  295. /**
  296. * Sets a shared portlet parameter.
  297. *
  298. * Internally, an action may be created and opened, as shared parameters
  299. * cannot be set directly from all types of requests.
  300. *
  301. * The parameters and values need to be kept in the context until sent. Any
  302. * memory leaks if the action fails are limited to the session.
  303. *
  304. * Shared parameters set or read by a portlet need to be declared in
  305. * portlet.xml .
  306. *
  307. * @param window
  308. * a window in which a temporary action URL can be opened if
  309. * necessary
  310. * @param name
  311. * parameter identifier
  312. * @param value
  313. * parameter value
  314. */
  315. public void setSharedRenderParameter(Window window, String name,
  316. String value) throws IllegalStateException {
  317. if (response instanceof MimeResponse) {
  318. String actionKey = "" + System.currentTimeMillis();
  319. while (sharedParameterActionNameMap.containsKey(actionKey)) {
  320. actionKey = actionKey + ".";
  321. }
  322. PortletURL actionUrl = generateActionURL(actionKey);
  323. if (actionUrl != null) {
  324. sharedParameterActionNameMap.put(actionKey, name);
  325. sharedParameterActionValueMap.put(actionKey, value);
  326. window.open(new ExternalResource(actionUrl.toString()));
  327. } else {
  328. // this should never happen as we already know the response is a
  329. // MimeResponse
  330. throw new IllegalStateException(
  331. "Shared parameters can only be set from a portlet request");
  332. }
  333. } else if (response instanceof StateAwareResponse) {
  334. ((StateAwareResponse) response).setRenderParameter(name, value);
  335. } else {
  336. throw new IllegalStateException(
  337. "Shared parameters can only be set from a portlet request");
  338. }
  339. }
  340. /**
  341. * Sets the portlet mode. This may trigger a new render request.
  342. *
  343. * Portlet modes used by a portlet need to be declared in portlet.xml .
  344. *
  345. * @param window
  346. * a window in which the render URL can be opened if necessary
  347. * @param portletMode
  348. * the portlet mode to switch to
  349. * @throws PortletModeException
  350. * if the portlet mode is not allowed for some reason
  351. * (configuration, permissions etc.)
  352. */
  353. public void setPortletMode(Window window, PortletMode portletMode)
  354. throws IllegalStateException, PortletModeException {
  355. if (response instanceof MimeResponse) {
  356. PortletURL url = ((MimeResponse) response).createRenderURL();
  357. url.setPortletMode(portletMode);
  358. window.open(new ExternalResource(url.toString()));
  359. } else if (response instanceof StateAwareResponse) {
  360. ((StateAwareResponse) response).setPortletMode(portletMode);
  361. } else {
  362. throw new IllegalStateException(
  363. "Portlet mode can only be changed from a portlet request");
  364. }
  365. }
  366. }