Articles/SimplePortalIntegration: DemoPortlet.java

File DemoPortlet.java, 4.3 kB (added by Marc Englund, 8 months ago)
Line 
1package com.itmill.toolkit.demo;
2
3import java.io.IOException;
4import java.io.PrintWriter;
5import java.security.Principal;
6import java.util.Map;
7
8import javax.portlet.ActionRequest;
9import javax.portlet.ActionResponse;
10import javax.portlet.Portlet;
11import javax.portlet.PortletConfig;
12import javax.portlet.PortletException;
13import javax.portlet.PortletMode;
14import javax.portlet.PortletRequest;
15import javax.portlet.PortletSession;
16import javax.portlet.RenderRequest;
17import javax.portlet.RenderResponse;
18import javax.portlet.WindowState;
19
20public class DemoPortlet implements Portlet {
21        // The application to show
22        protected String app = ""; // empty for root
23        // theme to use for the application
24        protected String theme = "default";
25        // some applications might require that the height is specified
26        protected String height = null; // e.g "200px"
27
28        PortletConfig config;
29
30        public void destroy() {
31                config = null;
32        }
33
34        public void init(PortletConfig config) throws PortletException {
35                this.config = config;
36        }
37
38        public void processAction(ActionRequest arg0, ActionResponse arg1)
39                        throws PortletException, IOException {
40                // not used
41        }
42
43        public void render(RenderRequest request, RenderResponse response)
44                        throws PortletException, IOException {
45
46                // display the IT Mill Toolkit application
47                writeAjaxWindow(request, response);
48        }
49
50        protected void writeAjaxWindow(RenderRequest request,
51                        RenderResponse response) throws IOException {
52
53                response.setContentType("text/html");
54                PrintWriter out = response.getWriter();
55
56                // make portlet accessible via session
57                // make the current window state, portlet mode and user principal
58                // available via session
59                PortletInfo uinfo = new PortletInfo(request.getPortletMode(), request
60                                .getWindowState(), request.getUserPrincipal(), (Map) request
61                                .getAttribute(PortletRequest.USER_INFO), config);
62                PortletSession sess = request.getPortletSession();
63                sess.setAttribute(sess.getPortletContext().getPortletContextName()
64                                + "/" + app, uinfo, PortletSession.APPLICATION_SCOPE);
65
66                // the main div, where the app will be rendered
67                out.print("<div id=\"itmill-ajax-window\""
68                                + (height == null ? "" : "style=\"height:" + height + "\"")
69                                + "></div>");
70
71                // load theme css
72                String themeUri = request.getContextPath() + "/ITMILL/themes/" + theme;
73                out.print("<link REL=\"stylesheet\" TYPE=\"text/css\" HREF=\""
74                                + themeUri + "/styles.css\">");
75                // some basic info for the app
76                out
77                                .print("<script type=\"text/javascript\">"
78                                                + "var itmill = {appUri:'"
79                                                + request.getContextPath()
80                                                + "/"
81                                                + app
82                                                + "', pathInfo: '/', themeUri: null, versionInfo : {toolkitVersion:\"\",applicationVersion:\"\"}};");
83
84                out.print("</script>");
85                // start client (gwt)
86                out
87                                .print("<script src='"
88                                                + request.getContextPath()
89                                                + "/ITMILL/widgetsets/com.itmill.toolkit.terminal.gwt.DefaultWidgetSet/com.itmill.toolkit.terminal.gwt.DefaultWidgetSet.nocache.js'></script>");
90
91        }
92
93        public class PortletInfo {
94
95                PortletMode mode;
96                WindowState state;
97                Principal userPrincipal;
98                Map userInfo;
99                PortletConfig config;
100
101                public PortletInfo(PortletMode mode, WindowState state,
102                                Principal userPrincipal, Map userInfo, PortletConfig config) {
103                        this.mode = mode;
104                        this.state = state;
105                        this.userPrincipal = userPrincipal;
106                        this.userInfo = userInfo;
107                        this.config = config;
108                }
109
110                /**
111                 * Gets the current portlet mode, VIEW / EDIT / HELP
112                 *
113                 * @return the current portlet mode
114                 */
115                public PortletMode getPortletMode() {
116                        return mode;
117                }
118
119                /**
120                 * Gets the current window state, NORMAL / MAXIMIZED / MINIMIZED
121                 *
122                 * @return the current window state
123                 */
124                public WindowState getWindowState() {
125                        return state;
126                }
127
128                /**
129                 * Gets the current UserPrincipal
130                 *
131                 * @return current UserPrincipal, null if not logged in
132                 */
133                public Principal getUserPrincipal() {
134                        return userPrincipal;
135                }
136
137                /**
138                 * Gets the PortletConfig for this portlet
139                 *
140                 * @return the PortletConfig
141                 */
142                public PortletConfig getConfig() {
143                        return config;
144                }
145
146                /**
147                 * Gets the user info for this portlet, as retreived from
148                 * request.getAttribute(PortletRequest.USER_INFO);
149                 *
150                 * @return the user info Map
151                 */
152                public Map getUserInfo() {
153                        return userInfo;
154                }
155        }
156}