Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 971E710A3D for ; Tue, 10 Feb 2015 18:00:06 +0000 (UTC) Received: (qmail 75473 invoked by uid 500); 10 Feb 2015 18:00:01 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 75415 invoked by uid 500); 10 Feb 2015 18:00:01 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 75405 invoked by uid 99); 10 Feb 2015 18:00:01 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 10 Feb 2015 18:00:01 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id EEF64E0159; Tue, 10 Feb 2015 18:00:00 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ay@apache.org To: commits@cxf.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: cxf git commit: [CXF-6250] WebSocket conduit fails to process String based responses and throws NPE Date: Tue, 10 Feb 2015 18:00:00 +0000 (UTC) Repository: cxf Updated Branches: refs/heads/3.0.x-fixes 941e65f73 -> 01c77bd95 [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/01c77bd9 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/01c77bd9 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/01c77bd9 Branch: refs/heads/3.0.x-fixes Commit: 01c77bd95a0b302b98f5dc8dc649d78d78afb56f Parents: 941e65f Author: Akitoshi Yoshida Authored: Tue Feb 10 17:37:59 2015 +0100 Committer: Akitoshi Yoshida Committed: Tue Feb 10 18:55:20 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/01c77bd9/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/01c77bd9/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()); + } +}