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      String _mimeType;
39      
40      public JSONPTransport(String jsonp)
41      {
42          _mimeType="text/javascript; charset=utf-8";
43          _jsonp=jsonp;
44      }
45      
46      public void send(Message message) throws IOException
47      {
48          if (message!=null)
49          {
50              if (_responses==0)
51              {
52                  HttpServletResponse response=getResponse();
53                  response.setContentType(_mimeType);
54                  _out=response.getWriter();
55                  _out.write(this._jsonp==null?__DEFAULT_CALLBACK:_jsonp);
56                  _out.write("([");
57              }
58              else
59              {
60                  _out.write(",\r\n");
61              }
62  
63              String r=(message instanceof MessageImpl)
64                  ?((MessageImpl)message).getJSON()
65                  :JSON.toString(message);
66              ((MessageImpl)message).decRef();
67              _responses++;
68              _out.write(r);
69          }
70      }
71  
72      public void complete() throws IOException
73      {
74          HttpServletResponse response=getResponse();
75          response.setStatus(200);
76  
77          if (_responses==0)
78          {
79              response.setContentType(_mimeType);
80              _out=response.getWriter();
81              _out.write(this._jsonp==null?__DEFAULT_CALLBACK:_jsonp);
82              _out.write("([");
83          }
84          _out.write("])\r\n");
85          _out.close();
86      }
87  
88      /* ------------------------------------------------------------ */
89      public boolean resumePoll()
90      {
91          return true;
92      }
93  
94      /* ------------------------------------------------------------ */
95      public String getJsonp()
96      {
97          return _jsonp;
98      }
99  
100     /* ------------------------------------------------------------ */
101     @Override
102     public String toString()
103     {
104         return "JSONPTransport[jsonp="+this._jsonp+"]";
105     }
106 }