org.atmosphere.jersey.util.JerseyBroadcasterUtil Maven / Gradle / Ivy
/*
* Copyright 2008-2020 Async-IO.org
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.atmosphere.jersey.util;
import com.sun.jersey.spi.container.ContainerResponse;
import org.atmosphere.cpr.ApplicationConfig;
import org.atmosphere.cpr.AtmosphereRequest;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.AtmosphereResourceEventImpl;
import org.atmosphere.cpr.AtmosphereResourceImpl;
import org.atmosphere.cpr.Broadcaster;
import org.atmosphere.cpr.DefaultBroadcaster;
import org.atmosphere.cpr.FrameworkConfig;
import org.atmosphere.jersey.AtmosphereFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
/**
* Simple util class shared among Jersey's Broadcaster.
*
* @author Jeanfrancois Arcand
*/
public final class JerseyBroadcasterUtil {
private static final Logger logger = LoggerFactory.getLogger(JerseyBroadcasterUtil.class);
public final static void broadcast(final AtmosphereResource r, final AtmosphereResourceEvent e, final Broadcaster broadcaster) {
AtmosphereRequest request = r.getRequest();
ContainerResponse cr = null;
// Make sure only one thread can play with the ContainerResponse. Threading issue can arise if there is a scheduler
// or if ContainerResponse is associated with more than Broadcaster.
cr = (ContainerResponse) request.getAttribute(FrameworkConfig.CONTAINER_RESPONSE);
if (cr == null || !r.isSuspended() && !r.getAtmosphereResourceEvent().isResumedOnTimeout()) {
if (cr == null) {
logger.warn("Unexpected state. ContainerResponse has been resumed. Caching message {} for {}",
e.getMessage(), r.uuid());
} else {
logger.warn("The AtmosphereResource {} hasn't been suspended yet.",
r.uuid(), e);
}
if (DefaultBroadcaster.class.isAssignableFrom(broadcaster.getClass())) {
DefaultBroadcaster.class.cast(broadcaster).cacheLostMessage(r, true);
}
AtmosphereResourceImpl.class.cast(r)._destroy();
return;
}
synchronized (cr) {
try {
// This is required when you change the response's type
String m = null;
if (request.getAttribute(FrameworkConfig.EXPECTED_CONTENT_TYPE) != null) {
m = (String) request.getAttribute(FrameworkConfig.EXPECTED_CONTENT_TYPE);
}
if (m == null || m.equalsIgnoreCase("text/event-stream")) {
if (cr.getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE) != null) {
m = cr.getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE).toString();
}
if (m == null || m.equalsIgnoreCase("application/octet-stream")) {
m = r.getAtmosphereConfig().getInitParameter(ApplicationConfig.SSE_CONTENT_TYPE);
if (m == null) {
m = "text/plain";
}
}
}
if (e.getMessage() instanceof Response) {
cr.setResponse((Response) e.getMessage());
cr.getHttpHeaders().add(HttpHeaders.CONTENT_TYPE, m);
cr.write();
try {
cr.getOutputStream().flush();
} catch (IOException ex) {
logger.trace("", ex);
}
} else if (e.getMessage() instanceof List) {
for (Object msg : (List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy