jicarilla-sandbox/platform/components/http/impl/src/java/org/jicarilla/http/selectors FieldSelector.java,NONE,1.1 PatternMatchingFieldSelector.java,NONE,1.1 HTTPFieldSelector.java,1.4,NONE PatternMatchingHTTPFieldSelector.java,1.4,NONE

Leo Simons <[email protected]>
Newsgroups gmane.comp.java.jicarilla.cvs
Message-ID <[email protected]>
Update of /cvsroot/jicarilla/jicarilla-sandbox/platform/components/http/impl/src/java/org/jicarilla/http/selectors
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18181/platform/components/http/impl/src/java/org/jicarilla/http/selectors

Added Files:
	FieldSelector.java PatternMatchingFieldSelector.java 
Removed Files:
	HTTPFieldSelector.java PatternMatchingHTTPFieldSelector.java 
Log Message:
remove the HTTP prefix from nearly every class and interface. Less typing, cleaner reading :-D

--- NEW FILE: PatternMatchingFieldSelector.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.http.selectors;

import org.jicarilla.http.Field;
import org.jicarilla.http.Message;
import org.jicarilla.lang.Assert;
import org.jicarilla.lang.CriterionExposingSelector;
import org.jicarilla.webserver.plumbing.HTTPEvent;

import java.util.Iterator;
import java.util.regex.Pattern;

/**
 *
 *
 * <p>Example:</p>
 *
 * <pre>
 * Selector selector = new PatternMatchingFieldSelector(
 *         "host", "(?:[a-zA-Z0-9]+\.)*myhostname\.org" );
 * boolean selected = selector.select(
 *         getHTTPMessage(
 *            "GET / HTTP/1.1",
 *            "Host: www.myhostname.org"
 *         )
 * );
 * Assert.assertTrue( selected );
 * selected = selector.select(
 *         getHTTPMessage(
 *            "GET / HTTP/1.1",
 *            "host: www3.dynamic.sub.myhostname.org"
 *         )
 * );
 * Assert.assertTrue( selected );
 * selected = selector.select(
 *         getHTTPMessage(
 *            "GET / HTTP/1.1",
 *            "Host: notmyhostname.org"
 *         )
 * );
 * Assert.assertFalse( selected );
 * </pre>
 *
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: PatternMatchingFieldSelector.java,v 1.1 2004/04/09 15:52:11 lsimons Exp $
 */
public class PatternMatchingFieldSelector
        implements CriterionExposingSelector
{
    protected Pattern m_namePattern;
    protected Pattern m_valuePattern;

    public PatternMatchingFieldSelector( final Pattern fieldNamePattern )
    {
        this( fieldNamePattern, null );
    }

    public PatternMatchingFieldSelector( final Pattern fieldNamePattern,
            final Pattern fieldValuePattern )
    {
        Assert.assertNotNull( "fieldNamePattern argument may not be null",
                fieldNamePattern );
        /*Assert.assertNotNull( "fieldValueMatcher argument may not be null",
        fieldValueMatcher );*/
        
        m_namePattern = fieldNamePattern;
        m_valuePattern = fieldValuePattern;
    }

    public Object getCriterion()
    {
        return new Pattern[] { m_namePattern, m_valuePattern };
    }

    public boolean select( final Object object )
    {
        if( object == null )
            return false;

        if( object instanceof Field )
        {
            final Field field = (Field)object;
            return selectField( field );
        }

        if( object instanceof Message )
        {
            final Message message = (Message)object;
            return selectHTTPMessage( message );
        }

        if( object instanceof HTTPEvent )
        {
            final HTTPEvent event = (HTTPEvent)object;
            final Message message = event.getHTTPRequest();
            return selectHTTPMessage( message );
        }

        return false;
    }

    protected boolean selectField( final Field field )
    {
        final String fieldName = field.getNameString();
        if( !m_namePattern.matcher( fieldName ).matches() )
        {
            return false;
        }
        else
        {
            if( m_valuePattern == null )
            {
                return false;
            }
            else
            {
                final String fieldValue = field.getValueString();
                return m_valuePattern.matcher( fieldValue ).matches();
            }
        }
    }

    protected boolean selectHTTPMessage( final Message message )
    {
        final Iterator it = message.getHeaders().iterator();
        while( it.hasNext() )
        {
            final Field field = (Field)it.next();
            if( selectField( field ) )
                return true;
        }
        return false;
    }

    public boolean equals( final Object o )
    {
        if( this == o )
            return true;
        if( !(o instanceof FieldSelector) )
            return false;

        final FieldSelector httpFieldSelector = (FieldSelector)o;

        if( !getCriterion().equals( httpFieldSelector.getCriterion() ) )
            return false;

        return true;
    }

    private final static int HASHCODE_ADD = 135;
    public int hashCode()
    {
        return getCriterion().hashCode() + HASHCODE_ADD;
    }
}

--- NEW FILE: FieldSelector.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.http.selectors;

import org.jicarilla.http.Field;
import org.jicarilla.http.Message;
import org.jicarilla.lang.Assert;
import org.jicarilla.lang.CriterionExposingSelector;
import org.jicarilla.webserver.plumbing.HTTPEvent;

import java.util.Iterator;

/**
 * 
 *
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: FieldSelector.java,v 1.1 2004/04/09 15:52:10 lsimons Exp $
 */
public class FieldSelector implements CriterionExposingSelector
{
    protected Field m_field;

    public FieldSelector( final Field field )
    {
        Assert.assertNotNull( "field argument may not be null", field );
        m_field = field;
    }

    public Object getCriterion()
    {
        return m_field;
    }

    public boolean select( final Object object )
    {
        if( object == null )
            return false;

        if( object instanceof Field )
        {
            final Field field = (Field)object;
            return selectField( field );
        }

        if( object instanceof Message )
        {
            final Message message = (Message)object;
            return selectHTTPMessage( message );
        }

        if( object instanceof HTTPEvent )
        {
            final HTTPEvent event = (HTTPEvent)object;
            final Message message = event.getHTTPRequest();
            return selectHTTPMessage( message );
        }

        return false;
    }

    protected boolean selectField( final Field field )
    {
        return m_field.equals( field );
    }

    protected boolean selectHTTPMessage( final Message message )
    {
        final Iterator it = message.getHeaders().iterator();
        while( it.hasNext() )
        {
            final Field field = (Field)it.next();
            if( selectField( field ) )
                return true;
        }
        return false;
    }

    public boolean equals( final Object o )
    {
        if( this == o )
            return true;
        if( !(o instanceof FieldSelector) )
            return false;

        final FieldSelector httpFieldSelector = (FieldSelector)o;

        if( !getCriterion().equals( httpFieldSelector.getCriterion() ) )
            return false;

        return true;
    }

    private final static int HASHCODE_ADD = 130;
    public int hashCode()
    {
        return getCriterion().hashCode() + HASHCODE_ADD;
    }
}

--- PatternMatchingHTTPFieldSelector.java DELETED ---

--- HTTPFieldSelector.java DELETED ---



-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.