org.atmosphere.cpr
Class AtmosphereResourceImpl

java.lang.Object
  extended by org.atmosphere.cpr.AtmosphereResourceImpl
All Implemented Interfaces:
AtmosphereResource

public class AtmosphereResourceImpl
extends Object
implements AtmosphereResource

AtmosphereResource implementation for supporting AtmosphereRequest and AtmosphereResponse.

Author:
Jeanfrancois Arcand

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.atmosphere.cpr.AtmosphereResource
AtmosphereResource.TRANSPORT
 
Field Summary
protected  Action action
           
protected  AsyncSupport asyncSupport
           
protected  Broadcaster broadcaster
           
static String METEOR
           
static String PRE_SUSPEND
           
protected  javax.servlet.http.HttpSession session
           
static String SKIP_BROADCASTER_CREATION
           
 
Constructor Summary
AtmosphereResourceImpl(AtmosphereConfig config, Broadcaster broadcaster, AtmosphereRequest req, AtmosphereResponse response, AsyncSupport asyncSupport, AtmosphereHandler atmosphereHandler)
          Create an AtmosphereResource.
 
Method Summary
 void _destroy()
           
 Action action()
          Return the Action which represent the state of the response, e.g.
 AtmosphereResource addEventListener(AtmosphereResourceEventListener e)
          Add a AtmosphereResourceEventListener.
 AtmosphereResourceImpl atmosphereHandler(AtmosphereHandler atmosphereHandler)
           
 ConcurrentLinkedQueue<AtmosphereResourceEventListener> atmosphereResourceEventListener()
           
 void cancel()
           
 AtmosphereResourceImpl cloneState(AtmosphereResource r)
           
 void cors()
           
static String createStreamingPadding(String padding)
          Output message when Atmosphere suspend a connection.
 AtmosphereResourceImpl disableSuspend(boolean disableSuspend)
           
 AtmosphereConfig getAtmosphereConfig()
          Return the AtmosphereConfig
 AtmosphereHandler getAtmosphereHandler()
          Return the associated AtmosphereHandler associated with this resource.
 AtmosphereResourceEventImpl getAtmosphereResourceEvent()
          Return the current AtmosphereResourceEvent.
 Broadcaster getBroadcaster()
          Return the current Broadcaster
protected  Broadcaster getBroadcaster(boolean autoCreate)
           
 AtmosphereRequest getRequest()
          Return the underlying AtmosphereRequest Request.
 AtmosphereRequest getRequest(boolean enforceScope)
          
 AtmosphereResponse getResponse()
          Return the AtmosphereResponse
 AtmosphereResponse getResponse(boolean enforceScope)
          
 Serializer getSerializer()
          Get the Serializer or null if not defined.
 boolean isCancelled()
          Return true if this object has been cancelled.
 boolean isInScope()
          Is the AtmosphereRequest still valid.
 boolean isResumed()
          Return true if this object has been resumed.
 boolean isSuspended()
          Return true is the AtmosphereResource.suspend() has been invoked.
 AtmosphereResource notifyListeners()
          Notify All AtmosphereResourceEventListener.
 AtmosphereResource notifyListeners(AtmosphereResourceEvent event)
          Notify AtmosphereResourceEventListener.
 void onThrowable(Throwable t)
          Notify AtmosphereResourceEventListener an unexpected exception occured.\
 AtmosphereResource padding(String padding)
          Set the padding to use when flushing the response when transport equals 'streaming' See ApplicationConfig.STREAMING_PADDING_MODE for more info.
 AtmosphereResource removeEventListener(AtmosphereResourceEventListener e)
          Remove aAtmosphereResourceEventListener.
 AtmosphereResource removeEventListeners()
          Remove all AtmosphereResourceEventListener.
 AtmosphereResource resume()
          Complete the AtmosphereResponse and finish/commit it.
 boolean resumeOnBroadcast()
          Return the is the AtmosphereResource.resumeOnBroadcast()
 AtmosphereResource resumeOnBroadcast(boolean resumeOnBroadcast)
          Set to true to resume the response once after the first broadcast.
 javax.servlet.http.HttpSession session()
          Return the HttpSession is supported, null if not
 javax.servlet.http.HttpSession session(boolean create)
          Return the HttpSession is supported, and creates it if not already created.
 AtmosphereResourceImpl session(javax.servlet.http.HttpSession session)
           
 AtmosphereResourceImpl setBroadcaster(Broadcaster broadcaster)
          Set the current Broadcaster.
 void setIsInScope(boolean isInScope)
          Protect the object for being used after it got cancelled.
 AtmosphereResource setSerializer(Serializer s)
          Set the Serializer used to write broadcasted object.
 AtmosphereResource suspend()
          Suspend the AtmosphereResponse indefinitely.
 AtmosphereResource suspend(long timeout)
          Suspend the AtmosphereResponse.
 AtmosphereResource suspend(long timeout, boolean flushComment)
          Suspend the AtmosphereResponse.
 AtmosphereResource suspend(long timeout, TimeUnit timeunit)
          Suspend the AtmosphereResponse.
 AtmosphereResource suspend(long timeout, TimeUnit timeunit, boolean flushComment)
          Suspend the AtmosphereResponse.
 String toString()
           
 AtmosphereResource.TRANSPORT transport()
          Return the current AtmosphereResource.TRANSPORT.
 String uuid()
          Return the unique ID associated with this AtmosphereResource.
 AtmosphereResource write(OutputStream os, Object o)
          Write the broadcasted object using the OutputStream.
 Object writeOnTimeout()
          Return the object that will be written when the resource times out;
 AtmosphereResource writeOnTimeout(Object o)
          Set a message that will be written when the resource times out.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PRE_SUSPEND

public static final String PRE_SUSPEND

SKIP_BROADCASTER_CREATION

public static final String SKIP_BROADCASTER_CREATION

METEOR

public static final String METEOR

action

protected final Action action

broadcaster

protected Broadcaster broadcaster

asyncSupport

protected final AsyncSupport asyncSupport

session

protected javax.servlet.http.HttpSession session
Constructor Detail

AtmosphereResourceImpl

public AtmosphereResourceImpl(AtmosphereConfig config,
                              Broadcaster broadcaster,
                              AtmosphereRequest req,
                              AtmosphereResponse response,
                              AsyncSupport asyncSupport,
                              AtmosphereHandler atmosphereHandler)
Create an AtmosphereResource.

Parameters:
config - The AtmosphereConfig
broadcaster - The Broadcaster.
req - The AtmosphereRequest
response - The AtmosphereResource
asyncSupport - The AsyncSupport
atmosphereHandler - The AtmosphereHandler
Method Detail

getAtmosphereResourceEvent

public AtmosphereResourceEventImpl getAtmosphereResourceEvent()
Return the current AtmosphereResourceEvent.

Specified by:
getAtmosphereResourceEvent in interface AtmosphereResource

getAtmosphereHandler

public AtmosphereHandler getAtmosphereHandler()
Return the associated AtmosphereHandler associated with this resource.

Specified by:
getAtmosphereHandler in interface AtmosphereResource
Returns:
the associated AtmosphereHandler associated with this resource.

writeOnTimeout

public AtmosphereResource writeOnTimeout(Object o)
Set a message that will be written when the resource times out. Cab be an Object or Callable

Specified by:
writeOnTimeout in interface AtmosphereResource

writeOnTimeout

public Object writeOnTimeout()
Return the object that will be written when the resource times out;

Specified by:
writeOnTimeout in interface AtmosphereResource

uuid

public String uuid()
Description copied from interface: AtmosphereResource
Return the unique ID associated with this AtmosphereResource.

Specified by:
uuid in interface AtmosphereResource
Returns:
the unique ID associated with this AtmosphereResource.

transport

public AtmosphereResource.TRANSPORT transport()
Return the current AtmosphereResource.TRANSPORT. The transport value is retrieved using the HeaderConfig.X_ATMOSPHERE_TRANSPORT header value.

Specified by:
transport in interface AtmosphereResource

resumeOnBroadcast

public AtmosphereResource resumeOnBroadcast(boolean resumeOnBroadcast)
Set to true to resume the response once after the first broadcast. False by default.

Specified by:
resumeOnBroadcast in interface AtmosphereResource

isSuspended

public boolean isSuspended()
Return true is the AtmosphereResource.suspend() has been invoked.

Specified by:
isSuspended in interface AtmosphereResource
Returns:
true is the AtmosphereResource.suspend() has been invoked.

resumeOnBroadcast

public boolean resumeOnBroadcast()
Return the is the AtmosphereResource.resumeOnBroadcast()

Specified by:
resumeOnBroadcast in interface AtmosphereResource

resume

public AtmosphereResource resume()
Complete the AtmosphereResponse and finish/commit it. If the AtmosphereResponse is in the process of being resumed, invoking that method has no effect.

Specified by:
resume in interface AtmosphereResource

suspend

public AtmosphereResource suspend()
Suspend the AtmosphereResponse indefinitely. Suspending a AtmosphereResponse will tell the underlying container to avoid recycling objects associated with the current instance, and also to avoid commiting response.

The Framework will output some HTML comments when suspending the response in order to make sure all Browser works well with suspended response.

Specified by:
suspend in interface AtmosphereResource

suspend

public AtmosphereResource suspend(long timeout)
Suspend the AtmosphereResponse. Suspending a AtmosphereResponse will tell the underlying container to avoid recycling objects associated with the current instance, and also to avoid commiting response. Invoking this method when a request is being timed out, e.g. AtmosphereResourceEvent.isResumedOnTimeout() return true, has no effect.

The Framework will output some HTML comments when suspending the response in order to make sure all Browser works well with suspended response. By default, the AtmosphereResponse.getWriter() will be used. You can change that behavior by setting a request attribute named org.atmosphere.useStream to so the framework will use AtmosphereResponse.getOutputStream()

Specified by:
suspend in interface AtmosphereResource
Parameters:
timeout - The maximum amount of time, in milliseconds, a AtmosphereResponse can be suspended. When the timeout expires, the AtmosphereResponse will be automatically resumed and committed. Usage of any methods of a AtmosphereResponse that times out will throw an IllegalStateException.

suspend

public AtmosphereResource suspend(long timeout,
                                  TimeUnit timeunit)
Suspend the AtmosphereResponse. Suspending a AtmosphereResponse will tell the underlying container to avoid recycling objects associated with the current instance, and also to avoid commiting response. Invoking this method when a request is being timed out, e.g. AtmosphereResourceEvent.isResumedOnTimeout() return true, has no effect.

The Framework will output some HTML comments when suspending the response in order to make sure all Browser works well with suspended response. By default, the AtmosphereResponse.getWriter() will be used. You can change that behavior by setting a request attribute named org.atmosphere.useStream to so the framework will use AtmosphereResponse.getOutputStream()

Specified by:
suspend in interface AtmosphereResource
Parameters:
timeout - The maximum amount of time, in milliseconds, a AtmosphereResponse can be suspended. When the timeout expires, the AtmosphereResponse will be automatically resumed and committed. Usage of any methods of a AtmosphereResponse that times out will throw an IllegalStateException.
timeunit - The time unit of the timeout value

suspend

public AtmosphereResource suspend(long timeout,
                                  TimeUnit timeunit,
                                  boolean flushComment)
Suspend the AtmosphereResponse. Suspending a AtmosphereResponse will tell the underlying container to avoid recycling objects associated with the current instance, and also to avoid commiting response. Invoking this method when a request is being timed out, e.g. AtmosphereResourceEvent.isResumedOnTimeout() return true, has no effect.

The Framework will output some HTML comments when suspending the response in order to make sure all Browser works well with suspended response. By default, the AtmosphereResponse.getWriter() will be used. You can change that behavior by setting a request attribute named org.atmosphere.useStream to so the framework will use AtmosphereResponse.getOutputStream()

Specified by:
suspend in interface AtmosphereResource
Parameters:
timeout - The maximum amount of time, in milliseconds, a AtmosphereResponse can be suspended. When the timeout expires, the AtmosphereResponse will be automatically resumed and committed. Usage of any methods of a AtmosphereResponse that times out will throw an IllegalStateException.
timeunit - The time unit of the timeout value
flushComment - By default, Atmosphere will output some comments to make WebKit based browser working. Set it to false if you want to remove it.

suspend

public AtmosphereResource suspend(long timeout,
                                  boolean flushComment)
Description copied from interface: AtmosphereResource
Suspend the AtmosphereResponse. Suspending a AtmosphereResponse will tell the underlying container to avoid recycling objects associated with the current instance, and also to avoid commiting response. Invoking this method when a request is being timed out, e.g. AtmosphereResourceEvent.isResumedOnTimeout() return true, has no effect.

The Framework will output some HTML comments when suspending the response in order to make sure all Browser works well with suspended response. By default, the AtmosphereResponse.getWriter() will be used. You can change that behavior by setting a request attribute named org.atmosphere.useStream to so the framework will use AtmosphereResponse.getOutputStream()

Specified by:
suspend in interface AtmosphereResource
Parameters:
timeout - The maximum amount of time, in milliseconds, a AtmosphereResponse can be suspended. When the timeout expires, the AtmosphereResponse will be automatically resumed and committed. Usage of any methods of a AtmosphereResponse that times out will throw an IllegalStateException.
flushComment - By default, Atmosphere will output some comments to make WebKit based browser working. Set it to false if you want to remove it.

getRequest

public AtmosphereRequest getRequest(boolean enforceScope)


getResponse

public AtmosphereResponse getResponse(boolean enforceScope)


getRequest

public AtmosphereRequest getRequest()
Return the underlying AtmosphereRequest Request.

Specified by:
getRequest in interface AtmosphereResource
Returns:
AtmosphereRequest the underlying Request.

getResponse

public AtmosphereResponse getResponse()
Return the AtmosphereResponse

Specified by:
getResponse in interface AtmosphereResource
Returns:
AtmosphereResponse the underlying Response.

getBroadcaster

public Broadcaster getBroadcaster()
Return the current Broadcaster

Specified by:
getBroadcaster in interface AtmosphereResource
Returns:
the current Broadcaster

getBroadcaster

protected Broadcaster getBroadcaster(boolean autoCreate)

