cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [2/3] git commit: updated refs/heads/master to 9c7e81e
Date Mon, 17 Aug 2015 11:56:58 GMT
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 <rajanikaruturi@gmail.com>
Authored: Thu Aug 13 17:52:51 2015 +0530
Committer: Rajani Karuturi <rajanikaruturi@gmail.com>
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<Long> 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


Mime
View raw message