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      
34      /* ------------------------------------------------------------ */
35      public JSONTransport()
36      {
37          _contentType="text/json; charset=utf-8";
38          _start="[";
39          _end="]\r\n";
40      }
41      
42      /* ------------------------------------------------------------ */
43      public synchronized void send(Message message) throws IOException
44      {
45          if (message!=null)
46          {
47              if (message.size()==0)
48                  throw new IllegalStateException();
49  
50              String r=(message instanceof MessageImpl)
51                  ?((MessageImpl)message).getJSON()
52                  :JSON.toString(message);
53              
54              HttpServletResponse response=getResponse();
55              
56              switch(_responses)
57              {
58                  case 0:
59                      response.setStatus(200);
60                      response.setContentType(_contentType);
61                      _out=response.getWriter();
62                      _out.write(_start);
63                      _out.write(r);
64                      break;
65                      
66                  default: 
67                      _out.write(',');
68                      _out.write(r);
69              }
70              
71              _responses++;
72          }
73      }
74  
75      /* ------------------------------------------------------------ */
76      public synchronized void complete() throws IOException
77      {
78          if (_responses==0)
79          {
80              HttpServletResponse response=getResponse();
81              response.setStatus(200);
82              _out=response.getWriter();
83              _out.write(_start);
84          }
85          _out.write(_end);
86          _out.close();
87          _responses=0;
88      }
89  
90      /* ------------------------------------------------------------ */
91      public boolean isMetaConnectDeliveryOnly()
92      {
93          return false;
94      }
95  }