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              ((MessageImpl)message).decRef();
55              
56              HttpServletResponse response=getResponse();
57              
58              switch(_responses)
59              {
60                  case 0:
61                      response.setStatus(200);
62                      response.setContentType(_contentType);
63                      _out=response.getWriter();
64                      _out.write(_start);
65                      _out.write(r);
66                      break;
67                      
68                  default: 
69                      _out.write(',');
70                      _out.write(r);
71              }
72              
73              _responses++;
74          }
75      }
76  
77      /* ------------------------------------------------------------ */
78      public synchronized void complete() throws IOException
79      {
80          if (_responses==0)
81          {
82              HttpServletResponse response=getResponse();
83              response.setStatus(200);
84              _out=response.getWriter();
85              _out.write(_start);
86          }
87          _out.write(_end);
88          _out.close();
89          _responses=0;
90      }
91  
92      /* ------------------------------------------------------------ */
93      public boolean resumePoll()
94      {
95          return false;
96      }
97  }