Author: dkulp
Date: Thu Sep 16 19:46:08 2010
New Revision: 997893
URL: http://svn.apache.org/viewvc?rev=997893&view=rev
Log:
[CXF-2996] Fix GET requests for enums, dates, and nulls
Patch from Christian Hvid applied.
Added:
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/JavaFirstHttpGetTest.java
(with props)
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyEnum.java
(with props)
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyImplementation.java
(with props)
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyInterface.java
(with props)
Modified:
cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/URIMappingInterceptor.java
Modified:
cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/URIMappingInterceptor.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/URIMappingInterceptor.java?rev=997893&r1=997892&r2=997893&view=diff
==============================================================================
---
cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/URIMappingInterceptor.java
(original)
+++
cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/URIMappingInterceptor.java
Thu Sep 16 19:46:08 2010
@@ -20,10 +20,15 @@
package org.apache.cxf.interceptor;
import java.io.UnsupportedEncodingException;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
import java.util.Arrays;
+import java.util.Calendar;
import java.util.Collection;
+import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
@@ -231,9 +236,40 @@ public class URIMappingInterceptor exten
}
return parameters;
}
+
+ private Date parseDate(String value, Class<?> type) {
+ SimpleDateFormat sdf;
+
+ if (value.length() == 10) {
+ sdf = new SimpleDateFormat("yyyy-MM-dd");
+ } else if (value.length() == 19) {
+ sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+ } else if (value.length() == 23) {
+ sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
+ } else if (value.length() == 25) {
+ value = value.substring(0, value.length() - 3)
+ + value.substring(value.length() - 2, value.length());
+ sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
+ } else if (value.length() == 29) {
+ value = value.substring(0, value.length() - 3)
+ + value.substring(value.length() - 2, value.length());
+ sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ");
+ } else {
+ throw new RuntimeException("Unable to create " + type + " out of
'" + value + "'");
+ }
+ try {
+ return sdf.parse(value);
+ } catch (ParseException e) {
+ throw new RuntimeException("Unable to create " + type + " out of
'" + value + "'");
+ }
+ }
+
private Object readType(String value, Class<?> type) {
Object ret = value;
- if (Integer.class == type) {
+
+ if (value == null) {
+ // let null be null regardless of target type
+ } else if (Integer.class == type) {
ret = Integer.valueOf(value);
} else if (Byte.class == type) {
ret = Byte.valueOf(value);
@@ -248,7 +284,22 @@ public class URIMappingInterceptor exten
} else if (Boolean.class == type) {
ret = Boolean.valueOf(value);
} else if (Character.class == type) {
- ret = value.charAt(0);
+ ret = value.charAt(0);
+ } else if (type != null && type.isEnum()) {
+ try {
+ ret = type.getMethod("valueOf", String.class).invoke(null,
value);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException("Unable to create " + type + " out
of '" + value + "'");
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException("Unable to create " + type + " out
of '" + value + "'");
+ } catch (NoSuchMethodException e) {
+ throw new RuntimeException("Unable to create " + type + " out
of '" + value + "'");
+ }
+ } else if (java.util.Date.class == type) {
+ ret = parseDate(value, type);
+ } else if (Calendar.class == type) {
+ ret = Calendar.getInstance();
+ ((Calendar)ret).setTime(parseDate(value, type));
}
return ret;
}
Added:
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/JavaFirstHttpGetTest.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/JavaFirstHttpGetTest.java?rev=997893&view=auto
==============================================================================
---
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/JavaFirstHttpGetTest.java
(added)
+++
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/JavaFirstHttpGetTest.java
Thu Sep 16 19:46:08 2010
@@ -0,0 +1,110 @@
+/**
+ * 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.systest.jaxws.httpget;
+
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.interceptor.LoggingInInterceptor;
+import org.apache.cxf.interceptor.LoggingOutInterceptor;
+import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.apache.cxf.testutil.common.TestUtil;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+
+public class JavaFirstHttpGetTest extends AbstractBusClientServerTestBase {
+ private static final String PORT = TestUtil.getPortNumber(Server.class);
+ private static final String BASE_URL = "http://localhost:"
+ + PORT + "/JavaFirstHttpGetTest";
+
+ public static class Server extends AbstractBusTestServerBase {
+ protected void run() {
+ MyImplementation implementor = new MyImplementation();
+ JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
+ svrFactory.setServiceClass(MyInterface.class);
+ svrFactory.setAddress(BASE_URL);
+ svrFactory.setServiceBean(implementor);
+ svrFactory.getInInterceptors().add(new LoggingInInterceptor());
+ svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
+ svrFactory.create();
+ }
+
+ public static void main(String[] args) {
+ try {
+ Server s = new Server();
+ s.start();
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ System.exit(-1);
+ } finally {
+ System.out.println("done!");
+ }
+ }
+ }
+
+
+ @BeforeClass
+ public static void startServers() throws Exception {
+ assertTrue("server did not launch correctly",
launchServer(Server.class));
+ }
+
+ @Test
+ public void testDate() throws Exception {
+ URLConnection urlCon = new URL(BASE_URL +
"/test2?date=2010-09-13T14:23:30.879%2B02:00")
+ .openConnection();
+ InputStream ins = urlCon.getInputStream();
+ String ret = IOUtils.toString(ins);
+ assertTrue(!ret.contains("Fault"));
+ assertTrue(ret.contains("2010"));
+ }
+ @Test
+ public void testEnum() throws Exception {
+ URLConnection urlCon = new URL(BASE_URL + "/test4?myEnum=A")
+ .openConnection();
+ InputStream ins = urlCon.getInputStream();
+ String ret = IOUtils.toString(ins);
+ assertTrue(ret, !ret.contains("Fault"));
+ assertTrue(ret, ret.contains("A"));
+ }
+ @Test
+ public void testNull() throws Exception {
+ URLConnection urlCon = new URL(BASE_URL + "/test7")
+ .openConnection();
+ InputStream ins = urlCon.getInputStream();
+ String ret = IOUtils.toString(ins);
+ assertTrue(ret, ret.contains("<null"));
+ }
+ @Test
+ public void testNullPrimitive() throws Exception {
+ HttpURLConnection urlCon = (HttpURLConnection)(new URL(BASE_URL +
"/test8")
+ .openConnection());
+ assertEquals(500, urlCon.getResponseCode()); //FAULT
+ InputStream ins = urlCon.getErrorStream();
+ String ret = IOUtils.toString(ins);
+ assertTrue(ret, ret.contains("Fault"));
+ }
+
+}
\ No newline at end of file
Propchange:
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/JavaFirstHttpGetTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/JavaFirstHttpGetTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added:
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyEnum.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyEnum.java?rev=997893&view=auto
==============================================================================
---
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyEnum.java
(added)
+++
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyEnum.java
Thu Sep 16 19:46:08 2010
@@ -0,0 +1,23 @@
+/**
+ * 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.systest.jaxws.httpget;
+
+public enum MyEnum {
+ A, B, C
+}
\ No newline at end of file
Propchange:
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyEnum.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyEnum.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added:
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyImplementation.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyImplementation.java?rev=997893&view=auto
==============================================================================
---
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyImplementation.java
(added)
+++
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyImplementation.java
Thu Sep 16 19:46:08 2010
@@ -0,0 +1,74 @@
+/**
+ * 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.systest.jaxws.httpget;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import javax.jws.WebParam;
+
+
+public class MyImplementation implements MyInterface {
+ public Date test1() {
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+ try {
+ return sdf.parse("1973-10-20");
+ } catch (ParseException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public String test2(Date date) {
+ return date.toString();
+ }
+
+ public MyEnum test3() {
+ return MyEnum.A;
+ }
+
+ public String test4(@WebParam(name = "myEnum")MyEnum myEnum) {
+ return myEnum.toString();
+ }
+
+ public Calendar test5() {
+ Calendar c = Calendar.getInstance();
+
+ c.clear();
+ c.set(23, 1, 3);
+
+ return c;
+ }
+
+ public String test6(Calendar calendar) {
+ System.out.println("" + calendar.getTime());
+ return calendar.getTime().toString();
+ }
+
+ public String test7(@WebParam(name = "d")Double d) {
+ System.out.println("d is " + d);
+ return d == null ? "<null>" : d.toString();
+ }
+
+ public String test8(@WebParam(name = "d")double d) {
+ return "" + d;
+ }
+
+
+}
\ No newline at end of file
Propchange:
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyImplementation.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyImplementation.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added:
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyInterface.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyInterface.java?rev=997893&view=auto
==============================================================================
---
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyInterface.java
(added)
+++
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyInterface.java
Thu Sep 16 19:46:08 2010
@@ -0,0 +1,44 @@
+/**
+ * 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.systest.jaxws.httpget;
+
+import java.util.Calendar;
+import java.util.Date;
+import javax.jws.WebParam;
+import javax.jws.WebService;
+
+
+...@webservice()
+public interface MyInterface {
+ Date test1();
+
+ String test2(@WebParam(name = "date")Date date);
+
+ MyEnum test3();
+
+ String test4(@WebParam(name = "myEnum")MyEnum myEnum);
+
+ Calendar test5();
+
+ String test6(@WebParam(name = "calendar")Calendar calendar);
+
+ String test7(@WebParam(name = "d")Double d);
+
+ String test8(@WebParam(name = "d")double d);
+}
\ No newline at end of file
Propchange:
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyInterface.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/httpget/MyInterface.java
------------------------------------------------------------------------------
svn:keywords = Rev Date