001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019
020package org.nuxeo.ecm.webdav;
021
022import javax.ws.rs.Produces;
023import javax.ws.rs.ext.ContextResolver;
024import javax.ws.rs.ext.Provider;
025import javax.xml.bind.JAXBContext;
026import javax.xml.bind.JAXBException;
027
028import org.nuxeo.ecm.webdav.jaxrs.Util;
029
030/**
031 * Injects the JAXBContext needed to parse our webdav XML payloads.
032 */
033@Provider
034@Produces({ "application/xml", "text/xml" })
035public class WebDavContextResolver implements ContextResolver<JAXBContext> {
036
037    private final JAXBContext ctx;
038
039    public WebDavContextResolver() {
040        try {
041            ctx = Util.getJaxbContext();
042        } catch (JAXBException e) {
043            throw new RuntimeException(e);
044        }
045    }
046
047    @Override
048    public JAXBContext getContext(Class<?> type) {
049        if (type.getPackage().getName().startsWith("net.java.dev.webdav.jaxrs.xml.elements")) {
050            return ctx;
051        } else {
052            return null;
053        }
054    }
055
056}