setBroadcaster

public AtmosphereResourceImpl setBroadcaster(Broadcaster broadcaster)
Set the current Broadcaster. If null, a new Broadcaster will be created with Broadcaster.SCOPE.REQUEST will be created if that resource hasn't been yet suspended.

Specified by:
setBroadcaster in interface AtmosphereResource

getAtmosphereConfig

public AtmosphereConfig getAtmosphereConfig()
Return the AtmosphereConfig

Specified by:
getAtmosphereConfig in interface AtmosphereResource
Returns:
the AtmosphereConfig

action

public Action action()
Return the Action which represent the state of the response, e.g. suspended, resumed or timedout.

Returns:
the Action

setIsInScope

public void setIsInScope(boolean isInScope)
Protect the object for being used after it got cancelled.

Parameters:
isInScope -

isInScope

public boolean isInScope()
Is the AtmosphereRequest still valid.

Returns:
true if the AtmosphereRequest still valid

setSerializer

public AtmosphereResource setSerializer(Serializer s)
Set the Serializer used to write broadcasted object.

Specified by:
setSerializer in interface AtmosphereResource
Parameters:
s -

isResumed

public boolean isResumed()
Return true if this object has been resumed.

Specified by:
isResumed in interface AtmosphereResource
Returns:
true if this object has been resumed.

isCancelled

public boolean isCancelled()
Return true if this object has been cancelled.

Specified by:
isCancelled in interface AtmosphereResource
Returns:
true if this object has been cancelled.

write

public AtmosphereResource write(OutputStream os,
                                Object o)
                         throws IOException
Write the broadcasted object using the OutputStream. If a Serializer is defined, the operation will be delagated to it. If not, the (Response) OutputStream will be used by calling Object.toString.getBytes()

Specified by:
write in interface AtmosphereResource
Parameters:
os - an OutputStream
o - an Object
Throws:
IOException

getSerializer

public Serializer getSerializer()
Get the Serializer or null if not defined.

Specified by:
getSerializer in interface AtmosphereResource
Returns:
the Serializer or null if not defined.

createStreamingPadding

public static String createStreamingPadding(String padding)
Output message when Atmosphere suspend a connection.

Returns:
message when Atmosphere suspend a connection.

addEventListener

public AtmosphereResource addEventListener(AtmosphereResourceEventListener e)
Add a AtmosphereResourceEventListener.

Specified by:
addEventListener in interface AtmosphereResource
Parameters:
e - an instance of AtmosphereResourceEventListener

removeEventListener

public AtmosphereResource removeEventListener(AtmosphereResourceEventListener e)
Remove aAtmosphereResourceEventListener.

Specified by:
removeEventListener in interface AtmosphereResource

removeEventListeners

public AtmosphereResource removeEventListeners()
Remove all AtmosphereResourceEventListener.

Specified by:
removeEventListeners in interface AtmosphereResource

notifyListeners

public AtmosphereResource notifyListeners()
Notify All AtmosphereResourceEventListener.

Specified by:
notifyListeners in interface AtmosphereResource

notifyListeners

public AtmosphereResource notifyListeners(AtmosphereResourceEvent event)
Notify AtmosphereResourceEventListener.

Specified by:
notifyListeners in interface AtmosphereResource
Parameters:
event - an instance of AtmosphereResourceEvent

onThrowable

public void onThrowable(Throwable t)
Notify AtmosphereResourceEventListener an unexpected exception occured.\


atmosphereResourceEventListener

public ConcurrentLinkedQueue<AtmosphereResourceEventListener> atmosphereResourceEventListener()

atmosphereHandler

public AtmosphereResourceImpl atmosphereHandler(AtmosphereHandler atmosphereHandler)

cancel

public void cancel()
            throws IOException
Throws:
IOException

_destroy

public void _destroy()

toString

public String toString()
Overrides:
toString in class Object

disableSuspend

public AtmosphereResourceImpl disableSuspend(boolean disableSuspend)

session

public javax.servlet.http.HttpSession session(boolean create)
Return the HttpSession is supported, and creates it if not already created.

Specified by:
session in interface AtmosphereResource
Returns:
the HttpSession is supported, and creates it if not already created

padding

public AtmosphereResource padding(String padding)
Set the padding to use when flushing the response when transport equals 'streaming' See ApplicationConfig.STREAMING_PADDING_MODE for more info.

Specified by:
padding in interface AtmosphereResource

session

public javax.servlet.http.HttpSession session()
Return the HttpSession is supported, null if not

Specified by:
session in interface AtmosphereResource
Returns:
the HttpSession is supported, null if not

session

public AtmosphereResourceImpl session(javax.servlet.http.HttpSession session)

cloneState

public AtmosphereResourceImpl cloneState(AtmosphereResource r)

cors

public void cors()


Copyright © 2013. All Rights Reserved.