001/*
002 * Copyright (c) 2013 Maxim Yunusov
003 *    Licensed under the Apache License, Version 2.0 (the "License");
004 *    you may not use this file except in compliance with the License.
005 *    You may obtain a copy of the License at
006 *
007 *        http://www.apache.org/licenses/LICENSE-2.0
008 *
009 *    Unless required by applicable law or agreed to in writing, software
010 *    distributed under the License is distributed on an "AS IS" BASIS,
011 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 *    See the License for the specific language governing permissions and
013 *    limitations under the License.
014 */
015
016package org.maxur.perfmodel.backend.rest;
017
018import com.fasterxml.jackson.databind.ObjectMapper;
019import com.fasterxml.jackson.databind.SerializationFeature;
020import org.maxur.perfmodel.backend.domain.Incident;
021import org.maxur.perfmodel.backend.domain.Project;
022
023import javax.ws.rs.ext.ContextResolver;
024import javax.xml.bind.JAXBException;
025import java.util.Arrays;
026import java.util.HashSet;
027import java.util.Set;
028
029/**
030 * @author Maxim Yunusov
031 * @version 1.0
032 * @since <pre>11/25/13</pre>
033 */
034public class PmcObjectMapperProvider implements ContextResolver<ObjectMapper> {
035
036    private final ObjectMapper context;
037    private final Set<Class<?>> types;
038
039    public PmcObjectMapperProvider() throws JAXBException{
040        final Class<?>[] cTypes = {Project.class, Incident.class};
041        this.types = new HashSet<>(Arrays.asList(cTypes));
042        this.context = createDefaultMapper();
043    }
044
045    @Override
046    public ObjectMapper getContext(Class<?> objectType) {
047        return types.contains(objectType) ? context : null;
048    }
049
050    private static ObjectMapper createDefaultMapper() {
051        final ObjectMapper result = new ObjectMapper();
052        result.configure(SerializationFeature.INDENT_OUTPUT, true);
053        return result;
054    }
055}