jicarilla-sandbox/platform/components/http/impl/src/java/org/jicarilla/http FileReaderImpl.java,NONE,1.1 MessageGenerator.java,NONE,1.1 MessageWriterImpl.java,NONE,1.1 ParserImpl.java,NONE,1.1 MessageFactory.java,1.3,1.4 MessageReceivedListener.java,1.2,1.3 NoopMessageReceivedListener.java,1.3,1.4 HTTPFileReaderImpl.java,1.2,NONE HTTPMessageWriterImpl.java,1.4,NONE HTTPParserImpl.java,1.13,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
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18181/platform/components/http/impl/src/java/org/jicarilla/http

Modified Files:
	MessageFactory.java MessageReceivedListener.java 
	NoopMessageReceivedListener.java 
Added Files:
	FileReaderImpl.java MessageGenerator.java 
	MessageWriterImpl.java ParserImpl.java 
Removed Files:
	HTTPFileReaderImpl.java HTTPMessageWriterImpl.java 
	HTTPParserImpl.java 
Log Message:
remove the HTTP prefix from nearly every class and interface. Less typing, cleaner reading :-D

Index: NoopMessageReceivedListener.java
===================================================================
RCS file: /cvsroot/jicarilla/jicarilla-sandbox/platform/components/http/impl/src/java/org/jicarilla/http/NoopMessageReceivedListener.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- NoopMessageReceivedListener.java	26 Feb 2004 16:51:55 -0000	1.3
+++ NoopMessageReceivedListener.java	9 Apr 2004 15:52:12 -0000	1.4
@@ -58,12 +58,12 @@
  * A MessageReceivedListener which doesn't do anything. Useful
  * for testing/debugging, where you'll often simply want
  * to ignore all messages being parsed, or if you're hardwiring
- * the HTTPMessageGenerator for some reason.
+ * the MessageGenerator for some reason.
  *
  * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
  * @version $Id$
  */
 public class NoopMessageReceivedListener implements MessageReceivedListener
 {
-    public void messageReceived( final HTTPMessage message ) {}
+    public void messageReceived( final Message message ) {}
 }

Index: MessageReceivedListener.java
===================================================================
RCS file: /cvsroot/jicarilla/jicarilla-sandbox/platform/components/http/impl/src/java/org/jicarilla/http/MessageReceivedListener.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- MessageReceivedListener.java	4 Jan 2004 16:10:17 -0000	1.2
+++ MessageReceivedListener.java	9 Apr 2004 15:52:11 -0000	1.3
@@ -55,7 +55,7 @@
 package org.jicarilla.http;
 
 /**
- * The HTTPMessageGenerator will send the http messages it
+ * The MessageGenerator will send the http messages it
  * receives here when complete.
  *
  * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
@@ -63,5 +63,5 @@
  */
 public interface MessageReceivedListener
 {
-    void messageReceived( HTTPMessage message );
+    void messageReceived( Message message );
 }

--- HTTPFileReaderImpl.java DELETED ---

--- HTTPParserImpl.java DELETED ---

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

import org.jicarilla.http.util.NioUtil;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Iterator;

/**
 * Rather small component that knows how to write Message
 * instances back to a channel.
 *
 * This is a IoC type-3 compatible component.
 *
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: MessageWriterImpl.java,v 1.1 2004/04/09 15:52:12 lsimons Exp $
 */
public class MessageWriterImpl implements MessageWriter
{
    // ----------------------------------------------------------------------
    //  Properties
    // ----------------------------------------------------------------------
    public final static ByteBuffer SP = NioUtil.toByteBuffer(" ");
    public final static ByteBuffer CRLF = NioUtil.toByteBuffer("\r\n");
    public final static ByteBuffer COLON_SP = NioUtil.toByteBuffer(": ");
    static
    {
        SP.rewind();
        CRLF.rewind();
        COLON_SP.rewind();
    }

    // ----------------------------------------------------------------------
    //  Work Interface: MessageWriter
    // ----------------------------------------------------------------------

    public void write( final Message m, final WritableByteChannel c )
            throws HTTPException, IOException
    {
        write( m, c, false );
    }

    public void write( final Message m, final WritableByteChannel c, final boolean close )
            throws HTTPException, IOException
    {
        writeStartLine( c, m );
        writeHeaders( m, c );
        writeBody( m, c );
        closeChannel( close, c );
    }

    // ----------------------------------------------------------------------
    //  Helper methods
    // ----------------------------------------------------------------------

