001package org.apache.archiva.redback.rest.services.interceptors;
002/*
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020
021import com.fasterxml.jackson.core.JsonGenerator;
022import com.fasterxml.jackson.core.json.JsonWriteFeature;
023import com.fasterxml.jackson.databind.DeserializationFeature;
024import com.fasterxml.jackson.databind.ObjectMapper;
025import com.fasterxml.jackson.databind.PropertyNamingStrategy;
026import com.fasterxml.jackson.databind.SerializationFeature;
027import com.fasterxml.jackson.dataformat.xml.XmlMapper;
028import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
029import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
030import org.eclipse.jetty.util.annotation.Name;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033import org.springframework.stereotype.Service;
034
035import javax.inject.Inject;
036import javax.inject.Named;
037import java.text.SimpleDateFormat;
038
039/**
040 * to setup some ObjectMapper configuration
041 *
042 * @author Olivier Lamy
043 * @since 2.0
044 */
045@Service("redbackJacksonJsonConfigurator")
046public class JacksonJsonConfigurator
047{
048    private Logger log = LoggerFactory.getLogger( getClass() );
049
050    @Inject
051    public JacksonJsonConfigurator( @Named( "redbackJacksonJsonMapper" ) ObjectMapper objectMapper,
052                                    @Name( "redbackJacksonXMLMapper" ) XmlMapper xmlMapper, @Named( "v2.redbackJacksonJsonMapper" ) ObjectMapper objectMapperV2 )
053    {
054
055        log.info( "configure jackson ObjectMapper" );
056        objectMapper.disable( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES );
057        objectMapper.enable( DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL );
058        objectMapper.enable( DeserializationFeature.USE_LONG_FOR_INTS );
059        objectMapper.setAnnotationIntrospector( new JaxbAnnotationIntrospector( objectMapper.getTypeFactory( ) ) );
060        objectMapper.findAndRegisterModules( );
061        objectMapper.registerModule( new JavaTimeModule( ) );
062        objectMapper.setDateFormat( new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ) );
063
064        objectMapperV2.disable( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES );
065        objectMapperV2.disable( DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES );
066        objectMapperV2.enable( DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL );
067        objectMapperV2.enable( DeserializationFeature.USE_LONG_FOR_INTS );
068        objectMapperV2.setAnnotationIntrospector( new JaxbAnnotationIntrospector( objectMapper.getTypeFactory( ) ) );
069        objectMapperV2.findAndRegisterModules( );
070        objectMapperV2.registerModule( new JavaTimeModule( ) );
071        objectMapperV2.setDateFormat( new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ) );
072        objectMapperV2.setPropertyNamingStrategy( PropertyNamingStrategy.SNAKE_CASE );
073
074
075        xmlMapper.disable( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES );
076        xmlMapper.setAnnotationIntrospector( new JaxbAnnotationIntrospector( xmlMapper.getTypeFactory( ) ) );
077
078    }
079}