001: /**
002: * $Id: CheckAuth.java,v 1.5 2005/10/24 05:22:07 hc109819 Exp $
003: * Copyright 2005 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.admin.console.common;
014:
015: import java.util.Locale;
016: import java.util.logging.Level;
017: import java.util.Map;
018:
019: import javax.faces.event.PhaseListener;
020: import javax.faces.event.PhaseId;
021: import javax.faces.event.PhaseEvent;
022: import javax.faces.context.FacesContext;
023: import javax.management.remote.JMXConnector;
024:
025: public class CheckAuth implements PhaseListener {
026:
027: private static Locale sysLocale = Locale.getDefault();
028:
029: public void afterPhase(PhaseEvent event) {
030: if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
031: Locale.setDefault(sysLocale);
032: return;
033: }
034: if (event.getPhaseId() != PhaseId.RESTORE_VIEW) {
035: return;
036: }
037:
038: if (isImage() || isHelpPage() || isVersionPage()) {
039: return;
040: }
041:
042: FacesContext fc = event.getFacesContext();
043: //Check if the login page is requested
044: boolean loginPage = (fc.getViewRoot().getViewId().lastIndexOf(
045: "Login.jsp") > -1 ? true : false);
046: if (isUserAuthenticated()) {
047: if (!loginPage) {
048: return;
049: } else {
050: try {
051: fc.getExternalContext().redirect(
052: fc.getExternalContext()
053: .getRequestContextPath()
054: + "/faces/common/ConsoleHome.jsp");
055: } catch (java.io.IOException ioe) {
056: PSBaseBean
057: .log(
058: Level.SEVERE,
059: "Request for Login page when already login : CheckAuth.afterPhase()",
060: ioe);
061: }
062: }
063: } else {
064: boolean logoutPage = (fc.getViewRoot().getViewId()
065: .lastIndexOf("Logout.jsp") > -1 ? true : false);
066: if ((!logoutPage) && (!loginPage)) {
067: try {
068: fc.getExternalContext().redirect(
069: fc.getExternalContext()
070: .getRequestContextPath()
071: + "/faces/common/Logout.jsp");
072: } catch (java.io.IOException ioe) {
073: PSBaseBean
074: .log(
075: Level.SEVERE,
076: "Auth check failed and cannot redirect to login page: CheckAuth.afterPhase()",
077: ioe);
078: }
079: }
080: }
081:
082: }
083:
084: public void beforePhase(PhaseEvent event) {
085: if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
086: Locale.setDefault(Locale.ENGLISH);
087: }
088: }
089:
090: public PhaseId getPhaseId() {
091: return PhaseId.ANY_PHASE;
092: }
093:
094: public boolean isHelpPage() {
095: FacesContext fc = FacesContext.getCurrentInstance();
096: if (fc != null) {
097: boolean helpPage = (fc.getViewRoot().getViewId()
098: .lastIndexOf("com_sun_web_ui/help") > -1 ? true
099: : false);
100: if (helpPage) {
101: return true;
102: }
103: }
104: return false;
105: }
106:
107: public boolean isImage() {
108: FacesContext fc = FacesContext.getCurrentInstance();
109: if (fc != null) {
110: String vid = fc.getViewRoot().getViewId();
111: if (vid.startsWith("/images/")
112: || vid.startsWith("/theme/com/sun/web/ui")) {
113: return true;
114: }
115: }
116: return false;
117: }
118:
119: public boolean isVersionPage() {
120: FacesContext fc = FacesContext.getCurrentInstance();
121: if (fc != null) {
122: boolean helpPage = (fc.getViewRoot().getViewId()
123: .lastIndexOf("ProductVersion.jsp") > -1 ? true
124: : false);
125: if (helpPage) {
126: return true;
127: }
128: }
129: return false;
130: }
131:
132: public boolean isUserAuthenticated() {
133: AuthCredentialBean abean = AuthCredentialBean
134: .getCurrentInstance();
135: if (abean != null) {
136: return abean.isUserAuthenticated();
137: }
138: return false;
139: }
140: }
|