1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.rest.delivery.jcr; |
35 |
|
|
36 |
|
import java.io.IOException; |
37 |
|
import java.io.OutputStream; |
38 |
|
import java.lang.annotation.Annotation; |
39 |
|
import java.lang.reflect.Type; |
40 |
|
|
41 |
|
import javax.jcr.Node; |
42 |
|
import javax.jcr.NodeIterator; |
43 |
|
import javax.json.Json; |
44 |
|
import javax.json.stream.JsonGenerator; |
45 |
|
import javax.ws.rs.Produces; |
46 |
|
import javax.ws.rs.WebApplicationException; |
47 |
|
import javax.ws.rs.core.Context; |
48 |
|
import javax.ws.rs.core.MediaType; |
49 |
|
import javax.ws.rs.core.MultivaluedMap; |
50 |
|
import javax.ws.rs.ext.MessageBodyWriter; |
51 |
|
import javax.ws.rs.ext.Provider; |
52 |
|
import javax.ws.rs.ext.Providers; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@Provider |
59 |
|
@Produces({ MediaType.APPLICATION_JSON }) |
|
|
| 84.6% |
Uncovered Elements: 4 (26) |
Complexity: 8 |
Complexity Density: 0.57 |
|
60 |
|
public class NodeIteratorWriter implements MessageBodyWriter<NodeIterator> { |
61 |
|
|
62 |
|
@Context |
63 |
|
private Providers providers; |
64 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
65 |
0 |
@Override... |
66 |
|
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { |
67 |
0 |
return NodeIterator.class.isAssignableFrom(type); |
68 |
|
} |
69 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
70 |
0 |
@Override... |
71 |
|
public long getSize(NodeIterator nodeIterator, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { |
72 |
|
|
73 |
0 |
return -1; |
74 |
|
} |
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 5 |
Complexity Density: 0.45 |
|
76 |
4 |
@Override... |
77 |
|
public void writeTo(NodeIterator nodeIterator, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { |
78 |
4 |
MessageBodyWriter<Node> nodeWriter = providers.getMessageBodyWriter(Node.class, genericType, annotations, mediaType); |
79 |
|
|
80 |
4 |
JsonGenerator jsonGenerator = entityStream instanceof OutputStreamWrapper ? ((OutputStreamWrapper) entityStream).getJsonGenerator() : Json.createGenerator(entityStream); |
81 |
|
|
82 |
4 |
OutputStream wrappedEntityStream = new OutputStreamWrapper(entityStream, jsonGenerator); |
83 |
|
|
84 |
4 |
if (!OutputStreamWrapper.isWrapped(entityStream)) { |
85 |
2 |
jsonGenerator.writeStartArray(); |
86 |
|
} |
87 |
|
|
88 |
16 |
while (nodeIterator.hasNext()) { |
89 |
12 |
Node node = nodeIterator.nextNode(); |
90 |
12 |
nodeWriter.writeTo(node, type, genericType, annotations, mediaType, httpHeaders, wrappedEntityStream); |
91 |
|
} |
92 |
4 |
jsonGenerator.writeEnd(); |
93 |
|
|
94 |
4 |
if (!OutputStreamWrapper.isWrapped(entityStream)) { |
95 |
2 |
jsonGenerator.close(); |
96 |
|
} |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
102 |
4 |
void setProviders(Providers providers) {... |
103 |
4 |
this.providers = providers; |
104 |
|
} |
105 |
|
} |