You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2006/09/20 08:03:49 UTC

svn commit: r448080 - in /directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver: codec/ stream/

Author: trustin
Date: Tue Sep 19 23:03:48 2006
New Revision: 448080

URL: http://svn.apache.org/viewvc?view=rev&rev=448080
Log:
Resolved issue: DIRMINA-244 (Example HTTP server using ProtocolCodeFactory)
* Added John Preston's contribution to the example package

Added:
    directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/
    directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpRequestDecoder.java   (with props)
    directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpRequestMessage.java   (with props)
    directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpResponseEncoder.java   (with props)
    directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpResponseMessage.java   (with props)
    directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpServerProtocolCodecFactory.java   (with props)
    directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/Server.java   (with props)
    directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/ServerHandler.java   (with props)
    directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/package.html   (with props)
Modified:
    directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/stream/package.html

Added: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpRequestDecoder.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpRequestDecoder.java?view=auto&rev=448080
==============================================================================
--- directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpRequestDecoder.java (added)
+++ directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpRequestDecoder.java Tue Sep 19 23:03:48 2006
@@ -0,0 +1,262 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.mina.example.httpserver.codec;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.CharsetDecoder;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.filter.codec.ProtocolDecoderOutput;
+import org.apache.mina.filter.codec.demux.MessageDecoderAdapter;
+import org.apache.mina.filter.codec.demux.MessageDecoderResult;
+import org.apache.mina.util.CharsetUtil;
+
+/**
+ * A {@link MessageDecoder} that decodes {@link HttpRequest}.
+ * 
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class HttpRequestDecoder extends MessageDecoderAdapter
+{
+    private static final byte[] CONTENT_LENGTH = new String( "Content-Length:" )
+            .getBytes();
+
+    private CharsetDecoder decoder = CharsetUtil.getDefaultCharset()
+            .newDecoder();
+
+    private HttpRequestMessage request = null;
+
+    public HttpRequestDecoder()
+    {
+    }
+
+    public MessageDecoderResult decodable( IoSession session, ByteBuffer in )
+    {
+        // Return NEED_DATA if the whole header is not read yet.
+        try
+        {
+            return messageComplete( in ) ? MessageDecoderResult.OK
+                    : MessageDecoderResult.NEED_DATA;
+        }
+        catch( Exception ex )
+        {
+            ex.printStackTrace();
+        }
+
+        return MessageDecoderResult.NOT_OK;
+    }
+
+    public MessageDecoderResult decode( IoSession session, ByteBuffer in,
+            ProtocolDecoderOutput out ) throws Exception
+    {
+        // Try to decode body
+        HttpRequestMessage m = decodeBody( in );
+
+        // Return NEED_DATA if the body is not fully read.
+        if( m == null )
+            return MessageDecoderResult.NEED_DATA;
+
+        out.write( m );
+
+        return MessageDecoderResult.OK;
+    }
+
+    private boolean messageComplete( ByteBuffer in ) throws Exception
+    {
+        int last = in.remaining() - 1;
+        if( in.remaining() < 4 )
+            return false;
+
+        // to speed up things we check if the Http request is a GET or POST
+        if( in.get( 0 ) == ( byte ) 'G' && in.get( 1 ) == ( byte ) 'E'
+                && in.get( 2 ) == ( byte ) 'T' )
+        {
+            // Http GET request therefore the last 4 bytes should be 0x0D 0x0A 0x0D 0x0A
+            return( in.get( last ) == ( byte ) 0x0A
+                    && in.get( last - 1 ) == ( byte ) 0x0D
+                    && in.get( last - 2 ) == ( byte ) 0x0A && in.get( last - 3 ) == ( byte ) 0x0D );
+        }
+        else if( in.get( 0 ) == ( byte ) 'P' && in.get( 1 ) == ( byte ) 'O'
+                && in.get( 2 ) == ( byte ) 'S' && in.get( 3 ) == ( byte ) 'T' )
+        {
+            // Http POST request
+            // first the position of the 0x0D 0x0A 0x0D 0x0A bytes
+            int eoh = -1;
+            for( int i = last; i > 2; i-- )
+            {
+                if( in.get( i ) == ( byte ) 0x0A
+                        && in.get( i - 1 ) == ( byte ) 0x0D
+                        && in.get( i - 2 ) == ( byte ) 0x0A
+                        && in.get( i - 3 ) == ( byte ) 0x0D )
+                {
+                    eoh = i + 1;
+                    break;
+                }
+            }
+            if( eoh == -1 )
+                return false;
+            for( int i = 0; i < last; i++ )
+            {
+                boolean found = false;
+                for( int j = 0; j < CONTENT_LENGTH.length; j++ )
+                {
+                    if( in.get( i + j ) != CONTENT_LENGTH[ j ] )
+                    {
+                        found = false;
+                        break;
+                    }
+                    found = true;
+                }
+                if( found )
+                {
+                    // retrieve value from this position till next 0x0D 0x0A
+                    StringBuilder contentLength = new StringBuilder();
+                    for( int j = i + CONTENT_LENGTH.length; j < last; j++ )
+                    {
+                        if( in.get( j ) == 0x0D )
+                            break;
+                        contentLength.append( new String( new byte[] { in
+                                .get( j ) } ) );
+                    }
+                    // if content-length worth of data has been received then the message is complete
+                    return( Integer.parseInt( contentLength.toString().trim() )
+                            + eoh == in.remaining() );
+                }
+            }
+        }
+
+        // the message is not complete and we need more data
+        return false;
+    }
+
+    private HttpRequestMessage decodeBody( ByteBuffer in )
+    {
+        request = new HttpRequestMessage();
+        try
+        {
+            request.setHeaders( parseRequest( new StringReader( in
+                    .getString( decoder ) ) ) );
+            return request;
+        }
+        catch( CharacterCodingException ex )
+        {
+            ex.printStackTrace();
+        }
+
+        return null;
+    }
+
+    private Map parseRequest( Reader is )
+    {
+        Map map = new HashMap();
+        BufferedReader rdr = new BufferedReader( is );
+
+        try
+        {
+            // Get request URL.
+            String line = rdr.readLine();
+            String[] url = line.split( " " );
+            if( url.length < 3 )
+                return map;
+
+            map.put( "URI", new String[] { line } );
+            map.put( "Method", new String[] { url[ 0 ].toUpperCase() } );
+            map.put( "Context", new String[] { url[ 1 ].substring( 1 ) } );
+            map.put( "Protocol", new String[] { url[ 2 ] } );
+            // Read header
+            while( ( line = rdr.readLine() ) != null && line.length() > 0 )
+            {
+                String[] tokens = line.split( ": " );
+                map.put( tokens[ 0 ], new String[] { tokens[ 1 ] } );
+            }
+
+            // If method 'POST' then read Content-Length worth of data
+            if( url[ 0 ].equalsIgnoreCase( "POST" ) )
+            {
+                int len = Integer.parseInt( ( ( String[] ) map
+                        .get( "Content-Length" ) )[ 0 ] );
+                char[] buf = new char[ len ];
+                if( rdr.read( buf ) == len )
+                {
+                    line = String.copyValueOf( buf );
+                }
+            }
+            else if( url[ 0 ].equalsIgnoreCase( "GET" ) )
+            {
+                int idx = url[ 1 ].indexOf( '?' );
+                if( idx != -1 )
+                {
+                    map.put( "Context", new String[] { url[ 1 ].substring( 1,
+                            idx ) } );
+                    line = url[ 1 ].substring( idx + 1 );
+                }
+                else
+                {
+                    line = null;
+                }
+            }
+            if( line != null )
+            {
+                String[] match = line.split( "\\&" );
+                for( int i = 0; i < match.length; i++ )
+                {
+                    String[] params = new String[ 1 ];
+                    String[] tokens = match[ i ].split( "=" );
+                    switch( tokens.length )
+                    {
+                    case 0:
+                        map.put( "@".concat( match[ i ] ), new String[] {} );
+                        break;
+                    case 1:
+                        map.put( "@".concat( tokens[ 0 ] ), new String[] {} );
+                        break;
+                    default:
+                        String name = "@".concat( tokens[ 0 ] );
+                        if( map.containsKey( name ) )
+                        {
+                            params = ( String[] ) map.get( name );
+                            String[] tmp = new String[ params.length + 1 ];
+                            for( int j = 0; j < params.length; j++ )
+                                tmp[ j ] = params[ j ];
+                            params = null;
+                            params = tmp;
+                        }
+                        params[ params.length - 1 ] = tokens[ 1 ].trim();
+                        map.put( name, params );
+                    }
+                }
+            }
+        }
+        catch( IOException ex )
+        {
+            ex.printStackTrace();
+        }
+
+        return map;
+    }
+}

Propchange: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpRequestDecoder.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpRequestMessage.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpRequestMessage.java?view=auto&rev=448080
==============================================================================
--- directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpRequestMessage.java (added)
+++ directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpRequestMessage.java Tue Sep 19 23:03:48 2006
@@ -0,0 +1,100 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.mina.example.httpserver.codec;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+
+/**
+ * A HTTP request message.
+ * 
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class HttpRequestMessage
+{
+    /** Map<String, String[]> */
+    private Map headers = null;
+
+    public void setHeaders( Map headers )
+    {
+        this.headers = headers;
+    }
+
+    public Map getHeaders()
+    {
+        return headers;
+    }
+
+    public String getContext()
+    {
+        String[] context = ( String[] ) headers.get( "Context" );
+        return context == null ? "" : context[ 0 ];
+    }
+
+    public String getParameter( String name )
+    {
+        String[] param = ( String[] ) headers.get( "@".concat( name ) );
+        return param == null ? "" : param[ 0 ];
+    }
+
+    public String[] getParameters( String name )
+    {
+        String[] param = ( String[] ) headers.get( "@".concat( name ) );
+        return param == null ? new String[] {} : param;
+    }
+
+    public String[] getHeader( String name )
+    {
+        return ( String[] ) headers.get( name );
+    }
+
+    public String toString()
+    {
+        StringBuilder str = new StringBuilder();
+
+        Iterator it = headers.entrySet().iterator();
+        while( it.hasNext() )
+        {
+            Entry e = ( Entry ) it.next();
+            str.append( e.getKey() + " : "
+                    + arrayToString( ( String[] ) e.getValue(), ',' ) + "\n" );
+        }
+        return str.toString();
+    }
+
+    public static String arrayToString( String[] s, char sep )
+    {
+        if( s == null || s.length == 0 )
+            return "";
+        StringBuffer buf = new StringBuffer();
+        if( s != null )
+        {
+            for( int i = 0; i < s.length; i++ )
+            {
+                if( i > 0 )
+                    buf.append( sep );
+                buf.append( s[ i ] );
+            }
+        }
+        return buf.toString();
+    }
+}

Propchange: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpRequestMessage.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpResponseEncoder.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpResponseEncoder.java?view=auto&rev=448080
==============================================================================
--- directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpResponseEncoder.java (added)
+++ directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpResponseEncoder.java Tue Sep 19 23:03:48 2006
@@ -0,0 +1,117 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.mina.example.httpserver.codec;
+
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.CharsetEncoder;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.filter.codec.ProtocolEncoderOutput;
+import org.apache.mina.filter.codec.demux.MessageEncoder;
+import org.apache.mina.util.CharsetUtil;
+
+/**
+ * A {@link MessageEncoder} that encodes {@link HttpResponseMessage}.
+ * 
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class HttpResponseEncoder implements MessageEncoder
+{
+    private static final Set TYPES;
+
+    static
+    {
+        Set types = new HashSet();
+        types.add( HttpResponseMessage.class );
+        TYPES = Collections.unmodifiableSet( types );
+    }
+
+    private static final byte[] CRLF = new byte[] { 0x0D, 0x0A };
+
+    public HttpResponseEncoder()
+    {
+    }
+
+    public void encode( IoSession session, Object message,
+            ProtocolEncoderOutput out ) throws Exception
+    {
+        HttpResponseMessage msg = ( HttpResponseMessage ) message;
+        ByteBuffer buf = ByteBuffer.allocate( 256 );
+        // Enable auto-expand for easier encoding
+        buf.setAutoExpand( true );
+
+        try
+        {
+            // output all headers except the content length
+            CharsetEncoder encoder = CharsetUtil.getDefaultCharset()
+                    .newEncoder();
+            buf.putString( "HTTP/1.1 ", encoder );
+            buf.putString( String.valueOf( msg.getResponseCode() ), encoder );
+            switch( msg.getResponseCode() )
+            {
+            case HttpResponseMessage.HTTP_STATUS_SUCCESS:
+                buf.putString( " OK", encoder );
+                break;
+            case HttpResponseMessage.HTTP_STATUS_NOT_FOUND:
+                buf.putString( " Not Found", encoder );
+                break;
+            }
+            buf.put( CRLF );
+            for( Iterator it = msg.getHeaders().entrySet().iterator(); it
+                    .hasNext(); )
+            {
+                Entry entry = ( Entry ) it.next();
+                buf.putString( ( String ) entry.getKey(), encoder );
+                buf.putString( ": ", encoder );
+                buf.putString( ( String ) entry.getValue(), encoder );
+                buf.put( CRLF );
+            }
+            // now the content length is the body length
+            buf.putString( "Content-Length: ", encoder );
+            buf.putString( String.valueOf( msg.getBodyLength() ), encoder );
+            buf.put( CRLF );
+            buf.put( CRLF );
+            // add body
+            buf.put( msg.getBody() );
+            //System.out.println("\n+++++++");
+            //for (int i=0; i<buf.position();i++)System.out.print(new String(new byte[]{buf.get(i)}));
+            //System.out.println("\n+++++++");
+        }
+        catch( CharacterCodingException ex )
+        {
+            ex.printStackTrace();
+        }
+
+        buf.flip();
+        out.write( buf );
+    }
+
+    public Set getMessageTypes()
+    {
+        return TYPES;
+    }
+}

Propchange: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpResponseEncoder.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpResponseMessage.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpResponseMessage.java?view=auto&rev=448080
==============================================================================
--- directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpResponseMessage.java (added)
+++ directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpResponseMessage.java Tue Sep 19 23:03:48 2006
@@ -0,0 +1,120 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.mina.example.httpserver.codec;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.mina.common.ByteBuffer;
+
+/**
+ * A HTTP response message.
+ * 
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class HttpResponseMessage
+{
+    /** HTTP response codes */
+    public static final int HTTP_STATUS_SUCCESS = 200;
+
+    public static final int HTTP_STATUS_NOT_FOUND = 404;
+
+    /** Map<String, String> */
+    private Map headers = new HashMap();
+
+    /** Storage for body of HTTP response. */
+    private ByteArrayOutputStream body = new ByteArrayOutputStream( 1024 );
+
+    private int responseCode = HTTP_STATUS_SUCCESS;
+
+    public HttpResponseMessage()
+    {
+        String[] ver = Server.VERSION_STRING.split( " " );
+        headers.put( "Server", "HttpServer - Revision: " + ver[ 1 ] + " "
+                + ver[ 7 ] + " " + ver[ 8 ] + " " + ver[ 9 ] + " " + ver[ 10 ] );
+        headers.put( "Cache-Control", "private" );
+        headers.put( "Content-Type", "text/html; charset=iso-8859-1" );
+        headers.put( "Connection", "keep-alive" );
+        headers.put( "Keep-Alive", "200" );
+        headers.put( "Date", new SimpleDateFormat(
+                "EEE, dd MMM yyyy HH:mm:ss zzz" ).format( new Date() ) );
+        headers.put( "Last-Modified", new SimpleDateFormat(
+                "EEE, dd MMM yyyy HH:mm:ss zzz" ).format( new Date() ) );
+    }
+
+    public Map getHeaders()
+    {
+        return headers;
+    }
+
+    public void setContentType( String contentType )
+    {
+        headers.put( "Content-Type", contentType );
+    }
+
+    public void setResponseCode( int responseCode )
+    {
+        this.responseCode = responseCode;
+    }
+
+    public int getResponseCode()
+    {
+        return this.responseCode;
+    }
+
+    public void appendBody( byte[] b )
+    {
+        try
+        {
+            body.write( b );
+        }
+        catch( IOException ex )
+        {
+            ex.printStackTrace();
+        }
+    }
+
+    public void appendBody( String s )
+    {
+        try
+        {
+            body.write( s.getBytes() );
+        }
+        catch( IOException ex )
+        {
+            ex.printStackTrace();
+        }
+    }
+
+    public ByteBuffer getBody()
+    {
+        return ByteBuffer.wrap( body.toByteArray() );
+    }
+
+    public int getBodyLength()
+    {
+        return body.size();
+    }
+}

