org.atmosphere.cpr
Class AtmosphereResourceImpl

java.lang.Object
  extended by org.atmosphere.cpr.AtmosphereResourceImpl
All Implemented Interfaces:
AtmosphereEventLifecycle, AtmosphereResource<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>, Trackable

public class AtmosphereResourceImpl
extends Object
implements AtmosphereResource<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>

AtmosphereResource implementation for supporting HttpServletRequest and HttpServletResponse.

Author:
Jeanfrancois Arcand

Field Summary
protected  AtmosphereServlet.Action action
           
protected  Broadcaster broadcaster
           
protected  CometSupport cometSupport
           
static String METEOR
           
static String PRE_SUSPEND
           
static String SKIP_BROADCASTER_CREATION
           
 
Constructor Summary
AtmosphereResourceImpl(AtmosphereServlet.AtmosphereConfig config, Broadcaster broadcaster, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse response, CometSupport cometSupport, AtmosphereHandler atmosphereHandler)
          Create an AtmosphereResource.
 
Method Summary
 AtmosphereServlet.Action action()
          Return the AtmosphereServlet.Action which represent the state of the response, e.g.
 void addEventListener(AtmosphereResourceEventListener e)
          Add a AtmosphereResourceEventListener.
 ConcurrentLinkedQueue<AtmosphereResourceEventListener> atmosphereResourceEventListener()
           
static String createStreamingPadding(String padding)
          Output message when Atmosphere suspend a connection.
 boolean equals(Object o)
           
 AtmosphereServlet.AtmosphereConfig getAtmosphereConfig()
          Return the AtmosphereServlet.AtmosphereConfig
 AtmosphereHandler getAtmosphereHandler()
          Return the associated AtmosphereHandler associated with this resource.
 AtmosphereResourceEventImpl getAtmosphereResourceEvent()
          Return the current AtmosphereResourceEvent.
 Broadcaster getBroadcaster()
          Return the current Broadcaster
 javax.servlet.http.HttpServletRequest getRequest()
          Return the underlying Request.
 javax.servlet.http.HttpServletResponse getResponse()
          Return the underlying Response.
 Serializer getSerializer()
          Get the Serializer or null if not defined.
 int hashCode()
           
 boolean isInScope()
          Is the HttpServletRequest still valid.
 void notifyListeners()
          Notify AtmosphereResourceEventListener.
 void notifyListeners(AtmosphereResourceEvent event)
          Notify AtmosphereResourceEventListener.
 void onThrowable(Throwable t)
          Notify AtmosphereResourceEventListener an unexpected exception occured.\
 void removeEventListener(AtmosphereResourceEventListener e)
          Add a AtmosphereResourceEventListener.
 void removeEventListeners()
          Remove all AtmosphereResourceEventListener.
 void resume()
          Complete the HttpServletResponse and finish/commit it.
 void setBroadcaster(Broadcaster broadcaster)
          Set the current Broadcaster.
 void setIsInScope(boolean isInScope)
          Protect the object for being used after it got cancelled.
 void setSerializer(Serializer s)
          Set the Serializer used to write broadcasted object.
 void suspend()
          Suspend the HttpServletResponse indefinitely.
 void suspend(long timeout)
          Suspend the HttpServletResponse.
 void suspend(long timeout, boolean flushComment)
          Suspend the HttpServletResponse.
 void suspend(long timeout, TimeUnit timeunit)
          Suspend the HttpServletResponse.
 void suspend(long timeout, TimeUnit timeunit, boolean flushComment)
          Suspend the HttpServletResponse.
 String toString()
           
 void write(OutputStream os, Object o)
          Write the broadcasted object using the OutputStream.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, 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 AtmosphereServlet.Action action

broadcaster

protected Broadcaster broadcaster

cometSupport

protected final CometSupport cometSupport
Constructor Detail

AtmosphereResourceImpl

public AtmosphereResourceImpl(AtmosphereServlet.AtmosphereConfig config,
                              Broadcaster broadcaster,
                              javax.servlet.http.HttpServletRequest req,
                              javax.servlet.http.HttpServletResponse response,
                              CometSupport cometSupport,
                              AtmosphereHandler atmosphereHandler)
Create an AtmosphereResource.

Parameters:
config - The AtmosphereServlet.AtmosphereConfig
broadcaster - The Broadcaster.
req - The HttpServletRequest
response - The HttpServletResponse
cometSupport - The CometSupport
atmosphereHandler - The AtmosphereHandler
Method Detail

getAtmosphereResourceEvent

public AtmosphereResourceEventImpl getAtmosphereResourceEvent()
Return the current AtmosphereResourceEvent.

Specified by:
getAtmosphereResourceEvent in interface AtmosphereResource<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>

getAtmosphereHandler

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

Specified by:
getAtmosphereHandler in interface AtmosphereResource<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>
Returns:
the associated AtmosphereHandler associated with this resource.

resume

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

Specified by:
resume in interface AtmosphereResource<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>

suspend

public void suspend()
Suspend the HttpServletResponse indefinitely. Suspending a HttpServletResponse 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<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>

suspend

public void suspend(long timeout)
Suspend the HttpServletResponse. Suspending a HttpServletResponse 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 ServletResponse.getWriter() will be used. You can change that behavior by setting a request attribute named org.atmosphere.useStream to so the framework will use ServletResponse.getOutputStream()

Specified by:
suspend in interface AtmosphereResource<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>
Parameters:
timeout - The maximum amount of time, in milliseconds, a HttpServletResponse can be suspended. When the timeout expires (because nothing has been written or because the AtmosphereResource.resume() , the HttpServletResponse will be automatically resumed and commited. Usage of any methods of a HttpServletResponse that times out will throw an IllegalStateException.

suspend

public void suspend(long timeout,
                    TimeUnit timeunit)
Suspend the HttpServletResponse. Suspending a HttpServletResponse 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 ServletResponse.getWriter() will be used. You can change that behavior by setting a request attribute named org.atmosphere.useStream to so the framework will use ServletResponse.getOutputStream()

Specified by:
suspend in interface AtmosphereResource<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>
Parameters:
timeout - The maximum amount of time, a HttpServletResponse can be suspended. When the timeout expires (because nothing has been written or because the AtmosphereResource.resume() , the HttpServletResponse will be automatically resumed and commited. Usage of any methods of a HttpServletResponse that times out will throw an IllegalStateException.
timeunit - The time unit of the timeout value

suspend

public void suspend(long timeout,
                    TimeUnit timeunit,
                    boolean flushComment)
Suspend the HttpServletResponse. Suspending a HttpServletResponse 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 ServletResponse.getWriter() will be used. You can change that behavior by setting a request attribute named org.atmosphere.useStream to so the framework will use ServletResponse.getOutputStream()

Specified by:
suspend in interface AtmosphereResource<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>
Parameters:
timeout - The maximum amount of time, in milliseconds, a HttpServletResponse can be suspended. When the timeout expires (because nothing has been written or because the AtmosphereResource.resume() , the HttpServletResponse will be automatically resumed and commited. Usage of any methods of a HttpServletResponse 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 void suspend(long timeout,
                    boolean flushComment)
Description copied from interface: AtmosphereResource
Suspend the HttpServletResponse. Suspending a HttpServletResponse 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 ServletResponse.getWriter() will be used. You can change that behavior by setting a request attribute named org.atmosphere.useStream to so the framework will use ServletResponse.getOutputStream()

Specified by:
suspend in interface AtmosphereResource<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>
Parameters:
timeout - The maximum amount of time, in milliseconds, a HttpServletResponse can be suspended. When the timeout expires (because nothing has been written or because the AtmosphereResource.resume() , the HttpServletResponse will be automatically resumed and commited. Usage of any methods of a HttpServletResponse 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 javax.servlet.http.HttpServletRequest getRequest()
Return the underlying Request.

Specified by:
getRequest in interface AtmosphereResource<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>
Returns:
E the underlying Request.

getResponse

public javax.servlet.http.HttpServletResponse getResponse()
Return the underlying Response.

Specified by:
getResponse in interface AtmosphereResource<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>
Returns:
F the underlying Response.

getBroadcaster

public Broadcaster getBroadcaster()
Return the current Broadcaster

Specified by:
getBroadcaster in interface AtmosphereResource<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>
Returns:
the current Broadcaster

setBroadcaster

public void 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<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>

getAtmosphereConfig

public AtmosphereServlet.AtmosphereConfig getAtmosphereConfig()
Return the AtmosphereServlet.AtmosphereConfig

Specified by:
getAtmosphereConfig in interface AtmosphereResource<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>
Returns:
the AtmosphereServlet.AtmosphereConfig

action

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

Returns:
the AtmosphereServlet.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 HttpServletRequest still valid.

Returns:
true if the HttpServletRequest still valid

setSerializer

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

Specified by:
setSerializer in interface AtmosphereResource<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>
Parameters:
s -

write

public void 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<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>
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<javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse>
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 void addEventListener(AtmosphereResourceEventListener e)
Add a AtmosphereResourceEventListener.

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

removeEventListener

public void removeEventListener(AtmosphereResourceEventListener e)
Add a AtmosphereResourceEventListener.

Specified by:
removeEventListener in interface AtmosphereEventLifecycle
Parameters:
e - an instance of AtmosphereResourceEventListener

removeEventListeners

public void removeEventListeners()
Remove all AtmosphereResourceEventListener.

Specified by:
removeEventListeners in interface AtmosphereEventLifecycle

notifyListeners

public void notifyListeners()
Notify AtmosphereResourceEventListener.

Specified by:
notifyListeners in interface AtmosphereEventLifecycle

notifyListeners

public void notifyListeners(AtmosphereResourceEvent event)
Notify AtmosphereResourceEventListener.

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

onThrowable

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


atmosphereResourceEventListener

public ConcurrentLinkedQueue<AtmosphereResourceEventListener> atmosphereResourceEventListener()

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2011. All Rights Reserved.