Repository: cxf
Updated Branches:
  refs/heads/master 79939ff18 -> 84d71635c


[CXF-7442] added a ParamConverterProvider for JSR 310 Date Time API


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/84d71635
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/84d71635
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/84d71635

Branch: refs/heads/master
Commit: 84d71635c7dae21cfa9b5fb3ae11d4950ad8729b
Parents: 79939ff
Author: Dennis Kieselhorst <d...@apache.org>
Authored: Wed Jul 19 16:10:55 2017 +0200
Committer: Dennis Kieselhorst <d...@apache.org>
Committed: Wed Jul 19 16:10:55 2017 +0200

----------------------------------------------------------------------
 .../JavaTimeTypesParamConverterProvider.java    | 183 +++++++++++++++++++
 ...JavaTimeTypesParamConverterProviderTest.java |  99 ++++++++++
 2 files changed, 282 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/84d71635/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JavaTimeTypesParamConverterProvider.java
----------------------------------------------------------------------
diff --git 
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JavaTimeTypesParamConverterProvider.java
 
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JavaTimeTypesParamConverterProvider.java
new file mode 100644
index 0000000..57ced8b
--- /dev/null
+++ 
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JavaTimeTypesParamConverterProvider.java
@@ -0,0 +1,183 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.jaxrs.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.OffsetDateTime;
+import java.time.OffsetTime;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+
+import javax.ws.rs.ext.ParamConverter;
+import javax.ws.rs.ext.ParamConverterProvider;
+import javax.ws.rs.ext.Provider;
+
+/**
+ * ParamConverterProvider for Java 8 JSR 310 Date Time API
+ */
+@Provider
+public class JavaTimeTypesParamConverterProvider implements 
ParamConverterProvider {
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public <T> ParamConverter<T> getConverter(Class<T> rawType, Type 
genericType, Annotation[] annotations) {
+
+        if (rawType.equals(LocalDateTime.class)) {
+            return (ParamConverter<T>) new LocalDateTimeConverter();
+        } else if (rawType.equals(LocalDate.class)) {
+            return (ParamConverter<T>) new LocalDateConverter();
+        } else if (rawType.equals(LocalTime.class)) {
+            return (ParamConverter<T>) new LocalTimeConverter();
+        } else if (rawType.equals(OffsetDateTime.class)) {
+            return (ParamConverter<T>) new OffsetDateTimeConverter();
+        } else if (rawType.equals(OffsetTime.class)) {
+            return (ParamConverter<T>) new OffsetTimeConverter();
+        } else if (rawType.equals(ZonedDateTime.class)) {
+            return (ParamConverter<T>) new ZonedDateTimeConverter();
+        } else {
+            return null;
+        }
+    }
+
+    public class LocalDateTimeConverter implements 
ParamConverter<LocalDateTime> {
+        @Override
+        public LocalDateTime fromString(String value) {
+            try {
+                return LocalDateTime.parse(value, getFormatter());
+            } catch (DateTimeParseException parseException) {
+                throw new IllegalArgumentException(parseException);
+            }
+        }
+
+        @Override
+        public String toString(LocalDateTime localDateTime) {
+            return getFormatter().format(localDateTime);
+        }
+
+        protected DateTimeFormatter getFormatter() {
+            return DateTimeFormatter.ISO_LOCAL_DATE_TIME;
+        }
+    }
+
+    public class LocalDateConverter implements ParamConverter<LocalDate> {
+        @Override
+        public LocalDate fromString(String value) {
+            try {
+                return LocalDate.parse(value, getFormatter());
+            } catch (DateTimeParseException parseException) {
+                throw new IllegalArgumentException(parseException);
+            }
+        }
+
+        @Override
+        public String toString(LocalDate localDate) {
+            return getFormatter().format(localDate);
+        }
+
+        protected DateTimeFormatter getFormatter() {
+            return DateTimeFormatter.ISO_LOCAL_DATE;
+        }
+    }
+
+    public class LocalTimeConverter implements ParamConverter<LocalTime> {
+        @Override
+        public LocalTime fromString(String value) {
+            try {
+                return LocalTime.parse(value, getFormatter());
+            } catch (DateTimeParseException parseException) {
+                throw new IllegalArgumentException(parseException);
+            }
+        }
+
+        @Override
+        public String toString(LocalTime localTime) {
+            return getFormatter().format(localTime);
+        }
+
+        protected DateTimeFormatter getFormatter() {
+            return DateTimeFormatter.ISO_LOCAL_TIME;
+        }
+    }
+
+    public class OffsetDateTimeConverter implements 
ParamConverter<OffsetDateTime> {
+        @Override
+        public OffsetDateTime fromString(String value) {
+            try {
+                return OffsetDateTime.parse(value, getFormatter());
+            } catch (DateTimeParseException parseException) {
+                throw new IllegalArgumentException(parseException);
+            }
+        }
+
+        @Override
+        public String toString(OffsetDateTime offsetDateTime) {
+            return getFormatter().format(offsetDateTime);
+        }
+
+        protected DateTimeFormatter getFormatter() {
+            return DateTimeFormatter.ISO_OFFSET_DATE_TIME;
+        }
+    }
+
+    public class OffsetTimeConverter implements ParamConverter<OffsetTime> {
+        @Override
+        public OffsetTime fromString(String value) {
+            try {
+                return OffsetTime.parse(value, getFormatter());
+            } catch (DateTimeParseException parseException) {
+                throw new IllegalArgumentException(parseException);
+            }
+        }
+
+        @Override
+        public String toString(OffsetTime offsetTime) {
+            return getFormatter().format(offsetTime);
+        }
+
+        protected DateTimeFormatter getFormatter() {
+            return DateTimeFormatter.ISO_OFFSET_TIME;
+        }
+    }
+
+    public class ZonedDateTimeConverter implements 
ParamConverter<ZonedDateTime> {
+        @Override
+        public ZonedDateTime fromString(String value) {
+            try {
+                return ZonedDateTime.parse(value, getFormatter());
+            } catch (DateTimeParseException parseException) {
+                throw new IllegalArgumentException(parseException);
+            }
+        }
+
+        @Override
+        public String toString(ZonedDateTime zonedDateTime) {
+            return getFormatter().format(zonedDateTime);
+        }
+
+        protected DateTimeFormatter getFormatter() {
+            return DateTimeFormatter.ISO_ZONED_DATE_TIME;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/84d71635/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JavaTimeTypesParamConverterProviderTest.java
----------------------------------------------------------------------
diff --git 
a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JavaTimeTypesParamConverterProviderTest.java
 
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JavaTimeTypesParamConverterProviderTest.java
new file mode 100644
index 0000000..6b4f4df
--- /dev/null
+++ 
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JavaTimeTypesParamConverterProviderTest.java
@@ -0,0 +1,99 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.jaxrs.provider;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.OffsetDateTime;
+import java.time.OffsetTime;
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
+import javax.ws.rs.ext.ParamConverter;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+@SuppressWarnings("unchecked")
+public class JavaTimeTypesParamConverterProviderTest {
+
+    JavaTimeTypesParamConverterProvider provider = new 
JavaTimeTypesParamConverterProvider();
+
+    @Test
+    public void localDate() {
+        LocalDate localDate = LocalDate.of(2016, 2, 24);
+        ParamConverter<LocalDate> converter = (ParamConverter<LocalDate>)
+                provider.getConverter(localDate.getClass(), 
localDate.getClass(),
+                        localDate.getClass().getAnnotations());
+        Assert.assertEquals(localDate, 
converter.fromString(converter.toString(localDate)));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void invalidLocalDate() {
+        ParamConverter<LocalDate> converter =
+                provider.getConverter(LocalDate.class, LocalDate.class, null);
+        converter.fromString("invalid");
+    }
+
+    @Test
+    public void localDateTime() {
+        LocalDateTime localDateTime = LocalDateTime.of(2016, 2, 24, 5, 55);
+        ParamConverter<LocalDateTime> converter = 
(ParamConverter<LocalDateTime>)
+                provider.getConverter(localDateTime.getClass(), 
localDateTime.getClass(),
+                        localDateTime.getClass().getAnnotations());
+        Assert.assertEquals(localDateTime, 
converter.fromString(converter.toString(localDateTime)));
+    }
+
+    @Test
+    public void localTime() {
+        LocalTime localTime = LocalTime.of(10, 33);
+        ParamConverter<LocalTime> converter = (ParamConverter<LocalTime>)
+                provider.getConverter(localTime.getClass(), 
localTime.getClass(),
+                        localTime.getClass().getAnnotations());
+        Assert.assertEquals(localTime, 
converter.fromString(converter.toString(localTime)));
+    }
+
+    @Test
+    public void offsetDateTime() {
+        OffsetDateTime offsetDateTime = OffsetDateTime.of(2016, 2, 24, 5, 55, 
0, 0, ZoneOffset.UTC);
+        ParamConverter<OffsetDateTime> converter = 
(ParamConverter<OffsetDateTime>)
+                provider.getConverter(offsetDateTime.getClass(), 
offsetDateTime.getClass(),
+                        offsetDateTime.getClass().getAnnotations());
+        Assert.assertEquals(offsetDateTime, 
converter.fromString(converter.toString(offsetDateTime)));
+    }
+
+    @Test
+    public void offsetTime() {
+        OffsetTime offsetTime = OffsetTime.of(10, 33, 24, 5, 
ZoneOffset.ofHours(2));
+        ParamConverter<OffsetTime> converter = (ParamConverter<OffsetTime>)
+                provider.getConverter(offsetTime.getClass(), 
offsetTime.getClass(),
+                        offsetTime.getClass().getAnnotations());
+        Assert.assertEquals(offsetTime, 
converter.fromString(converter.toString(offsetTime)));
+    }
+
+    @Test
+    public void zonedDateTime() {
+        ZonedDateTime zonedDateTime = ZonedDateTime.of(2016, 2, 24, 6, 10, 9, 
5, ZoneOffset.ofHours(6));
+        ParamConverter<ZonedDateTime> converter = 
(ParamConverter<ZonedDateTime>)
+                provider.getConverter(zonedDateTime.getClass(), 
zonedDateTime.getClass(),
+                        zonedDateTime.getClass().getAnnotations());
+        Assert.assertEquals(zonedDateTime, 
converter.fromString(converter.toString(zonedDateTime)));
+    }
+}

Reply via email to