FileDocCategorySizeDatePackage
CometdAdapter.javaAPI DocGlassfish v2 API9929Fri May 04 22:36:58 BST 2007com.sun.grizzly.cometd.standalone

CometdAdapter

public class CometdAdapter extends com.sun.enterprise.web.connector.grizzly.standalone.StaticResourcesAdapter implements org.apache.coyote.Adapter
Standalone Cometd implementation. This class is used when Cometd is enabled from the Grizzly standalone WebServer. To enable it, just add: -Dcom.sun.grizzly.adapterClass=com.sun.grizzly.cometd.standalone.CometdAdapter
author
Jeanfrancois Arcand

Fields Summary
private String
contextPath
All request to that context-path will be considered as cometd enabled.
private com.sun.grizzly.cometd.BayeuxCometHandler
bayeuxCometHandler
The Bayeux CometHandler implementation.
private com.sun.grizzly.cometd.EventRouter
eventRouter
The EventRouter used to route JSON message.
private boolean
initialized
Is the BayeuxCometHandler initialized and added to the Grizzly CometEngine.
public static final int
ADAPTER_NOTES
Constructors Summary
public CometdAdapter()

    
    
      
        super();
        CometEngine cometEngine = CometEngine.getEngine();
        CometContext cometContext = cometEngine.register(contextPath);
        cometContext.setExpirationDelay(60 * 60 * 1000);    
        cometContext.setBlockingNotification(true);
        cometContext.setNotificationHandler(new CometdNotificationHandler());
    
Methods Summary
public voidservice(org.apache.coyote.Request req, org.apache.coyote.Response res)
Route the request to the cometd implementation. If the request point to a static file, delegate the call to the Grizzly WebServer implementation.

        MessageBytes mb = req.requestURI();
        ByteChunk requestURI = mb.getByteChunk();
        String uri = req.requestURI().toString();
        File file = new File(getRootFolder(),uri);
        if (file.isDirectory()) {
            uri += "index.html";
            file = new File(file,uri);
        }

        if (file.canRead()) {
            super.service(req,res);
            return;
        }
             
        CometEngine cometEngine = CometEngine.getEngine();
        CometContext cometContext = cometEngine.getCometContext(contextPath);        
        if (!initialized){
            synchronized(cometContext){
                if (!initialized){                            
                    bayeuxCometHandler = new BayeuxCometHandler();
                    eventRouter = new EventRouterImpl(cometContext);    
                    int mainHandlerHash = 
                            cometContext.addCometHandler(bayeuxCometHandler,true);
                    cometContext.addAttribute(BayeuxCometHandler.BAYEUX_COMET_HANDLER,
                                              mainHandlerHash);
                    initialized = true;
                }
            }
        }
        
        CometdRequest cometdReq = (CometdRequest) req.getNote(ADAPTER_NOTES);
        CometdResponse cometdRes = (CometdResponse) res.getNote(ADAPTER_NOTES);
        if (cometdReq == null) {
            cometdReq = new CometdRequest<Request>(req) {
                
                private boolean requestParametersParsed = false;
                protected byte[] postData = null;
                private ByteChunk chunk = new ByteChunk();
                private byte[] body = new byte[8192];
                
                public String[] getParameterValues(String s) {
                    
                    Parameters parameters = request.getParameters();
                    requestParametersParsed = true;
                    
                    parameters.setEncoding
                            (org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING);
                    parameters.setQueryStringEncoding
                            (org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING);
                    parameters.handleQueryParameters();
                    int len = request.getContentLength();
                     
                    if (len > 0) {
                        try {
                            
                            byte[] formData = getPostBody();
                            if (formData != null) {
                                parameters.processParameters(formData, 0, len);
                            }
                        } catch (Throwable t) {
                            ; // Ignore
                        }
                    }
                    
                    return parameters.getParameterValues(s);
                }

                protected byte[] getPostBody() throws IOException {                   
                    int len = request.getContentLength();                    
                    int actualLen = readPostBody(chunk, len);
                    if (actualLen == len) {                       
                        chunk.substract(body,0,chunk.getLength());
                        chunk.recycle();
                        return body;
                    }                    
                    return null;
                }
                               
                /**
                 * Read post body in an array.
                 */
                protected int readPostBody(ByteChunk chunk,int len) throws IOException {
                    
                    int offset = 0;
                    do {
                        int inputLen = request.doRead(chunk);
                        if (inputLen <= 0) {
                            return offset;
                        }
                        offset += inputLen;
                    } while ((len - offset) > 0);
                    return len;                   
                }                
            };
            
            cometdRes = new CometdResponse<Response>(res) {
                
                private StringBuffer buf = new StringBuffer();              
                private ByteChunk chunk = new ByteChunk();
                
                public void write(String s) throws IOException {
                    buf.append(s);
                }
                
                public void flush() throws IOException {
                    int length = buf.length();
                    response.addHeader("Server", 
                                       SelectorThread.SERVER_NAME); 
                    response.sendHeaders();
                    chunk.setBytes(buf.toString().getBytes(),0,length);
                    res.doWrite(chunk);   
                    chunk.recycle();
                    buf.delete(0,length);
                }
                
                public void setContentType(String s) {
                    response.setContentType(s);
                }
            };
            
            // Set as notes so we don't create them on every request.'
            req.setNote(ADAPTER_NOTES, cometdReq);
            res.setNote(ADAPTER_NOTES, cometdRes);
        }  else {
            cometdReq.setRequest(req);
            cometdRes.setResponse(res);
        }

        eventRouter.route(cometdReq,cometdRes);