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