Skip to content

Commit fe41325

Browse files
author
Alena Prokharchyk
committed
ApiResponseSerializer - replaced all occurrences of string concatenation with StringBuffer.append
Conflicts: server/src/com/cloud/api/response/ApiResponseSerializer.java
1 parent 04a3c4f commit fe41325

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

server/src/com/cloud/api/response/ApiResponseSerializer.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
import com.cloud.api.ApiResponseGsonHelper;
3434
import com.cloud.api.ApiServer;
3535
import com.cloud.api.BaseCmd;
36-
import com.cloud.utils.IdentityProxy;
3736
import com.cloud.api.ResponseObject;
37+
import com.cloud.utils.IdentityProxy;
3838
import com.cloud.utils.encoding.URLEncoder;
3939
import com.cloud.utils.exception.CloudRuntimeException;
4040
import com.cloud.uuididentity.dao.IdentityDao;
@@ -70,7 +70,7 @@ public static String toJSONSerializedString(ResponseObject result) {
7070

7171
StringBuilder sb = new StringBuilder();
7272

73-
sb.append("{ \"" + result.getResponseName() + "\" : ");
73+
sb.append("{ \"").append(result.getResponseName()).append("\" : ");
7474
if (result instanceof ListResponse) {
7575
List<? extends ResponseObject> responses = ((ListResponse) result).getResponses();
7676
if ((responses != null) && !responses.isEmpty()) {
@@ -80,19 +80,20 @@ public static String toJSONSerializedString(ResponseObject result) {
8080
jsonStr = unescape(jsonStr);
8181

8282
if (count != null && count != 0) {
83-
sb.append("{ \"" + ApiConstants.COUNT + "\":" + count + " ,\"" + responses.get(0).getObjectName() + "\" : [ " + jsonStr);
83+
sb.append("{ \"").append(ApiConstants.COUNT).append("\":").append(count).append(" ,\"").
84+
append(responses.get(0).getObjectName()).append("\" : [ ").append(jsonStr);
8485
}
8586
for (int i = 1; i < ((ListResponse) result).getResponses().size(); i++) {
8687
jsonStr = gson.toJson(responses.get(i));
8788
jsonStr = unescape(jsonStr);
88-
sb.append(", " + jsonStr);
89+
sb.append(", ").append(jsonStr);
8990
}
9091
sb.append(" ] }");
9192
} else {
9293
sb.append("{ }");
9394
}
9495
} else if (result instanceof SuccessResponse) {
95-
sb.append("{ \"success\" : \"" + ((SuccessResponse) result).getSuccess() + "\"} ");
96+
sb.append("{ \"success\" : \"").append(((SuccessResponse) result).getSuccess()).append("\"} ");
9697
} else if (result instanceof ExceptionResponse) {
9798
String jsonErrorText = gson.toJson((ExceptionResponse) result);
9899
jsonErrorText = unescape(jsonErrorText);
@@ -104,7 +105,7 @@ public static String toJSONSerializedString(ResponseObject result) {
104105
if (result instanceof AsyncJobResponse || result instanceof CreateCmdResponse) {
105106
sb.append(jsonStr);
106107
} else {
107-
sb.append(" { \"" + result.getObjectName() + "\" : " + jsonStr + " } ");
108+
sb.append(" { \"").append(result.getObjectName()).append("\" : ").append(jsonStr).append(" } ");
108109
}
109110
} else {
110111
sb.append("{ }");
@@ -119,13 +120,14 @@ public static String toJSONSerializedString(ResponseObject result) {
119120
private static String toXMLSerializedString(ResponseObject result) {
120121
StringBuilder sb = new StringBuilder();
121122
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
122-
sb.append("<" + result.getResponseName() + " cloud-stack-version=\"" + ApiDBUtils.getVersion() + "\">");
123+
sb.append("<").append(result.getResponseName()).append(" cloud-stack-version=\"").append(ApiDBUtils.getVersion()).append("\">");
123124

124125
if (result instanceof ListResponse) {
125126
Integer count = ((ListResponse) result).getCount();
126127

127128
if (count != null && count != 0) {
128-
sb.append("<" + ApiConstants.COUNT + ">" + ((ListResponse) result).getCount() + "</" + ApiConstants.COUNT + ">");
129+
sb.append("<").append(ApiConstants.COUNT).append(">").append(((ListResponse) result).getCount()).
130+
append("</").append(ApiConstants.COUNT).append(">");
129131
}
130132
List<? extends ResponseObject> responses = ((ListResponse) result).getResponses();
131133
if ((responses != null) && !responses.isEmpty()) {
@@ -141,17 +143,17 @@ private static String toXMLSerializedString(ResponseObject result) {
141143
}
142144
}
143145

144-
sb.append("</" + result.getResponseName() + ">");
146+
sb.append("</").append(result.getResponseName()).append(">");
145147
return sb.toString();
146148
}
147149

148150
private static void serializeResponseObjXML(StringBuilder sb, ResponseObject obj) {
149151
if (!(obj instanceof SuccessResponse) && !(obj instanceof ExceptionResponse)) {
150-
sb.append("<" + obj.getObjectName() + ">");
152+
sb.append("<").append(obj.getObjectName()).append(">");
151153
}
152154
serializeResponseObjFieldsXML(sb, obj);
153155
if (!(obj instanceof SuccessResponse) && !(obj instanceof ExceptionResponse)) {
154-
sb.append("</" + obj.getObjectName() + ">");
156+
sb.append("</").append(obj.getObjectName()).append(">");
155157
}
156158
}
157159

@@ -221,24 +223,24 @@ private static void serializeResponseObjFieldsXML(StringBuilder sb, ResponseObje
221223
if(id != null && !id.isEmpty()) {
222224
// If this is the first IdentityProxy field encountered, put in a uuidList tag.
223225
if (!usedUuidList) {
224-
sb.append("<" + serializedName.value() + ">");
226+
sb.append("<").append(serializedName.value()).append(">");
225227
usedUuidList = true;
226228
}
227-
sb.append("<" + "uuid" + ">" + id + "</" + "uuid" + ">");
229+
sb.append("<uuid>").append(id).append("</uuid>");
228230
}
229231
// Append the new idFieldName property also.
230232
String idFieldName = idProxy.getidFieldName();
231233
if (idFieldName != null) {
232-
sb.append("<" + "uuidProperty" + ">" + idFieldName + "</" + "uuidProperty" + ">");
234+
sb.append("<uuidProperty>").append(idFieldName).append("</uuidProperty>");
233235
}
234236
}
235237
}
236238
if (usedUuidList) {
237239
// close the uuidList.
238-
sb.append("</" + serializedName.value() + ">");
240+
sb.append("</").append(serializedName.value()).append(">");
239241
}
240242
} else if (fieldValue instanceof Date) {
241-
sb.append("<" + serializedName.value() + ">" + BaseCmd.getDateString((Date) fieldValue) + "</" + serializedName.value() + ">");
243+
sb.append("<").append(">").append(BaseCmd.getDateString((Date) fieldValue)).append("</").append(serializedName.value()).append(">");
242244
} else if (fieldValue instanceof IdentityProxy) {
243245
IdentityProxy idProxy = (IdentityProxy)fieldValue;
244246
String id = (idProxy.getValue() != null ? String.valueOf(idProxy.getValue()) : "");
@@ -251,14 +253,14 @@ private static void serializeResponseObjFieldsXML(StringBuilder sb, ResponseObje
251253
}
252254
}
253255
if(id != null && !id.isEmpty())
254-
sb.append("<" + serializedName.value() + ">" + id + "</" + serializedName.value() + ">");
256+
sb.append("<").append(serializedName.value()).append(">").append(id).append("</").append(serializedName.value()).append(">");
255257
} else {
256258
String resultString = escapeSpecialXmlChars(fieldValue.toString());
257259
if (!(obj instanceof ExceptionResponse)) {
258260
resultString = encodeParam(resultString);
259261
}
260262

261-
sb.append("<" + serializedName.value() + ">" + resultString + "</" + serializedName.value() + ">");
263+
sb.append("<").append(serializedName.value()).append(">").append(resultString).append("</").append(serializedName.value()).append(">");
262264
}
263265
}
264266
}

0 commit comments

Comments
 (0)