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.MapperBuilder; |
22 | |
import org.apache.johnzon.mapper.access.AccessMode; |
23 | |
|
24 | |
import javax.json.JsonReaderFactory; |
25 | |
import javax.json.stream.JsonGeneratorFactory; |
26 | |
import javax.ws.rs.Consumes; |
27 | |
import javax.ws.rs.Produces; |
28 | |
import javax.ws.rs.core.MediaType; |
29 | |
import javax.ws.rs.core.MultivaluedMap; |
30 | |
import javax.ws.rs.ext.MessageBodyReader; |
31 | |
import javax.ws.rs.ext.MessageBodyWriter; |
32 | |
import javax.ws.rs.ext.Provider; |
33 | |
import java.io.IOException; |
34 | |
import java.io.InputStream; |
35 | |
import java.io.OutputStream; |
36 | |
import java.lang.annotation.Annotation; |
37 | |
import java.lang.reflect.Type; |
38 | |
import java.util.Comparator; |
39 | |
import java.util.List; |
40 | |
import java.util.concurrent.atomic.AtomicReference; |
41 | |
|
42 | |
import static java.util.Arrays.asList; |
43 | |
import static javax.ws.rs.core.MediaType.WILDCARD; |
44 | |
|
45 | |
@Provider |
46 | |
@Produces(WILDCARD) |
47 | |
@Consumes(WILDCARD) |
48 | 1 | public class ConfigurableJohnzonProvider<T> implements MessageBodyWriter<T>, MessageBodyReader<T> { |
49 | |
|
50 | 1 | private MapperBuilder builder = new MapperBuilder(); |
51 | |
private List<String> ignores; |
52 | |
|
53 | |
|
54 | 1 | private AtomicReference<JohnzonProvider<T>> delegate = new AtomicReference<JohnzonProvider<T>>(); |
55 | |
|
56 | |
private JohnzonProvider<T> instance() { |
57 | |
JohnzonProvider<T> instance; |
58 | |
do { |
59 | 10 | instance = delegate.get(); |
60 | 10 | if (builder != null && delegate.compareAndSet(null, new JohnzonProvider<T>(builder.build(), ignores))) { |
61 | |
|
62 | 1 | builder = null; |
63 | 1 | ignores = null; |
64 | |
} |
65 | 10 | } while (instance == null); |
66 | 9 | return instance; |
67 | |
} |
68 | |
|
69 | |
@Override |
70 | |
public boolean isReadable(final Class<?> rawType, final Type genericType, |
71 | |
final Annotation[] annotations, final MediaType mediaType) { |
72 | 1 | return instance().isReadable(rawType, genericType, annotations, mediaType); |
73 | |
} |
74 | |
|
75 | |
@Override |
76 | |
public T readFrom(final Class<T> rawType, final Type genericType, |
77 | |
final Annotation[] annotations, final MediaType mediaType, |
78 | |
final MultivaluedMap<String, String> httpHeaders, |
79 | |
final InputStream entityStream) throws IOException { |
80 | 1 | return instance().readFrom(rawType, genericType, annotations, mediaType, httpHeaders, entityStream); |
81 | |
} |
82 | |
|
83 | |
@Override |
84 | |
public long getSize(final T t, final Class<?> rawType, final Type genericType, |
85 | |
final Annotation[] annotations, final MediaType mediaType) { |
86 | 0 | return instance().getSize(t, rawType, genericType, annotations, mediaType); |
87 | |
} |
88 | |
|
89 | |
@Override |
90 | |
public boolean isWriteable(final Class<?> rawType, final Type genericType, |
91 | |
final Annotation[] annotations, final MediaType mediaType) { |
92 | 4 | return instance().isWriteable(rawType, genericType, annotations, mediaType); |
93 | |
} |
94 | |
|
95 | |
@Override |
96 | |
public void writeTo(final T t, final Class<?> rawType, final Type genericType, |
97 | |
final Annotation[] annotations, final MediaType mediaType, |
98 | |
final MultivaluedMap<String, Object> httpHeaders, |
99 | |
final OutputStream entityStream) throws IOException { |
100 | 3 | instance().writeTo(t, rawType, genericType, annotations, mediaType, httpHeaders, entityStream); |
101 | 3 | } |
102 | |
|
103 | |
public void setSupportConstructors(final boolean supportConstructors) { |
104 | 0 | builder.setSupportConstructors(supportConstructors); |
105 | 0 | } |
106 | |
|
107 | |
public void setPretty(final boolean pretty) { |
108 | 0 | builder.setPretty(pretty); |
109 | 0 | } |
110 | |
|
111 | |
public void setSupportGetterForCollections(final boolean supportGetterForCollections) { |
112 | 0 | builder.setSupportGetterForCollections(supportGetterForCollections); |
113 | 0 | } |
114 | |
|
115 | |
public void setSupportsComments(final boolean supportsComments) { |
116 | 0 | builder.setSupportsComments(supportsComments); |
117 | 0 | } |
118 | |
|
119 | |
public void setIgnores(final String ignores) { |
120 | 0 | this.ignores = ignores == null ? null : asList(ignores.split(" *, *")); |
121 | 0 | } |
122 | |
|
123 | |
public void setAccessMode(final AccessMode mode) { |
124 | 0 | builder.setAccessMode(mode); |
125 | 0 | } |
126 | |
|
127 | |
public void setAccessModeName(final String mode) { |
128 | 0 | builder.setAccessModeName(mode); |
129 | 0 | } |
130 | |
|
131 | |
public void setSupportHiddenAccess(final boolean supportHiddenAccess) { |
132 | 0 | builder.setSupportHiddenAccess(supportHiddenAccess); |
133 | 0 | } |
134 | |
|
135 | |
public void setAttributeOrder(final Comparator<String> attributeOrder) { |
136 | 0 | builder.setAttributeOrder(attributeOrder); |
137 | 0 | } |
138 | |
|
139 | |
public void setReaderFactory(final JsonReaderFactory readerFactory) { |
140 | 0 | builder.setReaderFactory(readerFactory); |
141 | 0 | } |
142 | |
|
143 | |
public void setGeneratorFactory(final JsonGeneratorFactory generatorFactory) { |
144 | 0 | builder.setGeneratorFactory(generatorFactory); |
145 | 0 | } |
146 | |
|
147 | |
public void setDoCloseOnStreams(final boolean doCloseOnStreams) { |
148 | 0 | builder.setDoCloseOnStreams(doCloseOnStreams); |
149 | 0 | } |
150 | |
|
151 | |
public void setVersion(final int version) { |
152 | 0 | builder.setVersion(version); |
153 | 0 | } |
154 | |
|
155 | |
public void setSkipNull(final boolean skipNull) { |
156 | 0 | builder.setSkipNull(skipNull); |
157 | 0 | } |
158 | |
|
159 | |
public void setSkipEmptyArray(final boolean skipEmptyArray) { |
160 | 0 | builder.setSkipEmptyArray(skipEmptyArray); |
161 | 0 | } |
162 | |
|
163 | |
public void setBufferSize(final int bufferSize) { |
164 | 0 | builder.setBufferSize(bufferSize); |
165 | 0 | } |
166 | |
|
167 | |
public void setBufferStrategy(final String bufferStrategy) { |
168 | 0 | builder.setBufferStrategy(bufferStrategy); |
169 | 0 | } |
170 | |
|
171 | |
public void setMaxSize(final int size) { |
172 | 0 | builder.setMaxSize(size); |
173 | 0 | } |
174 | |
|
175 | |
public void setTreatByteArrayAsBase64(final boolean treatByteArrayAsBase64) { |
176 | 0 | builder.setTreatByteArrayAsBase64(treatByteArrayAsBase64); |
177 | 0 | } |
178 | |
} |