001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Aug 2, 2005
014: * @author James Dixon
015: */
016:
017: package org.pentaho.ui.portlet;
018:
019: import java.io.IOException;
020: import java.util.MissingResourceException;
021:
022: import javax.portlet.ActionRequest;
023: import javax.portlet.ActionResponse;
024: import javax.portlet.GenericPortlet;
025: import javax.portlet.PortletException; // import javax.portlet.PortletPreferences;
026: import javax.portlet.PortletRequest;
027: import javax.portlet.PortletSession;
028: import javax.portlet.RenderRequest;
029: import javax.portlet.RenderResponse;
030:
031: import org.apache.commons.logging.Log;
032: import org.pentaho.core.session.BaseSession;
033: import org.pentaho.core.session.IPentahoSession;
034: import org.pentaho.core.system.PentahoSystem;
035: import org.pentaho.messages.Messages;
036: import org.pentaho.messages.util.LocaleHelper;
037: import org.pentaho.util.logging.ILogger;
038: import org.pentaho.util.logging.Logger;
039:
040: public abstract class BasePortlet extends GenericPortlet implements
041: ILogger {
042:
043: protected final static boolean debug = PentahoSystem.debug;
044:
045: protected String logId;
046:
047: protected static Log logger;
048:
049: private int logLevel = DEBUG;
050:
051: public BasePortlet() {
052:
053: }
054:
055: public abstract Log getLogger();
056:
057: public String getLogId() {
058: return logId;
059: }
060:
061: public void setLogId(String lId) {
062: logId = lId;
063: }
064:
065: public void init() throws PortletException {
066:
067: try {
068: if (logId == null) {
069: logId = getClass().getName()
070: + ":" + SESSION_LOG + ":portlet: "; //$NON-NLS-1$//$NON-NLS-2$
071: }
072:
073: // now call the init of the subclass
074: initPortlet();
075: logger = getLogger();
076: } catch (Throwable error) {
077: // fixes for JIRA case 'PLATFORM-152'
078: try {
079: if (logger == null) {
080: logger = getLogger();
081: }
082: if (logger != null) {
083: logger
084: .error(
085: Messages
086: .getErrorString("BasePortlet.ERROR_0002_COULD_NOT_INIT"), error); //$NON-NLS-1$
087: } else {
088: Logger
089: .error(
090: getClass().getName(),
091: Messages
092: .getErrorString("BasePortlet.ERROR_0002_COULD_NOT_INIT"), error); //$NON-NLS-1$
093: }
094: } catch (Throwable logError) {
095: Logger
096: .error(
097: getClass().getName(),
098: Messages
099: .getErrorString("BasePortlet.ERROR_0002_COULD_NOT_INIT"), error); //$NON-NLS-1$
100: }
101: throw new PortletException(
102: Messages
103: .getErrorString("BasePortlet.ERROR_0002_COULD_NOT_INIT"), error); //$NON-NLS-1$
104: }
105:
106: }
107:
108: public void initPortlet() {
109:
110: }
111:
112: public abstract void processPortletAction(ActionRequest request,
113: ActionResponse response, PentahoPortletSession userSession)
114: throws PortletException, java.io.IOException;
115:
116: public abstract void doPortletView(RenderRequest request,
117: RenderResponse response, PentahoPortletSession userSession)
118: throws PortletException, java.io.IOException;
119:
120: public abstract void doPortletHelp(RenderRequest request,
121: RenderResponse response, PentahoPortletSession userSession)
122: throws PortletException, java.io.IOException;
123:
124: public abstract void doPortletEdit(RenderRequest request,
125: RenderResponse response, PentahoPortletSession userSession)
126: throws PortletException, java.io.IOException;
127:
128: public final void processAction(ActionRequest request,
129: ActionResponse response) throws PortletException,
130: java.io.IOException {
131:
132: // JIRA case #PLATFORM 150
133: PentahoSystem.systemEntryPoint();
134:
135: try {
136: PentahoPortletSession userSession = getPentahoSession(request);
137: // PortletPreferences preferences = request.getPreferences();
138: String user = request.getRemoteUser();
139:
140: if (user != null && !userSession.isAuthenticated()) {
141: // the user was not logged in before but is now....
142: userSession.setAuthenticated(user);
143: }
144:
145: // TODO: perform validation and auditing of this action
146:
147: // now call the action of the subclass
148: processPortletAction(request, response, userSession);
149: } finally {
150: // JIRA case #PLATFORM 150
151: PentahoSystem.systemExitPoint();
152: }
153:
154: }
155:
156: protected void setupSession(PentahoPortletSession userSession) {
157:
158: PentahoSystem.sessionStartup(userSession);
159:
160: }
161:
162: public final void doView(RenderRequest request,
163: RenderResponse response) throws PortletException,
164: IOException {
165: // JIRA case #PLATFORM 150
166: PentahoSystem.systemEntryPoint();
167:
168: try {
169: PortletSession session = request.getPortletSession(true);
170: PentahoPortletSession userSession = getPentahoSession(request);
171:
172: // PortletPreferences preferences = request.getPreferences();
173: String user = request.getRemoteUser();
174: if (user != null && !userSession.isAuthenticated()) {
175: // the user was not logged in before but is now....
176: // Principal p = request.getUserPrincipal();
177: // System .out.println("***** Portlet Principal: " + p);
178:
179: userSession.setAuthenticated(user);
180: setupSession(userSession);
181: } else if (user == null && userSession.isAuthenticated()) {
182: // the user has logged out...
183: userSession.setNotAuthenticated();
184: removeUserSession(userSession);
185: } else if (user != null
186: && !userSession.getName().equals(user)) {
187: // this is a different user
188: removeUserSession(userSession);
189: userSession = getPentahoSession(request);
190: }
191:
192: // TODO: perform validation and auditing of this action
193:
194: // handle any message from the action
195:
196: String message = (String) session.getAttribute(
197: "action-message", PortletSession.PORTLET_SCOPE); //$NON-NLS-1$
198: if (message != null) {
199: /*
200: * String messageHtml = PortletUtil.getMessageHtml( message );
201: * session.removeAttribute( "action-message",
202: * PortletSession.PORTLET_SCOPE ); //$NON-NLS-1$
203: * response.setContentType( "text/html" ); //$NON-NLS-1$
204: * response.getWriter().write( messageHtml );
205: */
206: }
207:
208: // now call the action of the subclass
209: try {
210: doPortletView(request, response, userSession);
211: } catch (Throwable t) {
212: error(
213: Messages
214: .getErrorString("BasePortlet.ERROR_0003_PORTLET_ERROR"), t); //$NON-NLS-1$
215: }
216:
217: } finally {
218: // JIRA case #PLATFORM 150
219: PentahoSystem.systemExitPoint();
220: }
221: }
222:
223: public final void doHelp(RenderRequest request,
224: RenderResponse response) throws PortletException,
225: IOException {
226:
227: PentahoPortletSession userSession = getPentahoSession(request);
228:
229: // TODO: perform validation and auditing of this action
230:
231: // now call the action of the subclass
232: doPortletHelp(request, response, userSession);
233: }
234:
235: public final void doEdit(RenderRequest request,
236: RenderResponse response) throws PortletException,
237: IOException {
238:
239: PentahoPortletSession userSession = getPentahoSession(request);
240:
241: // TODO: perform validation and auditing of this action
242:
243: // now call the action of the subclass
244: doPortletEdit(request, response, userSession);
245: }
246:
247: protected void removeUserSession(PentahoPortletSession userSession) {
248: userSession.destroy();
249: }
250:
251: // TODO sbarkdull BasePortlet.getPentahoSession and UIUtil.getPentahoSession
252: // should likely be merged or have the common parts pulled out into
253: // a common method so there is only one place to get a pentaho session
254: protected PentahoPortletSession getPentahoSession(
255: PortletRequest request) {
256:
257: PortletSession session = request.getPortletSession(true);
258: IPentahoSession existingSession = (IPentahoSession) session
259: .getAttribute(BaseSession.PENTAHO_SESSION_KEY,
260: PortletSession.APPLICATION_SCOPE);
261:
262: if (existingSession != null) {
263: if (!(existingSession instanceof PentahoPortletSession)) {
264: // there is a session object of another type, we need to replace
265: // it
266: existingSession = null;
267: }
268: }
269:
270: LocaleHelper.setLocale(request.getLocale());
271: PentahoPortletSession userSession;
272: if (existingSession == null) {
273: if (debug)
274: debug(Messages
275: .getString("BasePortlet.DEBUG_CREATING_SESSION")); //$NON-NLS-1$
276: userSession = new PentahoPortletSession(request
277: .getRemoteUser(), session, request.getLocale());
278: session.removeAttribute(BaseSession.PENTAHO_SESSION_KEY);
279: session.setAttribute(BaseSession.PENTAHO_SESSION_KEY,
280: userSession, PortletSession.APPLICATION_SCOPE);
281: } else {
282: userSession = (PentahoPortletSession) existingSession;
283: }
284:
285: if (userSession != null) {
286: logId = Messages.getString(
287: "BasePortlet.CODE_LOG_ID", session.getId()); //$NON-NLS-1$
288: }
289:
290: return userSession;
291:
292: }
293:
294: protected void sendMessage(String message, PortletSession session) {
295: session
296: .setAttribute(
297: "action-message", message, PortletSession.PORTLET_SCOPE); //$NON-NLS-1$
298: }
299:
300: public int getLoggingLevel() {
301: return logLevel;
302: }
303:
304: public void setLoggingLevel(int logLevel) {
305: this .logLevel = logLevel;
306: if (debug)
307: debug(Messages
308: .getString("BasePortlet.DEBUG_SETTING_LOGGING_LEVEL") + logLevel); //$NON-NLS-1$
309: }
310:
311: public void trace(String message) {
312: if (logLevel <= TRACE) {
313: logger.trace(logId + message);
314: }
315: }
316:
317: public void debug(String message) {
318: if (logLevel <= DEBUG) {
319: logger.debug(logId + message);
320: }
321: }
322:
323: public void info(String message) {
324: if (logLevel <= INFO) {
325: logger.info(logId + message);
326: }
327: }
328:
329: public void warn(String message) {
330: if (logLevel <= WARN) {
331: logger.warn(logId + message);
332: }
333: }
334:
335: public void error(String message) {
336: if (logLevel <= ERROR) {
337: logger.error(logId + message);
338: }
339: }
340:
341: public void fatal(String message) {
342: if (logLevel <= FATAL) {
343: logger.fatal(logId + message);
344: }
345: }
346:
347: public void trace(String message, Throwable error) {
348: if (logLevel <= TRACE) {
349: logger.trace(logId + message, error);
350: }
351: }
352:
353: public void debug(String message, Throwable error) {
354: if (logLevel <= DEBUG) {
355: logger.debug(logId + message, error);
356: }
357: }
358:
359: public void info(String message, Throwable error) {
360: if (logLevel <= INFO) {
361: logger.info(logId + message, error);
362: }
363: }
364:
365: public void warn(String message, Throwable error) {
366: if (logLevel <= WARN) {
367: logger.warn(logId + message, error);
368: }
369: }
370:
371: public void error(String message, Throwable error) {
372: if (logLevel <= ERROR) {
373: logger.error(logId + message, error);
374: }
375: }
376:
377: public void fatal(String message, Throwable error) {
378: if (logLevel <= FATAL) {
379: logger.fatal(logId + message, error);
380: }
381: }
382:
383: /**
384: * Override
385: */
386: protected String getTitle(RenderRequest request) {
387: String title = null;
388: try {
389: title = getPortletConfig().getResourceBundle(
390: request.getLocale()).getString(
391: getPortletConfig().getPortletName()
392: + ".javax.portlet.title"); //$NON-NLS-1$
393: } catch (MissingResourceException e) {
394: return super.getTitle(request);
395: }
396: return title;
397: }
398:
399: }
|