    protected static void writeStartLine( final WritableByteChannel c, final Message m )
            throws IOException
    {
        c.write( m.getField1() );
        SP.rewind();
        c.write( SP );
        c.write( m.getField2() );
        SP.rewind();
        c.write( SP );
        c.write( m.getField3() );
        CRLF.rewind();
        c.write( CRLF );
    }

    protected static void writeHeaders( final Message m, final WritableByteChannel c )
            throws IOException
    {
        final Iterator it = m.getHeaders().iterator();
        while( it.hasNext() )
        {
            final Field field = (Field)it.next();

            c.write( field.getName() );
            COLON_SP.rewind();
            c.write( COLON_SP );
            c.write( field.getValue() );
            CRLF.rewind();
            c.write( CRLF );
        }
        CRLF.rewind();
        c.write( CRLF );
    }

    protected static void writeBody( final Message m, final WritableByteChannel c )
            throws IOException
    {
        final ByteBuffer[] bp = m.getBodyParts();
        for( int i = 0; i < bp.length; i++ )
        {
            bp[i].rewind();
            c.write( bp[i] );
        }
    }

    protected static void closeChannel( final boolean close, final WritableByteChannel c )
    {
        if( close )
        {
            try
            {
                if( c instanceof SocketChannel )
                    ((SocketChannel)c).socket().close();
                else
                    c.close();
            } catch( IOException ioe ) {}
        }
    }
}

--- NEW FILE: FileReaderImpl.java ---
package org.jicarilla.http;

import org.jicarilla.io.Filesystem;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;

/**
 * 
 *
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: FileReaderImpl.java,v 1.1 2004/04/09 15:52:11 lsimons Exp $
 */
public class FileReaderImpl implements FileReader
{
    public final static int BUFFER_SIZE = 8096;

    private Filesystem m_filesystem;

    public FileReaderImpl( final Filesystem fs )
    {
        setFilesystem( fs );
    }

    public Filesystem getFilesystem()
    {
        return m_filesystem;
    }

    public void setFilesystem( final Filesystem filesystem )
    {
        m_filesystem = filesystem;
    }

    public int readFile( final String file, final Message res )
            throws IOException
    {
        final ReadableByteChannel rbc = getFilesystem().getFile( file );
        int size = 0;
        while(true)
        {
            final ByteBuffer buf = ByteBuffer.allocate( BUFFER_SIZE );
            final int read = rbc.read( buf );
            if( read < 0 )
                break;
            if( read == 0 )
                continue;

            size += read;
            buf.rewind();
            buf.limit(read);
            res.addBodyPart(buf);
        }
        getFilesystem().returnFile( rbc );
        return size;
    }
}

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

import org.apache.commons.pool.ObjectPool;
import org.jicarilla.http.util.NioUtil;
import org.jicarilla.lang.Assert;
import org.jicarilla.lang.ExceptionListener;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Will populateContainer a Message instance based on the events
 * fired by a Parser. Not Threadsafe.
 *
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: MessageGenerator.java,v 1.1 2004/04/09 15:52:11 lsimons Exp $
 */
public class MessageGenerator implements MessageHandler, ErrorHandler
{
    // ----------------------------------------------------------------------
    //  Properties
    // ----------------------------------------------------------------------
    protected Message m_message = null;
    protected ByteBuffer m_headerNameCache = null;

    protected ExceptionListener m_exceptionListener;
    protected MessageReceivedListener m_messageListener;
    protected ObjectPool m_messagePool;

    // ----------------------------------------------------------------------
    //  Constructors
    // ----------------------------------------------------------------------
    public MessageGenerator( final ExceptionListener exceptionListener,
            final MessageReceivedListener messageListener, final ObjectPool messagePool )
    {
        Assert.assertNotNull( "exceptionListener argument may not be null",
                exceptionListener );
        Assert.assertNotNull( "messageListener argument may not be null",
                messageListener );
        Assert.assertNotNull( "messagePool argument may not be null",
                messagePool );

        m_exceptionListener = exceptionListener;
        m_messageListener = messageListener;
        m_messagePool = messagePool;
    }

    // ----------------------------------------------------------------------
    //  Getters/Setters
    // ----------------------------------------------------------------------
    public void setMessage( final Message message )
    {
        m_message = message;
    }
    public Message getMessage()
    {
        return m_message;
    }

    public void setMessageType( final boolean isRequest )
    {
        checkState();
        m_message.setMessageType( isRequest );
    }

    public boolean getMessageType()
    {
        checkState();
        return m_message.getMessageType();
    }

