2
package com.eireneh.bible.view.servlet;
6
import java.io.IOException;
9
import javax.servlet.http.*;
11
import com.eireneh.util.*;
12
import com.eireneh.config.choices.*;
13
import com.eireneh.config.*;
15
import com.eireneh.bible.util.*;
16
import com.eireneh.bible.book.*;
17
import com.eireneh.bible.control.*;
20
* The State class takes a Cookie and maintains some state on the current
21
* user and their preferences.
23
* <table border='1' cellPadding='3' cellSpacing='0' width="100%">
24
* <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
25
* Distribution Licence:<br />
26
* Project B is free software; you can redistribute it
27
* and/or modify it under the terms of the GNU General Public License,
28
* version 2 as published by the Free Software Foundation.<br />
29
* This program is distributed in the hope that it will be useful,
30
* but WITHOUT ANY WARRANTY; without even the implied warranty of
31
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32
* General Public License for more details.<br />
33
* The License is available on the internet
34
* <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
35
* <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
36
* MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
37
* The copyright to this program is held by it's authors.
38
* </font></td></tr></table>
39
* @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
43
public class CookieState extends State
46
* Create a State object from the data about the incomming request
47
* @param request A description of the request
48
* @param response Data on the reply
50
public CookieState(HttpServletRequest request, HttpServletResponse response)
52
this.request = request;
53
this.response = response;
55
// Find the id of the person
56
Cookie[] cookies = request.getCookies();
57
for (int i=0; i<cookies.length; i++)
59
if (cookies[i].getName().equals("id"))
61
id = cookies[i].getValue();
66
// New user or no ID. Create new ID
69
id = ""+System.currentTimeMillis();
70
Cookie cooked = new Cookie("id", id);
71
cooked.setMaxAge(MAX_AGE);
72
response.addCookie(cooked);
74
log.fine("Created config cookie id="+id+" ip="+request.getRemoteAddr());
82
* Save any changes to the Config back to disk
84
public void saveConfig() throws IOException
86
URL url = NetUtil.lengthenURL(Project.getCookiesRoot(), id+".properties");
87
config.localToPermanent(url);
91
* Load an the config with settings. This method should not fail so
92
* if for some reason there is an error we should just use some
93
* defaults and carry on (posibly with a note to the Log)
95
public void loadConfig()
99
URL url = NetUtil.lengthenURL(Project.getCookiesRoot(), id+".properties");
100
config.permanentToLocal(url);
101
config.localToApplication(true);
105
// prop = new Properties();
106
// Ignore they've probably just not changed their settings
110
/** Cookie Max Age */
111
public static final int MAX_AGE = 60 * 60 * 24 * 365 * 10;
113
/** The http request data */
114
private HttpServletRequest request;
116
/** The http response data */
117
private HttpServletResponse response;
119
/** The log stream */
120
protected static Logger log = Logger.getLogger(CookieState.class);