Repository: cxf Updated Branches: refs/heads/master daac77b18 -> c1a43f2e6
[CXF-6250] WebSocket conduit fails to process String based responses and throws NPE Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/c1a43f2e Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/c1a43f2e Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/c1a43f2e Branch: refs/heads/master Commit: c1a43f2e6593c7dbf3b7a40e6533e37fee2ef9a9 Parents: daac77b Author: Akitoshi Yoshida <a...@apache.org> Authored: Tue Feb 10 17:37:59 2015 +0100 Committer: Akitoshi Yoshida <a...@apache.org> Committed: Tue Feb 10 18:46:47 2015 +0100 ---------------------------------------------------------------------- .../websocket/ahc/AhcWebSocketConduit.java | 10 ++- .../websocket/ahc/AhcWebSocketConduitTest.java | 72 ++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/c1a43f2e/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/ahc/AhcWebSocketConduit.java ---------------------------------------------------------------------- diff --git a/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/ahc/AhcWebSocketConduit.java b/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/ahc/AhcWebSocketConduit.java index d6ef890..897b0d1 100644 --- a/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/ahc/AhcWebSocketConduit.java +++ b/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/ahc/AhcWebSocketConduit.java @@ -447,7 +447,15 @@ public class AhcWebSocketConduit extends URLConnectionHTTPConduit { } private int length(Object o) { - return o instanceof char[] ? ((String)o).length() : (o instanceof byte[] ? ((byte[])o).length : 0); + if (o instanceof String) { + return ((String)o).length(); + } else if (o instanceof char[]) { + return ((char[])o).length; + } else if (o instanceof byte[]) { + return ((byte[])o).length; + } else { + return 0; + } } private int getchar(Object o, int p) { http://git-wip-us.apache.org/repos/asf/cxf/blob/c1a43f2e/rt/transports/websocket/src/test/java/org/apache/cxf/transport/websocket/ahc/AhcWebSocketConduitTest.java ---------------------------------------------------------------------- diff --git a/rt/transports/websocket/src/test/java/org/apache/cxf/transport/websocket/ahc/AhcWebSocketConduitTest.java b/rt/transports/websocket/src/test/java/org/apache/cxf/transport/websocket/ahc/AhcWebSocketConduitTest.java new file mode 100644 index 0000000..5508fbd --- /dev/null +++ b/rt/transports/websocket/src/test/java/org/apache/cxf/transport/websocket/ahc/AhcWebSocketConduitTest.java @@ -0,0 +1,72 @@ +/** + * 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.transport.websocket.ahc; + +import org.apache.cxf.transport.websocket.WebSocketConstants; + +import org.junit.Assert; +import org.junit.Test; + +/** + * + */ +public class AhcWebSocketConduitTest extends Assert { + private static final String TEST_RESPONSE1 = + "200\r\nresponseId: 59610eed-d9de-4692-96d4-bb95a36c41ea\r\nContent-Type: text/plain\r\n\r\nHola!"; + private static final String TEST_RESPONSE2 = + "responseId: 59610eed-d9de-4692-96d4-bb95a36c41ea\r\n\r\nNada!"; + + @Test + public void testResponseParsing() throws Exception { + + // with all the headers using type string + AhcWebSocketConduit.Response resp = + new AhcWebSocketConduit.Response(WebSocketConstants.DEFAULT_RESPONSE_ID_KEY, TEST_RESPONSE1); + assertEquals(200, resp.getStatusCode()); + assertEquals("59610eed-d9de-4692-96d4-bb95a36c41ea", resp.getId()); + assertEquals("text/plain", resp.getContentType()); + assertTrue(resp.getEntity() instanceof String); + assertEquals("Hola!", resp.getEntity()); + + // with all the heaers using type byte[] + resp = new AhcWebSocketConduit.Response(WebSocketConstants.DEFAULT_RESPONSE_ID_KEY, TEST_RESPONSE1.getBytes()); + assertEquals(200, resp.getStatusCode()); + assertEquals("59610eed-d9de-4692-96d4-bb95a36c41ea", resp.getId()); + assertEquals("text/plain", resp.getContentType()); + assertTrue(resp.getEntity() instanceof byte[]); + assertEquals("Hola!", resp.getTextEntity()); + + // with only the id header using type String + resp = new AhcWebSocketConduit.Response(WebSocketConstants.DEFAULT_RESPONSE_ID_KEY, TEST_RESPONSE2); + assertEquals(0, resp.getStatusCode()); + assertEquals("59610eed-d9de-4692-96d4-bb95a36c41ea", resp.getId()); + assertNull(resp.getContentType()); + assertTrue(resp.getEntity() instanceof String); + assertEquals("Nada!", resp.getEntity()); + + // with only the id header using type byte[] + resp = new AhcWebSocketConduit.Response(WebSocketConstants.DEFAULT_RESPONSE_ID_KEY, TEST_RESPONSE2.getBytes()); + assertEquals(0, resp.getStatusCode()); + assertEquals("59610eed-d9de-4692-96d4-bb95a36c41ea", resp.getId()); + assertNull(resp.getContentType()); + assertTrue(resp.getEntity() instanceof byte[]); + assertEquals("Nada!", resp.getTextEntity()); + } +}