    protected ByteBuffer getHeaderNameCache()
    {
        return m_headerNameCache;
    }

    protected void setHeaderNameCache( final ByteBuffer headerNameCache )
    {
        m_headerNameCache = headerNameCache;
    }

    protected ExceptionListener getExceptionListener()
    {
        return m_exceptionListener;
    }

    protected void setExceptionListener( final ExceptionListener exceptionListener )
    {
        m_exceptionListener = exceptionListener;
    }

    protected MessageReceivedListener getMessageListener()
    {
        return m_messageListener;
    }

    protected void setMessageListener( final MessageReceivedListener messageListener )
    {
        m_messageListener = messageListener;
    }

    protected ObjectPool getMessagePool()
    {
        return m_messagePool;
    }

    protected void setMessagePool( final ObjectPool messagePool )
    {
        m_messagePool = messagePool;
    }

    // ----------------------------------------------------------------------
    //  Work Interface: MessageHandler
    // ----------------------------------------------------------------------

    public int getBodyType()
    {
        checkState();
        // default
        int type = Parser.BODY_TYPE_NORMAL;

        /* the spec says we should forward and ignore...

        if( Encoding.METHOD_GET.equals( m_message.getField1() ) )
            type = Parser.BODY_TYPE_NONE;*/

        // decide based on spec basics
        type = getTypeFromMessageProperties( type );

        // modify decision based on headers
        type = getTypeFromHeaders( type );

        return type;
    }

    public int getBodySize()
    {
        checkState();
        final int type = getBodyType();
        switch( type )
        {
            case Parser.BODY_TYPE_NONE:
                return 0;
            case Parser.BODY_TYPE_CHUNKING:
                return -1; // unknown
            case Parser.BODY_TYPE_NORMAL:
                return getNormalBodySize();

            default:
                return 0;
                //throw new IllegalStateException( "Parsing error: bad body type!" );
        }
    }

    public void newMessage()
    {
        try
        {
            setMessage( (Message)getMessagePool().borrowObject() );
        }
        catch( Exception e )
        {
            setMessage( new Message() );
        }
    }

    public void foundStartLineFirstField( final ByteBuffer firstField )
    {
        checkState();
        getMessage().setField1( firstField );
    }

    public void foundStartLineSecondField( final ByteBuffer secondField )
    {
        checkState();
        getMessage().setField2( secondField );
    }

    public void foundStartLineThirdField( final ByteBuffer thirdField )
    {
        checkState();
        getMessage().setField3( thirdField );
    }

    public void foundHeaderName( final ByteBuffer header )
    {
        checkState();
        setHeaderNameCache( header );
    }

    public void foundHeaderValue( final ByteBuffer value )
    {
        checkState();
        getMessage().addHeader( getHeaderNameCache(), value );
        setHeaderNameCache( null );
    }

    public void foundBody( final ByteBuffer buffer )
    {
        checkState();
        getMessage().addBodyPart( buffer );
    }

    public boolean hasTrailers()
    {
        checkState();
        Iterator it = getMessage().getHeaders().iterator();
        while( it.hasNext() )
        {
            Field field = (Field)it.next();
            if( Encoding.HEADER_TRAILER.equalsIgnoreCase(
                    field.getNameString() ) )
            {
                return true;
            }
        }
        return false;
    }

    public String[] getTrailerNames()
    {
        checkState();
        Iterator it = getMessage().getHeaders().iterator();
        List nameList = new ArrayList();
        while( it.hasNext() )
        {
            Field field = (Field)it.next();
            if( Encoding.HEADER_TRAILER.equalsIgnoreCase(
                    field.getNameString() ) )
            {
                String[] names = field.getValueString().split( "," );
                for( int i = 0; i < names.length; i++ )
                {
                    nameList.add( names[i].trim() );
                }
            }
        }
        return (String[])nameList.toArray( new String[nameList.size()] ); 
    }

    public void foundTrailerName( final ByteBuffer trailer )
    {
        checkState();
        final String trailerString = NioUtil.toString( trailer );
        Assert.assertFalse(
                "Illegal trailer",
                Encoding.HEADER_TRANSFER_ENCODING.equalsIgnoreCase( trailerString ) ||
                Encoding.HEADER_TRAILER.equalsIgnoreCase( trailerString ) ||
                Encoding.HEADER_CONTENT_LENGTH.equalsIgnoreCase( trailerString )
        );
        foundHeaderName( trailer );
    }

