Skip to content

Instantly share code, notes, and snippets.

@roalcantara
Created April 10, 2012 16:58
Show Gist options
  • Save roalcantara/2352850 to your computer and use it in GitHub Desktop.
Save roalcantara/2352850 to your computer and use it in GitHub Desktop.
Download of Protobuf in Vraptor3
//Controller
@Get
public final Download authenticate() {
AuthenticationProto.Authentication proto = AuthenticationProto.Authentication.newBuilder()
.setToken("token") //sample code
.build();
return new ProtoDownload(proto);
}
//ProtoDownload.java
package com.xpto.util;
import br.com.caelum.vraptor.interceptor.download.ByteArrayDownload;
import br.com.caelum.vraptor.interceptor.download.Download;
import com.google.protobuf.GeneratedMessage;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ProtoDownload implements Download {
private final GeneratedMessage proto;
public ProtoDownload(final GeneratedMessage proto) {
this.proto = proto;
}
@Override
public void write(final HttpServletResponse response) throws IOException {
byte[] bytes = this.proto.toByteArray();
ByteArrayDownload download = new ByteArrayDownload(bytes, "application/x-protobuf", this.proto.getClass().getSimpleName(), true);
download.write(response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment