freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/W3CDocumentReader.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Jan 2010 22:34:17 +0100
branchstatistics-and-elo
changeset 178 4b78d4f028b3
parent 134 6544ad908b05
child 264 d60370059c3c
permissions -rw-r--r--
Initial version of statistics and ELO rating. Donated by Martin Rexa
jaroslav@134
     1
/*
jaroslav@178
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@178
     3
 *
jaroslav@178
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@178
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@178
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@178
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@178
     8
 * License. You can obtain a copy of the License at
jaroslav@178
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@178
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@178
    11
 * specific language governing permissions and limitations under the
jaroslav@178
    12
 * License.  When distributing the software, include this License Header
jaroslav@178
    13
 * Notice in each file and include the License file at
jaroslav@178
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@178
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@178
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@178
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@178
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@178
    19
 * your own identifying information:
jaroslav@178
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@178
    21
 *
jaroslav@178
    22
 * Contributor(s):
jaroslav@178
    23
 *
jaroslav@178
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jaroslav@134
    25
 */
jaroslav@134
    26
jaroslav@134
    27
package cz.xelfi.quoridor.freemarkerdor;
jaroslav@134
    28
jaroslav@134
    29
import java.io.IOException;
jaroslav@134
    30
import java.io.InputStream;
jaroslav@134
    31
import java.lang.annotation.Annotation;
jaroslav@134
    32
import java.lang.reflect.Type;
jaroslav@134
    33
import javax.ws.rs.Consumes;
jaroslav@134
    34
import javax.ws.rs.WebApplicationException;
jaroslav@134
    35
import javax.ws.rs.core.MediaType;
jaroslav@134
    36
import javax.ws.rs.core.MultivaluedMap;
jaroslav@134
    37
import javax.ws.rs.ext.Provider;
jaroslav@134
    38
import javax.xml.parsers.DocumentBuilder;
jaroslav@134
    39
import javax.xml.parsers.DocumentBuilderFactory;
jaroslav@134
    40
import javax.xml.parsers.ParserConfigurationException;
jaroslav@134
    41
import org.w3c.dom.Document;
jaroslav@134
    42
import org.xml.sax.SAXException;
jaroslav@134
    43
jaroslav@134
    44
/**
jaroslav@134
    45
 *
jaroslav@134
    46
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@134
    47
 */
jaroslav@134
    48
@Provider
jaroslav@134
    49
@Consumes(MediaType.TEXT_XML)
jaroslav@134
    50
public class W3CDocumentReader implements javax.ws.rs.ext.MessageBodyReader<Document> {
jaroslav@134
    51
    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
jaroslav@134
    52
        return type == Document.class;
jaroslav@134
    53
    }
jaroslav@134
    54
jaroslav@134
    55
    public Document readFrom(Class<Document> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
jaroslav@134
    56
        try {
jaroslav@134
    57
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
jaroslav@134
    58
            DocumentBuilder builder = factory.newDocumentBuilder();
jaroslav@134
    59
            return builder.parse(entityStream);
jaroslav@134
    60
        } catch (ParserConfigurationException ex) {
jaroslav@134
    61
            throw new WebApplicationException(ex);
jaroslav@134
    62
        } catch (SAXException ex) {
jaroslav@134
    63
            throw new WebApplicationException(ex);
jaroslav@134
    64
        }
jaroslav@134
    65
    }
jaroslav@134
    66
jaroslav@134
    67
}