001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.webserver.plumbing;
027:
028: import org.jicarilla.http.HTTPField;
029: import org.jicarilla.http.HTTPMessage;
030: import org.jicarilla.lang.RegexpSelector;
031: import org.jicarilla.lang.Selector;
032:
033: import java.net.InetAddress;
034: import java.util.ArrayList;
035: import java.util.Iterator;
036: import java.util.List;
037: import java.util.regex.Pattern;
038:
039: /**
040: *
041: *
042: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
043: * @version $Id: HTTPSelector.java,v 1.1 2004/03/31 12:11:00 lsimons Exp $
044: */
045: public class HTTPSelector implements Selector {
046: protected List m_methodSelectors = new ArrayList();
047: protected List m_uriSelectors = new ArrayList();
048: protected List m_versionSelectors = new ArrayList();
049: protected List m_headerSelectors = new ArrayList();
050: protected List m_bodySelectors = new ArrayList();
051:
052: public final static boolean POLICY_AND = true;
053: public final static boolean POLICY_OR = false;
054:
055: protected boolean m_methodSelectorPolicy = POLICY_OR;
056: protected boolean m_uriSelectorPolicy = POLICY_OR;
057: protected boolean m_versionSelectorPolicy = POLICY_OR;
058: protected boolean m_headerSelectorPolicy = POLICY_AND;
059: protected boolean m_bodySelectorPolicy = POLICY_AND;
060: protected boolean m_globalPolicy = POLICY_AND;
061:
062: public boolean select(final Object object) {
063: if (!(object instanceof HTTPEvent))
064: return false;
065:
066: final HTTPEvent event = (HTTPEvent) object;
067: final HTTPMessage request = event.getHTTPRequest();
068:
069: if (m_globalPolicy == POLICY_AND)
070: return selectStartLine(request) && selectHeaders(request)
071: && selectBody(request);
072: else
073: // POLICY_OR
074: return selectStartLine(request) || selectHeaders(request)
075: || selectBody(request);
076: }
077:
078: public void addURLPattern(final Pattern pattern) {
079: final Selector s = new RegexpSelector(pattern);
080: m_uriSelectors.add(s);
081: }
082:
083: public void addVirtualHost(final InetAddress host) {
084: final String hostname = host.getHostName();
085: final Pattern pattern = Pattern.compile("Host:[ \\t]*"
086: + hostname, Pattern.CASE_INSENSITIVE);
087: final Selector s = new RegexpSelector(pattern);
088: m_uriSelectors.add(s);
089: }
090:
091: protected boolean selectStartLine(final HTTPMessage request) {
092: if (m_globalPolicy == POLICY_AND)
093: return selectMethod(request) && selectURI(request)
094: && selectVersion(request);
095: else
096: // POLICY_OR
097: return selectMethod(request) || selectURI(request)
098: || selectVersion(request);
099: }
100:
101: protected boolean selectMethod(final HTTPMessage request) {
102: if (m_methodSelectors.size() == 0)
103: return true;
104:
105: final String method = request.getField1String();
106: return select(method, m_methodSelectors, m_methodSelectorPolicy);
107: }
108:
109: protected boolean selectURI(final HTTPMessage request) {
110: if (m_uriSelectors.size() == 0)
111: return true;
112:
113: final String uri = request.getField2String();
114: return select(uri, m_uriSelectors, m_uriSelectorPolicy);
115: }
116:
117: protected boolean selectVersion(final HTTPMessage request) {
118: if (m_versionSelectors.size() == 0)
119: return true;
120:
121: final String version = request.getField3String();
122: return select(version, m_methodSelectors,
123: m_methodSelectorPolicy);
124: }
125:
126: protected boolean selectHeaders(final HTTPMessage request) {
127: if (m_versionSelectors.size() == 0)
128: return true;
129:
130: final List headers = request.getHeaders();
131: final Iterator it = headers.iterator();
132: if (m_headerSelectorPolicy == POLICY_AND) {
133: while (it.hasNext()) {
134: final HTTPField header = (HTTPField) it.next();
135: final String headerString = header.toExternalForm();
136: if (!selectHeader(headerString, m_headerSelectors,
137: m_headerSelectorPolicy))
138: return false;
139: }
140: return true;
141: } else {
142: while (it.hasNext()) {
143: final HTTPField header = (HTTPField) it.next();
144: final String headerString = header.toExternalForm();
145: if (selectHeader(headerString, m_headerSelectors,
146: m_headerSelectorPolicy))
147: return true;
148: }
149: return true;
150: }
151: }
152:
153: protected boolean selectBody(final HTTPMessage request) {
154: if (m_bodySelectors.size() == 0)
155: return true;
156:
157: return select(request.getBodyAsString(), m_bodySelectors,
158: m_bodySelectorPolicy);
159: }
160:
161: protected boolean select(final String field, final List selectors,
162: final boolean policy) {
163: final Iterator it = selectors.iterator();
164: if (policy == POLICY_AND) {
165: while (it.hasNext()) {
166: final Selector selector = (Selector) it.next();
167: if (!selector.select(field))
168: return false;
169: }
170: return true;
171: } else {
172: // POLICY_OR
173: while (it.hasNext()) {
174: final Selector selector = (Selector) it.next();
175: if (selector.select(field))
176: return true;
177: }
178: return false;
179: }
180: }
181:
182: protected boolean selectHeader(final String header,
183: final List selectors, final boolean policy) {
184: final Iterator it = selectors.iterator();
185: while (it.hasNext()) {
186: final Selector selector = (Selector) it.next();
187: if (selector.select(header))
188: return true;
189: }
190: return false;
191: }
192: }
|