001/*
002 * (C) Copyright 2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 */
017
018package org.nuxeo.ecm.webdav;
019
020import javax.ws.rs.Produces;
021import javax.ws.rs.ext.ContextResolver;
022import javax.ws.rs.ext.Provider;
023import javax.xml.bind.JAXBContext;
024import javax.xml.bind.JAXBException;
025
026import org.nuxeo.ecm.webdav.jaxrs.Util;
027
028/**
029 * Injects the JAXBContext needed to parse our webdav XML payloads.
030 */
031@Provider
032@Produces({ "application/xml", "text/xml" })
033public class WebDavContextResolver implements ContextResolver<JAXBContext> {
034
035    private final JAXBContext ctx;
036
037    public WebDavContextResolver() {
038        try {
039            ctx = Util.getJaxbContext();
040        } catch (JAXBException e) {
041            throw new RuntimeException(e);
042        }
043    }
044
045    @Override
046    public JAXBContext getContext(Class<?> type) {
047        if (type.getPackage().getName().startsWith("net.java.dev.webdav.jaxrs.xml.elements")) {
048            return ctx;
049        } else {
050            return null;
051        }
052    }
053
054}