jicarilla-sandbox/platform/components/http/impl/src/java/org/jicarilla/http MessageUtil.java,NONE,1.1

Leo Simons <[email protected]> Thu, 15 Apr 2004 09:42:55 +0000
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-serv4871/platform/components/http/impl/src/java/org/jicarilla/http

Added Files:
	MessageUtil.java 
Log Message:
I forgot to commit some work the other day. Mostly plumbing work on the HTTP server.

--- NEW FILE: MessageUtil.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 org.jicarilla.webserver.plumbing.HTTPEvent;

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

/**
 * 
 *
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: MessageUtil.java,v 1.1 2004/04/15 09:42:53 lsimons Exp $
 */
public class MessageUtil
{
    public static void setBody( Message response, ByteBuffer newBody )
    {
        // todo: refactor Message to be able to replace body
    }

    public static void setContentSize( Message response, int size )
    {
        // todo: refactor Message to be able to replace headers
        response.addHeader( Encoding.HEADER_CONTENT_LENGTH_BUFFER,
                NioUtil.toByteBuffer( ""+size ) );
    }

    public static void ensureStartLine( final HTTPEvent e )
    {
        final Message res = e.getHTTPResponse();

        if( res.getField1() == null )
            res.setField1( Encoding.VERSION_10 );
        if( res.getField2() == null )
            res.setStatusCode( Encoding.STATUS_200_OK );
        if( res.getField3() == null )
            res.setField3( Encoding.STATUS_MSG[res.getStatusCode()] );
    }

    public static void ensureContentLengthHeader( final HTTPEvent e )
    {
        final Message req = e.getHTTPRequest();
        final Message res = e.getHTTPResponse();

        if( contentLengthNotSet( res ) )
        {
            if( noBodySoNoContentLengthHeader( req, res ) )
                return;

            if( nonIdentifyTransferCodingPresent( req, res ) )
                return;

            final ByteBuffer[] body = res.getBodyParts();
            if( body.length == 0 )
            {
                res.addHeader(
                        Encoding.HEADER_CONTENT_LENGTH_BUFFER,
                        NioUtil.toByteBuffer( 0 )
                );
            }
            else
            {
                int size = 0;
                for( int i = 0; i < body.length; i++ )
                {
                    final ByteBuffer byteBuffer = body[i];
                    size += byteBuffer.remaining();
                }

                res.addHeader(
                        Encoding.HEADER_CONTENT_LENGTH_BUFFER,
                        NioUtil.toByteBuffer( size )
                );
            }
        }
    }

    public static boolean noBodySoNoContentLengthHeader( final Message req,
            final Message res )
    {
        if( Encoding.METHOD_HEAD.equals( req.getField1String() ) )
            return true;

        final int responseCode = res.getStatusCode();
        if( (Encoding.STATUS_100_Continue <= responseCode && responseCode < Encoding.STATUS_200_OK) ||
                responseCode == Encoding.STATUS_204_No_Content ||
                responseCode == Encoding.STATUS_304_Not_Modified )
            return true;

        return false;
    }

    public static boolean nonIdentifyTransferCodingPresent( final Message req,
            final Message res )
    {
        final Iterator it = res.getHeaders().iterator();
        while( it.hasNext() )
        {
            final Field field = (Field)it.next();
            if( Encoding.HEADER_TRANSFER_ENCODING.equals(
                    field.getNameString() ) )
            {
                return !Encoding.CONTENT_CODING_IDENTITY.equals(
                        field.getNameString() );
            }
        }
        return false;
    }

    public static boolean contentLengthNotSet( final Message response )
    {
        final Iterator it = response.getHeaders().iterator();
        while( it.hasNext() )
        {
            final Field field = (Field)it.next();
            if( field.getName().equals(
                    Encoding.HEADER_CONTENT_LENGTH_BUFFER ) )
            {
                return false;
            }
        }
        return true;
    }

    public static void setContentEncoding( Message response,
            ByteBuffer encoding )
    {
        // todo!
    }

    public static void setTransferCoding( Message response,
            ByteBuffer encoding )
    {
        // todo!
    }
}



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