Created
April 10, 2012 16:58
-
-
Save roalcantara/2352850 to your computer and use it in GitHub Desktop.
Download of Protobuf in Vraptor3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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