--- /dev/null
+/**\r
+ * \r
+ */\r
+package com.itmill.tookit.demo;\r
+\r
+import javax.portlet.ActionRequest;\r
+import javax.portlet.ActionResponse;\r
+import javax.portlet.PortletMode;\r
+import javax.portlet.PortletURL;\r
+import javax.portlet.RenderRequest;\r
+import javax.portlet.RenderResponse;\r
+import javax.portlet.WindowState;\r
+\r
+import com.itmill.toolkit.Application;\r
+import com.itmill.toolkit.terminal.ExternalResource;\r
+import com.itmill.toolkit.terminal.gwt.server.PortletApplicationContext;\r
+import com.itmill.toolkit.terminal.gwt.server.PortletApplicationContext.PortletListener;\r
+import com.itmill.toolkit.ui.Link;\r
+import com.itmill.toolkit.ui.TextField;\r
+import com.itmill.toolkit.ui.Window;\r
+import com.itmill.toolkit.ui.Window.Notification;\r
+\r
+/**\r
+ * @author marc\r
+ * \r
+ */\r
+public class PortletDemo extends Application {\r
+\r
+ Window main = new Window();\r
+ TextField tf = new TextField();\r
+ Link portletEdit = new Link();\r
+ Link portletMax = new Link();\r
+\r
+ public void init() {\r
+ main = new Window();\r
+ tf.setEnabled(false);\r
+ main.addComponent(tf);\r
+\r
+ portletEdit.setCaption("Portlet edit/view");\r
+ portletEdit.setEnabled(false);\r
+ main.addComponent(portletEdit);\r
+ portletMax.setCaption("Maximize/normal portlet");\r
+ portletMax.setEnabled(false);\r
+ main.addComponent(portletMax);\r
+\r
+ PortletApplicationContext ctx = (PortletApplicationContext) getContext();\r
+\r
+ ctx.addPortletListener(this, new DemoPortletListener());\r
+ }\r
+\r
+ private class DemoPortletListener implements PortletListener {\r
+\r
+ public void handleActionRequest(ActionRequest request,\r
+ ActionResponse response) {\r
+\r
+ getMainWindow().showNotification("Action received");\r
+\r
+ }\r
+\r
+ public void handleRenderRequest(RenderRequest request,\r
+ RenderResponse response) {\r
+ getMainWindow().showNotification(\r
+ "Portlet status",\r
+ "mode: " + request.getPortletMode() + "<br/> state: "\r
+ + request.getWindowState(),\r
+ Notification.TYPE_TRAY_NOTIFICATION);\r
+\r
+ PortletURL url = response.createActionURL();\r
+ try {\r
+ url\r
+ .setPortletMode((request.getPortletMode() == PortletMode.VIEW ? PortletMode.EDIT\r
+ : PortletMode.VIEW));\r
+ portletEdit.setResource(new ExternalResource(url.toString()));\r
+ } catch (Exception e) {\r
+ portletEdit.setEnabled(false);\r
+ }\r
+\r
+ url = response.createActionURL();\r
+ try {\r
+ url\r
+ .setWindowState((request.getWindowState() == WindowState.NORMAL ? WindowState.MAXIMIZED\r
+ : WindowState.NORMAL));\r
+ portletEdit.setResource(new ExternalResource(url.toString()));\r
+ } catch (Exception e) {\r
+ portletEdit.setEnabled(false);\r
+ }\r
+\r
+ }\r
+ }\r
+}\r
\r
import java.io.IOException;\r
import java.io.PrintWriter;\r
-import java.util.Map;\r
\r
import javax.portlet.ActionRequest;\r
import javax.portlet.ActionResponse;\r
import javax.portlet.Portlet;\r
import javax.portlet.PortletConfig;\r
import javax.portlet.PortletException;\r
-import javax.portlet.PortletMode;\r
-import javax.portlet.PortletModeException;\r
-import javax.portlet.PortletPreferences;\r
-import javax.portlet.PortletRequest;\r
import javax.portlet.PortletRequestDispatcher;\r
import javax.portlet.PortletSession;\r
-import javax.portlet.PortletURL;\r
import javax.portlet.RenderRequest;\r
import javax.portlet.RenderResponse;\r
\r
import com.itmill.toolkit.Application;\r
-import com.itmill.toolkit.terminal.gwt.server.PortletApplicationContext.PortletInfo;\r
-import com.itmill.toolkit.terminal.gwt.server.PortletApplicationContext.PortletInfoReceiver;\r
\r
public class ApplicationPortlet implements Portlet {\r
// The application to show\r
- protected String app = "Calc"; // empty for root\r
- // theme to use for the application\r
- protected String theme = "default";\r
+ protected String app = "PortletDemo";\r
// some applications might require that the height is specified\r
protected String height = null; // e.g "200px"\r
\r
- PortletConfig config;\r
+ private PortletConfig config;\r
\r
public void destroy() {\r
config = null;\r
\r
public void init(PortletConfig config) throws PortletException {\r
this.config = config;\r
+ app = config.getInitParameter("application");\r
+ if (app == null) {\r
+ app = "PortalDemo";\r
+ }\r
+ height = config.getInitParameter("height");\r
}\r
\r
public void processAction(ActionRequest request, ActionResponse response)\r
throws PortletException, IOException {\r
- // Update preferences (configured)\r
- PortletPreferences prefs = request.getPreferences();\r
- app = request.getParameter("app");\r
- if (app != null && app.length() > 0) {\r
- prefs.setValue("application", app);\r
- } else {\r
- app = null;\r
- }\r
- String theme = request.getParameter("theme");\r
- if (theme != null && theme.length() > 0) {\r
- prefs.setValue("theme", theme);\r
- } else {\r
- prefs.setValue("theme", null);\r
- }\r
- String height = request.getParameter("height");\r
- if (height != null && height.length() > 0) {\r
- prefs.setValue("height", height);\r
- } else {\r
- prefs.setValue("height", null);\r
- }\r
- prefs.store();\r
+ PortletApplicationContext.dispatchRequest(this, request, response);\r
}\r
\r
public void render(RenderRequest request, RenderResponse response)\r
throws PortletException, IOException {\r
\r
+ /*-\r
PortletPreferences prefs = request.getPreferences();\r
app = prefs.getValue("application", app);\r
theme = prefs.getValue("theme", "default");\r
height = prefs.getValue("height", null);\r
+ -*/\r
\r
// display the IT Mill Toolkit application\r
writeAjaxWindow(request, response);\r
response.setContentType("text/html");\r
PrintWriter out = response.getWriter();\r
\r
- // TODO check user == admin\r
- if (app == null) {\r
- // Display the configuration UI\r
- PortletURL submitUrl = response.createActionURL();\r
- try {\r
- submitUrl.setPortletMode(PortletMode.VIEW);\r
- } catch (PortletModeException e) {\r
- // Fine\r
- }\r
- out.println("<form method='POST' action='" + submitUrl + "'>");\r
- out.println("Application:");\r
- out.println(request.getContextPath() + "/");\r
- out.println("<input size='40' type='text' name='app' value='"\r
- + (app != null ? app : "") + "'>");\r
- out.println(" Theme:<input type='text' name='theme' value='"\r
- + theme + "'><br/>");\r
- out\r
- .println("Force height (optional, e.g \"200px\"): <input type='text' name='height' value='"\r
- + (height != null ? height : "") + "'><br/>");\r
- out.println("<input type='submit' value='Save'>");\r
- out.println("</form>");\r
- } else {\r
+ if (app != null) {\r
\r
PortletSession sess = request.getPortletSession();\r
PortletApplicationContext ctx = PortletApplicationContext\r
.getApplicationContext(sess);\r
\r
+ /*- TODO store som info somewhere?\r
PortletInfo pi = ctx.setPortletInfo(request.getContextPath() + "/"\r
+ app + (app.endsWith("/") ? "" : "/"), request\r
.getPortletMode(), request.getWindowState(), request\r
.getUserPrincipal(), (Map) request\r
.getAttribute(PortletRequest.USER_INFO), config);\r
+ -*/\r
\r
PortletRequestDispatcher dispatcher = sess.getPortletContext()\r
.getRequestDispatcher("/" + app);\r
\r
try {\r
+ // TODO height\r
dispatcher.include(request, response);\r
} catch (PortletException e) {\r
out.print("<h1>Servlet include failed!</h1>");\r
out.print("<div>" + e + "</div>");\r
+ ctx.setPortletApplication(this, null);\r
return;\r
}\r
\r
- Object app = request.getAttribute(Application.class.getName());\r
- if (app instanceof PortletInfoReceiver) {\r
- ((PortletInfoReceiver) app).receivePortletInfo(pi);\r
- }\r
+ Application app = (Application) request\r
+ .getAttribute(Application.class.getName());\r
+ ctx.setPortletApplication(this, app);\r
+ ctx.firePortletRenderRequest(this, request, response);\r
+\r
}\r
}\r
\r
*/\r
package com.itmill.toolkit.terminal.gwt.server;\r
\r
-import java.io.File;\r
-import java.security.Principal;\r
-import java.util.Collection;\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+import java.io.PrintWriter;\r
import java.util.HashMap;\r
+import java.util.Iterator;\r
+import java.util.LinkedHashSet;\r
+import java.util.Locale;\r
import java.util.Map;\r
+import java.util.Set;\r
\r
-import javax.portlet.PortletConfig;\r
-import javax.portlet.PortletMode;\r
+import javax.portlet.ActionRequest;\r
+import javax.portlet.ActionResponse;\r
+import javax.portlet.Portlet;\r
import javax.portlet.PortletSession;\r
-import javax.portlet.WindowState;\r
+import javax.portlet.PortletURL;\r
+import javax.portlet.RenderRequest;\r
+import javax.portlet.RenderResponse;\r
import javax.servlet.http.HttpSession;\r
\r
import com.itmill.toolkit.Application;\r
-import com.itmill.toolkit.service.ApplicationContext;\r
\r
/**\r
* @author marc\r
* \r
*/\r
-public class PortletApplicationContext implements ApplicationContext {\r
+public class PortletApplicationContext extends WebApplicationContext {\r
\r
- private final PortletSession session;\r
+ protected PortletSession portletSession;\r
\r
- private final Map portletInfoMap;\r
+ protected Map portletListeners = new HashMap();\r
+\r
+ protected Map portletToApplication = new HashMap();\r
+\r
+ PortletApplicationContext() {\r
\r
- PortletApplicationContext(PortletSession session) {\r
- this.session = session;\r
- portletInfoMap = new HashMap();\r
}\r
\r
static public PortletApplicationContext getApplicationContext(\r
PortletSession session) {\r
- PortletApplicationContext cx = (PortletApplicationContext) session\r
- .getAttribute(PortletApplicationContext.class.getName());\r
+ WebApplicationContext cx = (WebApplicationContext) session\r
+ .getAttribute(WebApplicationContext.class.getName(),\r
+ PortletSession.APPLICATION_SCOPE);\r
if (cx == null) {\r
- cx = new PortletApplicationContext(session);\r
- session.setAttribute(PortletApplicationContext.class.getName(), cx,\r
- PortletSession.APPLICATION_SCOPE);\r
+ cx = new PortletApplicationContext();\r
}\r
- return cx;\r
+ if (!(cx instanceof PortletApplicationContext)) {\r
+ // TODO Should we even try this? And should we leave original as-is?\r
+ PortletApplicationContext pcx = new PortletApplicationContext();\r
+ pcx.applications.addAll(cx.applications);\r
+ cx.applications.clear();\r
+ pcx.browser = cx.browser;\r
+ cx.browser = null;\r
+ pcx.listeners = cx.listeners;\r
+ cx.listeners = null;\r
+ pcx.session = cx.session;\r
+ cx = pcx;\r
+ }\r
+ if (((PortletApplicationContext) cx).portletSession == null) {\r
+ ((PortletApplicationContext) cx).portletSession = session;\r
+ }\r
+ session.setAttribute(WebApplicationContext.class.getName(), cx,\r
+ PortletSession.APPLICATION_SCOPE);\r
+ return (PortletApplicationContext) cx;\r
}\r
\r
- static public PortletApplicationContext getApplicationContext(\r
+ static public WebApplicationContext getApplicationContext(\r
HttpSession session) {\r
- PortletApplicationContext cx = (PortletApplicationContext) session\r
- .getAttribute(PortletApplicationContext.class.getName());\r
+ WebApplicationContext cx = (WebApplicationContext) session\r
+ .getAttribute(WebApplicationContext.class.getName());\r
+ if (cx == null) {\r
+ cx = new PortletApplicationContext();\r
+ }\r
+ if (cx.session == null) {\r
+ cx.session = session;\r
+ }\r
+ session.setAttribute(WebApplicationContext.class.getName(), cx);\r
return cx;\r
}\r
\r
+ protected void removeApplication(Application application) {\r
+ portletListeners.remove(application);\r
+ for (Iterator it = portletToApplication.keySet().iterator(); it\r
+ .hasNext();) {\r
+ Object key = it.next();\r
+ if (key == application) {\r
+ portletToApplication.remove(key);\r
+ }\r
+ }\r
+ super.removeApplication(application);\r
+ }\r
+\r
+ public boolean equals(Object obj) {\r
+ if (portletSession == null) {\r
+ return super.equals(obj);\r
+ }\r
+ return portletSession.equals(obj);\r
+ }\r
+\r
+ public int hashCode() {\r
+ if (portletSession == null) {\r
+ return super.hashCode();\r
+ }\r
+ return portletSession.hashCode();\r
+ }\r
+\r
+ public void setPortletApplication(Portlet portlet, Application app) {\r
+ portletToApplication.put(portlet, app);\r
+ }\r
+\r
+ public Application getPortletApplication(Portlet portlet) {\r
+ return (Application) portletToApplication.get(portlet);\r
+ }\r
+\r
public PortletSession getPortletSession() {\r
- return session;\r
+ return portletSession;\r
}\r
\r
- /*\r
- * (non-Javadoc)\r
- * \r
- * @see com.itmill.toolkit.service.ApplicationContext#addTransactionListener(com.itmill.toolkit.service.ApplicationContext.TransactionListener)\r
- */\r
- public void addTransactionListener(TransactionListener listener) {\r
- WebApplicationContext cx = (WebApplicationContext) session\r
- .getAttribute(WebApplicationContext.class.getName());\r
- if (cx != null) {\r
- cx.addTransactionListener(listener);\r
+ public void addPortletListener(Application app, PortletListener listener) {\r
+ Set l = (Set) portletListeners.get(app);\r
+ if (l == null) {\r
+ l = new LinkedHashSet();\r
+ portletListeners.put(app, l);\r
}\r
+ l.add(listener);\r
}\r
\r
- /*\r
- * (non-Javadoc)\r
- * \r
- * @see com.itmill.toolkit.service.ApplicationContext#getApplications()\r
- */\r
- public Collection getApplications() {\r
- WebApplicationContext cx = (WebApplicationContext) session\r
- .getAttribute(WebApplicationContext.class.getName());\r
- if (cx != null) {\r
- return cx.getApplications();\r
+ public void removePortletListener(Application app, PortletListener listener) {\r
+ Set l = (Set) portletListeners.get(app);\r
+ if (l != null) {\r
+ l.remove(listener);\r
}\r
- return null;\r
}\r
\r
- /*\r
- * (non-Javadoc)\r
- * \r
- * @see com.itmill.toolkit.service.ApplicationContext#getBaseDirectory()\r
- */\r
- public File getBaseDirectory() {\r
- WebApplicationContext cx = (WebApplicationContext) session\r
- .getAttribute(WebApplicationContext.class.getName());\r
- if (cx != null) {\r
- return cx.getBaseDirectory();\r
+ public static void dispatchRequest(Portlet portlet, RenderRequest request,\r
+ RenderResponse response) {\r
+ PortletApplicationContext ctx = getApplicationContext(request\r
+ .getPortletSession());\r
+ if (ctx != null) {\r
+ ctx.firePortletRenderRequest(portlet, request, response);\r
}\r
- return null;\r
}\r
\r
- /*\r
- * (non-Javadoc)\r
- * \r
- * @see com.itmill.toolkit.service.ApplicationContext#removeTransactionListener(com.itmill.toolkit.service.ApplicationContext.TransactionListener)\r
- */\r
- public void removeTransactionListener(TransactionListener listener) {\r
- WebApplicationContext cx = (WebApplicationContext) session\r
- .getAttribute(WebApplicationContext.class.getName());\r
- if (cx != null) {\r
- cx.removeTransactionListener(listener);\r
+ public static void dispatchRequest(Portlet portlet, ActionRequest request,\r
+ ActionResponse response) {\r
+ PortletApplicationContext ctx = getApplicationContext(request\r
+ .getPortletSession());\r
+ if (ctx != null) {\r
+ ctx.firePortletActionRequest(portlet, request, response);\r
}\r
- }\r
+ }\r
\r
- PortletInfo setPortletInfo(String path, PortletMode mode,\r
- WindowState state, Principal userPrincipal, Map userInfo,\r
- PortletConfig config) {\r
- System.err.println("SETTING PI: " + path);\r
- PortletInfo pi = (PortletInfo) portletInfoMap.get(path);\r
- if (pi == null) {\r
- pi = new PortletInfo(mode, state, userPrincipal, userInfo, config);\r
- portletInfoMap.put(path, pi);\r
- } else {\r
- pi.setInfo(mode, state, userPrincipal, userInfo, config);\r
+ public void firePortletRenderRequest(Portlet portlet,\r
+ RenderRequest request, RenderResponse response) {\r
+ Application app = getPortletApplication(portlet);\r
+ Set listeners = (Set) portletListeners.get(app);\r
+ if (listeners != null) {\r
+ for (Iterator it = listeners.iterator(); it.hasNext();) {\r
+ PortletListener l = (PortletListener) it.next();\r
+ l.handleRenderRequest(request, new RestrictedRenderResponse(\r
+ response));\r
+ }\r
}\r
- return pi;\r
}\r
\r
- public PortletInfo getPortletInfo(Application app) {\r
- if (app != null && app.getURL() != null) {\r
- // TODO remove\r
- System.err.println("GETTING PI: " + app.getURL().getPath());\r
- return (PortletInfo) portletInfoMap.get(app.getURL().getPath());\r
+ public void firePortletActionRequest(Portlet portlet,\r
+ ActionRequest request, ActionResponse response) {\r
+ Application app = getPortletApplication(portlet);\r
+ Set listeners = (Set) portletListeners.get(app);\r
+ if (listeners != null) {\r
+ for (Iterator it = listeners.iterator(); it.hasNext();) {\r
+ PortletListener l = (PortletListener) it.next();\r
+ l.handleActionRequest(request, response);\r
+ }\r
}\r
- return null;\r
}\r
\r
- public class PortletInfo {\r
+ public interface PortletListener {\r
+ public void handleRenderRequest(RenderRequest request,\r
+ RenderResponse response);\r
+\r
+ public void handleActionRequest(ActionRequest request,\r
+ ActionResponse response);\r
+ }\r
+\r
+ private class RestrictedRenderResponse implements RenderResponse {\r
\r
- PortletMode mode;\r
- WindowState state;\r
- Principal userPrincipal;\r
- Map userInfo;\r
- PortletConfig config;\r
+ private RenderResponse response;\r
\r
- public PortletInfo(PortletMode mode, WindowState state,\r
- Principal userPrincipal, Map userInfo, PortletConfig config) {\r
- this.mode = mode;\r
- this.state = state;\r
- this.userPrincipal = userPrincipal;\r
- this.userInfo = userInfo;\r
- this.config = config;\r
+ private RestrictedRenderResponse(RenderResponse response) {\r
+ this.response = response;\r
}\r
\r
- private void setInfo(PortletMode mode, WindowState state,\r
- Principal userPrincipal, Map userInfo, PortletConfig config) {\r
- this.mode = mode;\r
- this.state = state;\r
- this.userPrincipal = userPrincipal;\r
- this.userInfo = userInfo;\r
- this.config = config;\r
+ public void addProperty(String key, String value) {\r
+ response.addProperty(key, value);\r
}\r
\r
- /**\r
- * Gets the current portlet mode, VIEW / EDIT / HELP\r
- * \r
- * @return the current portlet mode\r
- */\r
- public PortletMode getPortletMode() {\r
- return mode;\r
+ public PortletURL createActionURL() {\r
+ return response.createActionURL();\r
}\r
\r
- /**\r
- * Gets the current window state, NORMAL / MAXIMIZED / MINIMIZED\r
- * \r
- * @return the current window state\r
- */\r
- public WindowState getWindowState() {\r
- return state;\r
+ public PortletURL createRenderURL() {\r
+ return response.createRenderURL();\r
}\r
\r
- /**\r
- * Gets the current UserPrincipal\r
- * \r
- * @return current UserPrincipal, null if not logged in\r
- */\r
- public Principal getUserPrincipal() {\r
- return userPrincipal;\r
+ public String encodeURL(String path) {\r
+ return response.encodeURL(path);\r
}\r
\r
- /**\r
- * Gets the PortletConfig for this portlet\r
- * \r
- * @return the PortletConfig\r
- */\r
- public PortletConfig getConfig() {\r
- return config;\r
+ public void flushBuffer() throws IOException {\r
+ // NOP\r
+ // TODO throw?\r
}\r
\r
- /**\r
- * Gets the user info for this portlet, as retreived from\r
- * request.getAttribute(PortletRequest.USER_INFO);\r
- * \r
- * @return the user info Map\r
- */\r
- public Map getUserInfo() {\r
- return userInfo;\r
+ public int getBufferSize() {\r
+ return response.getBufferSize();\r
}\r
\r
- public String toString() {\r
- return "PortletMode: " + getPortletMode() + " WindowState: "\r
- + getWindowState() + " UserPrincipal: "\r
- + getUserPrincipal() + " User info: " + getUserInfo();\r
+ public String getCharacterEncoding() {\r
+ return response.getCharacterEncoding();\r
+ }\r
+\r
+ public String getContentType() {\r
+ return response.getContentType();\r
+ }\r
+\r
+ public Locale getLocale() {\r
+ return response.getLocale();\r
+ }\r
+\r
+ public String getNamespace() {\r
+ return response.getNamespace();\r
+ }\r
+\r
+ public OutputStream getPortletOutputStream() throws IOException {\r
+ // write forbidden\r
+ return null;\r
+ }\r
+\r
+ public PrintWriter getWriter() throws IOException {\r
+ // write forbidden\r
+ return null;\r
+ }\r
+\r
+ public boolean isCommitted() {\r
+ return response.isCommitted();\r
+ }\r
+\r
+ public void reset() {\r
+ // NOP\r
+ // TODO throw?\r
+ }\r
+\r
+ public void resetBuffer() {\r
+ // NOP\r
+ // TODO throw?\r
+ }\r
+\r
+ public void setBufferSize(int size) {\r
+ // NOP\r
+ // TODO throw?\r
+ }\r
+\r
+ public void setContentType(String type) {\r
+ // NOP\r
+ // TODO throw?\r
+ }\r
+\r
+ public void setProperty(String key, String value) {\r
+ response.setProperty(key, value);\r
+ }\r
+\r
+ public void setTitle(String title) {\r
+ response.setTitle(title);\r
}\r
- }\r
\r
- public interface PortletInfoReceiver {\r
- public void receivePortletInfo(PortletInfo info);\r
}\r
+\r
}\r