jaroslav@134
|
1 |
/*
|
jaroslav@264
|
2 |
* Quoridor server and related libraries
|
jaroslav@264
|
3 |
* Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@178
|
4 |
*
|
jaroslav@264
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@264
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@264
|
7 |
* the Free Software Foundation, either version 3 of the License.
|
jaroslav@178
|
8 |
*
|
jaroslav@264
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@264
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@264
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@264
|
12 |
* GNU General Public License for more details.
|
jaroslav@178
|
13 |
*
|
jaroslav@264
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@264
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@264
|
16 |
* If not, see http://www.gnu.org/licenses/.
|
jaroslav@134
|
17 |
*/
|
jaroslav@134
|
18 |
package cz.xelfi.quoridor.freemarkerdor;
|
jaroslav@134
|
19 |
|
jaroslav@134
|
20 |
import java.io.IOException;
|
jaroslav@134
|
21 |
import java.io.InputStream;
|
jaroslav@134
|
22 |
import java.lang.annotation.Annotation;
|
jaroslav@134
|
23 |
import java.lang.reflect.Type;
|
jaroslav@134
|
24 |
import javax.ws.rs.Consumes;
|
jaroslav@134
|
25 |
import javax.ws.rs.WebApplicationException;
|
jaroslav@134
|
26 |
import javax.ws.rs.core.MediaType;
|
jaroslav@134
|
27 |
import javax.ws.rs.core.MultivaluedMap;
|
jaroslav@134
|
28 |
import javax.ws.rs.ext.Provider;
|
jaroslav@134
|
29 |
import javax.xml.parsers.DocumentBuilder;
|
jaroslav@134
|
30 |
import javax.xml.parsers.DocumentBuilderFactory;
|
jaroslav@134
|
31 |
import javax.xml.parsers.ParserConfigurationException;
|
jaroslav@134
|
32 |
import org.w3c.dom.Document;
|
jaroslav@134
|
33 |
import org.xml.sax.SAXException;
|
jaroslav@134
|
34 |
|
jaroslav@134
|
35 |
/**
|
jaroslav@134
|
36 |
*
|
jaroslav@134
|
37 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@134
|
38 |
*/
|
jaroslav@134
|
39 |
@Provider
|
jaroslav@134
|
40 |
@Consumes(MediaType.TEXT_XML)
|
jaroslav@134
|
41 |
public class W3CDocumentReader implements javax.ws.rs.ext.MessageBodyReader<Document> {
|
jaroslav@134
|
42 |
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
|
jaroslav@134
|
43 |
return type == Document.class;
|
jaroslav@134
|
44 |
}
|
jaroslav@134
|
45 |
|
jaroslav@134
|
46 |
public Document readFrom(Class<Document> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
|
jaroslav@134
|
47 |
try {
|
jaroslav@134
|
48 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
jaroslav@134
|
49 |
DocumentBuilder builder = factory.newDocumentBuilder();
|
jaroslav@134
|
50 |
return builder.parse(entityStream);
|
jaroslav@134
|
51 |
} catch (ParserConfigurationException ex) {
|
jaroslav@134
|
52 |
throw new WebApplicationException(ex);
|
jaroslav@134
|
53 |
} catch (SAXException ex) {
|
jaroslav@134
|
54 |
throw new WebApplicationException(ex);
|
jaroslav@134
|
55 |
}
|
jaroslav@134
|
56 |
}
|
jaroslav@134
|
57 |
|
jaroslav@134
|
58 |
}
|