All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.moodysalem.jaxrs.lib.converters.JodaTimeParamConverterProvider Maven / Gradle / Ivy

package com.moodysalem.jaxrs.lib.converters;

import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Allows Jersey to handle JodaTime types as parameters
 */
@SuppressWarnings("unchecked")
@Provider
public class JodaTimeParamConverterProvider implements ParamConverterProvider {

    @Override
    public  ParamConverter getConverter(Class rawType, Type genericType, Annotation[] annotations) {
        if (rawType.equals(LocalDate.class)) {
            return (ParamConverter) new LocalDateParamConverter();
        } else if (rawType.equals(LocalDateTime.class)) {
            return (ParamConverter) new LocalDateTimeParamConverter();
        } else if (rawType.equals(LocalTime.class)) {
            return (ParamConverter) new LocalTimeParamConverter();
        }
        return null;
    }

    public static class LocalDateParamConverter implements ParamConverter {
        private static final Logger LOG = Logger.getLogger(LocalDateParamConverter.class.getName());

        @Override
        public LocalDate fromString(String value) {
            if (value != null) {
                try {
                    return LocalDate.parse(value);
                } catch (DateTimeParseException e) {
                    LOG.log(Level.WARNING, "Invalid local date parameter value specified", e);
                }
            }
            return null;
        }

        @Override
        public String toString(LocalDate value) {
            if (value != null) {
                return value.toString();
            }
            return null;
        }
    }

    public static class LocalTimeParamConverter implements ParamConverter {
        private static final Logger LOG = Logger.getLogger(LocalDateParamConverter.class.getName());

        @Override
        public LocalTime fromString(String value) {
            if (value != null) {
                try {
                    return LocalTime.parse(value);
                } catch (DateTimeParseException e) {
                    LOG.log(Level.WARNING, "Invalid local time parameter value specified", e);
                }
            }
            return null;
        }

        @Override
        public String toString(LocalTime value) {
            if (value != null) {
                return value.toString();
            }
            return null;
        }
    }

    public static class LocalDateTimeParamConverter implements ParamConverter {
        private static final Logger LOG = Logger.getLogger(LocalDateTimeParamConverter.class.getName());

        @Override
        public LocalDateTime fromString(String value) {
            if (value != null) {
                try {
                    return LocalDateTime.parse(value);
                } catch (DateTimeParseException e) {
                    LOG.log(Level.WARNING, "Invalid local datetime parameter value specified", e);
                }
            }
            return null;
        }

        @Override
        public String toString(LocalDateTime value) {
            return null;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy