Skip to content

Commit 2b267c1

Browse files
committed
CLOUDSTACK-1142: Refactor handleRequest definition, clean redundant code
- Get rid of boolean decode arg - Method assumes that OTW params have been already decoded - Remove redundant code that tries to decode again based on boolean arg Signed-off-by: Rohit Yadav <bhaisaab@apache.org>
1 parent d8062e5 commit 2b267c1

2 files changed

Lines changed: 4 additions & 21 deletions

File tree

server/src/com/cloud/api/ApiServer.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
import java.io.ByteArrayInputStream;
2020
import java.io.IOException;
2121
import java.io.InterruptedIOException;
22-
import java.io.UnsupportedEncodingException;
2322
import java.net.InetAddress;
2423
import java.net.ServerSocket;
2524
import java.net.Socket;
2625
import java.net.URI;
2726
import java.net.URISyntaxException;
28-
import java.net.URLDecoder;
2927
import java.net.URLEncoder;
3028
import java.security.SecureRandom;
3129
import java.text.DateFormat;
@@ -292,7 +290,7 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
292290
// always trust commands from API port, user context will always be UID_SYSTEM/ACCOUNT_ID_SYSTEM
293291
UserContext.registerContext(_systemUser.getId(), _systemAccount, null, true);
294292
sb.insert(0, "(userId=" + User.UID_SYSTEM + " accountId=" + Account.ACCOUNT_ID_SYSTEM + " sessionId=" + null + ") ");
295-
String responseText = handleRequest(parameterMap, false, responseType, sb);
293+
String responseText = handleRequest(parameterMap, responseType, sb);
296294
sb.append(" 200 " + ((responseText == null) ? 0 : responseText.length()));
297295

298296
writeResponse(response, responseText, HttpStatus.SC_OK, responseType, null);
@@ -312,7 +310,7 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
312310
}
313311

314312
@SuppressWarnings("rawtypes")
315-
public String handleRequest(Map params, boolean decode, String responseType, StringBuffer auditTrailSb) throws ServerApiException {
313+
public String handleRequest(Map params, String responseType, StringBuffer auditTrailSb) throws ServerApiException {
316314
String response = null;
317315
String[] command = null;
318316
try {
@@ -338,22 +336,7 @@ public String handleRequest(Map params, boolean decode, String responseType, Str
338336
continue;
339337
}
340338
String[] value = (String[]) params.get(key);
341-
342-
String decodedValue = null;
343-
if (decode) {
344-
try {
345-
decodedValue = URLDecoder.decode(value[0], "UTF-8");
346-
} catch (UnsupportedEncodingException usex) {
347-
s_logger.warn(key + " could not be decoded, value = " + value[0]);
348-
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, key + " could not be decoded, received value " + value[0]);
349-
} catch (IllegalArgumentException iae) {
350-
s_logger.warn(key + " could not be decoded, value = " + value[0]);
351-
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, key + " could not be decoded, received value " + value[0] + " which contains illegal characters eg.%");
352-
}
353-
} else {
354-
decodedValue = value[0];
355-
}
356-
paramMap.put(key, decodedValue);
339+
paramMap.put(key, value[0]);
357340
}
358341

359342
Class<?> cmdClass = getCmdClass(command[0]);

server/src/com/cloud/api/ApiServlet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private void processRequest(HttpServletRequest req, HttpServletResponse resp) {
299299
auditTrailSb.insert(0, "(userId=" + UserContext.current().getCallerUserId() + " accountId="
300300
+ UserContext.current().getCaller().getId() + " sessionId=" + (session != null ? session.getId() : null) + ")");
301301

302-
String response = _apiServer.handleRequest(params, false, responseType, auditTrailSb);
302+
String response = _apiServer.handleRequest(params, responseType, auditTrailSb);
303303
writeResponse(resp, response != null ? response : "", HttpServletResponse.SC_OK, responseType);
304304
} else {
305305
if (session != null) {

0 commit comments

Comments
 (0)