Skip to content

Commit

Permalink
Fix for #183 [Jersey] Add support for customizable ContainerResponseW…
Browse files Browse the repository at this point in the history
…riter
  • Loading branch information
jfarcand committed Feb 15, 2012
1 parent 0e1cb59 commit f7bb1ca
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,9 @@ public interface ApplicationConfig {
* The location of classes implementing the {@link AtmosphereHandler} interface. Default to "/WEB-INF/classes".
*/
String ATMOSPHERE_HANDLER_PATH = ApplicationConfig.class.getPackage().getName() + ".atmosphereHandlerPath";
/**
* Jersey's ContainerResponseWriter.
*/
String JERSEY_CONTAINER_RESPONSE_WRITER_CLASS = "org.atmosphere.jersey.containerResponseWriterClass";

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@
import static org.atmosphere.cpr.ApplicationConfig.PROPERTY_SESSION_SUPPORT;
import static org.atmosphere.cpr.ApplicationConfig.PROPERTY_USE_STREAM;
import static org.atmosphere.cpr.ApplicationConfig.RESUME_AND_KEEPALIVE;
import static org.atmosphere.cpr.ApplicationConfig.SUPPORT_LOCATION_HEADER;
import static org.atmosphere.cpr.ApplicationConfig.SUPPORT_TRACKABLE;
import static org.atmosphere.cpr.ApplicationConfig.WEBSOCKET_PROTOCOL;
import static org.atmosphere.cpr.ApplicationConfig.WEBSOCKET_SUPPORT;
import static org.atmosphere.cpr.FrameworkConfig.ATMOSPHERE_CONFIG;
import static org.atmosphere.cpr.FrameworkConfig.HAZELCAST_BROADCASTER;
import static org.atmosphere.cpr.FrameworkConfig.JERSEY_BROADCASTER;
import static org.atmosphere.cpr.FrameworkConfig.JERSEY_CONTAINER;
Expand Down Expand Up @@ -1125,8 +1124,7 @@ public Action doCometSupport(HttpServletRequest req, HttpServletResponse res)
req.setAttribute(BROADCASTER_FACTORY, broadcasterFactory);
req.setAttribute(PROPERTY_USE_STREAM, useStreamForFlushingComments);
req.setAttribute(BROADCASTER_CLASS, broadcasterClassName);
req.setAttribute(SUPPORT_TRACKABLE, config.getInitParameter(SUPPORT_TRACKABLE));
req.setAttribute(SUPPORT_LOCATION_HEADER, config.getInitParameter(SUPPORT_LOCATION_HEADER));
req.setAttribute(ATMOSPHERE_CONFIG, config);

AtmosphereRequest r = null;
Action a = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,12 @@ public interface FrameworkConfig {
* Cance suspending a connection
*/
String CANCEL_SUSPEND_OPERATION = "doNotSuspend";
/**
* AtmosphereConfig instance
*/
String ATMOSPHERE_CONFIG = AtmosphereConfig.class.getName();
/**
* Instance of Jersey's ContainerResponseWriter that can be configured by a Framework running on top of Atmosphere
*/
String JERSEY_CONTAINER_RESPONSE_WRITER_INSTANCE = "org.atmosphere.jersey.containerResponseWriterInstance";
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.sun.jersey.spi.container.ContainerRequestFilter;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;
import com.sun.jersey.spi.container.ContainerResponseWriter;
import com.sun.jersey.spi.container.ResourceFilter;
import com.sun.jersey.spi.container.ResourceFilterFactory;
import org.atmosphere.annotation.Asynchronous;
Expand All @@ -53,6 +54,7 @@
import org.atmosphere.annotation.Subscribe;
import org.atmosphere.annotation.Suspend;
import org.atmosphere.cpr.ApplicationConfig;
import org.atmosphere.cpr.AtmosphereConfig;
import org.atmosphere.cpr.AtmosphereEventLifecycle;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceEvent;
Expand Down Expand Up @@ -94,6 +96,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import static org.atmosphere.cpr.FrameworkConfig.ATMOSPHERE_CONFIG;
import static org.atmosphere.cpr.HeaderConfig.ACCESS_CONTROL_ALLOW_CREDENTIALS;
import static org.atmosphere.cpr.HeaderConfig.ACCESS_CONTROL_ALLOW_ORIGIN;
import static org.atmosphere.cpr.HeaderConfig.CACHE_CONTROL;
Expand Down Expand Up @@ -241,11 +244,30 @@ public ContainerResponse filter(final ContainerRequest request, final ContainerR
return response;
}

// Check first if something was defined in web.xml
AtmosphereConfig config = (AtmosphereConfig) servletReq.getAttribute(ATMOSPHERE_CONFIG);
String p = config.getInitParameter(ApplicationConfig.JERSEY_CONTAINER_RESPONSE_WRITER_CLASS);
ContainerResponseWriter w = null;
if (p != null) {
try {
w = (ContainerResponseWriter) Thread.currentThread().getContextClassLoader().loadClass(p).newInstance();
logger.trace("Installing ContainerResponseWriter {}", p);
} catch (Throwable e) {
logger.error("Error loading ContainerResponseWriter {}", p, e);
}
}

// Now check if it was defined as an attribute
w = (ContainerResponseWriter) servletReq.getAttribute(FrameworkConfig.JERSEY_CONTAINER_RESPONSE_WRITER_INSTANCE);
if (w != null) {
response.setContainerResponseWriter(w);
}

AtmosphereResource<HttpServletRequest, HttpServletResponse> r =
(AtmosphereResource<HttpServletRequest, HttpServletResponse>) servletReq
.getAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE);

if (Boolean.parseBoolean((String) servletReq.getAttribute(ApplicationConfig.SUPPORT_LOCATION_HEADER))) {
if (Boolean.parseBoolean(config.getInitParameter(ApplicationConfig.SUPPORT_LOCATION_HEADER))) {
useResumeAnnotation = true;
}

Expand All @@ -262,16 +284,16 @@ public ContainerResponse filter(final ContainerRequest request, final ContainerR
StringBuffer s = new StringBuffer();
Enumeration<String> e = servletReq.getHeaderNames();
String t;
while(e.hasMoreElements()) {
while (e.hasMoreElements()) {
t = e.nextElement();
s.append(t).append("=").append(servletReq.getHeader(t)).append("\n");
}

logger.error("\nQueryString:\n{}\n\nHeaders:\n{}", servletReq.getQueryString(), s.toString());

throw new WebApplicationException(new IllegalStateException("Must specify transport using header value "
+ transport
+ " and uuid " + broadcasterName));
+ transport
+ " and uuid " + broadcasterName));
}
String subProtocol = (String) servletReq.getAttribute(FrameworkConfig.WEBSOCKET_SUBPROTOCOL);

Expand Down Expand Up @@ -360,7 +382,7 @@ public void onSuspend(AtmosphereResourceEvent<HttpServletRequest, HttpServletRes
bc = (Broadcaster) servletReq.getAttribute(INJECTED_BROADCASTER);
}

boolean supportTrackable = servletReq.getAttribute(ApplicationConfig.SUPPORT_TRACKABLE) != null;
boolean supportTrackable = config.getInitParameter(ApplicationConfig.SUPPORT_TRACKABLE) != null;
// Register our TrackableResource
boolean isTracked = response.getEntity() != null ? TrackableResource.class.isAssignableFrom(response.getEntity().getClass()) : supportTrackable;

Expand Down Expand Up @@ -412,7 +434,7 @@ public void onSuspend(AtmosphereResourceEvent<HttpServletRequest, HttpServletRes
}

// Tracking is enabled by default
supportTrackable = servletReq.getAttribute(ApplicationConfig.SUPPORT_TRACKABLE) != null;
supportTrackable = config.getInitParameter(ApplicationConfig.SUPPORT_TRACKABLE) != null;
// Register our TrackableResource
isTracked = response.getEntity() != null ? TrackableResource.class.isAssignableFrom(response.getEntity().getClass()) : supportTrackable;

Expand Down Expand Up @@ -1008,8 +1030,12 @@ public List<ResourceFilter> create(AbstractMethod am) {
}
}

// Nothing, normal Jersey application.
return list.size() > 0 ? list : null;
if (list.size() == 0) {
f = new Filter(Action.NONE);
list.addFirst(f);
}

return list;
}

private long translateTimeUnit(long period, TimeUnit tu) {
Expand Down

0 comments on commit f7bb1ca

Please sign in to comment.