Skip to content

Commit c180a6d

Browse files
committed
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 f03411c
1 parent a65339f commit c180a6d

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

‎vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.cloud.hypervisor.vmware.mo.DatacenterMO;
2020
import com.cloud.hypervisor.vmware.mo.DatastoreFile;
2121
import com.cloud.utils.ActionDelegate;
22+
import com.cloud.utils.StringUtils;
2223
import com.vmware.vim25.ManagedObjectReference;
2324
import com.vmware.vim25.ObjectContent;
2425
import com.vmware.vim25.ObjectSpec;
@@ -50,6 +51,7 @@
5051
import java.net.HttpURLConnection;
5152
import java.net.URL;
5253
import java.net.URLEncoder;
54+
import java.nio.charset.Charset;
5355
import java.util.ArrayList;
5456
import java.util.HashMap;
5557
import java.util.List;
@@ -357,7 +359,7 @@ public void uploadFile(String urlString, File localFile) throws Exception {
357359
}
358360
out.flush();
359361

360-
br = new BufferedReader(new InputStreamReader(conn.getInputStream(),conn.getContentEncoding()));
362+
br = new BufferedReader(new InputStreamReader(conn.getInputStream(), getCharSetFromConnection(conn)));
361363
String line;
362364
while ((line = br.readLine()) != null) {
363365
if (s_logger.isTraceEnabled())
@@ -375,6 +377,18 @@ public void uploadFile(String urlString, File localFile) throws Exception {
375377
}
376378
}
377379

380+
private Charset getCharSetFromConnection(HttpURLConnection conn) {
381+
String charsetName = conn.getContentEncoding();
382+
Charset charset;
383+
try {
384+
charset = Charset.forName(charsetName);
385+
} catch (IllegalArgumentException e) {
386+
s_logger.warn("Illegal/unsupported/null charset name from connection. charsetname from connection is " + charsetName);
387+
charset = StringUtils.getPreferredCharset();
388+
}
389+
return charset;
390+
}
391+
378392
public void uploadVmdkFile(String httpMethod, String urlString, String localFileName, long totalBytesUpdated, ActionDelegate<Long> progressUpdater) throws Exception {
379393

380394
HttpURLConnection conn = getRawHTTPConnection(urlString);
@@ -483,9 +497,9 @@ public void uploadResourceContent(String urlString, byte[] content) throws Excep
483497
out.write(content);
484498
out.flush();
485499

486-
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),conn.getContentEncoding()));
500+
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), getCharSetFromConnection(conn)));
487501
String line;
488-
while ((line = in.readLine()) != null) {
502+
while ((in.ready()) && (line = in.readLine()) != null) {
489503
if (s_logger.isTraceEnabled())
490504
s_logger.trace("Upload " + urlString + " response: " + line);
491505
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.cloud.hypervisor.vmware.util;
21+
22+
import java.io.InputStream;
23+
import java.io.OutputStream;
24+
import java.net.HttpURLConnection;
25+
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.mockito.Mockito;
29+
import org.mockito.runners.MockitoJUnitRunner;
30+
31+
@RunWith(MockitoJUnitRunner.class)
32+
public class VmwareContextTest {
33+
34+
@Test
35+
public void testUploadResourceContentCharsetException() throws Exception {
36+
VmwareClient client = Mockito.mock(VmwareClient.class);
37+
String address = "10.1.1.1";
38+
VmwareContext vmwareContext = Mockito.spy(new VmwareContext(client, address));
39+
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
40+
Mockito.doReturn(Mockito.mock(OutputStream.class)).when(conn).getOutputStream();
41+
Mockito.doReturn(Mockito.mock(InputStream.class)).when(conn).getInputStream();
42+
Mockito.doReturn(conn).when(vmwareContext).getHTTPConnection("http://example.com", "PUT");
43+
//This method should not throw any exception. Ref: CLOUDSTACK-8669
44+
vmwareContext.uploadResourceContent("http://example.com", "content".getBytes());
45+
}
46+
47+
}

0 commit comments

Comments
 (0)