Vibe Java Platform is a simple A abstraction layer B for full-stack web application frameworks C and raw web servers D running on Java Virtual Machine.
- A
- All the interfaces you need to know are
ServerHttpExchange
andServerWebSocket
. Indeed. - B
- An application based on Vibe Java Platform can run on any supported platforms seamlessly.
- C
- For example, Play, Spring MVC and JAX-RS.
- D
- For example, Servlet, Grizzly and Netty.
Quick Start
Vibe Java Platform is distributed through Maven Central. To write web application running on any platform Vibe Java Platform supports, you need two artifacts: org.atmosphere:vibe-platform-http:3.0.0-Alpha9
and org.atmosphere:vibe-platform-ws:3.0.0-Alpha9
.
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>vibe-platform-http</artifactId>
<version>3.0.0-Alpha9</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>vibe-platform-ws</artifactId>
<version>3.0.0-Alpha9</version>
</dependency>
Once you've set up the build, all you need to do is to write actions receiving ServerHttpExchange
and ServerWebSocket
. As a simple example, let's write echo actions sending any incoming messages such as HTTP chunk and WebSocket data frame back.
import org.atmosphere.vibe.platform.action.Action;
import org.atmosphere.vibe.platform.http.ServerHttpExchange;
import org.atmosphere.vibe.platform.ws.ServerWebSocket;
public class EchoHandler {
// You can use plain getter instead of public final field
public final Action<ServerHttpExchange> httpAction = new Action<ServerHttpExchange>() {
@Override
public void on(final ServerHttpExchange http) {
// Get the request header, content-type, and set it to the response header, content-type
http.setHeader("content-type", http.header("content-type"))
// When a chunk is read from the request body
.chunkAction(new Action<ByteBuffer>() {
@Override
public void on(ByteBuffer bytes) {
// Writes a read chunk to the response body
http.write(bytes);
}
})
// When the request is fully read
.endAction(new VoidAction() {
@Override
public void on() {
System.out.println("The end of request");
// Ends the response
http.end();
}
})
// Reads the request body as binary to circumvent encoding issue
.readAsBinary()
// When the response is fully written
.finishAction(new VoidAction() {
@Override
public void on() {
System.out.println("The end of response");
}
})
// When some error happens in request-response exchange
.errorAction(new Action<Throwable>() {
@Override
public void on(Throwable t) {
t.printStackTrace();
}
})
// When the underlying connection is terminated
.closeAction(new VoidAction() {
@Override
public void on() {
System.out.println("The end of request-response exchange");
}
});
}
};
public final Action<ServerWebSocket> wsAction = new Action<ServerWebSocket>() {
@Override
public void on(final ServerWebSocket ws) {
// When a text frame is arrived
ws.textAction(new Action<String>() {
@Override
public void on(String data) {
// Sends it back
ws.send(data);
}
})
// When a binary frame is arrived
.binaryAction(new Action<ByteBuffer>() {
@Override
public void on(ByteBuffer bytes) {
// Sends it back
ws.send(bytes);
}
})
// When some error happens in the connection
.errorAction(new Action<Throwable>() {
@Override
public void on(Throwable t) {
t.printStackTrace();
}
})
// When the connection is closed for any reason
.closeAction(new VoidAction() {
@Override
public void on() {
System.out.println("The end of connection");
}
});
}
};
}
Of course, you can use plain getter instead of public final field. Now to run this handler on the specific platform, we need to transform HTTP and WebSocket resources the platform produced into ServerHttpExchange
and ServerWebSocket
and feed them into an instance of EchoHandler
. The module playing such roles is called bridge and Vibe Java Platform provides various bridges which matches well with each platform's usage.
For example, to run EchoHandler
on Servlet 3 and Java WebSocket API 1 together, you can use Atmosphere 2 platform. Let's add the following bridge dependency.
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>vibe-platform-bridge-atmosphere2</artifactId>
<version>3.0.0-Alpha9</version>
</dependency>
Then, through VibeAtmosphereServlet
, you can configure EchoHandler
and run it on an implementation of Servlet 3 and Java WebSocket API 1 such as Jetty 9 and Tomcat 8.
import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import org.atmosphere.cpr.ApplicationConfig;
import org.atmosphere.vibe.platform.bridge.atmosphere2.VibeAtmosphereServlet;
@WebListener
public class Bootstrap implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
EchoHandler handler = new EchoHandler();
ServletContext context = event.getServletContext();
Servlet servlet = new VibeAtmosphereServlet().httpAction(handler.httpAction).wsAction(handler.wsAction);
ServletRegistration.Dynamic reg = context.addServlet(VibeAtmosphereServlet.class.getName(), servlet);
reg.setAsyncSupported(true);
reg.setInitParameter(ApplicationConfig.DISABLE_ATMOSPHEREINTERCEPTOR, Boolean.TRUE.toString());
reg.addMapping("/echo");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {}
}
Further Reading
- To get details of API, see API document.
- To have a thorough knowledge, read out the reference.