/**
* Copyright 1999-2001 by Nordija <www.nordija.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
**/
package com.nordija.portal;
import java.rmi.RemoteException;
import java.io.IOException;
import java.util.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.ejb.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import com.nordija.portal.entity.*;
/**
* The JspPortal gathers information from the main portal page and communicates
* with the portal service to prepare the page and its portlets
* The main purpose is to minimise the communication needed and thereby making
* the portal pages contain less code.
*/
public class JspPortal {
public JspPortal(){
System.out.println("JspPortal alive!");
accessCounter = 0;
userOK = false;
logInFromCookie = false;
logInFromLogIn = false;
}
/**
* The JNDI name on the Portal service
*/
private String jndi_portal = "nordija.portal.Portal";
/**
* A reference to the Portal service
*/
private transient Portal portal;
/**
* An iterator for narrow portles for the current
* users portal page
*/
private PortletInfoEnumeration narrowPortlets = new PortletInfoEnumeration();
/**
* An iterator for wide portles for the current
* users portal page
*/
private PortletInfoEnumeration widePortlets = new PortletInfoEnumeration();
/**
* The name of the current user
*/
private String user;
/**
* If the user wasn't found in the login-process, default user is used
*/
private String defaultUser = "guest";
/**
* The current action of the page
*/
private String action;
/**
* The portlet the current action is executed upon
* @see action
*/
private String portlet;
private long id;
private String password;
private String email;
/**
* The userOK is true if the user were found in the DB through validateUser()
*/
private boolean userOK;
/**
* The logInFromCookie is true if the user has login through cookie.
* The login process has then been transparent to the user
*/
private boolean logInFromCookie;
/**
* The logInFromLogIn is true if the user has manualy logged in.
*/
private boolean logInFromLogIn;
/**
* The number of times the user has accessed the page
*/
private int accessCounter;
/**
* The status message diplay current status
*/
private String statusMsg;
public void setId(long newId) {
this.id = newId;
}
public long getId() {
return id;
}
public void setUser(String newUser) {
this.user = newUser;
}
public String getUser() {
return user;
}
public void setEmail(String newEmail) {
this.email = newEmail;
}
public String getEmail() {
return email;
}
public void setPassword(String newPassword) {
this.password = newPassword;
}
public String getPassword() {
return password;
}
public void setAction(String action) {
this.action = action;
}
public String getAction() {
return action;
}
public void setPortlet(String newPortlet) {
this.portlet = newPortlet;
}
public String getPortlet() {
return portlet;
}
public void setUserOK(boolean OK) {
this.userOK = OK;
}
public boolean getUserOK(){
return userOK;
}
public void setLogInFromCookie(boolean clogIn) {
this.logInFromCookie = clogIn;
}
public boolean getLogInFromCookie() {
return logInFromCookie;
}
public void setLogInFromLogIn(boolean logIn) {
this.logInFromLogIn = logIn;
}
public boolean getLogInFromLogIn() {
return logInFromLogIn;
}
public int getAccessCounter() {
return accessCounter;
}
public String getDefaultUser() {
return defaultUser;
}
public String getStatusMsg() {
return statusMsg;
}
/**
* Collects information about the latest action on the portal page,
* performs it, and re-reads the portlets. Should page be requested
* to be reloaded here?
*/
public void processRequest(HttpServletRequest request, HttpServletResponse response)
throws FinderException, NamingException, CreateException, RemoteException, RemoveException, java.io.IOException {
/**
* keep track on how many times the user has tried to logIn
* Could be used, for max number of attempts
*/
accessCounter ++;
//If the user hasn't logged in, make him! If he askeds to, let him.
if(!userOK || (action != null && action.equals("login")))
doLogIn(request, response);
try {
// TODO: Get from weblogic.security
if(userOK) {
System.out.println(" JspPortal.processRequest");
System.out.println();
System.out.println(" USER: "+getUser());
System.out.println(" action = "+getAction());
System.out.println(" portlet = "+portlet);
System.out.println(" userOK: "+getUserOK());
System.out.println(" logInFromCookie = "+getLogInFromCookie());
System.out.println(" logInFromLogIn = "+getLogInFromLogIn());
System.out.println(" accessCounter: "+getAccessCounter());
System.out.println();
// Ignore when same command is repeated
if( !sameRequestAsLast(request) ) {
if(action != null && action.equals("register")) {
registerUser(request, response);
} else if(action != null && action.equals("logout")) {
logOut(request, response);
} else if(action != null && action.equals("add")) {
fetchPortal().addPortlet(getUser(), portlet);
} else if(action != null && action.equals("addnarrow")) {
fetchPortal().addPortlet(getUser(), portlet);
} else if(action != null && action.equals("addwide")) {
fetchPortal().addPortlet(getUser(), portlet);
} else if(action != null && action.equals("del")) {
fetchPortal().removePortlet(getUser(), Long.parseLong(portlet));
}
} else System.out.println(" Repeated "+action+" ignored");
// Prepare the layout of the page
lookupPortlets(getUser());
}
}
finally {
resetState();
}
}
/**
* take care of all the logIn logic
*/
private void doLogIn(HttpServletRequest request, HttpServletResponse response){
System.out.println(" Doing logIn!!");
//If the user has pressed login-link
if(action != null && action.equals("login")){
/**
* NOTE: if no username was found or the the user couldn't be found in the DB
* the user will get the default name "guest".
*/
if(request.getParameter("loginid") != null)
if(validateUser(request.getParameter("loginid"))){
userOK = true;
user = request.getParameter("loginid");
logInFromLogIn = true;
//make a another cookie so the user don't have to login on at his next visit
//Normaly this cookie will overide the old cookie
makeCookie(response);
}
else{
userOK = true;
user = defaultUser;
}
else{
userOK = true;
user = defaultUser;
}
}
/*
* If the user has NOT pressed login-link, try cookie-login
*/
else{
//First: try to obtain the user name from a cookie
//look for Nordija Cookie
Cookie c[] = request.getCookies();
if(c != null)
for(int i = 0; i < c.length; i++){
//Nordija cookies found!, Now look for the rigth one
if(c[i].getName().equals("NordijaPortal"))
/**
* validate the username against the DB.
* NOTE: if the user have checked Out at his last visit
* the still cookie exist, but the value-name is "NOGO" in order to prevent
* non-registert users to access the portale
*/
if(validateUser(c[i].getValue())){
setUser(c[i].getValue());
userOK = true;
logInFromCookie = true;
}
else{
userOK = false;
logInFromCookie = false;
}
}
/**
* If the user still = !userOK, the user has no cookie and has NOT pressed the login-link,
* make him a guest
*/
if(!userOK){
userOK = true;
user = defaultUser;
}
}
}//End doLogIn
/**
* This method will take care of the removel a user-sensitive information
*/
public void logOut(HttpServletRequest request, HttpServletResponse response){
userOK = false;
user = defaultUser;
//try to find a cookie
Cookie logOut[] = request.getCookies();
if(logOut != null)
for(int i = 0; i < logOut.length; i++)
if(logOut[i].getName().equals("NordijaPortal")){
Cookie temp = logOut[i];
//It's not possible to delete a cookie, so we'll put "NOGO" as the value
temp.setValue("NOGO");
response.addCookie(temp);
}
statusMsg = "User logged out!";
}
/**
* Create a persistent cookie
*/
private void makeCookie(HttpServletResponse response){
try{
Cookie logInCook = new Cookie("NordijaPortal", getUser());
//make it last an hour!!
logInCook.setMaxAge(60 * 60);
logInCook.setComment("registert" + new Date());
response.addCookie(logInCook);
}
catch(java.lang.IllegalArgumentException iae) {
System.out.println("illegal cookie values!");
}
}
/**
* Seach for the user in the DB through UserHome
* @return true if the user is found, else false
*/
public boolean validateUser(String valiUser){
boolean result;
System.out.println("ValiUser fra validateUser: " + valiUser);
try{
Context ctx = new InitialContext();
PortalAdminHome home = (PortalAdminHome)ctx.lookup("nordija.portal.PortalAdmin");
PortalAdmin admin = home.create();
User userObj = admin.findUserByName(valiUser);
result = true;
}
catch(javax.ejb.CreateException ce){
System.out.println("Create Exception catched!! " + ce.toString());
result = false;
}
catch(javax.ejb.FinderException fe){
System.out.println("FinderException in JspPortal " + fe.toString());
result = false;
}
catch(javax.naming.NamingException ne){
System.out.println("NamingException in JspPortal " + ne.toString());
result = false;
}
catch(java.rmi.RemoteException re){
System.out.println("RemoteException in JspPortal " + re.toString());
result = false;
}
if(result)
statusMsg = "User found!";
else
statusMsg = "User NOT found! Using default user.";
return result;
}
private void registerUser(HttpServletRequest request, HttpServletResponse response) {
if(createUser(request)){
makeCookie(response);
if(request.getParameter("name") != null)
user = request.getParameter("name");
if(request.getParameter("email") != null)
email = request.getParameter("email");
statusMsg = "New user registered!";
}
}
private boolean createUser(HttpServletRequest request) {
boolean result = false;
String paramName = "name";
String paramEmail = "";
if(request.getParameter("name") != null)
paramName = request.getParameter("name");
if(request.getParameter("email") != null)
paramEmail = request.getParameter("email");
//Check if a user by the specified name already exists
if(validateUser(paramName)){
System.out.println("Could NOT register new user! User name already exists!!");
statusMsg = "Could NOT register new user! User name already exists!";
}
else{
try{
Context ctx = new InitialContext();
PortalAdminHome home = (PortalAdminHome)ctx.lookup("nordija.portal.PortalAdmin");
PortalAdmin admin = home.create();
User userObj = admin.createUser(paramName, paramEmail);
result = true;
}
catch(javax.ejb.CreateException ce){
System.out.println("Create Exception catched!! in registerUser " + ce.toString());
result = false;
}
catch(javax.naming.NamingException ne){
System.out.println("NamingException in JspPortal in registerUser" + ne.toString());
result = false;
}
catch(java.rmi.RemoteException re){
System.out.println("RemoteException in JspPortal in registerUser" + re.toString());
result = false;
}
}
return result;
}
private void lookupPortlets(String user) throws FinderException, NamingException, CreateException, RemoteException {
narrowPortlets = new PortletInfoEnumeration(fetchPortal().getSelectedNarrowPortlets(user));
widePortlets = new PortletInfoEnumeration(fetchPortal().getSelectedWidePortlets(user));
}
protected Portal fetchPortal() throws NamingException, CreateException, RemoteException {
if(portal == null) {
Context ctx = new InitialContext();
PortalHome home = (PortalHome)ctx.lookup(jndi_portal);
portal = home.create();
}
return portal;
}
/**
* Provides direct access to the underlying portal bean.
* <b>Will be removed</b>
*/
public Portal getPortal() throws NamingException, CreateException, RemoteException {
return fetchPortal();
}
/**
* Makes sure no data lives beyond the transaction
*/
private void resetState() throws CreateException, NamingException, RemoteException, FinderException {
portlet = null;
action = null;
}
/**
* Operates on the JspPortal maintained iterator to minimise the amount of Java code in the Jsp files
*/
public boolean hasMoreWidePortlets() {
return widePortlets.hasMoreElements();
}
/**
* Operates on the JspPortal maintained iterator to minimise the amount of Java code in the Jsp files
*/
public boolean hasMoreNarrowPortlets() {
return narrowPortlets.hasMoreElements();
}
/**
* Operates on the JspPortal maintained iterator to minimise the amount of Java code in the Jsp files
*/
public PortletInfo getNextWidePortlet() throws RemoteException {
if(widePortlets.hasMoreElements())
return widePortlets.nextPortletInfo();
throw new RemoteException("No more wide portlets available");
}
/**
* Operates on the JspPortal maintained iterator to minimise the amount of Java code in the Jsp files
*/
public PortletInfo getNextNarrowPortlet()throws RemoteException {
if(narrowPortlets.hasMoreElements())
return narrowPortlets.nextPortletInfo();
throw new RemoteException("No more narrow portlets available");
}
/**
* Dispatches the request to the jspfile specified by the portletinfo.
* The jspfile is compiled if needed.
*/
public void JspDispatcher(HttpServletRequest request, HttpServletResponse response, PortletInfo mi)
throws IOException, ServletException
{
RequestDispatcher rd = request.getRequestDispatcher("/"+mi.jspfile);
if (rd != null)
rd.include(request, response);
else
System.out.println("DEBUG: JspPortal.JspDispatcher(): Unknown portlet exception should be thrown. JSP Portlet not found: "+mi.jspfile);
}
/**
* Initialises an iterator containing all portlets in the "wide" category, selected by the user.
*/
protected PortletInfoEnumeration getSelectedWidePortlets()
throws CreateException, FinderException, NamingException, RemoteException {
return new PortletInfoEnumeration(fetchPortal().getSelectedWidePortlets(getUser()));
}
/*
* Initialises an iterator containing all portlets in the "narrow" category, selected by the user.
*/
protected PortletInfoEnumeration getSelectedNarrowPortlets()
throws CreateException, FinderException, NamingException, RemoteException {
return new PortletInfoEnumeration(fetchPortal().getSelectedNarrowPortlets(getUser()));
}
private HttpServletRequest lastRequest;
public boolean sameRequestAsLast(HttpServletRequest request) {
boolean same=false;
if(lastRequest != null && request.getQueryString() != null)
same = request.getQueryString().equals(lastRequest.getQueryString());
lastRequest = request;
return same;
}
}