home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Jetty example source code file (AbstractCometdServlet.java)

This example Jetty source code file (AbstractCometdServlet.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

abstractbayeux, abstractbayeux, arraylist, client_attr, cookie, cookie, http, io, ioexception, list, map, message, request, response, servlet, servletexception, servletexception, string, string, util

The Jetty AbstractCometdServlet.java source code

// ========================================================================
// Copyright 2007 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package org.mortbay.cometd;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.cometd.filter.JSONDataFilter;
import org.mortbay.log.Log;
import org.mortbay.util.ajax.JSON;

import dojox.cometd.Bayeux;
import dojox.cometd.DataFilter;
import dojox.cometd.Message;

 * Cometd Filter Servlet implementing the {@link AbstractBayeux} protocol.
 * The Servlet can be initialized with a json file mapping channels to
 * {@link DataFilter} definitions. The servlet init parameter "filters" should
 * point to a webapplication resource containing a JSON array of filter
 * definitions. For example:
 * <pre>
 *  [
 *    { 
 *      "channels": "/**",
 *      "class"   : "org.mortbay.cometd.filter.NoMarkupFilter",
 *      "init"    : {}
 *    }
 *  ]
 * </pre>
 * The following init parameters can be used to configure the servlet:<dl>
 * <dt>timeout
 * <dd>The server side poll timeout in milliseconds (default 250000). This is how
 * long the server will hold a reconnect request before responding.</dd>
 * <dt>interval
 * <dd>The client side poll timeout in milliseconds (default 0). How long a client
 * will wait between reconnects</dd>
 * <dt>maxInterval
 * <dd>The max client side poll timeout in milliseconds (default 30000). A client will
 * be removed if a connection is not received in this time.
 * <dt>multiFrameInterval
 * <dd>the client side poll timeout
 * if multiple connections are detected from the same browser (default 1500).</dd>
 * <dt>JSONCommented
 * <dd>If "true" then the server will accept JSON wrapped
 * in a comment and will generate JSON wrapped in a comment. This is a defence against
 * Ajax Hijacking.</dd>
 * <dt>alwaysResumePoll
 * <dd>If true, then reconnect requests will always
 * be resumed when a message is delivered. This may be needed for some cross domain 
 * transports that need strict ordering of responses.</dd>
 * <dt>filters
 * <dd>the location of a JSON file describing {@link DataFilter} instances to be installed
 * <dt>requestAvailable
 * <dd>If true, the current request is made available via the {@link AbstractBayeux#getCurrentRequest()} method
 * <dt>loglevel
 * <dd>0=none, 1=info, 2=debug
 * <dt>directDeliver
 * <dd>true if published messages are delivered directly to subscribers (default). If false, a message copy is created with only supported fields
 * </dl>
 * @author gregw
 * @author aabeling: added JSONP transport
 * @see {@link AbstractBayeux}
 * @see {@link ChannelId}
public abstract class AbstractCometdServlet extends HttpServlet
    public static final String CLIENT_ATTR="org.mortbay.cometd.client";
    public static final String TRANSPORT_ATTR="org.mortbay.cometd.transport";
    public static final String MESSAGE_PARAM="message";
    public static final String TUNNEL_INIT_PARAM="tunnelInit";
    public static final String HTTP_CLIENT_ID="BAYEUX_HTTP_CLIENT";
    public final static String BROWSER_ID="BAYEUX_BROWSER";

    protected AbstractBayeux _bayeux;

    public AbstractBayeux getBayeux()
        return _bayeux;
    protected abstract AbstractBayeux newBayeux();
    public void init() throws ServletException
        synchronized (AbstractCometdServlet.class)
            if (_bayeux==null)
            boolean was_initialized=_bayeux.isInitialized();
            if (!was_initialized)
                String filters=getInitParameter("filters");
                if (filters!=null)
                        InputStream is = getServletContext().getResourceAsStream(filters);
                        if (is==null)
                            throw new FileNotFoundException(filters);
                        Object[] objects=(Object[])JSON.parse(new InputStreamReader(getServletContext().getResourceAsStream(filters),"utf-8"));
                        for (int i=0; objects!=null&&i<objects.length; i++)
                            Map<?,?> filter_def=(Map)objects[i];

                            String fc = (String)filter_def.get("class");
                            if (fc!=null)
                                Log.warn(filters+" file uses deprecated \"class\" name. Use \"filter\" instead");
                            Class<?> c=Thread.currentThread().getContextClassLoader().loadClass(fc);
                            DataFilter filter=(DataFilter)c.newInstance();

                            if (filter instanceof JSONDataFilter)

                    catch (Exception e)
                        getServletContext().log("Could not parse: "+filters,e);
                        throw new ServletException(e);

                String timeout=getInitParameter("timeout");
                if (timeout!=null)
                String maxInterval=getInitParameter("maxInterval");
                if (maxInterval!=null)

                String commentedJSON=getInitParameter("JSONCommented");
                _bayeux.setJSONCommented(commentedJSON!=null && Boolean.parseBoolean(commentedJSON));

                String l=getInitParameter("logLevel");
                if (l!=null&&l.length()>0)
                String interval=getInitParameter("interval");
                if (interval!=null)
                String mfInterval=getInitParameter("multiFrameInterval");
                if (mfInterval!=null)

                String requestAvailable=getInitParameter("requestAvailable");
                _bayeux.setRequestAvailable(requestAvailable!=null && Boolean.parseBoolean(requestAvailable));

                String direct=getInitParameter("directDeliver");
                if (direct!=null)


    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
        if (_bayeux.isRequestAvailable())
            if (_bayeux.isRequestAvailable())

    protected String browserId(HttpServletRequest request)
        Cookie[] cookies = request.getCookies();
        if (cookies!=null)
            for (Cookie cookie : cookies)
                if (BROWSER_ID.equals(cookie.getName()))
                    return cookie.getValue();
        return null;

    protected String newBrowserId(HttpServletRequest request,HttpServletResponse response)
        String browser_id=Long.toHexString(request.getRemotePort())+
        Cookie cookie = new Cookie(BROWSER_ID,browser_id);
        return browser_id;
    private static Message[] __EMPTY_BATCH=new Message[0];

    protected Message[] getMessages(HttpServletRequest request) throws IOException
        String fodder=null;
            // Get message batches either as JSON body or as message parameters
            if (request.getContentType() != null && !request.getContentType().startsWith("application/x-www-form-urlencoded"))
                return _bayeux.parse(request.getReader());

            String[] batches=request.getParameterValues(MESSAGE_PARAM);

            if (batches==null || batches.length==0)
                return __EMPTY_BATCH;

            if (batches.length==0)
                return _bayeux.parse(fodder);

            List<Message> messages = new ArrayList();
            for (int i=0;i<batches.length;i++)
                if (batches[i]==null)


            return messages.toArray(new Message[messages.size()]);
        catch(IOException e)
            throw e;
        catch(Exception e)
            throw new Error(fodder,e);


Other Jetty examples (source code examples)

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

my book on functional programming


new blog posts


Copyright 1998-2019 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.