• R/O
  • SSH
  • HTTPS

util: Commit


Commit MetaInfo

Revision151 (tree)
Time2018-03-14 19:44:23
Authorhirukawa_ryo

Log Message

* jdbc-util 0.2.9
jersey標準では戻り値として null を返すと 204 No Content になってしまいます。これを 200 OK で返し、ボディに null という4文字が設定されるようにしました。

Change Summary

Incremental Difference

--- jersey-util/trunk/src/main/java/net/osdn/util/jersey/InvocationHandler.java (nonexistent)
+++ jersey-util/trunk/src/main/java/net/osdn/util/jersey/InvocationHandler.java (revision 151)
@@ -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+}
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- jersey-util/trunk/src/main/java/net/osdn/util/jersey/InvocationHandlerBinder.java (nonexistent)
+++ jersey-util/trunk/src/main/java/net/osdn/util/jersey/InvocationHandlerBinder.java (revision 151)
@@ -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+}
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- jersey-util/trunk/src/main/java/net/osdn/util/jersey/InvocationHandlerProvider.java (nonexistent)
+++ jersey-util/trunk/src/main/java/net/osdn/util/jersey/InvocationHandlerProvider.java (revision 151)
@@ -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+}
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- jersey-util/trunk/src/main/java/net/osdn/util/jersey/RawString.java (nonexistent)
+++ jersey-util/trunk/src/main/java/net/osdn/util/jersey/RawString.java (revision 151)
@@ -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+}
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- jersey-util/trunk/src/main/java/net/osdn/util/jersey/RawStringMessageBodyWriter.java (nonexistent)
+++ jersey-util/trunk/src/main/java/net/osdn/util/jersey/RawStringMessageBodyWriter.java (revision 151)
@@ -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+}
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- jersey-util/trunk/src/main/java/net/osdn/util/jersey/RestApplication.java (revision 150)
+++ jersey-util/trunk/src/main/java/net/osdn/util/jersey/RestApplication.java (revision 151)
@@ -7,6 +7,8 @@
77 public RestApplication() {
88 packages("net.osdn.util.jersey");
99
10+ boolean isJerseyLoggerRegistered = false;
11+
1012 //jersey-logger
1113 try {
1214 if(exists("net.osdn.util.jersey.log.RequestLogger")) {
@@ -17,6 +19,7 @@
1719 try {
1820 invocationLoggerBinderClass = Class.forName("net.osdn.util.jersey.log.InvocationLoggerBinder");
1921 register(invocationLoggerBinderClass.newInstance());
22+ isJerseyLoggerRegistered = true;
2023 } catch(Exception e) {
2124 e.printStackTrace();
2225 }
@@ -24,6 +27,10 @@
2427 } catch(Exception e) {
2528 e.printStackTrace();
2629 }
30+
31+ if(!isJerseyLoggerRegistered) {
32+ register(new InvocationHandlerBinder());
33+ }
2734
2835 //jersey-freemarker
2936 try {
Show on old repository browser