1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.johnzon.jaxrs; |
20 | |
|
21 | |
import org.apache.johnzon.mapper.Mapper; |
22 | |
import org.apache.johnzon.mapper.MapperBuilder; |
23 | |
|
24 | |
import javax.json.JsonStructure; |
25 | |
import javax.ws.rs.Produces; |
26 | |
import javax.ws.rs.core.MediaType; |
27 | |
import javax.ws.rs.core.MultivaluedMap; |
28 | |
import javax.ws.rs.core.Response; |
29 | |
import javax.ws.rs.core.StreamingOutput; |
30 | |
import javax.ws.rs.ext.MessageBodyWriter; |
31 | |
import javax.ws.rs.ext.Provider; |
32 | |
import java.io.IOException; |
33 | |
import java.io.InputStream; |
34 | |
import java.io.OutputStream; |
35 | |
import java.io.Writer; |
36 | |
import java.lang.annotation.Annotation; |
37 | |
import java.lang.reflect.ParameterizedType; |
38 | |
import java.lang.reflect.Type; |
39 | |
import java.util.Collection; |
40 | |
|
41 | |
import static javax.ws.rs.core.MediaType.WILDCARD; |
42 | |
|
43 | |
@Provider |
44 | |
@Produces(WILDCARD) |
45 | |
public class JohnzonMessageBodyWriter<T> extends IgnorableTypes implements MessageBodyWriter<T> { |
46 | |
private final Mapper mapper; |
47 | |
|
48 | |
public JohnzonMessageBodyWriter() { |
49 | 0 | this(new MapperBuilder().setDoCloseOnStreams(false).build(), null); |
50 | 0 | } |
51 | |
|
52 | |
public JohnzonMessageBodyWriter(final Mapper mapper, final Collection<String> ignoredTypes) { |
53 | 10 | super(ignoredTypes); |
54 | 10 | this.mapper = mapper; |
55 | 10 | } |
56 | |
|
57 | |
@Override |
58 | |
public long getSize(final T t, final Class<?> rawType, final Type genericType, |
59 | |
final Annotation[] annotations, final MediaType mediaType) { |
60 | 0 | return -1; |
61 | |
} |
62 | |
|
63 | |
@Override |
64 | |
public boolean isWriteable(final Class<?> rawType, final Type genericType, |
65 | |
final Annotation[] annotations, final MediaType mediaType) { |
66 | 10 | return Jsons.isJson(mediaType) |
67 | |
&& !isIgnored(rawType) |
68 | |
&& InputStream.class != rawType |
69 | |
&& OutputStream.class != rawType |
70 | |
&& Writer.class != rawType |
71 | |
&& StreamingOutput.class != rawType |
72 | |
&& String.class != rawType |
73 | |
&& Response.class != rawType |
74 | |
&& !JsonStructure.class.isAssignableFrom(rawType); |
75 | |
} |
76 | |
|
77 | |
@Override |
78 | |
public void writeTo(final T t, final Class<?> rawType, final Type genericType, |
79 | |
final Annotation[] annotations, final MediaType mediaType, |
80 | |
final MultivaluedMap<String, Object> httpHeaders, |
81 | |
final OutputStream entityStream) throws IOException { |
82 | 8 | if (rawType.isArray()) { |
83 | 2 | mapper.writeArray(t, entityStream); |
84 | 6 | } else if (Collection.class.isAssignableFrom(rawType) && ParameterizedType.class.isInstance(genericType)) { |
85 | 2 | mapper.writeArray(Collection.class.cast(t), entityStream); |
86 | |
} else { |
87 | 4 | mapper.writeObject(t, entityStream); |
88 | |
} |
89 | 8 | } |
90 | |
} |