Skip to content

Commit 71929fd

Browse files
author
Boris Schrijver
committed
Replaced all occurences of Charset.forName(UTF-8) with StringUtils.getPreferredCharset().
1 parent 2bd61c3 commit 71929fd

File tree

8 files changed

+67
-78
lines changed

8 files changed

+67
-78
lines changed

plugins/hypervisors/hyperv/src/com/cloud/hypervisor/hyperv/discoverer/HypervServerDiscoverer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.net.InetAddress;
2020
import java.net.URI;
2121
import java.net.UnknownHostException;
22-
import java.nio.charset.Charset;
2322
import java.util.HashMap;
2423
import java.util.List;
2524
import java.util.Map;
@@ -43,8 +42,8 @@
4342
import com.cloud.agent.api.SetupCommand;
4443
import com.cloud.agent.api.StartupCommand;
4544
import com.cloud.agent.api.StartupRoutingCommand;
46-
import com.cloud.configuration.Config;
4745
import com.cloud.alert.AlertManager;
46+
import com.cloud.configuration.Config;
4847
import com.cloud.dc.ClusterVO;
4948
import com.cloud.dc.DataCenterVO;
5049
import com.cloud.dc.HostPodVO;
@@ -66,6 +65,7 @@
6665
import com.cloud.resource.ServerResource;
6766
import com.cloud.resource.UnableDeleteHostException;
6867
import com.cloud.storage.StorageLayer;
68+
import com.cloud.utils.StringUtils;
6969

7070
/**
7171
* Methods to discover and managem a Hyper-V agent. Prepares a
@@ -256,7 +256,7 @@ public final Map<? extends ServerResource, Map<String, String>> find(final long
256256
// pool in the database
257257
// This GUID may change.
258258
if (cluster.getGuid() == null) {
259-
cluster.setGuid(UUID.nameUUIDFromBytes(String.valueOf(clusterId).getBytes(Charset.forName("UTF-8"))).toString());
259+
cluster.setGuid(UUID.nameUUIDFromBytes(String.valueOf(clusterId).getBytes(StringUtils.getPreferredCharset())).toString());
260260
_clusterDao.update(clusterId, cluster);
261261
}
262262

@@ -322,7 +322,7 @@ public final Map<? extends ServerResource, Map<String, String>> find(final long
322322
* @return GUID in form of a string.
323323
*/
324324
public static String calcServerResourceGuid(final String uuidSeed) {
325-
String guid = UUID.nameUUIDFromBytes(uuidSeed.getBytes(Charset.forName("UTF-8"))).toString();
325+
String guid = UUID.nameUUIDFromBytes(uuidSeed.getBytes(StringUtils.getPreferredCharset())).toString();
326326
return guid;
327327
}
328328

plugins/hypervisors/hyperv/src/com/cloud/hypervisor/hyperv/resource/HypervDirectConnectResource.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.net.URISyntaxException;
2525
import java.net.URL;
2626
import java.nio.channels.SocketChannel;
27-
import java.nio.charset.Charset;
2827
import java.rmi.RemoteException;
2928
import java.security.KeyManagementException;
3029
import java.security.KeyStoreException;
@@ -43,6 +42,7 @@
4342
import javax.inject.Inject;
4443
import javax.naming.ConfigurationException;
4544

45+
import org.apache.cloudstack.storage.command.CopyCommand;
4646
import org.apache.commons.codec.binary.Base64;
4747
import org.apache.commons.lang.StringEscapeUtils;
4848
import org.apache.http.HttpResponse;
@@ -62,11 +62,6 @@
6262
import org.apache.http.util.EntityUtils;
6363
import org.apache.log4j.Logger;
6464

65-
import com.google.gson.Gson;
66-
import com.google.gson.reflect.TypeToken;
67-
68-
import org.apache.cloudstack.storage.command.CopyCommand;
69-
7065
import com.cloud.agent.api.Answer;
7166
import com.cloud.agent.api.CheckRouterAnswer;
7267
import com.cloud.agent.api.CheckRouterCommand;
@@ -156,6 +151,8 @@
156151
import com.cloud.vm.VirtualMachine;
157152
import com.cloud.vm.VirtualMachine.PowerState;
158153
import com.cloud.vm.VirtualMachineName;
154+
import com.google.gson.Gson;
155+
import com.google.gson.reflect.TypeToken;
159156

160157

161158
/**
@@ -653,7 +650,7 @@ public ExecutionResult executeInVR(String routerIP, String script, String args,
653650
public ExecutionResult createFileInVR(String routerIp, String filePath, String fileName, String content) {
654651
File keyFile = getSystemVMKeyFile();
655652
try {
656-
SshHelper.scpTo(routerIp, 3922, "root", keyFile, null, filePath, content.getBytes(Charset.forName("UTF-8")), fileName, null);
653+
SshHelper.scpTo(routerIp, 3922, "root", keyFile, null, filePath, content.getBytes(StringUtils.getPreferredCharset()), fileName, null);
657654
} catch (Exception e) {
658655
s_logger.warn("Fail to create file " + filePath + fileName + " in VR " + routerIp, e);
659656
return new ExecutionResult(false, e.getMessage());
@@ -1318,7 +1315,7 @@ protected Answer execute(final LoadBalancerConfigCommand cmd) {
13181315
}
13191316

13201317
try {
1321-
SshHelper.scpTo(controlIp, DEFAULT_DOMR_SSHPORT, "root", keyFile, null, "/tmp/", tmpCfgFileContents.toString().getBytes(Charset.forName("UTF-8")), routerIp.replace('.', '_') +
1318+
SshHelper.scpTo(controlIp, DEFAULT_DOMR_SSHPORT, "root", keyFile, null, "/tmp/", tmpCfgFileContents.toString().getBytes(StringUtils.getPreferredCharset()), routerIp.replace('.', '_') +
13221319
".cfg", null);
13231320

13241321
try {
@@ -1515,7 +1512,7 @@ protected Answer execute(VmDataCommand cmd) {
15151512
String json = new Gson().toJson(data);
15161513
s_logger.debug("VM data JSON IS:" + json);
15171514

1518-
json = Base64.encodeBase64String(json.getBytes(Charset.forName("UTF-8")));
1515+
json = Base64.encodeBase64String(json.getBytes(StringUtils.getPreferredCharset()));
15191516

15201517
String args = "-d " + json;
15211518

plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/BigSwitchBcfApi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.lang.reflect.Type;
2525
import java.net.MalformedURLException;
2626
import java.net.URL;
27-
import java.nio.charset.Charset;
2827
import java.util.ArrayList;
2928
import java.util.Collections;
3029
import java.util.List;
@@ -49,6 +48,7 @@
4948
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
5049
import org.apache.log4j.Logger;
5150

51+
import com.cloud.utils.StringUtils;
5252
import com.google.gson.Gson;
5353
import com.google.gson.reflect.TypeToken;
5454

@@ -301,7 +301,7 @@ private void setHttpHeader(final HttpMethodBase m) {
301301
}
302302

303303
String authString = username + ":" + password;
304-
String encodedAuthString = "Basic " + Base64.encodeBase64String(authString.getBytes(Charset.forName("UTF-8")));
304+
String encodedAuthString = "Basic " + Base64.encodeBase64String(authString.getBytes(StringUtils.getPreferredCharset()));
305305
m.setRequestHeader("Authorization", encodedAuthString);
306306
}
307307

plugins/network-elements/brocade-vcs/src/com/cloud/network/brocade/BrocadeVcsApi.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import java.io.StringWriter;
2424
import java.net.MalformedURLException;
2525
import java.net.URL;
26-
import java.nio.charset.Charset;
2726

27+
import javax.persistence.Basic;
2828
import javax.xml.bind.JAXBContext;
2929
import javax.xml.bind.JAXBException;
3030
import javax.xml.bind.Marshaller;
@@ -45,21 +45,9 @@
4545
import org.apache.http.impl.client.DefaultHttpClient;
4646
import org.apache.log4j.Logger;
4747

48-
import com.cloud.network.schema.interfacevlan.InterfaceVlan;
49-
import com.cloud.network.schema.interfacevlan.Interface;
50-
import com.cloud.network.schema.interfacevlan.Vlan;
51-
import com.cloud.network.schema.portprofile.PortProfile;
52-
import com.cloud.network.schema.portprofile.PortProfile.Activate;
53-
import com.cloud.network.schema.portprofile.PortProfile.Static;
54-
import com.cloud.network.schema.portprofile.PortProfileGlobal;
55-
import com.cloud.network.schema.portprofile.VlanProfile;
56-
import com.cloud.network.schema.portprofile.VlanProfile.Switchport;
57-
import com.cloud.network.schema.portprofile.VlanProfile.Switchport.Mode;
58-
import com.cloud.network.schema.portprofile.VlanProfile.Switchport.Trunk;
59-
import com.cloud.network.schema.portprofile.VlanProfile.Switchport.Trunk.Allowed;
60-
import com.cloud.network.schema.portprofile.VlanProfile.SwitchportBasic;
61-
import com.cloud.network.schema.portprofile.VlanProfile.SwitchportBasic.Basic;
62-
import com.cloud.network.schema.showvcs.Output;
48+
import com.cloud.dc.Vlan;
49+
import com.cloud.utils.StringUtils;
50+
import com.cloud.utils.cisco.n1kv.vsm.PortProfile;
6351

6452
public class BrocadeVcsApi {
6553
private static final Logger s_logger = Logger.getLogger(BrocadeVcsApi.class);
@@ -461,7 +449,7 @@ protected Output executeRetreiveStatus(String uri) throws BrocadeVcsApiException
461449
throw new BrocadeVcsApiException("Failed to retreive status : " + errorMessage);
462450
}
463451

464-
try (BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Charset.forName("UTF-8")))) {
452+
try (BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), StringUtils.getPreferredCharset()))) {
465453
sb = new StringBuffer();
466454

467455
while (((readLine = br.readLine()) != null)) {
@@ -531,7 +519,7 @@ private String responseToErrorMessage(HttpResponse response) throws IOException
531519

532520
if ("text/html".equals(response.getEntity().getContentType().getValue())) {
533521

534-
try (BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Charset.forName("UTF-8")))) {
522+
try (BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), StringUtils.getPreferredCharset()))) {
535523

536524
StringBuffer result = new StringBuffer();
537525
String line = "";

plugins/network-elements/nuage-vsp/src/com/cloud/network/manager/NuageVspManagerImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package com.cloud.network.manager;
2121

22-
import java.nio.charset.Charset;
2322
import java.util.ArrayList;
2423
import java.util.HashMap;
2524
import java.util.List;
@@ -73,6 +72,7 @@
7372
import com.cloud.resource.ResourceManager;
7473
import com.cloud.resource.ResourceState;
7574
import com.cloud.resource.ServerResource;
75+
import com.cloud.utils.StringUtils;
7676
import com.cloud.utils.component.ManagerBase;
7777
import com.cloud.utils.db.Transaction;
7878
import com.cloud.utils.db.TransactionCallback;
@@ -159,7 +159,7 @@ public NuageVspDeviceVO addNuageVspDevice(AddNuageVspDeviceCmd cmd) {
159159
params.put("name", "Nuage VSD - " + cmd.getHostName());
160160
params.put("hostname", cmd.getHostName());
161161
params.put("cmsuser", cmd.getUserName());
162-
String cmsUserPasswordBase64 = org.apache.commons.codec.binary.StringUtils.newStringUtf8(Base64.encodeBase64(cmd.getPassword().getBytes(Charset.forName("UTF-8"))));
162+
String cmsUserPasswordBase64 = org.apache.commons.codec.binary.StringUtils.newStringUtf8(Base64.encodeBase64(cmd.getPassword().getBytes(StringUtils.getPreferredCharset())));
163163
params.put("cmsuserpass", cmsUserPasswordBase64);
164164
int port = cmd.getPort();
165165
if (0 == port) {
@@ -301,6 +301,7 @@ private void initNuageScheduledTasks() {
301301

302302
if (numOfSyncThreads != null && syncUpIntervalInMinutes != null) {
303303
ThreadFactory threadFactory = new ThreadFactory() {
304+
@Override
304305
public Thread newThread(Runnable runnable) {
305306
Thread thread = new Thread(runnable, "Nuage Vsp sync task");
306307
if (thread.isDaemon())

plugins/user-authenticators/saml2/src/org/apache/cloudstack/saml/SAMLUtils.java

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,42 @@
1919

2020
package org.apache.cloudstack.saml;
2121

22-
import com.cloud.utils.HttpUtils;
22+
import java.io.ByteArrayInputStream;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.IOException;
25+
import java.io.StringWriter;
26+
import java.io.UnsupportedEncodingException;
27+
import java.math.BigInteger;
28+
import java.net.URLEncoder;
29+
import java.security.InvalidKeyException;
30+
import java.security.KeyFactory;
31+
import java.security.KeyPair;
32+
import java.security.KeyPairGenerator;
33+
import java.security.NoSuchAlgorithmException;
34+
import java.security.NoSuchProviderException;
35+
import java.security.PrivateKey;
36+
import java.security.PublicKey;
37+
import java.security.SecureRandom;
38+
import java.security.Security;
39+
import java.security.Signature;
40+
import java.security.SignatureException;
41+
import java.security.cert.CertificateEncodingException;
42+
import java.security.cert.X509Certificate;
43+
import java.security.spec.InvalidKeySpecException;
44+
import java.security.spec.PKCS8EncodedKeySpec;
45+
import java.security.spec.X509EncodedKeySpec;
46+
import java.util.List;
47+
import java.util.zip.Deflater;
48+
import java.util.zip.DeflaterOutputStream;
49+
50+
import javax.security.auth.x500.X500Principal;
51+
import javax.servlet.http.Cookie;
52+
import javax.servlet.http.HttpServletResponse;
53+
import javax.xml.parsers.DocumentBuilder;
54+
import javax.xml.parsers.DocumentBuilderFactory;
55+
import javax.xml.parsers.ParserConfigurationException;
56+
import javax.xml.stream.FactoryConfigurationError;
57+
2358
import org.apache.cloudstack.api.ApiConstants;
2459
import org.apache.cloudstack.api.response.LoginCmdResponse;
2560
import org.apache.log4j.Logger;
@@ -63,41 +98,8 @@
6398
import org.w3c.dom.Element;
6499
import org.xml.sax.SAXException;
65100

66-
import javax.security.auth.x500.X500Principal;
67-
import javax.servlet.http.Cookie;
68-
import javax.servlet.http.HttpServletResponse;
69-
import javax.xml.parsers.DocumentBuilder;
70-
import javax.xml.parsers.DocumentBuilderFactory;
71-
import javax.xml.parsers.ParserConfigurationException;
72-
import javax.xml.stream.FactoryConfigurationError;
73-
import java.io.ByteArrayInputStream;
74-
import java.io.ByteArrayOutputStream;
75-
import java.io.IOException;
76-
import java.io.StringWriter;
77-
import java.io.UnsupportedEncodingException;
78-
import java.math.BigInteger;
79-
import java.net.URLEncoder;
80-
import java.nio.charset.Charset;
81-
import java.security.InvalidKeyException;
82-
import java.security.KeyFactory;
83-
import java.security.KeyPair;
84-
import java.security.KeyPairGenerator;
85-
import java.security.NoSuchAlgorithmException;
86-
import java.security.NoSuchProviderException;
87-
import java.security.PrivateKey;
88-
import java.security.PublicKey;
89-
import java.security.SecureRandom;
90-
import java.security.Security;
91-
import java.security.Signature;
92-
import java.security.SignatureException;
93-
import java.security.cert.CertificateEncodingException;
94-
import java.security.cert.X509Certificate;
95-
import java.security.spec.InvalidKeySpecException;
96-
import java.security.spec.PKCS8EncodedKeySpec;
97-
import java.security.spec.X509EncodedKeySpec;
98-
import java.util.List;
99-
import java.util.zip.Deflater;
100-
import java.util.zip.DeflaterOutputStream;
101+
import com.cloud.utils.HttpUtils;
102+
import com.cloud.utils.StringUtils;
101103

102104
public class SAMLUtils {
103105
public static final Logger s_logger = Logger.getLogger(SAMLUtils.class);
@@ -219,7 +221,7 @@ public static String encodeSAMLRequest(XMLObject authnRequest)
219221
Deflater deflater = new Deflater(Deflater.DEFLATED, true);
220222
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
221223
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
222-
deflaterOutputStream.write(requestMessage.getBytes(Charset.forName("UTF-8")));
224+
deflaterOutputStream.write(requestMessage.getBytes(StringUtils.getPreferredCharset()));
223225
deflaterOutputStream.close();
224226
String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES);
225227
encodedRequestMessage = URLEncoder.encode(encodedRequestMessage, HttpUtils.UTF_8).trim();
@@ -263,7 +265,7 @@ public static String generateSAMLRequestSignature(final String urlEncodedString,
263265
String url = urlEncodedString + "&SigAlg=" + URLEncoder.encode(opensamlAlgoIdSignature, HttpUtils.UTF_8);
264266
Signature signature = Signature.getInstance(javaSignatureAlgorithmName);
265267
signature.initSign(signingKey);
266-
signature.update(url.getBytes(Charset.forName("UTF-8")));
268+
signature.update(url.getBytes(StringUtils.getPreferredCharset()));
267269
String signatureString = Base64.encodeBytes(signature.sign(), Base64.DONT_BREAK_LINES);
268270
if (signatureString != null) {
269271
return url + "&Signature=" + URLEncoder.encode(signatureString, HttpUtils.UTF_8);
@@ -287,7 +289,7 @@ public static String savePublicKey(PublicKey key) {
287289
KeyFactory keyFactory = SAMLUtils.getKeyFactory();
288290
if (keyFactory == null) return null;
289291
X509EncodedKeySpec spec = keyFactory.getKeySpec(key, X509EncodedKeySpec.class);
290-
return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()), Charset.forName("UTF-8"));
292+
return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()), StringUtils.getPreferredCharset());
291293
} catch (InvalidKeySpecException e) {
292294
s_logger.error("Unable to create KeyFactory:" + e.getMessage());
293295
}
@@ -300,7 +302,7 @@ public static String savePrivateKey(PrivateKey key) {
300302
if (keyFactory == null) return null;
301303
PKCS8EncodedKeySpec spec = keyFactory.getKeySpec(key,
302304
PKCS8EncodedKeySpec.class);
303-
return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()), Charset.forName("UTF-8"));
305+
return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()), StringUtils.getPreferredCharset());
304306
} catch (InvalidKeySpecException e) {
305307
s_logger.error("Unable to create KeyFactory:" + e.getMessage());
306308
}

utils/src/main/java/com/cloud/utils/ConstantTimeComparator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static boolean compareBytes(byte[] b1, byte[] b2) {
3636
}
3737

3838
public static boolean compareStrings(String s1, String s2) {
39-
final Charset encoding = Charset.forName("UTF-8");
39+
final Charset encoding = StringUtils.getPreferredCharset();
4040
return compareBytes(s1.getBytes(encoding), s2.getBytes(encoding));
4141
}
4242
}

utils/src/test/java/com/cloud/utils/storage/QCOW2UtilsTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
import java.io.IOException;
2525
import java.io.InputStream;
2626
import java.nio.ByteBuffer;
27-
import java.nio.charset.Charset;
2827

2928
import org.junit.Before;
3029
import org.junit.Test;
3130

31+
import com.cloud.utils.StringUtils;
32+
3233
public class QCOW2UtilsTest {
3334

3435
InputStream inputStream;
@@ -67,7 +68,7 @@ public void setup() {
6768
ByteBuffer byteBuffer = ByteBuffer.allocate(72);
6869

6970
// Magic
70-
byteBuffer.put("QFI".getBytes(Charset.forName("UTF-8")));
71+
byteBuffer.put("QFI".getBytes(StringUtils.getPreferredCharset()));
7172
byteBuffer.put((byte)0xfb);
7273

7374
// Version

0 commit comments

Comments
 (0)