2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
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;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpSession;
44 import mir.util.HTTPParsedRequest;
46 import org.apache.commons.fileupload.FileItem;
48 public class HTTPAdapters {
49 public static class HTTPRequestAdapter implements Request {
50 private HttpServletRequest request;
51 private String cachedHostName = null;
53 public HTTPRequestAdapter(HttpServletRequest aRequest) {
57 public String getHeader(String aHeaderName) {
58 if (aHeaderName.equals("ip"))
59 return request.getRemoteAddr();
61 if (aHeaderName.equals("hostname")) {
62 if (aHeaderName.equals("hostname")) {
63 if (cachedHostName==null) {
65 cachedHostName = InetAddress.getByName(request.getRemoteAddr()).getHostName();
67 catch (UnknownHostException e) {
68 cachedHostName = request.getRemoteAddr();
72 return cachedHostName;
77 return request.getHeader(aHeaderName);
80 public String getParameter(String aName) {
81 return request.getParameter(aName);
84 public List getPrefixedParameterNames(String aPrefix) {
85 List result = new ArrayList();
87 Enumeration enumeration = request.getParameterNames();
88 while (enumeration.hasMoreElements()) {
89 String name = (String) enumeration.nextElement();
90 if (name.startsWith(aPrefix)) {
98 public List getUploadedFiles() {
99 return Collections.EMPTY_LIST;
102 public List getParameters(String aName) {
103 return Arrays.asList(request.getParameterValues(aName));
106 public HttpServletRequest getRequest() {
111 public static class HTTPParsedRequestAdapter implements Request {
112 private HTTPParsedRequest request;
113 private String cachedHostName = null;
115 public HTTPParsedRequestAdapter(HTTPParsedRequest aRequest) {
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();
127 return request.getRequest().getRemoteAddr();
130 if (aHeaderName.equals("hostname")) {
131 if (cachedHostName==null) {
133 cachedHostName = InetAddress.getByName(getHeader("ip")).getHostName();
135 catch (UnknownHostException e) {
136 cachedHostName = request.getRequest().getRemoteAddr();
140 return cachedHostName;
143 return request.getHeader(aHeaderName);
146 public String getParameter(String aName) {
147 return request.getParameter(aName);
150 public List getParameters(String aName) {
151 return request.getParameterList(aName);
154 public List getPrefixedParameterNames(String aPrefix) {
155 List result = new ArrayList();
157 Iterator i = request.getParameterNames().iterator();
159 while (i.hasNext()) {
160 String name = (String) i.next();
161 if (name.startsWith(aPrefix)) {
168 public List getUploadedFiles() {
169 List result = new ArrayList();
170 List files = request.getFiles();
172 for (int i=0; i<files.size(); i++) {
173 result.add(new CommonsUploadedFileAdapter((FileItem) files.get(i)));
179 public HttpServletRequest getRequest() {
180 return request.getRequest();
184 public static class HTTPSessionAdapter implements Session {
185 private HttpSession session;
187 public HTTPSessionAdapter(HttpSession aSession) {
190 public Object getAttribute(String aName) {
191 return session.getAttribute(aName);
194 public void deleteAttribute(String aName) {
195 session.removeAttribute(aName);
198 public void setAttribute(String aName, Object aNewValue) {
199 if (aName.equals("$httpsessiontimeout")) {
200 if (aNewValue instanceof Number) {
202 session.setMaxInactiveInterval( ( (Number) aNewValue).intValue());
204 catch (Throwable t) {
209 if (aNewValue == null)
210 deleteAttribute(aName);
212 session.setAttribute(aName, aNewValue);
216 public void terminate() {
217 session.invalidate();