001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.atteo.moonshine.jersey; 015 016import javax.ws.rs.ext.ContextResolver; 017import javax.ws.rs.ext.Provider; 018import javax.xml.bind.JAXBContext; 019import javax.xml.bind.JAXBException; 020 021import org.atteo.classindex.ClassIndex; 022import org.atteo.moonshine.jaxrs.ResourceModel; 023 024import com.google.common.collect.Iterables; 025import com.google.inject.Singleton; 026import com.sun.jersey.api.json.JSONConfiguration; 027import com.sun.jersey.api.json.JSONJAXBContext; 028 029@Provider 030@Singleton 031public final class JAXBContextResolver implements ContextResolver<JAXBContext> { 032 private JAXBContext context; 033 034 public JAXBContextResolver() { 035 036 try { 037 context = new JSONJAXBContext(JSONConfiguration.natural().usePrefixesAtNaturalAttributes() 038 .rootUnwrapping(true).build(), 039 Iterables.toArray(ClassIndex.getAnnotated(ResourceModel.class), Class.class)); 040 } catch (JAXBException e) { 041 throw new RuntimeException(e); 042 } 043 } 044 045 @Override 046 public JAXBContext getContext(Class<?> objectType) { 047 return context; 048 } 049}