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.jaxrs.xml.WadlDocumentToJson; |
22 | |
import org.w3c.dom.Document; |
23 | |
|
24 | |
import javax.ws.rs.WebApplicationException; |
25 | |
import javax.ws.rs.core.MediaType; |
26 | |
import javax.ws.rs.core.MultivaluedMap; |
27 | |
import javax.ws.rs.ext.MessageBodyWriter; |
28 | |
import javax.xml.stream.XMLStreamException; |
29 | |
import java.io.IOException; |
30 | |
import java.io.OutputStream; |
31 | |
import java.lang.annotation.Annotation; |
32 | |
import java.lang.reflect.Type; |
33 | |
|
34 | |
import static org.apache.johnzon.jaxrs.Jsons.isJson; |
35 | |
|
36 | 0 | public class WadlDocumentMessageBodyWriter implements MessageBodyWriter<Document> { |
37 | 0 | private final WadlDocumentToJson converter = new WadlDocumentToJson(); |
38 | |
|
39 | |
@Override |
40 | |
public boolean isWriteable(final Class<?> aClass, final Type type, |
41 | |
final Annotation[] annotations, final MediaType mediaType) { |
42 | 0 | return isJson(mediaType) && Document.class.isAssignableFrom(aClass); |
43 | |
} |
44 | |
|
45 | |
@Override |
46 | |
public long getSize(final Document document, final Class<?> aClass, |
47 | |
final Type type, final Annotation[] annotations, |
48 | |
final MediaType mediaType) { |
49 | 0 | return -1; |
50 | |
} |
51 | |
|
52 | |
@Override |
53 | |
public void writeTo(final Document document, final Class<?> aClass, |
54 | |
final Type type, final Annotation[] annotations, |
55 | |
final MediaType mediaType, final MultivaluedMap<String, Object> stringObjectMultivaluedMap, |
56 | |
final OutputStream outputStream) throws IOException, WebApplicationException { |
57 | |
try { |
58 | 0 | outputStream.write(converter.convert(document).getBytes()); |
59 | 0 | } catch (final XMLStreamException e) { |
60 | 0 | throw new IllegalStateException(e); |
61 | 0 | } |
62 | 0 | } |
63 | |
} |