KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > servlets > HttpProxyServlet


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.servlets;
31
32 import com.caucho.util.L10N;
33 import com.caucho.vfs.Path;
34 import com.caucho.vfs.ReadStream;
35 import com.caucho.vfs.ReadWritePair;
36 import com.caucho.vfs.Vfs;
37 import com.caucho.vfs.WriteStream;
38
39 import javax.servlet.GenericServlet JavaDoc;
40 import javax.servlet.ServletException JavaDoc;
41 import javax.servlet.ServletRequest JavaDoc;
42 import javax.servlet.ServletResponse JavaDoc;
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.io.OutputStream JavaDoc;
48 import java.io.PrintWriter JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.Enumeration JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.logging.Level JavaDoc;
53 import java.util.logging.Logger JavaDoc;
54
55 /**
56  * HTTP proxy
57  *
58  * <pre>
59  * &lt;servlet>
60  * &lt;servlet-name>http-proxy&lt;/servlet-name>
61  * &lt;servlet-class>com.caucho.servlets.HttpProxyServlet&lt;/servlet-class>
62  * &lt;init host='localhost:8081'/>
63  * &lt;/servlet>
64  * </pre>
65  */

66 public class HttpProxyServlet extends GenericServlet JavaDoc {
67   static protected final Logger JavaDoc log =
68     Logger.getLogger(HttpProxyServlet.class.getName());
69   static final L10N L = new L10N(HttpProxyServlet.class);
70   
71   private ArrayList JavaDoc<String JavaDoc> _hosts = new ArrayList JavaDoc<String JavaDoc>();
72   private Path []_urlPaths;
73   private int _roundRobin;
74
75   /**
76    * Adds a host
77    */

78   public void addHost(String JavaDoc host)
79   {
80     _hosts.add(host);
81   }
82
83   /**
84    * Initialize the servlet with the server's sruns.
85    */

86   public void init()
87     throws ServletException JavaDoc
88   {
89     if (_hosts.size() == 0)
90       throw new ServletException JavaDoc(L.l("HttpProxyServlet needs at least one host."));
91
92     _urlPaths = new Path[_hosts.size()];
93
94     for (int i = 0; i < _hosts.size(); i++) {
95       String JavaDoc host = _hosts.get(i);
96
97       if (host.startsWith("http"))
98     _urlPaths[i] = Vfs.lookup(host);
99       else
100     _urlPaths[i] = Vfs.lookup("http://" + host);
101     }
102   }
103
104   /**
105    * Handle the request.
106    */

107   public void service(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
108     throws ServletException JavaDoc, IOException JavaDoc
109   {
110     HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) request;
111     HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) response;
112
113     PrintWriter JavaDoc out = res.getWriter();
114
115     int startIndex = _roundRobin;
116     _roundRobin = (_roundRobin + 1) % _urlPaths.length;
117
118     for (int i = 0; i < _urlPaths.length; i++) {
119       int index = (startIndex + i) % _urlPaths.length;
120     
121       if (handleRequest(req, res, _urlPaths[index]))
122     return;
123     }
124       
125     res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
126   }
127
128   private boolean handleRequest(HttpServletRequest JavaDoc req,
129                 HttpServletResponse JavaDoc res,
130                 Path path)
131     throws ServletException JavaDoc, IOException JavaDoc
132   {
133     String JavaDoc hostURL = path.getURL();
134     
135     String JavaDoc uri;
136     if (req.isRequestedSessionIdFromUrl()) {
137       uri = (req.getRequestURI() + ";jsessionid=" +
138           req.getRequestedSessionId());
139     }
140     else
141       uri = req.getRequestURI();
142
143     if (req.getQueryString() != null)
144       uri += '?' + req.getQueryString();
145
146     path = path.lookup(uri);
147
148     ReadWritePair pair = path.openReadWrite();
149
150     ReadStream rs = pair.getReadStream();
151     WriteStream ws = pair.getWriteStream();
152
153     ws.setAttribute("method", req.getMethod());
154
155     Enumeration JavaDoc e = req.getHeaderNames();
156     while (e.hasMoreElements()) {
157       String JavaDoc name = (String JavaDoc) e.nextElement();
158       String JavaDoc value = req.getHeader(name);
159
160       ws.setAttribute(name, value);
161     }
162
163      try {
164       InputStream JavaDoc is = req.getInputStream();
165       ws.writeStream(is);
166
167       String JavaDoc status = (String JavaDoc) rs.getAttribute("status");
168       int statusCode = 200;
169
170       if (status != null) {
171     try {
172       statusCode = Integer.parseInt(status);
173     } catch (Throwable JavaDoc e1) {
174     }
175       }
176
177       String JavaDoc location = null;
178       Iterator JavaDoc iter = rs.getAttributeNames();
179       while (iter.hasNext()) {
180     String JavaDoc name = (String JavaDoc) iter.next();
181
182     if (name.equalsIgnoreCase("status")) {
183     }
184     else if (name.equalsIgnoreCase("transfer-encoding")) {
185     }
186     else if (name.equalsIgnoreCase("content-length")) {
187     }
188     else if (name.equalsIgnoreCase("location"))
189       location = (String JavaDoc) rs.getAttribute("location");
190     else
191       res.addHeader(name, (String JavaDoc) rs.getAttribute(name));
192       }
193
194       if (location == null) {
195       }
196       else if (location.startsWith(hostURL)) {
197     location = location.substring(hostURL.length());
198
199     String JavaDoc prefix;
200     if (req.isSecure()) {
201       if (req.getServerPort() != 443)
202         prefix = ("https://" + req.getServerName() +
203               ":" + req.getServerPort());
204       else
205         prefix = ("https://" + req.getServerName());
206     }
207     else {
208       if (req.getServerPort() != 80)
209         prefix = ("http://" + req.getServerName() +
210               ":" + req.getServerPort());
211       else
212         prefix = ("http://" + req.getServerName());
213     }
214     
215     if (! location.startsWith("/"))
216       location = prefix + "/" + location;
217     else
218       location = prefix + location;
219       }
220
221       if (location != null)
222     res.setHeader("Location", location);
223
224       if (statusCode == 302 && location != null)
225     res.sendRedirect(location);
226       else if (statusCode != 200)
227     res.setStatus(statusCode);
228     
229       OutputStream JavaDoc os = res.getOutputStream();
230       rs.writeToStream(os);
231     } catch (IOException JavaDoc e1) {
232       log.log(Level.FINE, e1.toString(), e1);
233
234       return false;
235     } finally {
236       ws.close();
237       rs.close();
238     }
239
240     return true;
241   }
242 }
243
Popular Tags