View Javadoc

1   /*
2    * $Id: AjaxServletConnector.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.transport.ajax.container;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.api.MuleException;
14  import org.mule.api.construct.FlowConstruct;
15  import org.mule.api.endpoint.ImmutableEndpoint;
16  import org.mule.api.endpoint.InboundEndpoint;
17  import org.mule.api.transport.MessageReceiver;
18  import org.mule.api.transport.ReplyToHandler;
19  import org.mule.transport.AbstractConnector;
20  import org.mule.transport.ajax.AjaxMessageReceiver;
21  import org.mule.transport.ajax.AjaxReplyToHandler;
22  import org.mule.transport.ajax.BayeuxAware;
23  import org.mule.transport.ajax.embedded.AjaxConnector;
24  import org.mule.transport.servlet.ServletConnector;
25  
26  import org.cometd.DataFilter;
27  import org.mortbay.cometd.AbstractBayeux;
28  
29  /**
30   * A servlet connector that binds to the container and makes a configured 
31   * Bayeux available to dispatchers and receivers.
32   */
33  public class AjaxServletConnector extends ServletConnector implements BayeuxAware
34  {
35      public static final String PROTOCOL = "ajax-servlet";
36  
37      /**
38       * The client side poll timeout in milliseconds (default 0). How long a client
39       * will wait between reconnects
40       */
41      private int interval = INT_VALUE_NOT_SET;
42  
43      /**
44       * The max client side poll timeout in milliseconds (default 30000). A client 
45       * will be removed if a connection is not received in this time.
46       */
47      private int maxInterval = INT_VALUE_NOT_SET;
48  
49      /**
50       * The client side poll timeout if multiple connections are detected from the 
51       * same browser (default 1500).
52       */
53      private int multiFrameInterval = INT_VALUE_NOT_SET;
54  
55      /**
56       * 0=none, 1=info, 2=debug
57       */
58      private int logLevel = INT_VALUE_NOT_SET;
59  
60      /**
61       * The server side poll timeout in milliseconds (default 250000). This is how long 
62       * the server will hold a reconnect request before responding.
63       */
64      private int timeout = INT_VALUE_NOT_SET;
65  
66      /**
67       * If "true" (default) then the server will accept JSON wrapped in a comment and 
68       * will generate JSON wrapped in a comment. This is a defence against Ajax Hijacking.
69       */
70      private boolean jsonCommented = true;
71  
72      /**
73       * TODO SUPPORT FILTERS
74       * the location of a JSON file describing {@link DataFilter} instances to be installed
75       */
76      private String filters;
77  
78      /**
79       * If true, the current request is made available via the 
80       * {@link AbstractBayeux#getCurrentRequest()} method
81       */
82      private boolean requestAvailable = true;
83  
84      /**
85       * The number of message refs at which the a single message response will be
86       * cached instead of being generated for every client delivered to. Done to optimize
87       * a single message being sent to multiple clients.
88       */
89      private int refsThreshold = INT_VALUE_NOT_SET;
90      
91  
92      protected AbstractBayeux bayeux;
93  
94      public AjaxServletConnector(MuleContext context) 
95      {
96          super(context);
97          registerSupportedProtocolWithoutPrefix(AjaxConnector.PROTOCOL);
98          //Dont start until the servletContainer is up
99          setInitialStateStopped(true);
100     }
101 
102     public AbstractBayeux getBayeux()
103     {
104         return bayeux;
105     }
106 
107     public void setBayeux(AbstractBayeux bayeux) throws MuleException
108     {
109         this.bayeux = bayeux;
110         this.getBayeux().setJSONCommented(isJsonCommented());
111         if(getLogLevel() != AbstractConnector.INT_VALUE_NOT_SET) this.getBayeux().setLogLevel(getLogLevel());
112         if(getMaxInterval() != AbstractConnector.INT_VALUE_NOT_SET) this.getBayeux().setMaxInterval(getMaxInterval());
113         if(getInterval() != AbstractConnector.INT_VALUE_NOT_SET) this.getBayeux().setInterval(getMaxInterval());
114         if(getMultiFrameInterval() != AbstractConnector.INT_VALUE_NOT_SET) this.getBayeux().setMultiFrameInterval(getMultiFrameInterval());
115         if(getTimeout() != AbstractConnector.INT_VALUE_NOT_SET) this.getBayeux().setTimeout(getMultiFrameInterval());
116         //Only start once we have this
117         this.setInitialStateStopped(false);
118         
119         for (Object receiver : receivers.values())
120         {
121             ((AjaxMessageReceiver)receiver).setBayeux(getBayeux());
122         }
123         start();
124     }
125 
126     @Override
127     public String getProtocol()
128     {
129         return PROTOCOL;
130     }
131 
132     public int getInterval()
133     {
134         return interval;
135     }
136 
137     public void setInterval(int interval)
138     {
139         this.interval = interval;
140     }
141 
142     public int getMaxInterval()
143     {
144         return maxInterval;
145     }
146 
147     public void setMaxInterval(int maxInterval)
148     {
149         this.maxInterval = maxInterval;
150     }
151 
152     public int getMultiFrameInterval()
153     {
154         return multiFrameInterval;
155     }
156 
157     public void setMultiFrameInterval(int multiFrameInterval)
158     {
159         this.multiFrameInterval = multiFrameInterval;
160     }
161 
162     public int getLogLevel()
163     {
164         return logLevel;
165     }
166 
167     public void setLogLevel(int logLevel)
168     {
169         this.logLevel = logLevel;
170     }
171 
172     public int getTimeout()
173     {
174         return timeout;
175     }
176 
177     public void setTimeout(int timeout)
178     {
179         this.timeout = timeout;
180     }
181 
182     public boolean isJsonCommented()
183     {
184         return jsonCommented;
185     }
186 
187     public void setJsonCommented(boolean jsonCommented)
188     {
189         this.jsonCommented = jsonCommented;
190     }
191 
192     public String getFilters()
193     {
194         return filters;
195     }
196 
197     public void setFilters(String filters)
198     {
199         this.filters = filters;
200     }
201 
202     public boolean isRequestAvailable()
203     {
204         return requestAvailable;
205     }
206 
207     public void setRequestAvailable(boolean requestAvailable)
208     {
209         this.requestAvailable = requestAvailable;
210     }
211 
212     public int getRefsThreshold()
213     {
214         return refsThreshold;
215     }
216 
217     public void setRefsThreshold(int refsThreshold)
218     {
219         this.refsThreshold = refsThreshold;
220     }
221 
222     @Override
223     public ReplyToHandler getReplyToHandler(ImmutableEndpoint endpoint)
224     {
225         return new AjaxReplyToHandler(getDefaultResponseTransformers(endpoint), this);
226     }
227 
228     @Override
229     protected MessageReceiver createReceiver(FlowConstruct flowConstruct, InboundEndpoint endpoint) throws Exception
230     {
231         AjaxMessageReceiver receiver = (AjaxMessageReceiver) super.createReceiver(flowConstruct, endpoint);
232         //The Bayeux object will be null of the connector has not started yet, nothing to worry about
233         receiver.setBayeux(getBayeux());
234         return receiver;
235     }
236 }