From: Marc Englund Date: Fri, 11 Apr 2008 14:08:28 +0000 (+0000) Subject: Much redesigned portal integration; not-quite working yet... X-Git-Tag: 6.7.0.beta1~4903 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b0ccad9f0cd49013d969ca2ab7ab7f283fd6e77c;p=vaadin-framework.git Much redesigned portal integration; not-quite working yet... svn changeset:4164/svn branch:trunk --- diff --git a/WebContent/WEB-INF/liferay-display.xml b/WebContent/WEB-INF/liferay-display.xml index d92c1b4397..8ba4a93f7f 100644 --- a/WebContent/WEB-INF/liferay-display.xml +++ b/WebContent/WEB-INF/liferay-display.xml @@ -3,6 +3,8 @@ - + + + \ No newline at end of file diff --git a/WebContent/WEB-INF/liferay-portlet.xml b/WebContent/WEB-INF/liferay-portlet.xml index 5af07be367..d445af65a7 100644 --- a/WebContent/WEB-INF/liferay-portlet.xml +++ b/WebContent/WEB-INF/liferay-portlet.xml @@ -4,7 +4,17 @@ - ApplicationPortlet + PortletDemo + true + false + + + Calc + true + false + + + FeatureBrowser true false diff --git a/WebContent/WEB-INF/portlet.xml b/WebContent/WEB-INF/portlet.xml index 3f414ab07b..e0b00b5dae 100644 --- a/WebContent/WEB-INF/portlet.xml +++ b/WebContent/WEB-INF/portlet.xml @@ -5,9 +5,13 @@ xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"> - ApplicationPortlet - IT Mill Toolkit Portlet + PortletDemo + IT Mill Toolkit PortletDemo com.itmill.toolkit.terminal.gwt.server.ApplicationPortlet + + application + PortletDemo + text/html view @@ -15,8 +19,72 @@ help - IT Mill Toolkit ApplicationPortlet - ApplicationPortlet + IT Mill Toolkit PortletDemo + PortletDemo + + + + administrator + + + guest + + + power-user + + + user + + + + FeatureBrowser + IT Mill Toolkit FeatureBrowser + com.itmill.toolkit.terminal.gwt.server.ApplicationPortlet + + application + FeatureBrowser + + + text/html + view + edit + help + + + IT Mill Toolkit FeatureBrowser + FeatureBrowser + + + + administrator + + + guest + + + power-user + + + user + + + + Calc + IT Mill Toolkit Calc + com.itmill.toolkit.terminal.gwt.server.ApplicationPortlet + + application + Calc + + + text/html + view + edit + help + + + IT Mill Toolkit Calc + Calc diff --git a/portlet-src/com/itmill/tookit/demo/PortletDemo.java b/portlet-src/com/itmill/tookit/demo/PortletDemo.java new file mode 100644 index 0000000000..7e57132466 --- /dev/null +++ b/portlet-src/com/itmill/tookit/demo/PortletDemo.java @@ -0,0 +1,90 @@ +/** + * + */ +package com.itmill.tookit.demo; + +import javax.portlet.ActionRequest; +import javax.portlet.ActionResponse; +import javax.portlet.PortletMode; +import javax.portlet.PortletURL; +import javax.portlet.RenderRequest; +import javax.portlet.RenderResponse; +import javax.portlet.WindowState; + +import com.itmill.toolkit.Application; +import com.itmill.toolkit.terminal.ExternalResource; +import com.itmill.toolkit.terminal.gwt.server.PortletApplicationContext; +import com.itmill.toolkit.terminal.gwt.server.PortletApplicationContext.PortletListener; +import com.itmill.toolkit.ui.Link; +import com.itmill.toolkit.ui.TextField; +import com.itmill.toolkit.ui.Window; +import com.itmill.toolkit.ui.Window.Notification; + +/** + * @author marc + * + */ +public class PortletDemo extends Application { + + Window main = new Window(); + TextField tf = new TextField(); + Link portletEdit = new Link(); + Link portletMax = new Link(); + + public void init() { + main = new Window(); + tf.setEnabled(false); + main.addComponent(tf); + + portletEdit.setCaption("Portlet edit/view"); + portletEdit.setEnabled(false); + main.addComponent(portletEdit); + portletMax.setCaption("Maximize/normal portlet"); + portletMax.setEnabled(false); + main.addComponent(portletMax); + + PortletApplicationContext ctx = (PortletApplicationContext) getContext(); + + ctx.addPortletListener(this, new DemoPortletListener()); + } + + private class DemoPortletListener implements PortletListener { + + public void handleActionRequest(ActionRequest request, + ActionResponse response) { + + getMainWindow().showNotification("Action received"); + + } + + public void handleRenderRequest(RenderRequest request, + RenderResponse response) { + getMainWindow().showNotification( + "Portlet status", + "mode: " + request.getPortletMode() + "
state: " + + request.getWindowState(), + Notification.TYPE_TRAY_NOTIFICATION); + + PortletURL url = response.createActionURL(); + try { + url + .setPortletMode((request.getPortletMode() == PortletMode.VIEW ? PortletMode.EDIT + : PortletMode.VIEW)); + portletEdit.setResource(new ExternalResource(url.toString())); + } catch (Exception e) { + portletEdit.setEnabled(false); + } + + url = response.createActionURL(); + try { + url + .setWindowState((request.getWindowState() == WindowState.NORMAL ? WindowState.MAXIMIZED + : WindowState.NORMAL)); + portletEdit.setResource(new ExternalResource(url.toString())); + } catch (Exception e) { + portletEdit.setEnabled(false); + } + + } + } +} diff --git a/portlet-src/com/itmill/toolkit/terminal/gwt/server/ApplicationPortlet.java b/portlet-src/com/itmill/toolkit/terminal/gwt/server/ApplicationPortlet.java index a0c31b1ecf..bb88456059 100644 --- a/portlet-src/com/itmill/toolkit/terminal/gwt/server/ApplicationPortlet.java +++ b/portlet-src/com/itmill/toolkit/terminal/gwt/server/ApplicationPortlet.java @@ -2,36 +2,26 @@ package com.itmill.toolkit.terminal.gwt.server; import java.io.IOException; import java.io.PrintWriter; -import java.util.Map; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.Portlet; import javax.portlet.PortletConfig; import javax.portlet.PortletException; -import javax.portlet.PortletMode; -import javax.portlet.PortletModeException; -import javax.portlet.PortletPreferences; -import javax.portlet.PortletRequest; import javax.portlet.PortletRequestDispatcher; import javax.portlet.PortletSession; -import javax.portlet.PortletURL; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import com.itmill.toolkit.Application; -import com.itmill.toolkit.terminal.gwt.server.PortletApplicationContext.PortletInfo; -import com.itmill.toolkit.terminal.gwt.server.PortletApplicationContext.PortletInfoReceiver; public class ApplicationPortlet implements Portlet { // The application to show - protected String app = "Calc"; // empty for root - // theme to use for the application - protected String theme = "default"; + protected String app = "PortletDemo"; // some applications might require that the height is specified protected String height = null; // e.g "200px" - PortletConfig config; + private PortletConfig config; public void destroy() { config = null; @@ -39,40 +29,27 @@ public class ApplicationPortlet implements Portlet { public void init(PortletConfig config) throws PortletException { this.config = config; + app = config.getInitParameter("application"); + if (app == null) { + app = "PortalDemo"; + } + height = config.getInitParameter("height"); } public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException { - // Update preferences (configured) - PortletPreferences prefs = request.getPreferences(); - app = request.getParameter("app"); - if (app != null && app.length() > 0) { - prefs.setValue("application", app); - } else { - app = null; - } - String theme = request.getParameter("theme"); - if (theme != null && theme.length() > 0) { - prefs.setValue("theme", theme); - } else { - prefs.setValue("theme", null); - } - String height = request.getParameter("height"); - if (height != null && height.length() > 0) { - prefs.setValue("height", height); - } else { - prefs.setValue("height", null); - } - prefs.store(); + PortletApplicationContext.dispatchRequest(this, request, response); } public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException { + /*- PortletPreferences prefs = request.getPreferences(); app = prefs.getValue("application", app); theme = prefs.getValue("theme", "default"); height = prefs.getValue("height", null); + -*/ // display the IT Mill Toolkit application writeAjaxWindow(request, response); @@ -84,54 +61,38 @@ public class ApplicationPortlet implements Portlet { response.setContentType("text/html"); PrintWriter out = response.getWriter(); - // TODO check user == admin - if (app == null) { - // Display the configuration UI - PortletURL submitUrl = response.createActionURL(); - try { - submitUrl.setPortletMode(PortletMode.VIEW); - } catch (PortletModeException e) { - // Fine - } - out.println("
"); - out.println("Application:"); - out.println(request.getContextPath() + "/"); - out.println(""); - out.println(" Theme:
"); - out - .println("Force height (optional, e.g \"200px\"):
"); - out.println(""); - out.println("
"); - } else { + if (app != null) { PortletSession sess = request.getPortletSession(); PortletApplicationContext ctx = PortletApplicationContext .getApplicationContext(sess); + /*- TODO store som info somewhere? PortletInfo pi = ctx.setPortletInfo(request.getContextPath() + "/" + app + (app.endsWith("/") ? "" : "/"), request .getPortletMode(), request.getWindowState(), request .getUserPrincipal(), (Map) request .getAttribute(PortletRequest.USER_INFO), config); + -*/ PortletRequestDispatcher dispatcher = sess.getPortletContext() .getRequestDispatcher("/" + app); try { + // TODO height dispatcher.include(request, response); } catch (PortletException e) { out.print("

Servlet include failed!

"); out.print("
" + e + "
"); + ctx.setPortletApplication(this, null); return; } - Object app = request.getAttribute(Application.class.getName()); - if (app instanceof PortletInfoReceiver) { - ((PortletInfoReceiver) app).receivePortletInfo(pi); - } + Application app = (Application) request + .getAttribute(Application.class.getName()); + ctx.setPortletApplication(this, app); + ctx.firePortletRenderRequest(this, request, response); + } } diff --git a/portlet-src/com/itmill/toolkit/terminal/gwt/server/PortletApplicationContext.java b/portlet-src/com/itmill/toolkit/terminal/gwt/server/PortletApplicationContext.java index a87ff638c2..4ddbabdb96 100644 --- a/portlet-src/com/itmill/toolkit/terminal/gwt/server/PortletApplicationContext.java +++ b/portlet-src/com/itmill/toolkit/terminal/gwt/server/PortletApplicationContext.java @@ -3,216 +3,281 @@ */ package com.itmill.toolkit.terminal.gwt.server; -import java.io.File; -import java.security.Principal; -import java.util.Collection; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.Locale; import java.util.Map; +import java.util.Set; -import javax.portlet.PortletConfig; -import javax.portlet.PortletMode; +import javax.portlet.ActionRequest; +import javax.portlet.ActionResponse; +import javax.portlet.Portlet; import javax.portlet.PortletSession; -import javax.portlet.WindowState; +import javax.portlet.PortletURL; +import javax.portlet.RenderRequest; +import javax.portlet.RenderResponse; import javax.servlet.http.HttpSession; import com.itmill.toolkit.Application; -import com.itmill.toolkit.service.ApplicationContext; /** * @author marc * */ -public class PortletApplicationContext implements ApplicationContext { +public class PortletApplicationContext extends WebApplicationContext { - private final PortletSession session; + protected PortletSession portletSession; - private final Map portletInfoMap; + protected Map portletListeners = new HashMap(); + + protected Map portletToApplication = new HashMap(); + + PortletApplicationContext() { - PortletApplicationContext(PortletSession session) { - this.session = session; - portletInfoMap = new HashMap(); } static public PortletApplicationContext getApplicationContext( PortletSession session) { - PortletApplicationContext cx = (PortletApplicationContext) session - .getAttribute(PortletApplicationContext.class.getName()); + WebApplicationContext cx = (WebApplicationContext) session + .getAttribute(WebApplicationContext.class.getName(), + PortletSession.APPLICATION_SCOPE); if (cx == null) { - cx = new PortletApplicationContext(session); - session.setAttribute(PortletApplicationContext.class.getName(), cx, - PortletSession.APPLICATION_SCOPE); + cx = new PortletApplicationContext(); } - return cx; + if (!(cx instanceof PortletApplicationContext)) { + // TODO Should we even try this? And should we leave original as-is? + PortletApplicationContext pcx = new PortletApplicationContext(); + pcx.applications.addAll(cx.applications); + cx.applications.clear(); + pcx.browser = cx.browser; + cx.browser = null; + pcx.listeners = cx.listeners; + cx.listeners = null; + pcx.session = cx.session; + cx = pcx; + } + if (((PortletApplicationContext) cx).portletSession == null) { + ((PortletApplicationContext) cx).portletSession = session; + } + session.setAttribute(WebApplicationContext.class.getName(), cx, + PortletSession.APPLICATION_SCOPE); + return (PortletApplicationContext) cx; } - static public PortletApplicationContext getApplicationContext( + static public WebApplicationContext getApplicationContext( HttpSession session) { - PortletApplicationContext cx = (PortletApplicationContext) session - .getAttribute(PortletApplicationContext.class.getName()); + WebApplicationContext cx = (WebApplicationContext) session + .getAttribute(WebApplicationContext.class.getName()); + if (cx == null) { + cx = new PortletApplicationContext(); + } + if (cx.session == null) { + cx.session = session; + } + session.setAttribute(WebApplicationContext.class.getName(), cx); return cx; } + protected void removeApplication(Application application) { + portletListeners.remove(application); + for (Iterator it = portletToApplication.keySet().iterator(); it + .hasNext();) { + Object key = it.next(); + if (key == application) { + portletToApplication.remove(key); + } + } + super.removeApplication(application); + } + + public boolean equals(Object obj) { + if (portletSession == null) { + return super.equals(obj); + } + return portletSession.equals(obj); + } + + public int hashCode() { + if (portletSession == null) { + return super.hashCode(); + } + return portletSession.hashCode(); + } + + public void setPortletApplication(Portlet portlet, Application app) { + portletToApplication.put(portlet, app); + } + + public Application getPortletApplication(Portlet portlet) { + return (Application) portletToApplication.get(portlet); + } + public PortletSession getPortletSession() { - return session; + return portletSession; } - /* - * (non-Javadoc) - * - * @see com.itmill.toolkit.service.ApplicationContext#addTransactionListener(com.itmill.toolkit.service.ApplicationContext.TransactionListener) - */ - public void addTransactionListener(TransactionListener listener) { - WebApplicationContext cx = (WebApplicationContext) session - .getAttribute(WebApplicationContext.class.getName()); - if (cx != null) { - cx.addTransactionListener(listener); + public void addPortletListener(Application app, PortletListener listener) { + Set l = (Set) portletListeners.get(app); + if (l == null) { + l = new LinkedHashSet(); + portletListeners.put(app, l); } + l.add(listener); } - /* - * (non-Javadoc) - * - * @see com.itmill.toolkit.service.ApplicationContext#getApplications() - */ - public Collection getApplications() { - WebApplicationContext cx = (WebApplicationContext) session - .getAttribute(WebApplicationContext.class.getName()); - if (cx != null) { - return cx.getApplications(); + public void removePortletListener(Application app, PortletListener listener) { + Set l = (Set) portletListeners.get(app); + if (l != null) { + l.remove(listener); } - return null; } - /* - * (non-Javadoc) - * - * @see com.itmill.toolkit.service.ApplicationContext#getBaseDirectory() - */ - public File getBaseDirectory() { - WebApplicationContext cx = (WebApplicationContext) session - .getAttribute(WebApplicationContext.class.getName()); - if (cx != null) { - return cx.getBaseDirectory(); + public static void dispatchRequest(Portlet portlet, RenderRequest request, + RenderResponse response) { + PortletApplicationContext ctx = getApplicationContext(request + .getPortletSession()); + if (ctx != null) { + ctx.firePortletRenderRequest(portlet, request, response); } - return null; } - /* - * (non-Javadoc) - * - * @see com.itmill.toolkit.service.ApplicationContext#removeTransactionListener(com.itmill.toolkit.service.ApplicationContext.TransactionListener) - */ - public void removeTransactionListener(TransactionListener listener) { - WebApplicationContext cx = (WebApplicationContext) session - .getAttribute(WebApplicationContext.class.getName()); - if (cx != null) { - cx.removeTransactionListener(listener); + public static void dispatchRequest(Portlet portlet, ActionRequest request, + ActionResponse response) { + PortletApplicationContext ctx = getApplicationContext(request + .getPortletSession()); + if (ctx != null) { + ctx.firePortletActionRequest(portlet, request, response); } - } + } - PortletInfo setPortletInfo(String path, PortletMode mode, - WindowState state, Principal userPrincipal, Map userInfo, - PortletConfig config) { - System.err.println("SETTING PI: " + path); - PortletInfo pi = (PortletInfo) portletInfoMap.get(path); - if (pi == null) { - pi = new PortletInfo(mode, state, userPrincipal, userInfo, config); - portletInfoMap.put(path, pi); - } else { - pi.setInfo(mode, state, userPrincipal, userInfo, config); + public void firePortletRenderRequest(Portlet portlet, + RenderRequest request, RenderResponse response) { + Application app = getPortletApplication(portlet); + Set listeners = (Set) portletListeners.get(app); + if (listeners != null) { + for (Iterator it = listeners.iterator(); it.hasNext();) { + PortletListener l = (PortletListener) it.next(); + l.handleRenderRequest(request, new RestrictedRenderResponse( + response)); + } } - return pi; } - public PortletInfo getPortletInfo(Application app) { - if (app != null && app.getURL() != null) { - // TODO remove - System.err.println("GETTING PI: " + app.getURL().getPath()); - return (PortletInfo) portletInfoMap.get(app.getURL().getPath()); + public void firePortletActionRequest(Portlet portlet, + ActionRequest request, ActionResponse response) { + Application app = getPortletApplication(portlet); + Set listeners = (Set) portletListeners.get(app); + if (listeners != null) { + for (Iterator it = listeners.iterator(); it.hasNext();) { + PortletListener l = (PortletListener) it.next(); + l.handleActionRequest(request, response); + } } - return null; } - public class PortletInfo { + public interface PortletListener { + public void handleRenderRequest(RenderRequest request, + RenderResponse response); + + public void handleActionRequest(ActionRequest request, + ActionResponse response); + } + + private class RestrictedRenderResponse implements RenderResponse { - PortletMode mode; - WindowState state; - Principal userPrincipal; - Map userInfo; - PortletConfig config; + private RenderResponse response; - public PortletInfo(PortletMode mode, WindowState state, - Principal userPrincipal, Map userInfo, PortletConfig config) { - this.mode = mode; - this.state = state; - this.userPrincipal = userPrincipal; - this.userInfo = userInfo; - this.config = config; + private RestrictedRenderResponse(RenderResponse response) { + this.response = response; } - private void setInfo(PortletMode mode, WindowState state, - Principal userPrincipal, Map userInfo, PortletConfig config) { - this.mode = mode; - this.state = state; - this.userPrincipal = userPrincipal; - this.userInfo = userInfo; - this.config = config; + public void addProperty(String key, String value) { + response.addProperty(key, value); } - /** - * Gets the current portlet mode, VIEW / EDIT / HELP - * - * @return the current portlet mode - */ - public PortletMode getPortletMode() { - return mode; + public PortletURL createActionURL() { + return response.createActionURL(); } - /** - * Gets the current window state, NORMAL / MAXIMIZED / MINIMIZED - * - * @return the current window state - */ - public WindowState getWindowState() { - return state; + public PortletURL createRenderURL() { + return response.createRenderURL(); } - /** - * Gets the current UserPrincipal - * - * @return current UserPrincipal, null if not logged in - */ - public Principal getUserPrincipal() { - return userPrincipal; + public String encodeURL(String path) { + return response.encodeURL(path); } - /** - * Gets the PortletConfig for this portlet - * - * @return the PortletConfig - */ - public PortletConfig getConfig() { - return config; + public void flushBuffer() throws IOException { + // NOP + // TODO throw? } - /** - * Gets the user info for this portlet, as retreived from - * request.getAttribute(PortletRequest.USER_INFO); - * - * @return the user info Map - */ - public Map getUserInfo() { - return userInfo; + public int getBufferSize() { + return response.getBufferSize(); } - public String toString() { - return "PortletMode: " + getPortletMode() + " WindowState: " - + getWindowState() + " UserPrincipal: " - + getUserPrincipal() + " User info: " + getUserInfo(); + public String getCharacterEncoding() { + return response.getCharacterEncoding(); + } + + public String getContentType() { + return response.getContentType(); + } + + public Locale getLocale() { + return response.getLocale(); + } + + public String getNamespace() { + return response.getNamespace(); + } + + public OutputStream getPortletOutputStream() throws IOException { + // write forbidden + return null; + } + + public PrintWriter getWriter() throws IOException { + // write forbidden + return null; + } + + public boolean isCommitted() { + return response.isCommitted(); + } + + public void reset() { + // NOP + // TODO throw? + } + + public void resetBuffer() { + // NOP + // TODO throw? + } + + public void setBufferSize(int size) { + // NOP + // TODO throw? + } + + public void setContentType(String type) { + // NOP + // TODO throw? + } + + public void setProperty(String key, String value) { + response.setProperty(key, value); + } + + public void setTitle(String title) { + response.setTitle(title); } - } - public interface PortletInfoReceiver { - public void receivePortletInfo(PortletInfo info); } + }