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  public class JSONTransport extends AbstractTransport
    27  {
    28      private int _responses=0;
    29      private PrintWriter _out;
    30      protected String _contentType;
    31      protected String _start;
    32      protected String _end;
    33      private boolean _commented;
    34
    35      /* ------------------------------------------------------------ */
    36      public JSONTransport(boolean commented)
    37      {
    38          setJSONCommented(commented);
    39      }
    40
    41      /* ------------------------------------------------------------ */
    42      /**
    43       * @return the commented
    44       */
    45      public boolean isJSONCommented()
    46      {
    47          return _commented;
    48      }
    49
    50      /* ------------------------------------------------------------ */
    51      /**
    52       * @param commented the commented to set
    53       */
    54      public void setJSONCommented(boolean commented)
    55      {
    56          _commented=commented;
    57          if (commented)
    58          {
    59              _contentType="text/json-comment-filtered; charset=utf-8";
    60              _start="/*[";
    61              _end="]*/\r\n";
    62          }
    63          else
    64          {
    65              _contentType="text/json; charset=utf-8";
    66              _start="[";
    67              _end="]\r\n";
    68          }
    69      }
    70
    71      /* ------------------------------------------------------------ */
    72      public synchronized void send(Message message) throws IOException
    73      {
    74          if (message!=null)
    75          {
    76              if (message.size()==0)
    77                  throw new IllegalStateException();
    78
    79              String r=(message instanceof MessageImpl)
    80                  ?((MessageImpl)message).getJSON()
    81                  :JSON.toString(message);
    82
    83              ((MessageImpl)message).decRef();
    84
    85              HttpServletResponse response=getResponse();
    86
    87              switch(_responses)
    88              {
    89                  case 0:
    90                      response.setStatus(200);
    91                      response.setContentType(_contentType);
    92                      _out=response.getWriter();
    93                      _out.write(_start);
    94                      _out.write(r);
    95                      break;
    96
    97                  default:
    98                      _out.write(',');
    99                      _out.write(r);
    100             }
    101
    102             _responses++;
    103         }
    104     }
    105
    106     /* ------------------------------------------------------------ */
    107     public synchronized void complete() throws IOException
    108     {
    109         if (_responses==0)
    110         {
    111             HttpServletResponse response=getResponse();
    112             response.setStatus(200);
    113             _out=response.getWriter();
    114             _out.write(_start);
    115         }
    116         _out.write(_end);
    117         _out.close();
    118         _responses=0;
    119     }
    120
    121     /* ------------------------------------------------------------ */
    122     public boolean resumePoll()
    123     {
    124         return false;
    125     }
    126 }