01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2007 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.lib.web.micro.http;
28:
29: import java.io.IOException;
30: import java.net.URI;
31: import java.util.Map;
32: import javax.servlet.ServletException;
33: import javax.servlet.http.HttpServletRequest;
34: import javax.servlet.http.HttpServletResponse;
35:
36: import org.cougaar.lib.web.micro.base.ClientFactory;
37: import org.cougaar.lib.web.micro.base.Connection;
38: import org.cougaar.lib.web.micro.base.ServletTunnel;
39: import org.cougaar.lib.web.micro.base.SocketClientFactory;
40: import org.cougaar.lib.web.redirect.HttpServletRedirector;
41:
42: /**
43: * This component tunnels servlet requests through HTTP.
44: */
45: public class HttpServletTunnel extends HttpServletRedirector {
46:
47: protected boolean isSupported(String s) {
48: return ("http_tunnel".equals(s) || "-".equals(s));
49: }
50:
51: protected void doRedirect(String encName, String location,
52: HttpServletRequest req, HttpServletResponse res)
53: throws ServletException, IOException {
54:
55: if (log.isInfoEnabled()) {
56: log.info("Tunneling request for " + encName + " to "
57: + location);
58: }
59:
60: // encode for redirect -- typically a no-op
61: String encLoc = res.encodeRedirectURL(location);
62:
63: // extract host:port
64: URI uri;
65: try {
66: uri = URI.create(encLoc);
67: } catch (Exception e) {
68: throw new RuntimeException("Invalid tunnel location URI: "
69: + encLoc);
70: }
71:
72: // TODO use encLoc in ServletTunnel header line, in case the contextPath
73: // is different on the remote host
74:
75: // tunnel
76: Map metaData = ServletTunnel.extractMetaData(req);
77: ClientFactory client_factory = new SocketClientFactory();
78: Connection con = client_factory.connect(uri, metaData);
79: ServletTunnel.tunnel(req, res, con);
80: }
81:
82: }
|