修訂 | 151 (tree) |
---|---|
時間 | 2018-03-14 19:44:23 |
作者 | hirukawa_ryo |
* jdbc-util 0.2.9
jersey標準では戻り値として null を返すと 204 No Content になってしまいます。これを 200 OK で返し、ボディに null という4文字が設定されるようにしました。
@@ -0,0 +1,63 @@ | ||
1 | +package net.osdn.util.jersey; | |
2 | + | |
3 | +import java.io.IOException; | |
4 | +import java.lang.annotation.Annotation; | |
5 | +import java.lang.reflect.Method; | |
6 | + | |
7 | +import javax.ws.rs.Produces; | |
8 | +import javax.ws.rs.core.MediaType; | |
9 | + | |
10 | +import com.fasterxml.jackson.core.JsonGenerator; | |
11 | +import com.fasterxml.jackson.core.JsonProcessingException; | |
12 | +import com.fasterxml.jackson.databind.JsonSerializer; | |
13 | +import com.fasterxml.jackson.databind.SerializerProvider; | |
14 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; | |
15 | + | |
16 | +public class InvocationHandler implements java.lang.reflect.InvocationHandler { | |
17 | + | |
18 | + private static final NullObject NULL = new NullObject(); | |
19 | + | |
20 | + @Override | |
21 | + public Object invoke(Object obj, Method method, Object[] args) throws Throwable { | |
22 | + Object result = method.invoke(obj, args); | |
23 | + if(result == null) { | |
24 | + try { | |
25 | + if(isProduceMediaTypeJson(method)) { | |
26 | + result = NULL; | |
27 | + } | |
28 | + } catch(Exception e) { | |
29 | + // ignore | |
30 | + } | |
31 | + } | |
32 | + return result; | |
33 | + } | |
34 | + | |
35 | + private static boolean isProduceMediaTypeJson(Method method) { | |
36 | + Annotation[] annotations = method.getAnnotations(); | |
37 | + if(annotations != null) { | |
38 | + for(int i = 0; i < annotations.length; i++) { | |
39 | + Annotation annotation = annotations[i]; | |
40 | + if(annotation instanceof Produces) { | |
41 | + Produces produces = (Produces)annotation; | |
42 | + for(String s : produces.value()) { | |
43 | + if(MediaType.APPLICATION_JSON.equals(s)) { | |
44 | + return true; | |
45 | + } | |
46 | + } | |
47 | + } | |
48 | + } | |
49 | + } | |
50 | + return false; | |
51 | + } | |
52 | + | |
53 | + @JsonSerialize(using=NullObjectSerializer.class) | |
54 | + public static class NullObject { | |
55 | + } | |
56 | + | |
57 | + public static class NullObjectSerializer extends JsonSerializer<NullObject> { | |
58 | + @Override | |
59 | + public void serialize(NullObject value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { | |
60 | + gen.writeNull(); | |
61 | + } | |
62 | + } | |
63 | +} |
@@ -0,0 +1,13 @@ | ||
1 | +package net.osdn.util.jersey; | |
2 | + | |
3 | +import org.glassfish.hk2.utilities.binding.AbstractBinder; | |
4 | +import org.glassfish.jersey.server.spi.internal.ResourceMethodInvocationHandlerProvider; | |
5 | + | |
6 | +public class InvocationHandlerBinder extends AbstractBinder { | |
7 | + | |
8 | + @Override | |
9 | + protected void configure() { | |
10 | + bind(InvocationHandlerProvider.class).to(ResourceMethodInvocationHandlerProvider.class); | |
11 | + } | |
12 | + | |
13 | +} |
@@ -0,0 +1,13 @@ | ||
1 | +package net.osdn.util.jersey; | |
2 | + | |
3 | +import org.glassfish.jersey.server.model.Invocable; | |
4 | +import org.glassfish.jersey.server.spi.internal.ResourceMethodInvocationHandlerProvider; | |
5 | + | |
6 | +public class InvocationHandlerProvider implements ResourceMethodInvocationHandlerProvider { | |
7 | + | |
8 | + @Override | |
9 | + public java.lang.reflect.InvocationHandler create(Invocable method) { | |
10 | + return new InvocationHandler(); | |
11 | + } | |
12 | + | |
13 | +} |
@@ -0,0 +1,23 @@ | ||
1 | +package net.osdn.util.jersey; | |
2 | + | |
3 | +public class RawString { | |
4 | + | |
5 | + private CharSequence s; | |
6 | + | |
7 | + public RawString(CharSequence s) { | |
8 | + this.s = s; | |
9 | + } | |
10 | + | |
11 | + public CharSequence getCharSequence() { | |
12 | + return s; | |
13 | + } | |
14 | + | |
15 | + @Override | |
16 | + public String toString() { | |
17 | + if(s == null) { | |
18 | + return ""; | |
19 | + } else { | |
20 | + return s.toString(); | |
21 | + } | |
22 | + } | |
23 | +} |
@@ -0,0 +1,41 @@ | ||
1 | +package net.osdn.util.jersey; | |
2 | + | |
3 | +import java.io.IOException; | |
4 | +import java.io.OutputStream; | |
5 | +import java.lang.annotation.Annotation; | |
6 | +import java.lang.reflect.Type; | |
7 | +import java.nio.charset.StandardCharsets; | |
8 | + | |
9 | +import javax.ws.rs.Produces; | |
10 | +import javax.ws.rs.WebApplicationException; | |
11 | +import javax.ws.rs.core.MediaType; | |
12 | +import javax.ws.rs.core.MultivaluedMap; | |
13 | +import javax.ws.rs.ext.MessageBodyWriter; | |
14 | +import javax.ws.rs.ext.Provider; | |
15 | + | |
16 | +@Provider | |
17 | +@Produces(MediaType.APPLICATION_JSON) | |
18 | +public class RawStringMessageBodyWriter implements MessageBodyWriter<RawString> { | |
19 | + | |
20 | + private static final byte[] NULL = "null".getBytes(); | |
21 | + | |
22 | + @Override | |
23 | + public long getSize(RawString s, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | |
24 | + return -1; | |
25 | + } | |
26 | + | |
27 | + @Override | |
28 | + public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | |
29 | + return (type == RawString.class && mediaType != null && mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE)); | |
30 | + } | |
31 | + | |
32 | + @Override | |
33 | + public void writeTo(RawString s, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { | |
34 | + if(s == null) { | |
35 | + entityStream.write(NULL); | |
36 | + } else { | |
37 | + byte[] buf = s.toString().getBytes(StandardCharsets.UTF_8); | |
38 | + entityStream.write(buf); | |
39 | + } | |
40 | + } | |
41 | +} |
@@ -7,6 +7,8 @@ | ||
7 | 7 | public RestApplication() { |
8 | 8 | packages("net.osdn.util.jersey"); |
9 | 9 | |
10 | + boolean isJerseyLoggerRegistered = false; | |
11 | + | |
10 | 12 | //jersey-logger |
11 | 13 | try { |
12 | 14 | if(exists("net.osdn.util.jersey.log.RequestLogger")) { |
@@ -17,6 +19,7 @@ | ||
17 | 19 | try { |
18 | 20 | invocationLoggerBinderClass = Class.forName("net.osdn.util.jersey.log.InvocationLoggerBinder"); |
19 | 21 | register(invocationLoggerBinderClass.newInstance()); |
22 | + isJerseyLoggerRegistered = true; | |
20 | 23 | } catch(Exception e) { |
21 | 24 | e.printStackTrace(); |
22 | 25 | } |
@@ -24,6 +27,10 @@ | ||
24 | 27 | } catch(Exception e) { |
25 | 28 | e.printStackTrace(); |
26 | 29 | } |
30 | + | |
31 | + if(!isJerseyLoggerRegistered) { | |
32 | + register(new InvocationHandlerBinder()); | |
33 | + } | |
27 | 34 | |
28 | 35 | //jersey-freemarker |
29 | 36 | try { |