alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Jetty example source code file (HttpProtocolFilter.java)

This example Jetty source code file (HttpProtocolFilter.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Jetty tags/keywords

bytebuffer, bytebuffer, grizzlyendpoint, grizzlyendpoint, grizzlysocketchannel, grizzlysocketchannel, handler, httpparser, httpprotocolfilter, io, ioexception, ioexception, log, logging, nio, protocolfilter, selectionkey, selectorhandler

The Jetty HttpProtocolFilter.java source code

//========================================================================
// Parts Copyright 2006 Mort Bay Consulting Pty. Ltd.
//------------------------------------------------------------------------
// Licensed 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.mortbay.jetty.grizzly;

import com.sun.grizzly.Context;
import com.sun.grizzly.Controller;
import com.sun.grizzly.ProtocolFilter;
import com.sun.grizzly.SelectorHandler;
import com.sun.grizzly.util.WorkerThread;
import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.logging.Level;
import org.mortbay.io.nio.NIOBuffer;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.HttpConnection;
import org.mortbay.jetty.HttpParser;

/**
 * Delegate the processing of the request to a <code>GrizzlyEndPoint
 *
 * @author Jeanfrancois Arcand
 */
public class HttpProtocolFilter implements ProtocolFilter
{
    private Handler handler;
    
    private boolean keepAlive=true;
    private GrizzlyEndPoint endPoint;
    private GrizzlySocketChannel _channel = new GrizzlySocketChannel();
    private HttpParser parser;
    boolean isError = false;
    
    
    public HttpProtocolFilter()
    {
    }
    
    
    /**
     * Read available bytes and delegate the processing of them to the next
     * ProtocolFilter in the ProtocolChain.
     * @return <tt>true if the next ProtocolFilter on the ProtocolChain
     *                       need to bve invoked.
     */
    public boolean execute(Context ctx) throws IOException
    {
        ctx.getSelectionKey().attach(null);
        
        isError = false;
        _channel.setSelectionKey(ctx.getSelectionKey());
        _channel.setSocketChannel((SocketChannel)ctx.getSelectionKey().channel());
        endPoint.setChannel(_channel);
        ByteBuffer bb = ((WorkerThread)Thread.currentThread()).getByteBuffer();

        try
        {
            int nRead = 1;
            while (nRead > 0)
            {
                endPoint.handle();
                if (endPoint.isComplete())
                {
                    break;
                }
                else
                {
                    nRead = endPoint.fill(parser.getHeaderBuffer());
                }
            }
        }
        catch(Throwable t)
        {
            isError = true;
            Controller.logger().log(Level.FINE,"endPoint.handler",t);
            return false;
        }
        finally
        {
            if (isError)
            {
                endPoint.getHttpConnection().reset(true);
            }
            parser.reset(true);
            _channel.setSelectionKey(null);
            endPoint.setChannel(null);
        }
        bb.clear();
        if (parser.getBodyBuffer() != null) {
            ByteBuffer bodyBuffer = ((NIOBuffer)parser.getBodyBuffer()).getByteBuffer();
            if (bodyBuffer != null){
                bodyBuffer.clear();
                Thread.currentThread().dumpStack();
            }
        }
                    
        ctx.getSelectionKey().attach(null);
        return true;
    }
    
    /**
     * If no bytes were available, close the connection by cancelling the
     * SelectionKey. If bytes were available, register the SelectionKey
     * for new bytes.
     *
     * @return <tt>true if the previous ProtocolFilter postExecute method
     *         needs to be invoked.
     */
    public boolean postExecute(Context ctx) throws IOException
    {
        final SelectorHandler selectorHandler =
                ctx.getSelectorHandler();
        final SelectionKey key = ctx.getSelectionKey();
        
        if (!isError && endPoint.keepAlive())
        {
            ctx.setKeyRegistrationState(Context.KeyRegistrationState.REGISTER);
        }
        else
        {
            ctx.setKeyRegistrationState(Context.KeyRegistrationState.CANCEL);
        }
        return true;
    }
    
    public GrizzlyEndPoint getEndPoint()
    {
        return endPoint;
    }
    
    public void setEndPoint(GrizzlyEndPoint endPoint)
    {
        this.endPoint = endPoint;
    }
    
    public HttpParser getParser()
    {
        return parser;
    }
    
    public void setParser(HttpParser parser)
    {
        this.parser = parser;
    }
    
}

Other Jetty examples (source code examples)

Here is a short list of links related to this Jetty HttpProtocolFilter.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.