KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > portlet > BasePortlet


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Aug 2, 2005
14  * @author James Dixon
15  */

16
17 package org.pentaho.ui.portlet;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLClassLoader JavaDoc;
24 // import java.security.Principal;
25
import java.util.ResourceBundle JavaDoc;
26 import javax.portlet.ActionRequest;
27 import javax.portlet.ActionResponse;
28 import javax.portlet.GenericPortlet;
29 import javax.portlet.PortletException;
30 import javax.portlet.PortletPreferences;
31 import javax.portlet.PortletRequest;
32 import javax.portlet.PortletSession;
33 import javax.portlet.RenderRequest;
34 import javax.portlet.RenderResponse;
35 import org.apache.commons.logging.Log;
36 import org.pentaho.core.session.IPentahoSession;
37 import org.pentaho.core.system.PentahoSystem;
38 import org.pentaho.messages.Messages;
39 import org.pentaho.messages.util.LocaleHelper;
40 import org.pentaho.util.logging.ILogger;
41 import org.pentaho.util.logging.Logger;
42
43 public abstract class BasePortlet extends GenericPortlet implements ILogger {
44
45     protected final static boolean debug = PentahoSystem.debug;
46
47     protected String JavaDoc logId;
48
49     protected static Log logger;
50
51     private int logLevel = DEBUG;
52     private static final String JavaDoc LOGIN_URL_KEY = "login-url";
53
54     public BasePortlet() {
55
56     }
57
58     public abstract Log getLogger();
59
60     public String JavaDoc getLogId() {
61         return logId;
62     }
63
64     public void setLogId(String JavaDoc lId) {
65         logId = lId;
66     }
67
68     public void init() throws PortletException {
69
70         try {
71             if (logId == null) {
72                 logId = getClass().getName() + ":" + SESSION_LOG + ":portlet: "; //$NON-NLS-1$//$NON-NLS-2$
73
}
74
75             // now call the init of the subclass
76
initPortlet();
77             logger = getLogger();
78         } catch (Throwable JavaDoc error) {
79             // fixes for JIRA case 'PLATFORM-152'
80
try {
81                 if (logger == null) {
82                     logger = getLogger();
83                 }
84                 if (logger != null) {
85                     logger.error(Messages.getErrorString("BasePortlet.ERROR_0002_COULD_NOT_INIT"), error); //$NON-NLS-1$
86
} else {
87                     Logger.error(getClass().getName(), Messages.getErrorString("BasePortlet.ERROR_0002_COULD_NOT_INIT"), error); //$NON-NLS-1$
88
}
89             } catch (Throwable JavaDoc logError) {
90                 Logger.error(getClass().getName(), Messages.getErrorString("BasePortlet.ERROR_0002_COULD_NOT_INIT"), error); //$NON-NLS-1$
91
}
92             throw new PortletException(Messages.getErrorString("BasePortlet.ERROR_0002_COULD_NOT_INIT"), error); //$NON-NLS-1$
93
}
94
95     }
96
97     public void initPortlet() {
98
99     }
100
101     public abstract void processPortletAction(ActionRequest request, ActionResponse response, PentahoPortletSession userSession) throws PortletException, java.io.IOException JavaDoc;
102
103     public abstract void doPortletView(RenderRequest request, RenderResponse response, PentahoPortletSession userSession) throws PortletException, java.io.IOException JavaDoc;
104
105     public abstract void doPortletHelp(RenderRequest request, RenderResponse response, PentahoPortletSession userSession) throws PortletException, java.io.IOException JavaDoc;
106
107     public abstract void doPortletEdit(RenderRequest request, RenderResponse response, PentahoPortletSession userSession) throws PortletException, java.io.IOException JavaDoc;
108
109     public final void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException JavaDoc {
110
111         // JIRA case #PLATFORM 150
112
PentahoSystem.systemEntryPoint();
113
114         try {
115             PentahoPortletSession userSession = getPentahoSession(request);
116             PortletPreferences preferences = request.getPreferences();
117             String JavaDoc user = request.getRemoteUser();
118
119             if (user != null && !userSession.isAuthenticated()) {
120                 // the user was not logged in before but is now....
121
userSession.setAuthenticated(user);
122             }
123
124             if (!securityOk(user, preferences)) {
125                 return;
126             }
127
128             // TODO: perform validation and auditing of this action
129

130             // now call the action of the subclass
131
processPortletAction(request, response, userSession);
132         } finally {
133             // JIRA case #PLATFORM 150
134
PentahoSystem.systemExitPoint();
135         }
136
137     }
138
139     protected boolean securityOk(String JavaDoc user, PortletPreferences preferences) {
140         boolean secure = preferences.getValue("secure", "true").equals("true"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
141
return (!secure || user != null);
142     }
143
144     protected void setupSession(PentahoPortletSession userSession) {
145
146         PentahoSystem.sessionStartup(userSession);
147
148     }
149
150     private String JavaDoc getSecurityMessage( RenderRequest request ) {
151
152         // this is a secured portlet and the user has not logged in yet.....
153
String JavaDoc defaultLoginUrl = PentahoSystem.getSystemSetting( LOGIN_URL_KEY, null); //$NON-NLS-1$
154

155         PortletPreferences preferences = request.getPreferences();
156         
157         String JavaDoc loginUrl = preferences.getValue( LOGIN_URL_KEY, defaultLoginUrl );
158         
159         return Messages.getString("BasePortlet.USER_YOU_MUST_LOGIN") + //$NON-NLS-1$
160
"<a class=\"portlet-font\" HREF=\"" + loginUrl + "\">" + //$NON-NLS-1$ //$NON-NLS-2$
161
Messages.getString("BasePortlet.USER_CLICK_HERE") + //$NON-NLS-1$
162
"</a>"; //$NON-NLS-1$
163
}
164
165     public final void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException JavaDoc {
166         // JIRA case #PLATFORM 150
167
PentahoSystem.systemEntryPoint();
168
169         try {
170             PortletSession session = request.getPortletSession(true);
171             PentahoPortletSession userSession = getPentahoSession(request);
172
173             // see if we can localize the portlet title
174
String JavaDoc title = getTitle(request);
175             if (title.charAt(0) == '%') {
176                 // look this up in the WEB-INF/portlets.properties
177
String JavaDoc filePath = PentahoSystem.getApplicationContext().getApplicationPath("WEB-INF"); //$NON-NLS-1$
178
File JavaDoc webInfDir = new File JavaDoc(filePath);
179                 if (webInfDir.exists() && webInfDir.isDirectory()) {
180                     try {
181                         URL JavaDoc webInfUrl = webInfDir.toURL();
182                         URLClassLoader JavaDoc loader = new URLClassLoader JavaDoc(new URL JavaDoc[] { webInfUrl });
183
184                         ResourceBundle JavaDoc bundle = ResourceBundle.getBundle("portlet", LocaleHelper.getLocale(), loader); //$NON-NLS-1$
185

186                         String JavaDoc localeText = bundle.getString(title.substring(1));
187                         if (localeText != null) {
188                             response.setTitle(localeText);
189                         }
190                     } catch (Exception JavaDoc e) {
191                         error(Messages.getErrorString("BasePortlet.ERROR_0001_RESOURCE_PROPERTY_MISSING", title.substring(1))); //$NON-NLS-1$
192
}
193
194                 }
195             }
196
197             PortletPreferences preferences = request.getPreferences();
198             String JavaDoc user = request.getRemoteUser();
199             if (user != null && !userSession.isAuthenticated()) {
200                 // the user was not logged in before but is now....
201
// Principal p = request.getUserPrincipal();
202
// System .out.println("***** Portlet Principal: " + p);
203

204                 userSession.setAuthenticated(user);
205                 setupSession(userSession);
206             } else if (user == null && userSession.isAuthenticated()) {
207                 // the user has logged out...
208
userSession.setNotAuthenticated();
209                 removeUserSession(userSession);
210             } else if (user != null && !userSession.getName().equals(user)) {
211                 // this is a different user
212
removeUserSession(userSession);
213                 userSession = getPentahoSession(request);
214             }
215
216             if (!securityOk(user, preferences)) {
217                 response.setContentType("text/html"); //$NON-NLS-1$
218
PrintWriter JavaDoc writer = response.getWriter();
219                 String JavaDoc securityMessage = getSecurityMessage( request );
220                 writer.write(securityMessage);
221                 return;
222             }
223
224             // TODO: perform validation and auditing of this action
225

226             // handle any message from the action
227

228             String JavaDoc message = (String JavaDoc) session.getAttribute("action-message", PortletSession.PORTLET_SCOPE); //$NON-NLS-1$
229
if (message != null) {
230                 /*
231                  * String messageHtml = PortletUtil.getMessageHtml( message );
232                  * session.removeAttribute( "action-message",
233                  * PortletSession.PORTLET_SCOPE ); //$NON-NLS-1$
234                  * response.setContentType( "text/html" ); //$NON-NLS-1$
235                  * response.getWriter().write( messageHtml );
236                  */

237             }
238
239             // now call the action of the subclass
240
try {
241                 doPortletView(request, response, userSession);
242             } catch (Throwable JavaDoc t) {
243                 error(Messages.getErrorString("BasePortlet.ERROR_0003_PORTLET_ERROR"), t); //$NON-NLS-1$
244
}
245
246         } finally {
247             // JIRA case #PLATFORM 150
248
PentahoSystem.systemExitPoint();
249         }
250     }
251
252     public final void doHelp(RenderRequest request, RenderResponse response) throws PortletException, IOException JavaDoc {
253
254         PentahoPortletSession userSession = getPentahoSession(request);
255
256         // TODO: perform validation and auditing of this action
257

258         // now call the action of the subclass
259
doPortletHelp(request, response, userSession);
260     }
261
262     public final void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException JavaDoc {
263
264         PentahoPortletSession userSession = getPentahoSession(request);
265
266         // TODO: perform validation and auditing of this action
267

268         // now call the action of the subclass
269
doPortletEdit(request, response, userSession);
270     }
271
272     protected void removeUserSession(PentahoPortletSession userSession) {
273         userSession.destroy();
274     }
275
276     protected PentahoPortletSession getPentahoSession(PortletRequest request) {
277
278         PortletSession session = request.getPortletSession(true);
279         IPentahoSession existingSession = (IPentahoSession) session.getAttribute("pentaho-session", PortletSession.APPLICATION_SCOPE); //$NON-NLS-1$
280

281         if (existingSession != null) {
282             if (!(existingSession instanceof PentahoPortletSession)) {
283                 // there is a session object of another type, we need to replace
284
// it
285
existingSession = null;
286             }
287         }
288
289         LocaleHelper.setLocale(request.getLocale());
290         PentahoPortletSession userSession;
291         if (existingSession == null) {
292             if (debug)
293                 debug(Messages.getString("BasePortlet.DEBUG_CREATING_SESSION")); //$NON-NLS-1$
294
userSession = new PentahoPortletSession(request.getRemoteUser(), session, request.getLocale());
295             session.removeAttribute("pentaho-session"); //$NON-NLS-1$
296
session.setAttribute("pentaho-session", userSession, PortletSession.APPLICATION_SCOPE); //$NON-NLS-1$
297
} else {
298             userSession = (PentahoPortletSession) existingSession;
299         }
300
301         if (userSession != null) {
302             logId = Messages.getString("BasePortlet.CODE_LOG_ID", session.getId()); //$NON-NLS-1$
303
}
304
305         return userSession;
306
307     }
308
309     protected void sendMessage(String JavaDoc message, PortletSession session) {
310         session.setAttribute("action-message", message, PortletSession.PORTLET_SCOPE); //$NON-NLS-1$
311
}
312
313     public int getLoggingLevel() {
314         return logLevel;
315     }
316
317     public void setLoggingLevel(int logLevel) {
318         this.logLevel = logLevel;
319         if (debug)
320             debug(Messages.getString("BasePortlet.DEBUG_SETTING_LOGGING_LEVEL") + logLevel); //$NON-NLS-1$
321
}
322
323     public void trace(String JavaDoc message) {
324         if (logLevel <= TRACE) {
325             logger.trace(logId + message);
326         }
327     }
328
329     public void debug(String JavaDoc message) {
330         if (logLevel <= DEBUG) {
331             logger.debug(logId + message);
332         }
333     }
334
335     public void info(String JavaDoc message) {
336         if (logLevel <= INFO) {
337             logger.info(logId + message);
338         }
339     }
340
341     public void warn(String JavaDoc message) {
342         if (logLevel <= WARN) {
343             logger.warn(logId + message);
344         }
345     }
346
347     public void error(String JavaDoc message) {
348         if (logLevel <= ERROR) {
349             logger.error(logId + message);
350         }
351     }
352
353     public void fatal(String JavaDoc message) {
354         if (logLevel <= FATAL) {
355             logger.fatal(logId + message);
356         }
357     }
358
359     public void trace(String JavaDoc message, Throwable JavaDoc error) {
360         if (logLevel <= TRACE) {
361             logger.trace(logId + message, error);
362         }
363     }
364
365     public void debug(String JavaDoc message, Throwable JavaDoc error) {
366         if (logLevel <= DEBUG) {
367             logger.debug(logId + message, error);
368         }
369     }
370
371     public void info(String JavaDoc message, Throwable JavaDoc error) {
372         if (logLevel <= INFO) {
373             logger.info(logId + message, error);
374         }
375     }
376
377     public void warn(String JavaDoc message, Throwable JavaDoc error) {
378         if (logLevel <= WARN) {
379             logger.warn(logId + message, error);
380         }
381     }
382
383     public void error(String JavaDoc message, Throwable JavaDoc error) {
384         if (logLevel <= ERROR) {
385             logger.error(logId + message, error);
386         }
387     }
388
389     public void fatal(String JavaDoc message, Throwable JavaDoc error) {
390         if (logLevel <= FATAL) {
391             logger.fatal(logId + message, error);
392         }
393     }
394
395 }
396
Popular Tags