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:
036: /**
037: *
038: *
039: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
040: * @version $Id: HTTPFieldSelector.java,v 1.4 2004/03/31 12:10:59 lsimons Exp $
041: */
042: public class HTTPFieldSelector implements CriterionExposingSelector {
043: protected HTTPField m_field;
044:
045: public HTTPFieldSelector(final HTTPField field) {
046: Assert.assertNotNull("field argument may not be null", field);
047: m_field = field;
048: }
049:
050: public Object getCriterion() {
051: return m_field;
052: }
053:
054: public boolean select(final Object object) {
055: if (object == null)
056: return false;
057:
058: if (object instanceof HTTPField) {
059: final HTTPField field = (HTTPField) object;
060: return selectField(field);
061: }
062:
063: if (object instanceof HTTPMessage) {
064: final HTTPMessage message = (HTTPMessage) object;
065: return selectHTTPMessage(message);
066: }
067:
068: if (object instanceof HTTPEvent) {
069: final HTTPEvent event = (HTTPEvent) object;
070: final HTTPMessage message = event.getHTTPRequest();
071: return selectHTTPMessage(message);
072: }
073:
074: return false;
075: }
076:
077: protected boolean selectField(final HTTPField field) {
078: return m_field.equals(field);
079: }
080:
081: protected boolean selectHTTPMessage(final HTTPMessage message) {
082: final Iterator it = message.getHeaders().iterator();
083: while (it.hasNext()) {
084: final HTTPField field = (HTTPField) it.next();
085: if (selectField(field))
086: return true;
087: }
088: return false;
089: }
090:
091: public boolean equals(final Object o) {
092: if (this == o)
093: return true;
094: if (!(o instanceof HTTPFieldSelector))
095: return false;
096:
097: final HTTPFieldSelector httpFieldSelector = (HTTPFieldSelector) o;
098:
099: if (!getCriterion().equals(httpFieldSelector.getCriterion()))
100: return false;
101:
102: return true;
103: }
104:
105: private final static int HASHCODE_ADD = 130;
106:
107: public int hashCode() {
108: return getCriterion().hashCode() + HASHCODE_ADD;
109: }
110: }
|