001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
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.mitre.openid.connect.service.impl;
017
018import java.text.ParseException;
019import java.util.Date;
020import java.util.Locale;
021
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024import org.springframework.format.annotation.DateTimeFormat.ISO;
025import org.springframework.format.datetime.DateFormatter;
026
027public abstract class MITREidDataServiceSupport {
028        private final DateFormatter dateFormatter;
029        /**
030         * Logger for this class
031         */
032        private static final Logger logger = LoggerFactory.getLogger(MITREidDataServiceSupport.class);
033
034        public MITREidDataServiceSupport() {
035                dateFormatter = new DateFormatter();
036                dateFormatter.setIso(ISO.DATE_TIME);
037        }
038
039        protected Date utcToDate(String value) {
040                if (value == null) {
041                        return null;
042                }
043                try {
044                        return dateFormatter.parse(value, Locale.ENGLISH);
045                } catch (ParseException ex) {
046                        logger.error("Unable to parse datetime {}", value, ex);
047                }
048                return null;
049        }
050
051        protected String toUTCString(Date value) {
052                if (value == null) {
053                        return null;
054                }
055                return dateFormatter.print(value, Locale.ENGLISH);
056        }
057
058}