View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.common.rest.api;
20  
21  import java.lang.annotation.Annotation;
22  import java.lang.reflect.Type;
23  import java.time.LocalDateTime;
24  import java.time.OffsetDateTime;
25  import java.time.format.DateTimeFormatter;
26  import java.time.format.DateTimeParseException;
27  import javax.ws.rs.ext.ParamConverter;
28  import javax.ws.rs.ext.ParamConverterProvider;
29  
30  public class DateParamConverterProvider implements ParamConverterProvider {
31  
32      protected static class OffsetDateTimeParamConverter implements ParamConverter<OffsetDateTime> {
33  
34          @Override
35          public OffsetDateTime fromString(final String value) {
36              try {
37                  return OffsetDateTime.parse(value, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
38              } catch (DateTimeParseException e) {
39                  throw new IllegalArgumentException("Unparsable date: " + value, e);
40              }
41          }
42  
43          @Override
44          public String toString(final OffsetDateTime value) {
45              return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(value);
46          }
47      }
48  
49      protected static class LocalDateTimeParamConverter implements ParamConverter<LocalDateTime> {
50  
51          @Override
52          public LocalDateTime fromString(final String value) {
53              try {
54                  return LocalDateTime.parse(value, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
55              } catch (DateTimeParseException e) {
56                  throw new IllegalArgumentException("Unparsable date: " + value, e);
57              }
58          }
59  
60          @Override
61          public String toString(final LocalDateTime value) {
62              return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value);
63          }
64      }
65  
66      @Override
67      @SuppressWarnings("unchecked")
68      public <T> ParamConverter<T> getConverter(
69              final Class<T> rawType, final Type genericType, final Annotation[] annotations) {
70  
71          if (OffsetDateTime.class.equals(rawType)) {
72              return (ParamConverter<T>) new OffsetDateTimeParamConverter();
73          }
74          if (LocalDateTime.class.equals(rawType)) {
75              return (ParamConverter<T>) new LocalDateTimeParamConverter();
76          }
77  
78          return null;
79      }
80  }