Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.tomcat.bayeux;
 
 import static org.jboss.web.CoyoteMessages.MESSAGES;
 
 
 

Author(s):
Filip Hanik
Guy Molinari
Version:
1.0
 
 public class BayeuxServlet extends HttpServlet implements HttpEventServlet {


    
The timeout.
 
     protected int timeout = 0;


    
The reconnect interval.
 
     protected int reconnectInterval = 0;


    
Attribute to hold the TomcatBayeux object in the servlet context
 
     public static final String TOMCAT_BAYEUX_ATTR = .;


    
Reference to the global TomcatBayeux object
 
     protected TomcatBayeux tb;
    
    
Upon servlet destruction, the servlet will clean up the TomcatBayeux object and terminate any outstanding events.
 
     public void destroy() {
         // FIXME, close all outstanding comet events
         //tb.destroy();
          = null;//FIXME, close everything down
         
     }
    
    
Returns the preconfigured connection timeout. If no timeout has been configured as a servlet init parameter named timeout then the default of 2min will be used.

Returns:
int - the timeout for a connection in milliseconds
 
     protected int getTimeout() {
         return ;
     }
     
     protected int getReconnectInterval() {
         return ;
     }
 
 
     public void event(HttpEvent cometEventthrows IOExceptionServletException {
         HttpEvent.EventType type = cometEvent.getType();
         if (..isTraceEnabled()) {
             ..trace("["+Thread.currentThread().getName()+"] Received Comet Event type="+type);
        }
        switch (type) {
        case :
            cometEvent.setTimeout(getTimeout());
            break;
        case :
            checkBayeux(cometEvent);
            break;
        case :
        case :
        case :
            break;
        case :
        case :
        case :
            .remove(cometEvent);
            cometEvent.close();
            break;
        }
    }//event

    

Parameters:
cometEvent CometEvent
Returns:
boolean - true if we comet event stays open
Throws:
java.io.IOException
java.lang.UnsupportedOperationException
    protected void checkBayeux(HttpEvent cometEventthrows IOException,
            UnsupportedOperationException {
        // we actually have data.
        // data can be text/json or
        if (..equals(cometEvent.getHttpServletRequest()
                .getContentType())) {
            // read and decode the bytes according to content length
            int contentlength = cometEvent.getHttpServletRequest()
                    .getContentLength();
            throw new UnsupportedOperationException("Decoding "
                    + . + " not yet implemented.");
        } else { // GET method or application/x-www-form-urlencoded
            String message = cometEvent.getHttpServletRequest().getParameter(
                    .);
            if (..isTraceEnabled()) {
                ..trace("[" + Thread.currentThread().getName()
                        + "] Received JSON message:" + message);
            }
            try {
                int action = handleBayeux(messagecometEvent);
                if (..isTraceEnabled()) {
                    ..trace("[" + Thread.currentThread().getName()
                            + "] Bayeux handling complete, action result="
                            + action);
                }
                if (action <= 0) {
                    cometEvent.close();
                }
            } catch (Exception e) {
                .remove(cometEvent);
                ..errorInCheckBayeux(e);
                cometEvent.close();
            }
        }
    }
    
    protected int handleBayeux(String messageHttpEvent eventthrows IOExceptionServletException {
        int result = 0;
        if (message==null || message.length()==0) return result;
        try {
            BayeuxRequest request = null;
            //a message can be an array of messages
            JSONArray jsArray = new JSONArray(message);
            for (int i = 0; i < jsArray.length(); i++) {
                JSONObject msg = jsArray.getJSONObject(i);
                
                if (..isTraceEnabled()) {
                    ..trace("["+Thread.currentThread().getName()+"] Processing bayeux message:"+msg);
                }
                request = RequestFactory.getRequest(,event,msg);
                if (..isTraceEnabled()) {
                    ..trace("["+Thread.currentThread().getName()+"] Processing bayeux message using request:"+request);
                }
                result = request.process(result);
                if (..isTraceEnabled()) {
                    ..trace("["+Thread.currentThread().getName()+"] Processing bayeux message result:"+result);
                }
            }
            if (result>0 && request!=null) {
                event.getHttpServletRequest().setAttribute(.request);
                ClientImpl ci = (ClientImpl).getClient(((RequestBase)request).getClientId());
                ci.addCometEvent(event);
                if (..isTraceEnabled()) {
                    ..trace("["+Thread.currentThread().getName()+"] Done bayeux message added to request attribute");
                }
            } else if (result == 0 && request!=null) {
                RequestBase.deliver(event,(ClientImpl).getClient(((RequestBase)request).getClientId()));
                if (..isTraceEnabled()) {
                    ..trace("["+Thread.currentThread().getName()+"] Done bayeux message, delivered to client");
                }
            }
            
        }catch (JSONException e) {
            result = -1;
        }catch (BayeuxException e) {
            result = -1;
        }
        return result;
    }
    public String getServletInfo() {
        return "Tomcat/BayeuxServlet/1.0";
    }
    public void init() throws ServletException {
        
        if (getServletConfig().getInitParameter("timeout") != null) {
             = Integer.parseInt(getServletConfig().getInitParameter("timeout"));
        }
        if (getServletConfig().getInitParameter("reconnectInterval") != null) {
             = Integer.parseInt(getServletConfig().getInitParameter("reconnectInterval"));
        }
        if (ctx.getAttribute()==null)
            ctx.setAttribute(,new TomcatBayeux());
        this. = (TomcatBayeux)ctx.getAttribute();
    }
    public void service(ServletRequest servletRequestServletResponse servletResponsethrows ServletExceptionIOException {
        if (servletResponse instanceof HttpServletResponse) {
            ( (HttpServletResponseservletResponse).sendError(500, .invalidBayeuxConfiguration());
        } else {
            throw new ServletException(.invalidBayeuxConfiguration());
        }
    }
New to GrepCode? Check out our FAQ X