Return-Path: X-Original-To: apmail-cloudstack-commits-archive@www.apache.org Delivered-To: apmail-cloudstack-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 8EDE9186FC for ; Mon, 17 Aug 2015 11:56:57 +0000 (UTC) Received: (qmail 13212 invoked by uid 500); 17 Aug 2015 11:56:57 -0000 Delivered-To: apmail-cloudstack-commits-archive@cloudstack.apache.org Received: (qmail 13180 invoked by uid 500); 17 Aug 2015 11:56:57 -0000 Mailing-List: contact commits-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list commits@cloudstack.apache.org Received: (qmail 13165 invoked by uid 99); 17 Aug 2015 11:56:57 -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; Mon, 17 Aug 2015 11:56:57 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 3E4E9E01DD; Mon, 17 Aug 2015 11:56:57 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: remi@apache.org To: commits@cloudstack.apache.org Date: Mon, 17 Aug 2015 11:56:58 -0000 Message-Id: <177184a3a897452f8c78d22a19863267@git.apache.org> In-Reply-To: <87d092ff256146f8ac4c272d20241760@git.apache.org> References: <87d092ff256146f8ac4c272d20241760@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/3] git commit: updated refs/heads/master to 9c7e81e CLOUDSTACK-8669: create volume failed due to null charset Added a new private method getCharSetFromConnection() which checks if the connection charset is null and if it is null, returns StringUtils.getPreferredCharset regression caused by commit f03411ca0436c8b52f5e60b0c8820fd1d1ba2ff6 Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/c180a6db Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/c180a6db Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/c180a6db Branch: refs/heads/master Commit: c180a6db0326dd460e7aa64a4085eac52aa74fb0 Parents: a65339f Author: Rajani Karuturi Authored: Thu Aug 13 17:52:51 2015 +0530 Committer: Rajani Karuturi Committed: Mon Aug 17 10:20:28 2015 +0530 ---------------------------------------------------------------------- .../hypervisor/vmware/util/VmwareContext.java | 20 +++++++-- .../vmware/util/VmwareContextTest.java | 47 ++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c180a6db/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java ---------------------------------------------------------------------- diff --git a/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java b/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java index d4766dc..daf29ea 100644 --- a/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java @@ -19,6 +19,7 @@ package com.cloud.hypervisor.vmware.util; import com.cloud.hypervisor.vmware.mo.DatacenterMO; import com.cloud.hypervisor.vmware.mo.DatastoreFile; import com.cloud.utils.ActionDelegate; +import com.cloud.utils.StringUtils; import com.vmware.vim25.ManagedObjectReference; import com.vmware.vim25.ObjectContent; import com.vmware.vim25.ObjectSpec; @@ -50,6 +51,7 @@ import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -357,7 +359,7 @@ public class VmwareContext { } out.flush(); - br = new BufferedReader(new InputStreamReader(conn.getInputStream(),conn.getContentEncoding())); + br = new BufferedReader(new InputStreamReader(conn.getInputStream(), getCharSetFromConnection(conn))); String line; while ((line = br.readLine()) != null) { if (s_logger.isTraceEnabled()) @@ -375,6 +377,18 @@ public class VmwareContext { } } + private Charset getCharSetFromConnection(HttpURLConnection conn) { + String charsetName = conn.getContentEncoding(); + Charset charset; + try { + charset = Charset.forName(charsetName); + } catch (IllegalArgumentException e) { + s_logger.warn("Illegal/unsupported/null charset name from connection. charsetname from connection is " + charsetName); + charset = StringUtils.getPreferredCharset(); + } + return charset; + } + public void uploadVmdkFile(String httpMethod, String urlString, String localFileName, long totalBytesUpdated, ActionDelegate progressUpdater) throws Exception { HttpURLConnection conn = getRawHTTPConnection(urlString); @@ -483,9 +497,9 @@ public class VmwareContext { out.write(content); out.flush(); - BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),conn.getContentEncoding())); + BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), getCharSetFromConnection(conn))); String line; - while ((line = in.readLine()) != null) { + while ((in.ready()) && (line = in.readLine()) != null) { if (s_logger.isTraceEnabled()) s_logger.trace("Upload " + urlString + " response: " + line); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c180a6db/vmware-base/test/com/cloud/hypervisor/vmware/util/VmwareContextTest.java ---------------------------------------------------------------------- diff --git a/vmware-base/test/com/cloud/hypervisor/vmware/util/VmwareContextTest.java b/vmware-base/test/com/cloud/hypervisor/vmware/util/VmwareContextTest.java new file mode 100644 index 0000000..4a768e6 --- /dev/null +++ b/vmware-base/test/com/cloud/hypervisor/vmware/util/VmwareContextTest.java @@ -0,0 +1,47 @@ +/* + * 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 com.cloud.hypervisor.vmware.util; + +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class VmwareContextTest { + + @Test + public void testUploadResourceContentCharsetException() throws Exception { + VmwareClient client = Mockito.mock(VmwareClient.class); + String address = "10.1.1.1"; + VmwareContext vmwareContext = Mockito.spy(new VmwareContext(client, address)); + HttpURLConnection conn = Mockito.mock(HttpURLConnection.class); + Mockito.doReturn(Mockito.mock(OutputStream.class)).when(conn).getOutputStream(); + Mockito.doReturn(Mockito.mock(InputStream.class)).when(conn).getInputStream(); + Mockito.doReturn(conn).when(vmwareContext).getHTTPConnection("http://example.com", "PUT"); + //This method should not throw any exception. Ref: CLOUDSTACK-8669 + vmwareContext.uploadResourceContent("http://example.com", "content".getBytes()); + } + +} \ No newline at end of file