Skip to content

Commit 3e65883

Browse files
committed
Extract to constants
1 parent 43f8b78 commit 3e65883

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

runtime/jaxrs/src/main/java/com/flit/runtime/jaxrs/ProtobufMessageProvider.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,26 @@
2222
public 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()

runtime/jaxrs/src/test/java/com/flit/runtime/jaxrs/FlitExceptionMapperTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import javax.servlet.http.HttpServletRequest;
99
import javax.ws.rs.core.MediaType;
1010
import javax.ws.rs.core.Response;
11+
import javax.ws.rs.core.Response.Status;
1112
import org.junit.Before;
1213
import org.junit.Test;
1314
import org.mockito.InjectMocks;
@@ -35,7 +36,7 @@ public void testToResponse() {
3536
.withMessage("with this message")
3637
.build();
3738
Response response = flitExceptionMapper.toResponse(flit);
38-
assertEquals(response.getStatus(), 500);
39+
assertEquals(response.getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
3940
Map<String, Object> expectedResult = Map.of("msg", "with this message", "code", "internal");
4041
assertEquals(response.getEntity(), expectedResult);
4142
assertEquals(response.getMediaType(), MediaType.APPLICATION_JSON_TYPE);

0 commit comments

Comments
 (0)