Propchange: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpResponseMessage.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpServerProtocolCodecFactory.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpServerProtocolCodecFactory.java?view=auto&rev=448080
==============================================================================
--- directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpServerProtocolCodecFactory.java (added)
+++ directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpServerProtocolCodecFactory.java Tue Sep 19 23:03:48 2006
@@ -0,0 +1,38 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.mina.example.httpserver.codec;
+
+import org.apache.mina.filter.codec.demux.DemuxingProtocolCodecFactory;
+
+/**
+ * Provides a protocol codec for HTTP server.
+ * 
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class HttpServerProtocolCodecFactory extends
+        DemuxingProtocolCodecFactory
+{
+    public HttpServerProtocolCodecFactory()
+    {
+        super.register( HttpRequestDecoder.class );
+        super.register( HttpResponseEncoder.class );
+    }
+}

Propchange: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/HttpServerProtocolCodecFactory.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/Server.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/Server.java?view=auto&rev=448080
==============================================================================
--- directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/Server.java (added)
+++ directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/Server.java Tue Sep 19 23:03:48 2006
@@ -0,0 +1,69 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.mina.example.httpserver.codec;
+
+import java.net.InetSocketAddress;
+
+import org.apache.mina.common.IoAcceptor;
+import org.apache.mina.transport.socket.nio.SocketAcceptor;
+import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
+
+/**
+ * (<b>Entry point</b>) HTTP server
+ * 
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class Server
+{
+    /** Default HTTP port */
+    private static int DEFAULT_PORT = 80;
+
+    /** Tile server revision number */
+    public static final String VERSION_STRING = "$Revision$ $Date$";
+
+    public static void main( String[] args )
+    {
+        int port = DEFAULT_PORT;
+
+        for( int i = 0; i < args.length; i++ )
+        {
+            if( args[ i ].equals( "-port" ) )
+            {
+                port = Integer.parseInt( args[ i + 1 ] );
+            }
+        }
+
+        try
+        {
+            // Create ServiceRegistry.
+            IoAcceptor acceptor = new SocketAcceptor();
+            ( ( SocketAcceptorConfig ) acceptor.getDefaultConfig() )
+                    .setReuseAddress( true );
+            acceptor.bind( new InetSocketAddress( port ), new ServerHandler() );
+
+            System.out.println( "Server now listening on port " + port );
+        }
+        catch( Exception ex )
+        {
+            ex.printStackTrace();
+        }
+    }
+}

