2222public class ProtobufMessageProvider implements MessageBodyWriter <Message >,
2323 MessageBodyReader <Message > {
2424
25+ private static final String JSON = "json" ;
26+ private static final String PROTOBUF = "protobuf" ;
27+
2528 @ Override
2629 public boolean isWriteable (Class <?> type , Type genericType ,
2730 Annotation [] annotations , MediaType mediaType ) {
2831 return Message .class .isAssignableFrom (type ) && (
29- "json" .equals (mediaType .getSubtype ()) || "protobuf" .equals (mediaType .getSubtype ()));
32+ JSON .equals (mediaType .getSubtype ()) || PROTOBUF .equals (mediaType .getSubtype ()));
3033 }
3134
3235 @ Override
33- public long getSize (Message t , Class <?> type ,
36+ public long getSize (Message message , Class <?> type ,
3437 Type genericType , Annotation [] annotations ,
3538 MediaType mediaType ) {
36- if (t == null ) {
39+ if (message == null ) {
3740 return -1 ;
3841 }
3942 ByteArrayOutputStream out = new java .io .ByteArrayOutputStream ();
4043 try {
41- writeTo (t , type , genericType , annotations , mediaType , null , out );
44+ writeTo (message , type , genericType , annotations , mediaType , null , out );
4245 } catch (java .io .IOException e ) {
4346 return -1 ;
4447 }
@@ -53,10 +56,10 @@ public void writeTo(Message t, Class<?> type,
5356 OutputStream entityStream )
5457 throws IOException , WebApplicationException {
5558 switch (mediaType .getSubtype ()) {
56- case "protobuf" :
59+ case PROTOBUF :
5760 t .writeTo (entityStream );
5861 break ;
59- case "json" :
62+ case JSON :
6063 entityStream
6164 .write (JsonFormat .printer ().print (t ).getBytes (StandardCharsets .UTF_8 ));
6265 break ;
@@ -69,7 +72,7 @@ public void writeTo(Message t, Class<?> type,
6972 public boolean isReadable (Class <?> type , Type genericType ,
7073 Annotation [] annotations , MediaType mediaType ) {
7174 return Message .class .isAssignableFrom (type ) && (
72- "json" .equals (mediaType .getSubtype ()) || "protobuf" .equals (mediaType .getSubtype ()));
75+ JSON .equals (mediaType .getSubtype ()) || PROTOBUF .equals (mediaType .getSubtype ()));
7376 }
7477
7578 @ Override
@@ -78,10 +81,10 @@ public Message readFrom(Class<Message> type, Type genericType, Annotation[] anno
7881 throws WebApplicationException {
7982 try {
8083 switch (mediaType .getSubtype ()) {
81- case "protobuf" :
84+ case PROTOBUF :
8285 Method m = type .getMethod ("parseFrom" , InputStream .class );
8386 return (Message ) m .invoke (null , entityStream );
84- case "json" :
87+ case JSON :
8588 Message .Builder msg = (Message .Builder ) type
8689 .getMethod ("newBuilder" ).invoke (null );
8790 JsonFormat .parser ()
0 commit comments