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

Jetty example source code file (JSONPTransport.java)

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

abstracttransport, http, httpservletresponse, httpservletresponse, io, ioexception, ioexception, jsonptransport, jsonptransport, messageimpl, printwriter, request, response, servlet, string, string

The Jetty JSONPTransport.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,
// 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.cometd;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.mortbay.util.ajax.JSON;

import dojox.cometd.Message;

/* ------------------------------------------------------------ */
/**
 * @author aabeling
 * @author gregw
 * 
 */
public class JSONPTransport extends AbstractTransport
{
    public final static String __DEFAULT_CALLBACK="jsonpcallback";
    int _responses=0;
    PrintWriter _out;
    String _jsonp= null;
    boolean _commented;
    String _mimeType;
    
    public JSONPTransport(boolean commented,String jsonp)
    {
        setJSONCommented(commented);
        _commented=commented;
        _jsonp=jsonp;
    }
    
    public void send(Message message) throws IOException
    {
        if (message!=null)
        {
            if (_responses==0)
            {
                HttpServletResponse response=getResponse();
                response.setContentType(_mimeType);
                _out=response.getWriter();
                if (_commented)
                    _out.write("/*");
                _out.write(this._jsonp==null?__DEFAULT_CALLBACK:_jsonp);
                _out.write("([");
            }
            else
            {
                _out.write(",\r\n");
            }

            String r=(message instanceof MessageImpl)
                ?((MessageImpl)message).getJSON()
                :JSON.toString(message);
            ((MessageImpl)message).decRef();
            _responses++;
            _out.write(r);
        }
    }

    public void complete() throws IOException
    {
        HttpServletResponse response=getResponse();
        response.setStatus(200);

        if (_responses==0)
        {
            response.setContentType(_mimeType);
            _out=response.getWriter();
            if (_commented)
                _out.write("/*");
            _out.write(this._jsonp==null?__DEFAULT_CALLBACK:_jsonp);
            _out.write("([");
        }
        if (_commented)
            _out.write("])*/\r\n");
        else
            _out.write("])\r\n");
        _out.flush();
    }

    /* ------------------------------------------------------------ */
    public boolean alwaysResumePoll()
    {
        return true;
    }

    /* ------------------------------------------------------------ */
    public String getJsonp()
    {
        return _jsonp;
    }

    /* ------------------------------------------------------------ */
    public String toString()
    {
        return "JSONPTransport[jsonp="+this._jsonp+"]";
    }

    /* ------------------------------------------------------------ */
    /**
     * @return the commented
     */
    public boolean isJSONCommented()
    {
        return _commented;
    }

    /* ------------------------------------------------------------ */
    /**
     * @param commented the commented to set
     */
    public void setJSONCommented(boolean commented)
    {
        _commented=commented;
        _mimeType=commented?"text/javascript-comment-filtered; charset=utf-8":"text/javascript; charset=utf-8";
    }
}

Other Jetty examples (source code examples)

Here is a short list of links related to this Jetty JSONPTransport.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.