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