/*
* $Id: SessionPreferences.java,v 1.20 2002/09/16 08:05:06 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.server;
import java.io.IOException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import anvil.session.Session;
/**
* class SessionPreferences
*
* @author: Jani Lehtim�ki
*/
public class SessionPreferences extends Preferences
{
public static final String[] PREFERENCES = {
"required", "boolean",
"lifetime", "int",
"timeout", "int",
"autoredirect", "boolean",
"cookies", "boolean",
"cookiename", "string",
"cookiepath", "boolean",
"cookiedomain", "boolean",
"cookiesecured", "boolean",
"cookielifetime", "int",
};
private boolean _required = false;
private int _lifetime = 7200;
private int _timeout = 1800;
private boolean _auto_redirect = true;
private boolean _use_cookies = false;
private String _cookie_name = "sessionid";
private String _cookie_path = null;
private String _cookie_domain = null;
private boolean _cookie_secured = false;
private int _cookie_lifetime = 7200;
public SessionPreferences(Zone parent)
{
super(parent);
}
public void setRequired(boolean required)
{
_required = required;
}
public boolean getRequired()
{
return _required;
}
public void setLifetime(int timeout)
{
_lifetime = timeout;
}
public int getLifetime()
{
return _lifetime;
}
public void setTimeout(int timeout)
{
_timeout = timeout;
}
public int getTimeout()
{
return _timeout;
}
public void setAutoRedirect(boolean redirect)
{
_auto_redirect = redirect;
}
public boolean getAutoRedirect()
{
return _auto_redirect;
}
public void setUseCookies(boolean cookies)
{
_use_cookies = cookies;
}
public boolean getUseCookies()
{
return _use_cookies;
}
public void setCookieName(String name)
{
if (name != null) {
_cookie_name = name;
}
}
public String getCookieName()
{
return _cookie_name;
}
public void setCookiePath(String path)
{
if (path != null) {
_cookie_path = path;
}
}
public String getCookiePath()
{
return _cookie_path;
}
public void setCookieDomain(String domain)
{
if (domain != null) {
_cookie_domain = domain;
}
}
public String getCookieDomain()
{
return _cookie_domain;
}
public void setCookieSecured(boolean secure)
{
_cookie_secured = secure;
}
public boolean getCookieSecured()
{
return _cookie_secured;
}
public void setCookieLifetime(int lifetime)
{
_cookie_lifetime = lifetime;
}
public int getCookieLifetime()
{
return _cookie_lifetime;
}
public int getType()
{
return SESSION;
}
public String[] getPreferences()
{
return PREFERENCES;
}
public Object getPreference(String name)
{
if (name.equalsIgnoreCase("required")) {
return _required ? Boolean.TRUE : Boolean.FALSE;
}
if (name.equalsIgnoreCase("lifetime")) {
return new Integer(_lifetime);
}
if (name.equalsIgnoreCase("timeout")) {
return new Integer(_timeout);
}
if (name.equalsIgnoreCase("autoredirect")) {
return _auto_redirect ? Boolean.TRUE : Boolean.FALSE;
}
if (name.equalsIgnoreCase("cookies")) {
return _use_cookies ? Boolean.TRUE : Boolean.FALSE;
}
if (name.equalsIgnoreCase("cookiename")) {
return _cookie_name;
}
if (name.equalsIgnoreCase("cookiepath")) {
return _cookie_path;
}
if (name.equalsIgnoreCase("cookiedomain")) {
return _cookie_domain;
}
if (name.equalsIgnoreCase("cookiesecured")) {
return _cookie_secured ? Boolean.TRUE : Boolean.FALSE;
}
if (name.equalsIgnoreCase("cookielifetime")) {
return new Integer(_cookie_lifetime);
}
return null;
}
public boolean setPreference(String name, String value)
{
if (name.equalsIgnoreCase("required")) {
setRequired(ConfigReader.toBoolean(value));
} else if (name.equalsIgnoreCase("lifetime")) {
setLifetime(ConfigReader.toInt(value));
} else if (name.equalsIgnoreCase("timeout")) {
setTimeout(ConfigReader.toInt(value));
} else if (name.equalsIgnoreCase("autoredirect")) {
setAutoRedirect(ConfigReader.toBoolean(value));
} else if (name.equalsIgnoreCase("cookies")) {
setUseCookies(ConfigReader.toBoolean(value));
} else if (name.equalsIgnoreCase("cookiename")) {
setCookieName(value);
} else if (name.equalsIgnoreCase("cookiepath")) {
setCookiePath(value);
} else if (name.equalsIgnoreCase("cookiedomain")) {
setCookieDomain(value);
} else if (name.equalsIgnoreCase("cookiesecured")) {
setCookieSecured(ConfigReader.toBoolean(value));
} else if (name.equalsIgnoreCase("cookielifetime")) {
setCookieLifetime(ConfigReader.toInt(value));
} else {
return false;
}
return true;
}
public boolean service(Context context)
throws IOException
{
if (_required) {
String pathinfo = context.getPathinfo();
Session session = context.getSession();
if (session != null) {
return true;
}
boolean hascookie = false;
if (_use_cookies) {
Cookie cookies[] = context.getRequest().getCookies();
if (cookies != null) {
int n = cookies.length;
for(int i=0; i<n; i++) {
Cookie c = cookies[i];
if (c.getName().equals(_cookie_name)) {
String s = c.getValue();
session = context.getSession(s);
if (session != null) {
context.setSession(session);
hascookie = true;
break;
}
}
}
}
}
if (session == null) {
session = context.createSession();
context.setSession(session);
}
if (_use_cookies && !hascookie) {
Cookie c = new Cookie(_cookie_name, session.getId());
c.setMaxAge(_cookie_lifetime);
c.setSecure(_cookie_secured);
if (_cookie_domain != null) {
c.setDomain(_cookie_domain);
}
if (_cookie_path != null) {
c.setPath(_cookie_path);
}
context.getResponse().addCookie(c);
}
if (_auto_redirect) {
throw new RedirectException(session.getId(), context.getOriginalPathinfo());
} else {
return true;
}
}
return true;
}
}