Propchange: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/Server.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/ServerHandler.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/ServerHandler.java?view=auto&rev=448080
==============================================================================
--- directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/ServerHandler.java (added)
+++ directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/ServerHandler.java Tue Sep 19 23:03:48 2006
@@ -0,0 +1,119 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.mina.example.httpserver.codec;
+
+import org.apache.mina.common.IdleStatus;
+import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.filter.codec.ProtocolCodecFactory;
+import org.apache.mina.filter.codec.ProtocolCodecFilter;
+
+/**
+ * An {@link IoHandler} for HTTP.
+ * 
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class ServerHandler implements IoHandler
+{
+    public ServerHandler()
+    {
+    }
+
+    public void sessionCreated( IoSession session ) throws Exception
+    {
+        ProtocolCodecFactory codec = new HttpServerProtocolCodecFactory();
+
+        session.getFilterChain().addFirst( "protocolFilter",
+                new ProtocolCodecFilter( codec ) );
+        //    session.getFilterChain().addLast("logger", new LoggingFilter());
+    }
+
+    public void sessionOpened( IoSession session )
+    {
+        System.out.println( "SERVER SESSIOn OPENED - "
+                + session.getRemoteAddress() );
+        // set idle time to 60 seconds
+        session.setIdleTime( IdleStatus.BOTH_IDLE, 60 );
+    }
+
+    public void sessionClosed( IoSession session )
+    {
+        System.out.println( "SERVER SESSIOn CLOSED" );
+    }
+
+    public void messageReceived( IoSession session, Object message )
+    {
+        HttpResponseMessage response = null;
+
+        HttpRequestMessage msg = ( HttpRequestMessage ) message;
+
+        System.out
+                .println( "***************************  REQUEST  ***************************" );
+        System.out.println( msg );
+        System.out
+                .println( "*****************************************************************" );
+
+        // Check that we can service the request context
+        response = new HttpResponseMessage();
+        response.setContentType( "text/plain" );
+        response.setResponseCode( HttpResponseMessage.HTTP_STATUS_SUCCESS );
+        response.appendBody( "CONNECTED" );
+
+        // msg.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);
+        // byte[] b = new byte[ta.buffer.limit()];
+        // ((ByteBuffer)ta.buffer.rewind()).get(b);
+        // msg.appendBody(b);
+        // System.out.println("####################");
+        // System.out.println("  GET_TILE RESPONSE SENT - ATTACHMENT GOOD DIAMOND.SI="+d.si+
+        // ", "+new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss.SSS").format(new java.util.Date()));
+        // System.out.println("#################### - status="+ta.state+", index="+message.getIndex());
+
+        //// Unknown request
+        // response = new HttpResponseMessage();
+        // response.setResponseCode(HttpResponseMessage.HTTP_STATUS_NOT_FOUND);
+        // response.appendBody(String.format(
+        // "<html><body><h1>UNKNOWN REQUEST %d</h1></body></html>",
+        // HttpResponseMessage.HTTP_STATUS_NOT_FOUND));
+
+        if( response != null )
+            session.write( response ).join();
+    }
+
+    public void messageSent( IoSession session, Object message )
+    {
+        System.out.println( "  MESSAGE SENT - WRITTEN BYTES="
+                + session.getWrittenBytes() );
+    }
+
+    public void sessionIdle( IoSession session, IdleStatus status )
+    {
+        System.out.println( "Disconnecting the idle." );
+        // disconnect an idle client
+        session.close();
+    }
+
+    public void exceptionCaught( IoSession session, Throwable cause )
+    {
+        cause.printStackTrace();
+        // close the connection on exceptional situation
+        session.close();
+    }
+}

Propchange: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/ServerHandler.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/package.html
URL: http://svn.apache.org/viewvc/directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/package.html?view=auto&rev=448080
==============================================================================
--- directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/package.html (added)
+++ directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/package.html Tue Sep 19 23:03:48 2006
@@ -0,0 +1,8 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+</head>
+<body>
+A HTTP server implemented with protocol codec.
+</body>
+</html>

Propchange: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/codec/package.html
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Modified: directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/stream/package.html
URL: http://svn.apache.org/viewvc/directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/stream/package.html?view=diff&rev=448080&r1=448079&r2=448080
==============================================================================
--- directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/stream/package.html (original)
+++ directory/trunks/mina/example/src/main/java/org/apache/mina/example/httpserver/stream/package.html Tue Sep 19 23:03:48 2006
@@ -3,6 +3,6 @@
 <head>
 </head>
 <body>
-Simplistic HTTP server which demonstates stream-based I/O support.
+A simplistic HTTP server which demonstates stream-based I/O support.
 </body>
 </html>