    public void foundTrailerValue( final ByteBuffer value )
    {
        checkState();
        foundHeaderValue( value );
    }

    public void endMessage()
    {
        checkState();
        getMessageListener().messageReceived( getMessage() );
        getMessage().setComplete( true );
    }

    // ----------------------------------------------------------------------
    //  Work Interface: ErrorHandler
    // ----------------------------------------------------------------------

    public void exceptionOccurred( final HTTPException he )
            throws HTTPException
    {
        m_exceptionListener.exceptionOccurred( he );
        throw he;
    }

    // ----------------------------------------------------------------------
    //  Helper Methods
    // ----------------------------------------------------------------------

    protected void checkState()
    {
        Assert.assertNotNull( "setMessage() must be called first!",
                getMessage() );
    }

    protected int getTypeFromMessageProperties( final int fallbackType )
    {
        int type = fallbackType;
        if(!getMessage().getMessageType())
        {
            // handle response defaults
            final int statuscode = getMessage().getStatusCode();
            if(     (statuscode >= Encoding.STATUS_100_Continue &&
                            statuscode < Encoding.STATUS_200_OK) ||
                    statuscode == Encoding.STATUS_204_No_Content ||
                    statuscode == Encoding.STATUS_304_Not_Modified )
                type = Parser.BODY_TYPE_NONE;
        }
        else
        {
            // handle request defaults
            final String method = getMessage().getField1String();
            if( Encoding.METHOD_GET.equals( method ) ||
                    Encoding.METHOD_HEAD.equals( method ) ||
                    Encoding.METHOD_OPTIONS.equals( method ) ||
                    Encoding.METHOD_CONNECT.equals( method ) ||
                    Encoding.METHOD_DELETE.equals( method ) ||
                    Encoding.METHOD_TRACE.equals( method ) )
                type = Parser.BODY_TYPE_NONE;
        }
        return type;
    }

    protected int getTypeFromHeaders( final int fallbackType )
    {
        int type = fallbackType;
        final Iterator it = getMessage().getHeaders().iterator();
        while( it.hasNext() )
        {
            // modify based on headers
            final Field h = (Field)it.next();
            final String name = NioUtil.toString( h.getName() );
            final String value = NioUtil.toString( h.getValue() );

            if( name == null || value == null )
                continue;

            if( name.equalsIgnoreCase( Encoding.TRANSFER_CODING ) &&
                    value.equalsIgnoreCase( Encoding.TRANSFER_CODING_CHUNKED ) )
            {
                type = Parser.BODY_TYPE_CHUNKING;
            }
        }
        return type;
    }

    protected int getNormalBodySize()
    {
        // -1 will result in exception!
        final Iterator it = getMessage().getHeaders().iterator();

        while( it.hasNext() )
        {
            final Field h = (Field)it.next();
            final String name = NioUtil.toString( h.getName() );
            final String value = NioUtil.toString( h.getValue() );

            if( name == null ) // ignore
                continue;

            if( name.equalsIgnoreCase( Encoding.HEADER_CONTENT_LENGTH ) )
            {
                if( value == null )
                    //throw new IllegalStateException(
                    //        "Parsing error: no value for content-length header given!" );
                    return 0;
                return new Integer( value ).intValue();
            }
        }

        //throw new IllegalStateException( "Parsing error: no content-length given!" );
        return 0;
    }
}
--- NEW FILE: ParserImpl.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
[...1618 lines suppressed...]
                );
            //Assert.assertTrue( "size may not be empty", size.hasRemaining() );
            
            chunkSize = Integer.parseInt( NioUtil.toString( size ), 16 );
        }

        /**
         * Update the current buffer {@link Context#view view}, setting its
         * {@link ByteBuffer#limit(int) limit} to the current location. Used
         * during state transitions.
         */ 
        public void markFieldLimit()
        {
            getContext().view.limit(
                    getContext().source.position() -
                    getContext().slice - 1 );
        }

    }
}

--- HTTPMessageWriterImpl.java DELETED ---

Index: MessageFactory.java
===================================================================
RCS file: /cvsroot/jicarilla/jicarilla-sandbox/platform/components/http/impl/src/java/org/jicarilla/http/MessageFactory.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- MessageFactory.java	23 Mar 2004 13:37:49 -0000	1.3
+++ MessageFactory.java	9 Apr 2004 15:52:11 -0000	1.4
@@ -37,6 +37,6 @@
 {
     public Object makeObject() throws Exception
     {
-        return new HTTPMessage();
+        return new Message();
     }
 }



-------------------------------------------------------
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.