|
| 1 | +package com.flit.runtime.jaxrs; |
| 2 | + |
| 3 | +import com.google.protobuf.Message; |
| 4 | +import com.google.protobuf.util.JsonFormat; |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.InputStreamReader; |
| 9 | +import java.io.OutputStream; |
| 10 | +import java.lang.annotation.Annotation; |
| 11 | +import java.lang.reflect.Method; |
| 12 | +import java.lang.reflect.Type; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import javax.ws.rs.WebApplicationException; |
| 15 | +import javax.ws.rs.core.MediaType; |
| 16 | +import javax.ws.rs.core.MultivaluedMap; |
| 17 | +import javax.ws.rs.ext.MessageBodyReader; |
| 18 | +import javax.ws.rs.ext.MessageBodyWriter; |
| 19 | +import javax.ws.rs.ext.Provider; |
| 20 | + |
| 21 | +@Provider |
| 22 | +public class ProtobufMessageProvider implements MessageBodyWriter<Message>, |
| 23 | + MessageBodyReader<Message> { |
| 24 | + |
| 25 | + @Override |
| 26 | + public boolean isWriteable(Class<?> type, Type genericType, |
| 27 | + Annotation[] annotations, MediaType mediaType) { |
| 28 | + return Message.class.isAssignableFrom(type) && ( |
| 29 | + "json".equals(mediaType.getSubtype()) || "protobuf".equals(mediaType.getSubtype())); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public long getSize(Message t, Class<?> type, |
| 34 | + Type genericType, Annotation[] annotations, |
| 35 | + MediaType mediaType) { |
| 36 | + if (t == null) { |
| 37 | + return -1; |
| 38 | + } |
| 39 | + ByteArrayOutputStream out = new java.io.ByteArrayOutputStream(); |
| 40 | + try { |
| 41 | + writeTo(t, type, genericType, annotations, mediaType, null, out); |
| 42 | + } catch (java.io.IOException e) { |
| 43 | + return -1; |
| 44 | + } |
| 45 | + return out.size(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void writeTo(Message t, Class<?> type, |
| 50 | + Type genericType, Annotation[] annotations, |
| 51 | + MediaType mediaType, |
| 52 | + MultivaluedMap<String, Object> httpHeaders, |
| 53 | + OutputStream entityStream) |
| 54 | + throws IOException, WebApplicationException { |
| 55 | + switch (mediaType.getSubtype()) { |
| 56 | + case "protobuf": |
| 57 | + t.writeTo(entityStream); |
| 58 | + break; |
| 59 | + case "json": |
| 60 | + entityStream |
| 61 | + .write(JsonFormat.printer().print(t).getBytes(StandardCharsets.UTF_8)); |
| 62 | + break; |
| 63 | + default: |
| 64 | + throw new WebApplicationException("MediaType not supported!"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean isReadable(Class<?> type, Type genericType, |
| 70 | + Annotation[] annotations, MediaType mediaType) { |
| 71 | + return Message.class.isAssignableFrom(type) && ( |
| 72 | + "json".equals(mediaType.getSubtype()) || "protobuf".equals(mediaType.getSubtype())); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Message readFrom(Class<Message> type, Type genericType, Annotation[] annotations, |
| 77 | + MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) |
| 78 | + throws WebApplicationException { |
| 79 | + try { |
| 80 | + switch (mediaType.getSubtype()) { |
| 81 | + case "protobuf": |
| 82 | + Method m = type.getMethod("parseFrom", InputStream.class); |
| 83 | + return (Message) m.invoke(null, entityStream); |
| 84 | + case "json": |
| 85 | + Message.Builder msg = (Message.Builder) type |
| 86 | + .getMethod("newBuilder").invoke(null); |
| 87 | + JsonFormat.parser() |
| 88 | + .merge(new InputStreamReader(entityStream), msg); |
| 89 | + return msg.build(); |
| 90 | + default: |
| 91 | + throw new WebApplicationException("MediaType not supported!"); |
| 92 | + } |
| 93 | + } catch (Exception e) { |
| 94 | + throw new WebApplicationException(e); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments