Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
 package com.sun.grizzly.cometd.standalone;
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

Jeanfrancois Arcand
 public class CometdAdapter extends StaticResourcesAdapter implements Adapter {
All request to that context-path will be considered as cometd enabled.
     private String contextPath = "/cometd/cometd";
The Bayeux CometHandler implementation.
     private BayeuxCometHandler bayeuxCometHandler;
The EventRouter used to route JSON message.
     private EventRouter eventRouter;
Is the BayeuxCometHandler initialized and added to the Grizzly CometEngine.
     private boolean initialized = false;
     public static final int ADAPTER_NOTES = 1;
    public CometdAdapter() {
        CometEngine cometEngine = CometEngine.getEngine();
        CometContext cometContext = cometEngine.register();
        cometContext.setNotificationHandler(new CometdNotificationHandler());
Route the request to the cometd implementation. If the request point to a static file, delegate the call to the Grizzly WebServer implementation.
    public void service(Request reqfinal Response resthrows Exception {
        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()) {
        CometEngine cometEngine = CometEngine.getEngine();
        CometContext cometContext = cometEngine.getCometContext();        
        if (!){
                if (!){                            
                     = new BayeuxCometHandler();
                     = new EventRouterImpl(cometContext);    
                    int mainHandlerHash = 
                     = true;
        CometdRequest cometdReq = (CometdRequestreq.getNote();
        CometdResponse cometdRes = (CometdResponseres.getNote();
        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 = .getParameters();
                     = true;
                    int len = .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 = .getContentLength();                    
                    int actualLen = readPostBody(len);
                    if (actualLen == len) {                       
                        return ;
                    return null;
Read post body in an array.
                protected int readPostBody(ByteChunk chunk,int lenthrows IOException {
                    int offset = 0;
                    do {
                        int inputLen = .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 sthrows IOException {
                public void flush() throws IOException {
                    int length = .length();
                public void setContentType(String s) {
            // Set as notes so we don't create them on every request.'
        }  else {
New to GrepCode? Check out our FAQ X