[CXF-6356] Adding a Date header delegate, patch from Iris Ding applied
Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/1731258d Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/1731258d Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/1731258d Branch: refs/heads/3.0.x-fixes Commit: 1731258de6957803540284199f3a4b9863e19d0d Parents: f3eaa61 Author: Sergey Beryozkin <[email protected]> Authored: Mon Apr 20 12:35:57 2015 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Mon Apr 20 12:39:25 2015 +0100 ---------------------------------------------------------------------- .../cxf/jaxrs/impl/DateHeaderProvider.java | 43 ++++++++++++++++++++ .../cxf/jaxrs/impl/RuntimeDelegateImpl.java | 2 + .../org/apache/cxf/jaxrs/utils/HttpUtils.java | 2 +- .../cxf/jaxrs/impl/DateHeaderProviderTest.java | 43 ++++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/1731258d/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/DateHeaderProvider.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/DateHeaderProvider.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/DateHeaderProvider.java new file mode 100644 index 0000000..2d2e046 --- /dev/null +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/DateHeaderProvider.java @@ -0,0 +1,43 @@ +/** + * 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.impl; + +import java.util.Date; + +import javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate; + +import org.apache.cxf.jaxrs.utils.HttpUtils; + +public class DateHeaderProvider implements HeaderDelegate<Date> { + + public Date fromString(String value) { + + if (value == null) { + throw new IllegalArgumentException("Date value can not be null"); + } + return HttpUtils.getHttpDate(value); + } + + public String toString(Date date) { + return HttpUtils.toHttpDate(date); + } + + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/1731258d/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RuntimeDelegateImpl.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RuntimeDelegateImpl.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RuntimeDelegateImpl.java index aadd3b2..dcd5946 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RuntimeDelegateImpl.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RuntimeDelegateImpl.java @@ -19,6 +19,7 @@ package org.apache.cxf.jaxrs.impl; +import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -51,6 +52,7 @@ public class RuntimeDelegateImpl extends RuntimeDelegate { headerProviders.put(Cookie.class, new CookieHeaderProvider()); headerProviders.put(NewCookie.class, new NewCookieHeaderProvider()); headerProviders.put(Link.class, new LinkHeaderProvider()); + headerProviders.put(Date.class, new DateHeaderProvider()); } http://git-wip-us.apache.org/repos/asf/cxf/blob/1731258d/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java index 5cb02dd..76c66e9 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java @@ -200,7 +200,7 @@ public final class HttpUtils { } public static HeaderDelegate<Object> getHeaderDelegate(Object o) { - return getHeaderDelegate(getOtherRuntimeDelegate(), o); + return getHeaderDelegate(RuntimeDelegate.getInstance(), o); } @SuppressWarnings("unchecked") http://git-wip-us.apache.org/repos/asf/cxf/blob/1731258d/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/DateHeaderProviderTest.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/DateHeaderProviderTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/DateHeaderProviderTest.java new file mode 100644 index 0000000..6384f7a --- /dev/null +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/DateHeaderProviderTest.java @@ -0,0 +1,43 @@ +/** + * 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.impl; + +import java.util.Date; + +import javax.ws.rs.ServiceUnavailableException; + +import org.apache.cxf.jaxrs.utils.HttpUtils; + +import org.junit.Assert; +import org.junit.Test; + +public class DateHeaderProviderTest extends Assert { + + @Test + public void testToFromSimpleString() { + Date retry = new Date(); + ServiceUnavailableException ex = new ServiceUnavailableException(retry); + Date retry2 = ex.getRetryTime(new Date()); + assertEquals(HttpUtils.toHttpDate(retry), HttpUtils.toHttpDate(retry2)); + } + + + +}
