FileDocCategorySizeDatePackage
ContextRootAlgorithm.javaAPI DocGlassfish v2 API8234Fri May 04 22:37:02 BST 2007com.sun.enterprise.web.ara.algorithms

ContextRootAlgorithm

public class ContextRootAlgorithm extends Object implements com.sun.enterprise.web.connector.grizzly.StreamAlgorithm
Parse the request bytes and seek for the context-root value of the HTTP method.
author
Jeanfrancois Arcand

Fields Summary
private int
port
private SocketChannel
socketChannel
Constructors Summary
public ContextRootAlgorithm()

    
      
    
Methods Summary
public java.nio.ByteBufferallocate(boolean useDirect, boolean useView, int size)
Allocate a ByteBuffer

param
useDirect allocate a direct ByteBuffer.
param
useView allocate a view ByteBuffer.
return
a new ByteBuffer

        throw new UnsupportedOperationException();
    
public intcontentLength()
Return the stream content-length. If the content-length wasn't parsed, return -1.

        throw new UnsupportedOperationException();
    
private java.lang.Stringdump(java.nio.ByteBuffer byteBuffer)
Dump the ByteBuffer content. This is used only for debugging purpose.

                   
        ByteBuffer dd = byteBuffer.duplicate();
        dd.flip();
        
        int length = dd.limit();    
        byte[] dump = new byte[length];
        dd.get(dump,0,length);
        return(new String(dump) + "\n----------------------------" + dd); 
    
public com.sun.enterprise.web.connector.grizzly.HandlergetHandler()
Return null as handler aren't required.

        return null;
    
public intgetPort()
Return the port

        return port;
    
public java.lang.ClassgetReadTask(com.sun.enterprise.web.connector.grizzly.SelectorThread selectorThread)
Return the class responsible for handling OP_READ.

        return com.sun.enterprise.web.connector.grizzly.DefaultReadTask.class;
    
public intheaderLength()
Return the stream header length. The header length is the length between the start of the stream and the first occurance of character '\r\n' .

        throw new UnsupportedOperationException();
    
public booleanparse(java.nio.ByteBuffer byteBuffer)
Parse the request line in search of the context-root bytes of the HTTP Method. The ByteBuffer position and limit refer respectively to the start and the end of the context root.

param
byteBuffer The byteBuffer containing the requests bytes
return
true if the context-root has been found.

        boolean isFound = false;
                          
        int curPosition = byteBuffer.position();
        int curLimit = byteBuffer.limit();
      
        // Rule a - If nothing, return to the Selector.
        if (byteBuffer.position() == 0)
            return false;
       
        byteBuffer.position(0);
        byteBuffer.limit(curPosition);
        int state =0;
        int start =0;
        int end = 0;        
        
        try {                         
            byte c;            
            
            // Rule b - try to determine the context-root
            while(byteBuffer.hasRemaining()) {
                c = byteBuffer.get();

                // State Machine
                // 0 - Search for the first SPACE ' ' between the method and the
                //     the request URI
                // 1 - Search for the second SPACE ' ' between the request URI
                //     and the method
                switch(state) {
                    case 0: // Search for first ' '
                        if (c == 0x20){
                            state = 1;
                            start = byteBuffer.position() + 1;
                        }
                        break;
                    case 1: // Search for next ' '
                        if (c == 0x20){
                            end = byteBuffer.position() - 1;
                            return true;
                        }
                        break;
                    default:
                        throw new IllegalArgumentException("Unexpected state");
                }      
            }
            return false;
        } catch (BufferUnderflowException bue) {
            return false;
        } finally {     
            if ( end > 0 ){
                byteBuffer.position(start);
                byteBuffer.limit(end);
            } else {
                byteBuffer.limit(curLimit);
                byteBuffer.position(curPosition);                               
            }
        }       
    
public java.nio.ByteBufferpostParse(java.nio.ByteBuffer byteBuffer)
After parsing the bytes, post process the ByteBuffer

param
byteBuffer the ByteBuffer used by this algorithm
return
ByteBuffer used by this algorithm

        throw new UnsupportedOperationException();
    
public java.nio.ByteBufferpreParse(java.nio.ByteBuffer byteBuffer)
Before parsing the bytes, initialize and prepare the algorithm.

param
byteBuffer the ByteBuffer used by this algorithm
return
ByteBuffer used by this algorithm

        throw new UnsupportedOperationException();
    
public voidrecycle()
Recycle the algorithm.

    
public java.nio.ByteBufferrollbackParseState(java.nio.ByteBuffer byteBuffer)
Rollback the ByteBuffer to its previous state in case an error as occured.

        throw new UnsupportedOperationException();
    
public voidsetPort(int port)
Set the port

        this.port = port;
    
public voidsetSocketChannel(java.nio.channels.SocketChannel socketChannel)
Set the SocketChannel used by this class.

        this.socketChannel = socketChannel;