jtulach@54
|
1 |
/*
|
jtulach@54
|
2 |
* To change this template, choose Tools | Templates
|
jtulach@54
|
3 |
* and open the template in the editor.
|
jtulach@54
|
4 |
*/
|
jtulach@54
|
5 |
|
jtulach@54
|
6 |
package cz.xelfi.quoridor.webidor;
|
jtulach@54
|
7 |
|
jtulach@54
|
8 |
import java.io.IOException;
|
jtulach@54
|
9 |
import java.io.InputStream;
|
jtulach@54
|
10 |
import java.lang.annotation.Annotation;
|
jtulach@54
|
11 |
import java.lang.reflect.Type;
|
jtulach@54
|
12 |
import javax.ws.rs.Consumes;
|
jtulach@54
|
13 |
import javax.ws.rs.WebApplicationException;
|
jtulach@54
|
14 |
import javax.ws.rs.core.MediaType;
|
jtulach@54
|
15 |
import javax.ws.rs.core.MultivaluedMap;
|
jtulach@54
|
16 |
import javax.ws.rs.ext.Provider;
|
jtulach@54
|
17 |
import javax.xml.parsers.DocumentBuilder;
|
jtulach@54
|
18 |
import javax.xml.parsers.DocumentBuilderFactory;
|
jtulach@54
|
19 |
import javax.xml.parsers.ParserConfigurationException;
|
jtulach@54
|
20 |
import org.w3c.dom.Document;
|
jtulach@54
|
21 |
import org.xml.sax.SAXException;
|
jtulach@54
|
22 |
|
jtulach@54
|
23 |
/**
|
jtulach@54
|
24 |
*
|
jtulach@54
|
25 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@54
|
26 |
*/
|
jtulach@54
|
27 |
@Provider
|
jtulach@54
|
28 |
@Consumes(MediaType.TEXT_XML)
|
jtulach@54
|
29 |
public class W3CDocumentReader implements javax.ws.rs.ext.MessageBodyReader<Document> {
|
jtulach@54
|
30 |
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
|
jtulach@54
|
31 |
return type == Document.class;
|
jtulach@54
|
32 |
}
|
jtulach@54
|
33 |
|
jtulach@54
|
34 |
public Document readFrom(Class<Document> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
|
jtulach@54
|
35 |
try {
|
jtulach@54
|
36 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
jtulach@54
|
37 |
DocumentBuilder builder = factory.newDocumentBuilder();
|
jtulach@54
|
38 |
return builder.parse(entityStream);
|
jtulach@54
|
39 |
} catch (ParserConfigurationException ex) {
|
jtulach@54
|
40 |
throw new WebApplicationException(ex);
|
jtulach@54
|
41 |
} catch (SAXException ex) {
|
jtulach@54
|
42 |
throw new WebApplicationException(ex);
|
jtulach@54
|
43 |
}
|
jtulach@54
|
44 |
}
|
jtulach@54
|
45 |
|
jtulach@54
|
46 |
}
|