22
22
public class ProtobufMessageProvider implements MessageBodyWriter <Message >,
23
23
MessageBodyReader <Message > {
24
24
25
+ private static final String JSON = "json" ;
26
+ private static final String PROTOBUF = "protobuf" ;
27
+
25
28
@ Override
26
29
public boolean isWriteable (Class <?> type , Type genericType ,
27
30
Annotation [] annotations , MediaType mediaType ) {
28
31
return Message .class .isAssignableFrom (type ) && (
29
- "json" .equals (mediaType .getSubtype ()) || "protobuf" .equals (mediaType .getSubtype ()));
32
+ JSON .equals (mediaType .getSubtype ()) || PROTOBUF .equals (mediaType .getSubtype ()));
30
33
}
31
34
32
35
@ Override
33
- public long getSize (Message t , Class <?> type ,
36
+ public long getSize (Message message , Class <?> type ,
34
37
Type genericType , Annotation [] annotations ,
35
38
MediaType mediaType ) {
36
- if (t == null ) {
39
+ if (message == null ) {
37
40
return -1 ;
38
41
}
39
42
ByteArrayOutputStream out = new java .io .ByteArrayOutputStream ();
40
43
try {
41
- writeTo (t , type , genericType , annotations , mediaType , null , out );
44
+ writeTo (message , type , genericType , annotations , mediaType , null , out );
42
45
} catch (java .io .IOException e ) {
43
46
return -1 ;
44
47
}
@@ -53,10 +56,10 @@ public void writeTo(Message t, Class<?> type,
53
56
OutputStream entityStream )
54
57
throws IOException , WebApplicationException {
55
58
switch (mediaType .getSubtype ()) {
56
- case "protobuf" :
59
+ case PROTOBUF :
57
60
t .writeTo (entityStream );
58
61
break ;
59
- case "json" :
62
+ case JSON :
60
63
entityStream
61
64
.write (JsonFormat .printer ().print (t ).getBytes (StandardCharsets .UTF_8 ));
62
65
break ;
@@ -69,7 +72,7 @@ public void writeTo(Message t, Class<?> type,
69
72
public boolean isReadable (Class <?> type , Type genericType ,
70
73
Annotation [] annotations , MediaType mediaType ) {
71
74
return Message .class .isAssignableFrom (type ) && (
72
- "json" .equals (mediaType .getSubtype ()) || "protobuf" .equals (mediaType .getSubtype ()));
75
+ JSON .equals (mediaType .getSubtype ()) || PROTOBUF .equals (mediaType .getSubtype ()));
73
76
}
74
77
75
78
@ Override
@@ -78,10 +81,10 @@ public Message readFrom(Class<Message> type, Type genericType, Annotation[] anno
78
81
throws WebApplicationException {
79
82
try {
80
83
switch (mediaType .getSubtype ()) {
81
- case "protobuf" :
84
+ case PROTOBUF :
82
85
Method m = type .getMethod ("parseFrom" , InputStream .class );
83
86
return (Message ) m .invoke (null , entityStream );
84
- case "json" :
87
+ case JSON :
85
88
Message .Builder msg = (Message .Builder ) type
86
89
.getMethod ("newBuilder" ).invoke (null );
87
90
JsonFormat .parser ()
0 commit comments