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.http.selectors;
027:
028: import org.jicarilla.http.HTTPField;
029: import org.jicarilla.http.HTTPMessage;
030: import org.jicarilla.lang.Assert;
031: import org.jicarilla.lang.CriterionExposingSelector;
032: import org.jicarilla.webserver.plumbing.HTTPEvent;
033:
034: import java.util.Iterator;
035: import java.util.regex.Pattern;
036:
037: /**
038: *
039: *
040: * <p>Example:</p>
041: *
042: * <pre>
043: * Selector selector = new PatternMatchingHTTPFieldSelector(
044: * "host", "(?:[a-zA-Z0-9]+\.)*myhostname\.org" );
045: * boolean selected = selector.select(
046: * getHTTPMessage(
047: * "GET / HTTP/1.1",
048: * "Host: www.myhostname.org"
049: * )
050: * );
051: * Assert.assertTrue( selected );
052: * selected = selector.select(
053: * getHTTPMessage(
054: * "GET / HTTP/1.1",
055: * "host: www3.dynamic.sub.myhostname.org"
056: * )
057: * );
058: * Assert.assertTrue( selected );
059: * selected = selector.select(
060: * getHTTPMessage(
061: * "GET / HTTP/1.1",
062: * "Host: notmyhostname.org"
063: * )
064: * );
065: * Assert.assertFalse( selected );
066: * </pre>
067: *
068: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
069: * @version $Id: PatternMatchingHTTPFieldSelector.java,v 1.4 2004/03/31 12:10:59 lsimons Exp $
070: */
071: public class PatternMatchingHTTPFieldSelector implements
072: CriterionExposingSelector {
073: protected Pattern m_namePattern;
074: protected Pattern m_valuePattern;
075:
076: public PatternMatchingHTTPFieldSelector(
077: final Pattern fieldNamePattern) {
078: this (fieldNamePattern, null);
079: }
080:
081: public PatternMatchingHTTPFieldSelector(
082: final Pattern fieldNamePattern,
083: final Pattern fieldValuePattern) {
084: Assert.assertNotNull(
085: "fieldNamePattern argument may not be null",
086: fieldNamePattern);
087: /*Assert.assertNotNull( "fieldValueMatcher argument may not be null",
088: fieldValueMatcher );*/
089:
090: m_namePattern = fieldNamePattern;
091: m_valuePattern = fieldValuePattern;
092: }
093:
094: public Object getCriterion() {
095: return new Pattern[] { m_namePattern, m_valuePattern };
096: }
097:
098: public boolean select(final Object object) {
099: if (object == null)
100: return false;
101:
102: if (object instanceof HTTPField) {
103: final HTTPField field = (HTTPField) object;
104: return selectField(field);
105: }
106:
107: if (object instanceof HTTPMessage) {
108: final HTTPMessage message = (HTTPMessage) object;
109: return selectHTTPMessage(message);
110: }
111:
112: if (object instanceof HTTPEvent) {
113: final HTTPEvent event = (HTTPEvent) object;
114: final HTTPMessage message = event.getHTTPRequest();
115: return selectHTTPMessage(message);
116: }
117:
118: return false;
119: }
120:
121: protected boolean selectField(final HTTPField field) {
122: final String fieldName = field.getNameString();
123: if (!m_namePattern.matcher(fieldName).matches()) {
124: return false;
125: } else {
126: if (m_valuePattern == null) {
127: return false;
128: } else {
129: final String fieldValue = field.getValueString();
130: return m_valuePattern.matcher(fieldValue).matches();
131: }
132: }
133: }
134:
135: protected boolean selectHTTPMessage(final HTTPMessage message) {
136: final Iterator it = message.getHeaders().iterator();
137: while (it.hasNext()) {
138: final HTTPField field = (HTTPField) it.next();
139: if (selectField(field))
140: return true;
141: }
142: return false;
143: }
144:
145: public boolean equals(final Object o) {
146: if (this == o)
147: return true;
148: if (!(o instanceof HTTPFieldSelector))
149: return false;
150:
151: final HTTPFieldSelector httpFieldSelector = (HTTPFieldSelector) o;
152:
153: if (!getCriterion().equals(httpFieldSelector.getCriterion()))
154: return false;
155:
156: return true;
157: }
158:
159: private final static int HASHCODE_ADD = 135;
160:
161: public int hashCode() {
162: return getCriterion().hashCode() + HASHCODE_ADD;
163: }
164: }
|