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

com.nitorcreations.nflow.rest.config.DateTimeParamConverterProvider Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
package com.nitorcreations.nflow.rest.config;

import static java.lang.String.format;
import static org.joda.time.format.ISODateTimeFormat.dateTime;
import static org.joda.time.format.ISODateTimeFormat.dateTimeNoMillis;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;

import org.joda.time.DateTime;

@Provider
public class DateTimeParamConverterProvider implements ParamConverterProvider {

  @SuppressWarnings("unchecked")
  @Override
  public  ParamConverter getConverter(Class type, Type genericType, Annotation[] annotations) {
    if (type.equals(DateTime.class)) {
      return (ParamConverter) new DateTimeParamConverter();
    }
    return null;
  }

  static final class DateTimeParamConverter implements ParamConverter {
    @Override
    public DateTime fromString(String value) {
      try {
        return dateTimeNoMillis().parseDateTime(value);
      } catch (IllegalArgumentException e) {
        try {
          return dateTime().parseDateTime(value);
        } catch (IllegalArgumentException e2) {
          throw new BadRequestException(format("Unrecognized date format: %s", value), e2);
        }
      }
    }

    @Override
    public String toString(DateTime value) {
      return value.toString();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy