~sword-devel/jsword/trunk

« back to all changes in this revision

Viewing changes to jsword/java/historic/org/crosswire/bible/view/servlet/CookieState.java

  • Committer: joe
  • Date: 2002-10-08 21:36:18 UTC
  • Revision ID: svn-v4:a88caf3b-7e0a-0410-8d0d-cecb45342206:trunk:80
big config and comment update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
package com.eireneh.bible.view.servlet;
3
 
 
4
 
import java.util.*;
5
 
import java.io.File;
6
 
import java.io.IOException;
7
 
import java.net.URL;
8
 
 
9
 
import javax.servlet.http.*;
10
 
 
11
 
import com.eireneh.util.*;
12
 
import com.eireneh.config.choices.*;
13
 
import com.eireneh.config.*;
14
 
 
15
 
import com.eireneh.bible.util.*;
16
 
import com.eireneh.bible.book.*;
17
 
import com.eireneh.bible.control.*;
18
 
 
19
 
/**
20
 
* The State class takes a Cookie and maintains some state on the current
21
 
* user and their preferences.
22
 
*
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>
40
 
* @see docs.Licence
41
 
* @author Joe Walker
42
 
*/
43
 
public class CookieState extends State
44
 
{
45
 
    /**
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
49
 
    */
50
 
    public CookieState(HttpServletRequest request, HttpServletResponse response)
51
 
    {
52
 
        this.request = request;
53
 
        this.response = response;
54
 
 
55
 
        // Find the id of the person
56
 
        Cookie[] cookies = request.getCookies();
57
 
        for (int i=0; i<cookies.length; i++)
58
 
        {
59
 
            if (cookies[i].getName().equals("id"))
60
 
            {
61
 
                id = cookies[i].getValue();
62
 
                break;
63
 
            }
64
 
        }
65
 
 
66
 
        // New user or no ID. Create new ID
67
 
        if (id == null)
68
 
        {
69
 
            id = ""+System.currentTimeMillis();
70
 
            Cookie cooked = new Cookie("id", id);
71
 
            cooked.setMaxAge(MAX_AGE);
72
 
            response.addCookie(cooked);
73
 
 
74
 
            log.fine("Created config cookie id="+id+" ip="+request.getRemoteAddr());
75
 
        }
76
 
 
77
 
        initConfig();
78
 
        loadConfig();
79
 
    }
80
 
 
81
 
    /**
82
 
    * Save any changes to the Config back to disk
83
 
    */
84
 
    public void saveConfig() throws IOException
85
 
    {
86
 
        URL url = NetUtil.lengthenURL(Project.getCookiesRoot(), id+".properties");
87
 
        config.localToPermanent(url);
88
 
    }
89
 
 
90
 
    /**
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)
94
 
     */
95
 
    public void loadConfig()
96
 
    {
97
 
        try
98
 
        {
99
 
            URL url = NetUtil.lengthenURL(Project.getCookiesRoot(), id+".properties");
100
 
            config.permanentToLocal(url);
101
 
            config.localToApplication(true);
102
 
        }
103
 
        catch (Exception ex)
104
 
        {
105
 
            // prop = new Properties();
106
 
            // Ignore they've probably just not changed their settings
107
 
        }
108
 
    }
109
 
 
110
 
    /** Cookie Max Age */
111
 
    public static final int MAX_AGE = 60 * 60 * 24 * 365 * 10;
112
 
 
113
 
    /** The http request data */
114
 
    private HttpServletRequest request;
115
 
 
116
 
    /** The http response data */
117
 
    private HttpServletResponse response;
118
 
 
119
 
    /** The log stream */
120
 
    protected static Logger log = Logger.getLogger(CookieState.class);
121
 
}