jicarilla-sandbox/platform/components/http/impl/src/java/org/jicarilla/http/plumbing HTTPSelector.java,NONE,1.1 HTTPScreenerBuilderImpl.java,NONE,1.1

[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/plumbing
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23839/platform/components/http/impl/src/java/org/jicarilla/http/plumbing

Added Files:
	HTTPSelector.java HTTPScreenerBuilderImpl.java 
Log Message:
Initial version of the really big message redirector.

--- NEW FILE: HTTPSelector.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.plumbing;

import org.jicarilla.framework.Selector;
import org.jicarilla.framework.RegexpSelector;
import org.jicarilla.framework.EquivalenceSelector;
import org.jicarilla.http.HTTPMessage;
import org.jicarilla.http.HTTPField;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Pattern;
import java.net.InetAddress;

/**
 * 
 *
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: HTTPSelector.java,v 1.1 2004/02/20 14:21:41 lsimons Exp $
 */
public class HTTPSelector implements Selector
{
    protected List m_methodSelectors = new ArrayList();
    protected List m_uriSelectors = new ArrayList();
    protected List m_versionSelectors = new ArrayList();
    protected List m_headerSelectors = new ArrayList();
    protected List m_bodySelectors = new ArrayList();

    public final static boolean POLICY_AND = true;
    public final static boolean POLICY_OR = false;

    protected boolean m_methodSelectorPolicy = POLICY_OR;
    protected boolean m_uriSelectorPolicy = POLICY_OR;
    protected boolean m_versionSelectorPolicy = POLICY_OR;
    protected boolean m_headerSelectorPolicy = POLICY_AND;
    protected boolean m_bodySelectorPolicy = POLICY_AND;
    protected boolean m_globalPolicy = POLICY_AND;

    public boolean select( Object object )
    {
        if(! (object instanceof HTTPEvent) )
            return false;

        HTTPEvent event = (HTTPEvent)object;
        HTTPMessage request = event.getHTTPRequest();

        if( m_globalPolicy == POLICY_AND )
            return selectStartLine(request) &&
                selectHeaders(request) &&
                selectBody(request);
        else
            // POLICY_OR
            return selectStartLine(request) ||
                selectHeaders(request) ||
                selectBody(request);
    }

    public void addURLPattern( Pattern pattern )
    {
        Selector s = new RegexpSelector( pattern );
        m_uriSelectors.add( s );
    }

    public void addVirtualHost( InetAddress host )
    {
        String hostname = host.getHostName();
        Pattern pattern = Pattern.compile( "Host:[ \\t]*" + hostname,
                Pattern.CASE_INSENSITIVE );
        Selector s = new RegexpSelector( pattern );
        m_uriSelectors.add( s );
    }

    protected boolean selectStartLine( HTTPMessage request )
    {
        if( m_globalPolicy == POLICY_AND )
            return selectMethod(request) &&
                selectURI(request) &&
                selectVersion(request);
        else
            // POLICY_OR
            return selectMethod(request) ||
                selectURI(request) ||
                selectVersion(request);
    }

    protected boolean selectMethod( HTTPMessage request )
    {
        if( m_methodSelectors.size() == 0 )
            return true;

        String method = request.getField1String();
        return select( method, m_methodSelectors, m_methodSelectorPolicy );
    }

    protected boolean selectURI( HTTPMessage request )
    {
        if( m_uriSelectors.size() == 0 )
            return true;

        String uri = request.getField2String();
        return select( uri, m_uriSelectors, m_uriSelectorPolicy );
    }

    protected boolean selectVersion( HTTPMessage request )
    {
        if( m_versionSelectors.size() == 0 )
            return true;

        String version = request.getField3String();
        return select( version, m_methodSelectors, m_methodSelectorPolicy );
    }

    protected boolean selectHeaders( HTTPMessage request )
    {
        if( m_versionSelectors.size() == 0 )
            return true;

        List headers = request.getHeaders();
        Iterator it = headers.iterator();
        if( m_headerSelectorPolicy == POLICY_AND )
        {
            while( it.hasNext() )
            {
                HTTPField header = (HTTPField)it.next();
                String headerString = header.toExternalForm();
                    if(!selectHeader( headerString, m_headerSelectors, m_headerSelectorPolicy ) )
                        return false;
            }
            return true;
        }
        else
        {
            while( it.hasNext() )
            {
                HTTPField header = (HTTPField)it.next();
                String headerString = header.toExternalForm();
                    if(selectHeader( headerString, m_headerSelectors, m_headerSelectorPolicy ) )
                        return true;
            }
            return true;
        }
    }

    protected boolean selectBody( HTTPMessage request )
    {
        if( m_bodySelectors.size() == 0 )
            return true;

        return select( request.getBodyAsString(), m_bodySelectors, m_bodySelectorPolicy );
    }

    protected boolean select( String field, List selectors, boolean policy )
    {
        Iterator it = selectors.iterator();
        if( policy == POLICY_AND )
        {
            while( it.hasNext() )
            {
                Selector selector = (Selector)it.next();
                if( !selector.select( field ) )
                    return false;
            }
            return true;
        }
        else
        {
            // POLICY_OR
            while( it.hasNext() )
            {
                Selector selector = (Selector)it.next();
                if( selector.select( field ) )
                    return true;
            }
            return false;
        }
    }

    protected boolean selectHeader( String header, List selectors, boolean policy )
    {
        Iterator it = selectors.iterator();
        while( it.hasNext() )
        {
            Selector selector = (Selector)it.next();
            if( selector.select( header ) )
                return true;
        }
        return false;
    }
}

--- NEW FILE: HTTPScreenerBuilderImpl.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.plumbing;

import org.jicarilla.framework.plumbing.Screener;
import org.jicarilla.framework.plumbing.SimpleScreener;
import org.jicarilla.framework.plumbing.Sink;
import org.jicarilla.framework.Assert;
import org.jicarilla.framework.Selector;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.regex.Pattern;
import java.net.InetAddress;

/**
 * 
 *
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: HTTPScreenerBuilderImpl.java,v 1.1 2004/02/20 14:21:41 lsimons Exp $
 */
public class HTTPScreenerBuilderImpl implements HTTPScreenerBuilder
{
    List sinks = new ArrayList();

    public Screener create() throws Exception
    {
        Screener s = new SimpleScreener();
        populate( s );
        return s;
    }

    public HTTPScreenerBuilder addStage( Object selectionCriterion,
            Sink sink )
    {
        sinks.add( new Entry( selectionCriterion, sink ) );
        return this;
    }

    protected void populate( Screener s )
    {
        Iterator it = sinks.iterator();
        while( it.hasNext() )
        {
            Entry entry = (Entry)it.next();
            if( entry.criterion instanceof HTTPSelector )
            {
                s.addSink( (Selector)entry.criterion, entry.sink );
                continue;
            }

            if( entry.criterion instanceof Pattern )
            {
                HTTPSelector selector = new HTTPSelector();
                selector.addURLPattern( (Pattern)entry.criterion );
                s.addSink( selector, entry.sink );
                continue;
            }

            if( entry.criterion instanceof InetAddress )
            {
                HTTPSelector selector = new HTTPSelector();
                selector.addVirtualHost( (InetAddress)entry.criterion );
                s.addSink( selector, entry.sink );
                continue;
            }
        }
    }

    protected static class Entry
    {
        public Object criterion;
        public Sink sink;

        public Entry( Object criterion, Sink sink )
        {
            Assert.assertNotNull( "criterion argument may not be null",
                    criterion );
            Assert.assertNotNull( "sink argument may not be null", sink );

            this.criterion = criterion;
            this.sink = sink;
        }
    }
}



-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id=1356&alloc_id=3438&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.