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