support for custom session timeouts
[mir.git] / source / mir / session / HTTPAdapters.java
1 /*\r
2  * Copyright (C) 2001, 2002 The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with  any library licensed under the Apache Software License,\r
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
23  * (or with modified versions of the above that use the same license as the above),\r
24  * and distribute linked combinations including the two.  You must obey the\r
25  * GNU General Public License in all respects for all of the code used other than\r
26  * the above mentioned libraries.  If you modify this file, you may extend this\r
27  * exception to your version of the file, but you are not obligated to do so.\r
28  * If you do not wish to do so, delete this exception statement from your version.\r
29  */\r
30 package mir.session;\r
31 \r
32 import java.util.Arrays;\r
33 import java.util.List;\r
34 import java.util.Vector;\r
35 import javax.servlet.http.HttpServletRequest;\r
36 import javax.servlet.http.HttpSession;\r
37 \r
38 import org.apache.commons.fileupload.FileItem;\r
39 \r
40 import mir.util.HTTPParsedRequest;\r
41 \r
42 public class HTTPAdapters {\r
43   public static class HTTPRequestAdapter implements Request {\r
44     private HttpServletRequest request;\r
45 \r
46     public HTTPRequestAdapter(HttpServletRequest aRequest) {\r
47       request = aRequest;\r
48     }\r
49 \r
50     public String getHeader(String aHeaderName) {\r
51       if (aHeaderName.equals("ip"))\r
52         return request.getRemoteAddr();\r
53 \r
54       return request.getHeader(aHeaderName);\r
55     };\r
56 \r
57     public String getParameter(String aName) {\r
58       return request.getParameter(aName);\r
59     };\r
60 \r
61     public List getUploadedFiles() {\r
62       return new Vector();\r
63     };\r
64 \r
65     public List getParameters(String aName) {\r
66       return Arrays.asList(request.getParameterValues(aName));\r
67     };\r
68 \r
69     public HttpServletRequest getRequest() {\r
70       return request;\r
71     }\r
72   }\r
73 \r
74   public static class HTTPParsedRequestAdapter implements Request {\r
75     private HTTPParsedRequest request;\r
76 \r
77     public HTTPParsedRequestAdapter(HTTPParsedRequest aRequest) {\r
78       request = aRequest;\r
79     }\r
80 \r
81     public String getHeader(String aHeaderName) {\r
82       if (aHeaderName.equals("ip"))\r
83         return request.getRequest().getRemoteAddr();\r
84 \r
85       return request.getHeader(aHeaderName);\r
86     };\r
87 \r
88     public String getParameter(String aName) {\r
89       return request.getParameter(aName);\r
90     };\r
91 \r
92     public List getParameters(String aName) {\r
93       return request.getParameterList(aName);\r
94     };\r
95 \r
96     public List getUploadedFiles() {\r
97       List result = new Vector();\r
98       List files = request.getFiles();\r
99 \r
100       for (int i=0; i<files.size(); i++) {\r
101         result.add(new CommonsUploadedFileAdapter((FileItem) files.get(i)));\r
102       }\r
103 \r
104       return result;\r
105     };\r
106 \r
107     public HttpServletRequest getRequest() {\r
108       return request.getRequest();\r
109     }\r
110   }\r
111 \r
112   public static class HTTPSessionAdapter implements Session {\r
113     private HttpSession session;\r
114 \r
115     public HTTPSessionAdapter(HttpSession aSession) {\r
116       session = aSession;\r
117     }\r
118     public Object getAttribute(String aName) {\r
119       return session.getAttribute(aName);\r
120     }\r
121 \r
122     public void deleteAttribute(String aName) {\r
123       session.removeAttribute(aName);\r
124     }\r
125 \r
126     public void setAttribute(String aName, Object aNewValue) {\r
127       if (aName.equals("$httpsessiontimeout")) {\r
128         if (aNewValue instanceof Number) {\r
129           try {\r
130             session.setMaxInactiveInterval( ( (Number) aNewValue).intValue());\r
131           }\r
132           catch (Throwable t) {\r
133           }\r
134         }\r
135       }\r
136       else {\r
137         if (aNewValue == null)\r
138           deleteAttribute(aName);\r
139         else\r
140           session.setAttribute(aName, aNewValue);\r
141       }\r
142     }\r
143 \r
144     public void terminate() {\r
145       session.invalidate();\r
146     }\r
147   }\r
148 }