1 16 17 package org.pentaho.ui.portlet; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.PrintWriter ; 22 import java.net.URL ; 23 import java.net.URLClassLoader ; 24 import java.util.ResourceBundle ; 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 logId; 48 49 protected static Log logger; 50 51 private int logLevel = DEBUG; 52 private static final String LOGIN_URL_KEY = "login-url"; 53 54 public BasePortlet() { 55 56 } 57 58 public abstract Log getLogger(); 59 60 public String getLogId() { 61 return logId; 62 } 63 64 public void setLogId(String 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: "; } 74 75 initPortlet(); 77 logger = getLogger(); 78 } catch (Throwable error) { 79 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); } else { 87 Logger.error(getClass().getName(), Messages.getErrorString("BasePortlet.ERROR_0002_COULD_NOT_INIT"), error); } 89 } catch (Throwable logError) { 90 Logger.error(getClass().getName(), Messages.getErrorString("BasePortlet.ERROR_0002_COULD_NOT_INIT"), error); } 92 throw new PortletException(Messages.getErrorString("BasePortlet.ERROR_0002_COULD_NOT_INIT"), error); } 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 ; 102 103 public abstract void doPortletView(RenderRequest request, RenderResponse response, PentahoPortletSession userSession) throws PortletException, java.io.IOException ; 104 105 public abstract void doPortletHelp(RenderRequest request, RenderResponse response, PentahoPortletSession userSession) throws PortletException, java.io.IOException ; 106 107 public abstract void doPortletEdit(RenderRequest request, RenderResponse response, PentahoPortletSession userSession) throws PortletException, java.io.IOException ; 108 109 public final void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException { 110 111 PentahoSystem.systemEntryPoint(); 113 114 try { 115 PentahoPortletSession userSession = getPentahoSession(request); 116 PortletPreferences preferences = request.getPreferences(); 117 String user = request.getRemoteUser(); 118 119 if (user != null && !userSession.isAuthenticated()) { 120 userSession.setAuthenticated(user); 122 } 123 124 if (!securityOk(user, preferences)) { 125 return; 126 } 127 128 130 processPortletAction(request, response, userSession); 132 } finally { 133 PentahoSystem.systemExitPoint(); 135 } 136 137 } 138 139 protected boolean securityOk(String user, PortletPreferences preferences) { 140 boolean secure = preferences.getValue("secure", "true").equals("true"); return (!secure || user != null); 142 } 143 144 protected void setupSession(PentahoPortletSession userSession) { 145 146 PentahoSystem.sessionStartup(userSession); 147 148 } 149 150 private String getSecurityMessage( RenderRequest request ) { 151 152 String defaultLoginUrl = PentahoSystem.getSystemSetting( LOGIN_URL_KEY, null); 155 PortletPreferences preferences = request.getPreferences(); 156 157 String loginUrl = preferences.getValue( LOGIN_URL_KEY, defaultLoginUrl ); 158 159 return Messages.getString("BasePortlet.USER_YOU_MUST_LOGIN") + "<a class=\"portlet-font\" HREF=\"" + loginUrl + "\">" + Messages.getString("BasePortlet.USER_CLICK_HERE") + "</a>"; } 164 165 public final void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException { 166 PentahoSystem.systemEntryPoint(); 168 169 try { 170 PortletSession session = request.getPortletSession(true); 171 PentahoPortletSession userSession = getPentahoSession(request); 172 173 String title = getTitle(request); 175 if (title.charAt(0) == '%') { 176 String filePath = PentahoSystem.getApplicationContext().getApplicationPath("WEB-INF"); File webInfDir = new File (filePath); 179 if (webInfDir.exists() && webInfDir.isDirectory()) { 180 try { 181 URL webInfUrl = webInfDir.toURL(); 182 URLClassLoader loader = new URLClassLoader (new URL [] { webInfUrl }); 183 184 ResourceBundle bundle = ResourceBundle.getBundle("portlet", LocaleHelper.getLocale(), loader); 186 String localeText = bundle.getString(title.substring(1)); 187 if (localeText != null) { 188 response.setTitle(localeText); 189 } 190 } catch (Exception e) { 191 error(Messages.getErrorString("BasePortlet.ERROR_0001_RESOURCE_PROPERTY_MISSING", title.substring(1))); } 193 194 } 195 } 196 197 PortletPreferences preferences = request.getPreferences(); 198 String user = request.getRemoteUser(); 199 if (user != null && !userSession.isAuthenticated()) { 200 204 userSession.setAuthenticated(user); 205 setupSession(userSession); 206 } else if (user == null && userSession.isAuthenticated()) { 207 userSession.setNotAuthenticated(); 209 removeUserSession(userSession); 210 } else if (user != null && !userSession.getName().equals(user)) { 211 removeUserSession(userSession); 213 userSession = getPentahoSession(request); 214 } 215 216 if (!securityOk(user, preferences)) { 217 response.setContentType("text/html"); PrintWriter writer = response.getWriter(); 219 String securityMessage = getSecurityMessage( request ); 220 writer.write(securityMessage); 221 return; 222 } 223 224 226 228 String message = (String ) session.getAttribute("action-message", PortletSession.PORTLET_SCOPE); if (message != null) { 230 237 } 238 239 try { 241 doPortletView(request, response, userSession); 242 } catch (Throwable t) { 243 error(Messages.getErrorString("BasePortlet.ERROR_0003_PORTLET_ERROR"), t); } 245 246 } finally { 247 PentahoSystem.systemExitPoint(); 249 } 250 } 251 252 public final void doHelp(RenderRequest request, RenderResponse response) throws PortletException, IOException { 253 254 PentahoPortletSession userSession = getPentahoSession(request); 255 256 258 doPortletHelp(request, response, userSession); 260 } 261 262 public final void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException { 263 264 PentahoPortletSession userSession = getPentahoSession(request); 265 266 268 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); 281 if (existingSession != null) { 282 if (!(existingSession instanceof PentahoPortletSession)) { 283 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")); userSession = new PentahoPortletSession(request.getRemoteUser(), session, request.getLocale()); 295 session.removeAttribute("pentaho-session"); session.setAttribute("pentaho-session", userSession, PortletSession.APPLICATION_SCOPE); } else { 298 userSession = (PentahoPortletSession) existingSession; 299 } 300 301 if (userSession != null) { 302 logId = Messages.getString("BasePortlet.CODE_LOG_ID", session.getId()); } 304 305 return userSession; 306 307 } 308 309 protected void sendMessage(String message, PortletSession session) { 310 session.setAttribute("action-message", message, PortletSession.PORTLET_SCOPE); } 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); } 322 323 public void trace(String message) { 324 if (logLevel <= TRACE) { 325 logger.trace(logId + message); 326 } 327 } 328 329 public void debug(String message) { 330 if (logLevel <= DEBUG) { 331 logger.debug(logId + message); 332 } 333 } 334 335 public void info(String message) { 336 if (logLevel <= INFO) { 337 logger.info(logId + message); 338 } 339 } 340 341 public void warn(String message) { 342 if (logLevel <= WARN) { 343 logger.warn(logId + message); 344 } 345 } 346 347 public void error(String message) { 348 if (logLevel <= ERROR) { 349 logger.error(logId + message); 350 } 351 } 352 353 public void fatal(String message) { 354 if (logLevel <= FATAL) { 355 logger.fatal(logId + message); 356 } 357 } 358 359 public void trace(String message, Throwable error) { 360 if (logLevel <= TRACE) { 361 logger.trace(logId + message, error); 362 } 363 } 364 365 public void debug(String message, Throwable error) { 366 if (logLevel <= DEBUG) { 367 logger.debug(logId + message, error); 368 } 369 } 370 371 public void info(String message, Throwable error) { 372 if (logLevel <= INFO) { 373 logger.info(logId + message, error); 374 } 375 } 376 377 public void warn(String message, Throwable error) { 378 if (logLevel <= WARN) { 379 logger.warn(logId + message, error); 380 } 381 } 382 383 public void error(String message, Throwable error) { 384 if (logLevel <= ERROR) { 385 logger.error(logId + message, error); 386 } 387 } 388 389 public void fatal(String message, Throwable error) { 390 if (logLevel <= FATAL) { 391 logger.fatal(logId + message, error); 392 } 393 } 394 395 } 396 | Popular Tags |