001/*
002 * Copyright 2015 DuraSpace, Inc.
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 */
016package org.fcrepo.transform.http.responses;
017
018import com.fasterxml.jackson.databind.ObjectMapper;
019import org.slf4j.Logger;
020
021import javax.ws.rs.ext.ContextResolver;
022import javax.ws.rs.ext.Provider;
023
024import java.text.DateFormat;
025import java.text.SimpleDateFormat;
026
027import static org.slf4j.LoggerFactory.getLogger;
028
029/**
030 * This {@link Provider} adds configuration for the serialization of JSON resources.
031 *
032 * @author awoods
033 * @since Feb 9, 2016
034 */
035@Provider
036public class JsonObjectProvider implements ContextResolver<ObjectMapper> {
037
038    private static final Logger LOGGER = getLogger(JsonObjectProvider.class);
039
040    public static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
041
042    private final ObjectMapper defaultObjectMapper;
043
044    public JsonObjectProvider() {
045        defaultObjectMapper = createDefaultMapper();
046    }
047
048    @Override
049    public ObjectMapper getContext(Class<?> aClass) {
050        LOGGER.debug("Object mapping for: {}", aClass.getCanonicalName());
051        return defaultObjectMapper;
052    }
053
054    private static ObjectMapper createDefaultMapper() {
055        final ObjectMapper mapper = new ObjectMapper();
056        mapper.setDateFormat(DATE_FORMAT);
057
058        return mapper;
059    }
060}