Skip to content

Instantly share code, notes, and snippets.

@byteit101
Last active September 19, 2022 14:55
Show Gist options
  • Save byteit101/63c5fe645cbfa7e2222948bd77e72d51 to your computer and use it in GitHub Desktop.
Save byteit101/63c5fe645cbfa7e2222948bd77e72d51 to your computer and use it in GitHub Desktop.
Simple atmosphere returning errors
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>org.atmosphere.samples</groupId>
<artifactId>atmosphere-samples</artifactId>
<version>2.7.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.atmosphere.samples</groupId>
<artifactId>atmosphere-sse-rest-chat-mod</artifactId>
<packaging>war</packaging>
<version>2.7.0-SNAPSHOT</version>
<name>atmosphere-sse-rest-chat</name>
<url>http://maven.apache.org</url>
<properties>
<atmosphere-version>2.7.7</atmosphere-version>
</properties>
<dependencies>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-runtime</artifactId>
<version>${atmosphere-version}</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-annotations</artifactId>
<version>${atmosphere-version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback-version}</version>
</dependency>
<dependency>
<groupId>org.atmosphere.client</groupId>
<artifactId>javascript</artifactId>
<version>${client-version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
/*
* Copyright 2008-2022 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.samples.chat.jersey;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.atmosphere.annotation.Suspend;
import org.atmosphere.config.service.AtmosphereService;
import org.atmosphere.cpr.ApplicationConfig;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResource.TRANSPORT;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.AtmosphereResourceEventListener;
import org.atmosphere.cpr.Broadcaster;
import org.atmosphere.interceptor.AtmosphereResourceLifecycleInterceptor;
import org.atmosphere.interceptor.HeartbeatInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Simple chat resource demonstrating the power of Atmosphere. This resource supports transport like WebSocket, Streaming, JSONP and Long-Polling.
*
* @author Jeanfrancois Arcand
*/
@Path("/the/sse")
@Produces("application/json")
@AtmosphereService(
dispatch = true,
interceptors = {AtmosphereResourceLifecycleInterceptor.class, HeartbeatInterceptor.class},
path = "/chat/the/sse*",
servlet = "org.glassfish.jersey.servlet.ServletContainer")
public class ResourceChat {
private static final Logger LOG = LoggerFactory.getLogger(ResourceChat.class);
@GET
@Path("/error")
@Suspend(contentType = "application/json")
public Response doA(
@Context HttpServletRequest request,
@Context HttpServletResponse resp
) throws InterruptedException, IOException, ServletException
{
final AtmosphereResource r = (AtmosphereResource) request.getAttribute(ApplicationConfig.ATMOSPHERE_RESOURCE);
if (r.transport() != TRANSPORT.SSE)
{
LOG.error("NOT SSE!");
return Response.status(Status.BAD_REQUEST).entity("SSE is the only thing supported").build();
}
else
{
LOG.error("Doing error path");
return Response.status(451).entity("{my-error-body}").build();
}
}
@GET
@Path("/normal")
@Suspend(contentType = "application/json")
public Response doB(
@Context HttpServletRequest request,
@Context HttpServletResponse resp
) throws InterruptedException, IOException, ServletException
{
final AtmosphereResource r = (AtmosphereResource) request.getAttribute(ApplicationConfig.ATMOSPHERE_RESOURCE);
if (r.transport() != TRANSPORT.SSE)
{
LOG.error("NOT SSE!");
return Response.status(Status.BAD_REQUEST).entity("SSE is the only thing supported").build();
}
else
{
LOG.error("Doing normal path");
Broadcaster b = r.getAtmosphereConfig().getBroadcasterFactory().lookup("foo", true);
r.setBroadcaster(b);
r.addEventListener(new AtmosphereResourceEventListener() {
@Override
public void onHeartbeat(AtmosphereResourceEvent event) {
b.broadcast("ping");
}
@Override
public void onThrowable(AtmosphereResourceEvent event) {}
@Override
public void onSuspend(AtmosphereResourceEvent event) {
b.broadcast("suspend");
}
@Override
public void onResume(AtmosphereResourceEvent event) {}
@Override
public void onPreSuspend(AtmosphereResourceEvent event) {}
@Override
public void onDisconnect(AtmosphereResourceEvent event) {}
@Override
public void onClose(AtmosphereResourceEvent event) {}
@Override
public void onBroadcast(AtmosphereResourceEvent event) {}
});
r.suspend();
return null;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:j2ee="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<description>Atmosphere REST Chat</description>
<display-name>Atmosphere REST Chat</display-name>
<servlet>
<description>AtmosphereServlet</description>
<servlet-name>AtmosphereServlet</servlet-name>
<servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.atmosphere.samples.chat.jersey</param-value>
</init-param>
<async-supported>true</async-supported>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AtmosphereServlet</servlet-name>
<url-pattern>/chat/*</url-pattern>
</servlet-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment