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