FileDocCategorySizeDatePackage
HttpProtocolHandler.javaAPI DocGlassfish v2 API5046Thu Jun 21 07:58:30 BST 2007com.sun.enterprise.web.portunif

HttpProtocolHandler

public class HttpProtocolHandler extends Object implements ProtocolHandler
Redirect the request to the proper protocol, which can be http or https.
author
Jeanfrancois Arcand

Fields Summary
private static final int
DEFAULT_HTTP_HEADER_BUFFER_SIZE
protected String[]
protocols
The protocols supported by this handler.
private com.sun.enterprise.web.portunif.util.Redirector
redirector
Util to redirect protocol.
Constructors Summary
public HttpProtocolHandler()

    
    
      
    
Methods Summary
public booleanexpireKey(java.nio.channels.SelectionKey key)
Invoked when the SelectorThread is about to expire a SelectionKey.

return
true if the SelectorThread should expire the SelectionKey, false if not.

        return true;
    
public java.lang.String[]getProtocols()
Returns an array of supported protocols.

return
an array of supported protocols.

        return protocols;
    
public voidhandle(com.sun.enterprise.web.portunif.util.ProtocolInfo protocolInfo)
Redirect the request to the protocol defined in the protocolInfo. Protocols supported are http and https.

param
protocolInfo The protocol that needs to be redirected.

        if (redirector == null){
            redirector = new Redirector();
        }
        
        if (protocolInfo.protocol.equalsIgnoreCase("https")) {
            redirector.redirectSSL(protocolInfo);
        } else {
            redirector.redirect(protocolInfo);
        }
        protocolInfo.keepAlive = false;
        
        /* ======================================================
         * Java HTTP(S) client sends request in 2 chunks: header, payload
         * We need to make sure client started to send payload before redirecting/closing
         * the connection. Otherwise client can not receive "HTTP 302 redirect" response.
         */ 
        ByteBuffer tmpBuffer = protocolInfo.byteBuffer;
        tmpBuffer.clear();
        ByteBufferInputStream is = new ByteBufferInputStream(tmpBuffer);
        try {
            is.setReadTimeout(2);
            is.setSelectionKey(protocolInfo.key);
            int count = 0;
            while (tmpBuffer.hasRemaining() && count < DEFAULT_HTTP_HEADER_BUFFER_SIZE) {
                tmpBuffer.position(tmpBuffer.limit());
                int readBytes = is.read();
                if (readBytes == -1) break;
                count += readBytes;
            }
        } catch(IOException e) {
            // ignore
        } finally {
            is.close();
        }
        //=========================================================