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              _responses++;
67              _out.write(r);
68          }
69      }
70  
71      public void complete() throws IOException
72      {
73          HttpServletResponse response=getResponse();
74          response.setStatus(200);
75  
76          if (_responses==0)
77          {
78              response.setContentType(_mimeType);
79              _out=response.getWriter();
80              _out.write(this._jsonp==null?__DEFAULT_CALLBACK:_jsonp);
81              _out.write("([");
82          }
83          _out.write("])\r\n");
84          _out.close();
85      }
86  
87      /* ------------------------------------------------------------ */
88      public boolean isMetaConnectDeliveryOnly()
89      {
90          return true;
91      }
92  
93      /* ------------------------------------------------------------ */
94      public String getJsonp()
95      {
96          return _jsonp;
97      }
98  
99      /* ------------------------------------------------------------ */
100     @Override
101     public String toString()
102     {
103         return "JSONPTransport[jsonp="+this._jsonp+"]";
104     }
105 }