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

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