View Javadoc

    1   // ========================================================================
    2   // Copyright 2007 Mort Bay Consulting Pty. Ltd.
    3   // ------------------------------------------------------------------------
    4   // Licensed under the Apache License, Version 2.0 (the "License");
    5   // you may not use this file except in compliance with the License.
    6   // You may obtain a copy of the License at
    7   // http://www.apache.org/licenses/LICENSE-2.0
    8   // Unless required by applicable law or agreed to in writing, software
    9   // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  //========================================================================
    14
    15  package org.mortbay.cometd;
    16
    17  import java.io.IOException;
    18  import java.io.PrintWriter;
    19
    20  import javax.servlet.http.HttpServletResponse;
    21
    22  import org.cometd.Message;
    23  import org.mortbay.util.ajax.JSON;
    24
    25
    26  /* ------------------------------------------------------------ */
    27  /**
    28   * @author aabeling
    29   * @author gregw
    30   *
    31   */
    32  public class JSONPTransport extends AbstractTransport
    33  {
    34      public final static String __DEFAULT_CALLBACK="jsonpcallback";
    35      int _responses=0;
    36      PrintWriter _out;
    37      String _jsonp= null;
    38      boolean _commented;
    39      String _mimeType;
    40
    41      public JSONPTransport(boolean commented,String jsonp)
    42      {
    43          setJSONCommented(commented);
    44          _commented=commented;
    45          _jsonp=jsonp;
    46      }
    47
    48      public void send(Message message) throws IOException
    49      {
    50          if (message!=null)
    51          {
    52              if (_responses==0)
    53              {
    54                  HttpServletResponse response=getResponse();
    55                  response.setContentType(_mimeType);
    56                  _out=response.getWriter();
    57                  if (_commented)
    58                      _out.write("/*");
    59                  _out.write(this._jsonp==null?__DEFAULT_CALLBACK:_jsonp);
    60                  _out.write("([");
    61              }
    62              else
    63              {
    64                  _out.write(",\r\n");
    65              }
    66
    67              String r=(message instanceof MessageImpl)
    68                  ?((MessageImpl)message).getJSON()
    69                  :JSON.toString(message);
    70              ((MessageImpl)message).decRef();
    71              _responses++;
    72              _out.write(r);
    73          }
    74      }
    75
    76      public void complete() throws IOException
    77      {
    78          HttpServletResponse response=getResponse();
    79          response.setStatus(200);
    80
    81          if (_responses==0)
    82          {
    83              response.setContentType(_mimeType);
    84              _out=response.getWriter();
    85              if (_commented)
    86                  _out.write("/*");
    87              _out.write(this._jsonp==null?__DEFAULT_CALLBACK:_jsonp);
    88              _out.write("([");
    89          }
    90          if (_commented)
    91              _out.write("])*/\r\n");
    92          else
    93              _out.write("])\r\n");
    94          _out.close();
    95      }
    96
    97      /* ------------------------------------------------------------ */
    98      public boolean resumePoll()
    99      {
    100         return true;
    101     }
    102
    103     /* ------------------------------------------------------------ */
    104     public String getJsonp()
    105     {
    106         return _jsonp;
    107     }
    108
    109     /* ------------------------------------------------------------ */
    110     @Override
    111     public String toString()
    112     {
    113         return "JSONPTransport[jsonp="+this._jsonp+"]";
    114     }
    115
    116     /* ------------------------------------------------------------ */
    117     /**
    118      * @return the commented
    119      */
    120     public boolean isJSONCommented()
    121     {
    122         return _commented;
    123     }
    124
    125     /* ------------------------------------------------------------ */
    126     /**
    127      * @param commented the commented to set
    128      */
    129     public void setJSONCommented(boolean commented)
    130     {
    131         _commented=commented;
    132         _mimeType=commented?"text/javascript-comment-filtered; charset=utf-8":"text/javascript; charset=utf-8";
    133     }
    134 }