HTTP streaming transport should allow to exchange binary message #78

Closed
flowersinthesand opened this Issue Feb 3, 2015 · 1 comment

Projects

None yet

1 participant

@flowersinthesand
Contributor

Following the decision from vibe-project/vibe-protocol#56 (comment), we need a library to handle Base64. Unfortunately, Java has built-in Base64 encoder/decoder as of 8. However, surprisingly I found out that Jackson supports Base64 through conversion from byte/string to string/byte.

ObjectMapper mapper = new ObjectMapper();
// Using byte[]
String encoded = mapper.convertValue(new byte[]{'h','e','l','l','o'}, String.class);
System.out.println(encoded); // aGVsbG8=
byte[] decoded = mapper.convertValue(encoded, byte[].class);
for (byte b : decoded) {
    System.out.print((char) b);
}
System.out.println(); // hello

// Using ByteBuffer
ByteBuffer bytes = ByteBuffer.wrap(new byte[]{'h','e','l','l','o'});
String encoded1 = mapper.convertValue(bytes, String.class);
System.out.println(encoded1); // aGVsbG8=
byte[] decoded1 = mapper.convertValue(encoded1, ByteBuffer.class).array();
for (byte b : decoded1) {
    System.out.print((char) b);
}
System.out.println(); // hello

As we already have used Jackson for JSON, no new dependency will be added.

@flowersinthesand flowersinthesand added this to the 3.0.0-Alpha15 milestone Feb 3, 2015
@flowersinthesand
Contributor

Test result along with vibe-project/vibe-protocol#56 (comment)

package test;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.WebListener;

import org.atmosphere.cpr.ApplicationConfig;
import org.atmosphere.vibe.platform.action.Action;
import org.atmosphere.vibe.platform.bridge.atmosphere2.VibeAtmosphereServlet;
import org.atmosphere.vibe.transport.ServerTransport;
import org.atmosphere.vibe.transport.http.HttpTransportServer;

@WebListener
public class HttpStreamTransportTest implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent event) {
        ServletContext context = event.getServletContext();
        HttpTransportServer server = new HttpTransportServer().transportAction(new Action<ServerTransport>() {
            @Override
            public void on(final ServerTransport transport) {
                System.out.println("opened");
                transport.textAction(new Action<String>() {
                    @Override
                    public void on(String data) {
                        System.out.println("on text " + data);
                        transport.send(data);
                    }
                })
                .binaryAction(new Action<ByteBuffer>() {
                    @Override
                    public void on(ByteBuffer data) {
                        System.out.println("on binary " + data + " " + new String(data.array(), Charset.forName("utf-8")));
                        transport.send(data);
                    }
                });
            }
        });
        Servlet servlet = new VibeAtmosphereServlet().httpAction(server);
        ServletRegistration.Dynamic reg = context.addServlet(VibeAtmosphereServlet.class.getName(), servlet);
        reg.setAsyncSupported(true);
        reg.setInitParameter(ApplicationConfig.DISABLE_ATMOSPHEREINTERCEPTOR, Boolean.TRUE.toString());
        reg.addMapping("/vibe");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {}
}

// opened
// on text hello
// on binary java.nio.HeapByteBuffer[pos=0 lim=5 cap=5] hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment