cc16c218fb7a3fdeefa5c92d0e0baf1b14ed1269
[mir.git] / source / mir / session / HTTPAdapters.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mir.session;
31
32 import java.net.InetAddress;
33 import java.net.UnknownHostException;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.Enumeration;
38 import java.util.Iterator;
39 import java.util.List;
40
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpSession;
43
44 import mir.util.HTTPParsedRequest;
45
46 import org.apache.commons.fileupload.FileItem;
47
48 public class HTTPAdapters {
49   public static class HTTPRequestAdapter implements Request {
50     private HttpServletRequest request;
51     private String cachedHostName = null;
52
53     public HTTPRequestAdapter(HttpServletRequest aRequest) {
54       request = aRequest;
55     }
56
57     public String getHeader(String aHeaderName) {
58       if (aHeaderName.equals("ip"))
59         return request.getRemoteAddr();
60
61       if (aHeaderName.equals("hostname")) {
62         if (aHeaderName.equals("hostname")) {
63           if (cachedHostName==null) {
64             try {
65               cachedHostName = InetAddress.getByName(request.getRemoteAddr()).getHostName();
66             }
67             catch (UnknownHostException e) {
68               cachedHostName = request.getRemoteAddr();
69             }
70           }
71
72           return cachedHostName;
73         }
74
75       }
76
77       return request.getHeader(aHeaderName);
78     }
79
80     public String getParameter(String aName) {
81       return request.getParameter(aName);
82     }
83
84     public List getPrefixedParameterNames(String aPrefix) {
85       List result = new ArrayList();
86
87       Enumeration enumeration = request.getParameterNames();
88       while (enumeration.hasMoreElements()) {
89         String name = (String) enumeration.nextElement();
90         if (name.startsWith(aPrefix)) {
91           result.add(name);
92         }
93       }
94
95       return result;
96     }
97
98     public List getUploadedFiles() {
99       return Collections.EMPTY_LIST;
100     }
101
102     public List getParameters(String aName) {
103       return Arrays.asList(request.getParameterValues(aName));
104     }
105
106     public HttpServletRequest getRequest() {
107       return request;
108     }
109   }
110
111   public static class HTTPParsedRequestAdapter implements Request {
112     private HTTPParsedRequest request;
113     private String cachedHostName = null;
114
115     public HTTPParsedRequestAdapter(HTTPParsedRequest aRequest) {
116       request = aRequest;
117     }
118
119     public String getHeader(String aHeaderName) {
120       if (aHeaderName.equals("ip")) {
121         // transparent proxies propagate the originating ip in the x-forwarded-for
122         // header. So if it's there, we should use it.
123         Enumeration headers = request.getRequest().getHeaders("x-forwarded-for");
124         if (headers.hasMoreElements()) {
125           return (String) headers.nextElement();
126         }
127                                 return request.getRequest().getRemoteAddr();
128       }
129
130       if (aHeaderName.equals("hostname")) {
131         if (cachedHostName==null) {
132           try {
133             cachedHostName = InetAddress.getByName(getHeader("ip")).getHostName();
134           }
135           catch (UnknownHostException e) {
136             cachedHostName = request.getRequest().getRemoteAddr();
137           }
138         }
139
140         return cachedHostName;
141       }
142
143       return request.getHeader(aHeaderName);
144     }
145
146     public String getParameter(String aName) {
147       return request.getParameter(aName);
148     }
149
150     public List getParameters(String aName) {
151       return request.getParameterList(aName);
152     }
153
154     public List getPrefixedParameterNames(String aPrefix) {
155       List result = new ArrayList();
156
157       Iterator i = request.getParameterNames().iterator();
158
159       while (i.hasNext()) {
160         String name = (String) i.next();
161         if (name.startsWith(aPrefix)) {
162           result.add(name);
163         }
164       }
165       return result;
166     }
167
168     public List getUploadedFiles() {
169       List result = new ArrayList();
170       List files = request.getFiles();
171
172       for (int i=0; i<files.size(); i++) {
173         result.add(new CommonsUploadedFileAdapter((FileItem) files.get(i)));
174       }
175
176       return result;
177     }
178
179     public HttpServletRequest getRequest() {
180       return request.getRequest();
181     }
182   }
183
184   public static class HTTPSessionAdapter implements Session {
185     private HttpSession session;
186
187     public HTTPSessionAdapter(HttpSession aSession) {
188       session = aSession;
189     }
190     public Object getAttribute(String aName) {
191       return session.getAttribute(aName);
192     }
193
194     public void deleteAttribute(String aName) {
195       session.removeAttribute(aName);
196     }
197
198     public void setAttribute(String aName, Object aNewValue) {
199       if (aName.equals("$httpsessiontimeout")) {
200         if (aNewValue instanceof Number) {
201           try {
202             session.setMaxInactiveInterval( ( (Number) aNewValue).intValue());
203           }
204           catch (Throwable t) {
205           }
206         }
207       }
208       else {
209         if (aNewValue == null)
210           deleteAttribute(aName);
211         else
212           session.setAttribute(aName, aNewValue);
213       }
214     }
215
216     public void terminate() {
217       session.invalidate();
218     }
219   }
220 }