01: /*
02: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
03: *
04: * This file is part of Resin(R) Open Source
05: *
06: * Each copy or derived work must preserve the copyright notice and this
07: * notice unmodified.
08: *
09: * 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 Sam
28: */
29:
30: package com.caucho.server.rewrite;
31:
32: import javax.servlet.FilterChain;
33: import javax.servlet.ServletException;
34: import javax.servlet.ServletRequest;
35: import javax.servlet.ServletResponse;
36: import javax.servlet.http.*;
37: import java.io.IOException;
38: import java.util.logging.Level;
39: import java.util.logging.Logger;
40:
41: class ConditionFilterChain implements FilterChain {
42: private static final Logger log = Logger
43: .getLogger(ConditionFilterChain.class.getName());
44:
45: private final String _logPrefix;
46: private final String _uri;
47: private final String _targetUri;
48: private final FilterChain _failChain;
49: private final Condition[] _conditions;
50: private final FilterChain _passChain;
51:
52: private final boolean _isFiner;
53: private final boolean _isFinest;
54:
55: public ConditionFilterChain(String logPrefix, String uri,
56: String targetUri, Condition[] conditions,
57: FilterChain passChain, FilterChain failChain) {
58: assert conditions != null;
59: assert passChain != null;
60: assert failChain != null;
61:
62: _logPrefix = logPrefix;
63: _uri = uri;
64: _targetUri = targetUri;
65:
66: _conditions = conditions;
67: _passChain = passChain;
68: _failChain = failChain;
69:
70: _isFiner = log.isLoggable(Level.FINER);
71: _isFinest = log.isLoggable(Level.FINEST);
72: }
73:
74: public void doFilter(ServletRequest request,
75: ServletResponse response) throws ServletException,
76: IOException {
77: HttpServletRequest req = (HttpServletRequest) request;
78: HttpServletResponse res = (HttpServletResponse) response;
79:
80: for (int i = 0; i < _conditions.length; i++) {
81: if (!_conditions[i].isMatch(req, res)) {
82: if (_isFiner)
83: log.finer(_logPrefix + " '" + _uri + "' --> '"
84: + _targetUri + "'");
85:
86: _failChain.doFilter(request, response);
87: return;
88: }
89: }
90:
91: if (_isFiner)
92: log.finer(_logPrefix + " '" + _uri + "' --> '" + _targetUri
93: + "'");
94:
95: _passChain.doFilter(request, response);
96: }
97: }
|