001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.rewrite;
031:
032: import com.caucho.config.program.ContainerProgram;
033: import com.caucho.config.program.ConfigProgram;
034: import com.caucho.config.*;
035: import com.caucho.config.types.*;
036: import com.caucho.server.connection.*;
037: import com.caucho.server.dispatch.*;
038: import com.caucho.server.webapp.*;
039: import com.caucho.util.L10N;
040:
041: import javax.servlet.*;
042: import javax.servlet.http.*;
043: import java.io.*;
044: import java.util.logging.*;
045:
046: public class ProxyRule extends AbstractRuleWithConditions {
047: private static final L10N L = new L10N(ProxyRule.class);
048: private static final Logger log = Logger.getLogger(ProxyRule.class
049: .getName());
050:
051: private final WebApp _webApp;
052:
053: private ServletConfigImpl _servlet;
054: private String _target;
055:
056: private ContainerProgram _program = new ContainerProgram();
057:
058: ProxyRule(RewriteDispatch rewriteDispatch, WebApp webApp) {
059: super (rewriteDispatch);
060:
061: _webApp = webApp;
062: }
063:
064: public String getTagName() {
065: return "proxy";
066: }
067:
068: public void setTarget(String target) {
069: _target = target;
070: }
071:
072: public void addBuilderProgram(ConfigProgram program) {
073: _program.addProgram(program);
074: }
075:
076: @Override
077: public FilterChain dispatch(String uri, String queryString,
078: FilterChain accept, FilterChainMapper next)
079: throws ServletException {
080: if (_target != null)
081: return new ProxyFilterChain(_servlet.createServletChain(),
082: _target, queryString);
083: else
084: return new ProxyFilterChain(_servlet.createServletChain(),
085: uri, queryString);
086: }
087:
088: @Override
089: public void init() throws ConfigException {
090: super .init();
091:
092: try {
093: _servlet = new ServletConfigImpl();
094:
095: _servlet.setServletName("resin-dispatch-lb");
096: Class cl = Class
097: .forName("com.caucho.servlets.HttpProxyServlet");
098: _servlet
099: .setServletClass("com.caucho.servlets.HttpProxyServlet");
100:
101: _servlet.setInit(_program);
102:
103: _webApp.addServlet(_servlet);
104: } catch (ServletException ex) {
105: throw ConfigException.create(ex);
106: } catch (ClassNotFoundException e) {
107: log.log(Level.FINER, e.toString(), e);
108:
109: throw new ConfigException(L
110: .l("load-balance requires Resin Professional"));
111: }
112: }
113:
114: public static class ProxyFilterChain extends AbstractFilterChain {
115: private final FilterChain _next;
116: private final String _uri;
117: private final String _queryString;
118:
119: ProxyFilterChain(FilterChain next, String uri,
120: String queryString) {
121: _next = next;
122:
123: _uri = uri;
124: _queryString = queryString;
125: }
126:
127: public void doFilter(ServletRequest req, ServletResponse res)
128: throws IOException, ServletException {
129: _next.doFilter(new ProxyRequest(req, _uri, _queryString),
130: res);
131: }
132: }
133:
134: public static class ProxyRequest extends HttpServletRequestWrapper {
135: private String _uri;
136: private String _queryString;
137:
138: ProxyRequest(ServletRequest req, String uri, String queryString) {
139: super ((HttpServletRequest) req);
140:
141: _uri = uri;
142: _queryString = queryString;
143: }
144:
145: /**
146: * Returns the proxy uri
147: */
148: public String getRequestURI() {
149: return _uri;
150: }
151:
152: /**
153: * Returns the proxy query string
154: */
155: public String getQueryString() {
156: return _queryString;
157: }
158: }
159: }
|