Skip to content

Commit bfe1122

Browse files
author
Vijayendra Bhamidipati
committed
Bug 13127: API error text refer to database ids instead of uuids
Description: Added a field name for the db id in the IdentityProxy class, and modified setProxyObject() to take an additional id name parameter. This will let us know the name of the uuid that we are returning. E.g.- domainId, zoneId, etc. The client can view this field in the json/xml output. Modified the JSON/XML serialization routines to append this new parameter to the serialized output for Exception Responses.
1 parent e5b4cf5 commit bfe1122

File tree

10 files changed

+86
-60
lines changed

10 files changed

+86
-60
lines changed

api/src/com/cloud/api/response/ExceptionResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ public void setErrorText(String errorText) {
4747
this.errorText = errorText;
4848
}
4949

50-
public void setProxyObject(String table_name, Long id) {
50+
public void setProxyObject(String table_name, String idFieldName, Long id) {
5151
this.id = new IdentityProxy();
5252
this.id.setTableName(table_name);
5353
this.id.setValue(id);
54+
this.id.setidFieldName(idFieldName);
5455
return;
5556
}
5657

api/src/com/cloud/exception/CloudException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ public CloudException() {
5252
super();
5353
}
5454

55-
public void setProxyObject(String table_name, Long id) {
55+
public void setProxyObject(String table_name, String idFieldName, Long id) {
5656
this.id = new IdentityProxy();
5757
this.id.setTableName(table_name);
5858
this.id.setValue(id);
59+
this.id.setidFieldName(idFieldName);
5960
return;
6061
}
6162

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -142,51 +142,51 @@ public void dispatch(BaseCmd cmd, Map<String, String> params) {
142142
// earlier, we'd log the db id as part of the log message, but now since we've pushed
143143
// the id into a IdentityProxy object, we would need to dump that object alongwith the
144144
// message.
145-
InvalidParameterValueException ref = (InvalidParameterValueException)t;
145+
InvalidParameterValueException ref = (InvalidParameterValueException) t;
146146
ServerApiException ex = new ServerApiException(BaseCmd.PARAM_ERROR, t.getMessage());
147147
// copy over the IdentityProxy information as well and throw the serverapiexception.
148148
IdentityProxy id = ref.getProxyObject();
149149
if (id != null) {
150-
ex.setProxyObject(id.getTableName(), id.getValue());
151-
s_logger.info(t.getMessage() + " uuid: " + id.getValue());
150+
ex.setProxyObject(id.getTableName(), id.getidFieldName(), id.getValue());
151+
s_logger.info(t.getMessage() + " db_id: " + id.getValue());
152152
} else {
153153
s_logger.info(t.getMessage());
154-
}
154+
}
155155
throw ex;
156-
} else if(t instanceof IllegalArgumentException) {
156+
} else if(t instanceof IllegalArgumentException) {
157157
throw new ServerApiException(BaseCmd.PARAM_ERROR, t.getMessage());
158-
} else if (t instanceof PermissionDeniedException) {
158+
} else if (t instanceof PermissionDeniedException) {
159159
PermissionDeniedException ref = (PermissionDeniedException)t;
160160
ServerApiException ex = new ServerApiException(BaseCmd.ACCOUNT_ERROR, t.getMessage());
161161
// copy over the IdentityProxy information as well and throw the serverapiexception.
162162
IdentityProxy id = ref.getProxyObject();
163163
if (id != null) {
164-
ex.setProxyObject(id.getTableName(), id.getValue());
164+
ex.setProxyObject(id.getTableName(), id.getidFieldName(), id.getValue());
165165
s_logger.info("PermissionDenied: " + t.getMessage() + "uuid: " + id.getValue());
166166
} else {
167167
s_logger.info("PermissionDenied: " + t.getMessage());
168168
}
169169
throw ex;
170-
} else if (t instanceof AccountLimitException) {
170+
} else if (t instanceof AccountLimitException) {
171171
AccountLimitException ref = (AccountLimitException)t;
172172
ServerApiException ex = new ServerApiException(BaseCmd.ACCOUNT_RESOURCE_LIMIT_ERROR, t.getMessage());
173173
// copy over the IdentityProxy information as well and throw the serverapiexception.
174174
IdentityProxy id = ref.getProxyObject();
175175
if (id != null) {
176-
ex.setProxyObject(id.getTableName(), id.getValue());
177-
s_logger.info(t.getMessage() + "uuid: " + id.getValue());
176+
ex.setProxyObject(id.getTableName(), id.getidFieldName(), id.getValue());
177+
s_logger.info(t.getMessage() + "db_id: " + id.getValue());
178178
} else {
179179
s_logger.info(t.getMessage());
180180
}
181181
throw ex;
182-
} else if (t instanceof InsufficientCapacityException) {
182+
} else if (t instanceof InsufficientCapacityException) {
183183
InsufficientCapacityException ref = (InsufficientCapacityException)t;
184184
ServerApiException ex = new ServerApiException(BaseCmd.INSUFFICIENT_CAPACITY_ERROR, t.getMessage());
185185
// copy over the IdentityProxy information as well and throw the serverapiexception.
186186
IdentityProxy id = ref.getIdProxy();
187187
if (id != null) {
188-
ex.setProxyObject(id.getTableName(), id.getValue());
189-
s_logger.info(t.getMessage() + "uuid: " + id.getValue());
188+
ex.setProxyObject(id.getTableName(), id.getidFieldName(), id.getValue());
189+
s_logger.info(t.getMessage() + "db_id: " + id.getValue());
190190
} else {
191191
s_logger.info(t.getMessage());
192192
}
@@ -197,8 +197,8 @@ public void dispatch(BaseCmd cmd, Map<String, String> params) {
197197
// copy over the IdentityProxy information as well and throw the serverapiexception.
198198
IdentityProxy id = ref.getIdProxy();
199199
if (id != null) {
200-
ex.setProxyObject(id.getTableName(), id.getValue());
201-
s_logger.warn("Exception: " + t.getMessage() + "uuid: " + ref.getIdProxy().getValue());
200+
ex.setProxyObject(id.getTableName(), id.getidFieldName(), id.getValue());
201+
s_logger.warn("Exception: " + t.getMessage() + "db_id: " + ref.getIdProxy().getValue());
202202
} else {
203203
s_logger.warn("Exception: ", t);
204204
}
@@ -209,8 +209,8 @@ public void dispatch(BaseCmd cmd, Map<String, String> params) {
209209
// copy over the IdentityProxy information as well and throw the serverapiexception.
210210
IdentityProxy id = ref.getIdProxy();
211211
if (id != null) {
212-
ex.setProxyObject(id.getTableName(), id.getValue());
213-
s_logger.warn("Exception: " + t.getMessage() + "uuid: " + ref.getIdProxy().getValue());
212+
ex.setProxyObject(id.getTableName(), id.getidFieldName(), id.getValue());
213+
s_logger.warn("Exception: " + t.getMessage() + "db_id: " + ref.getIdProxy().getValue());
214214
} else {
215215
s_logger.warn("Exception: ", t);
216216
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
350350

351351
writeResponse(response, responseText, HttpStatus.SC_OK, responseType, null);
352352
} catch (ServerApiException se) {
353-
String responseText = getSerializedApiError(se.getErrorCode(), se.getDescription(), parameterMap, responseType, se);
353+
String responseText = getSerializedApiError(se.getErrorCode(), se.getDescription(), parameterMap, responseType, se);
354354
writeResponse(response, responseText, se.getErrorCode(), responseType, se.getDescription());
355355
sb.append(" " + se.getErrorCode() + " " + se.getDescription());
356356
} catch (RuntimeException e) {
@@ -432,8 +432,8 @@ public String handleRequest(Map params, boolean decode, String responseType, Str
432432
ServerApiException e = new ServerApiException(BaseCmd.PARAM_ERROR, ex.getMessage());
433433
// copy over the IdentityProxy information as well and throw the serverapiexception.
434434
IdentityProxy id = ref.getProxyObject();
435-
if (id != null) {
436-
e.setProxyObject(id.getTableName(), id.getValue());
435+
if (id != null) {
436+
e.setProxyObject(id.getTableName(), id.getidFieldName(), id.getValue());
437437
}
438438
throw e;
439439
} else if (ex instanceof PermissionDeniedException) {
@@ -442,7 +442,7 @@ public String handleRequest(Map params, boolean decode, String responseType, Str
442442
// copy over the IdentityProxy information as well and throw the serverapiexception.
443443
IdentityProxy id = ref.getProxyObject();
444444
if (id != null) {
445-
e.setProxyObject(id.getTableName(), id.getValue());
445+
e.setProxyObject(id.getTableName(), id.getidFieldName(), id.getValue());
446446
}
447447
throw e;
448448
} else if (ex instanceof ServerApiException) {
@@ -1004,7 +1004,6 @@ public String getSerializedApiError(int errorCode, String errorText, Map<String,
10041004
}
10051005
}
10061006
}
1007-
10081007
ExceptionResponse apiResponse = new ExceptionResponse();
10091008
apiResponse.setErrorCode(errorCode);
10101009
apiResponse.setErrorText(errorText);
@@ -1018,23 +1017,23 @@ public String getSerializedApiError(int errorCode, String errorText, Map<String,
10181017
if (ex instanceof ServerApiException || ex instanceof PermissionDeniedException
10191018
|| ex instanceof InvalidParameterValueException) {
10201019
// Cast the exception appropriately and retrieve the IdentityProxy
1021-
if (ex instanceof ServerApiException) {
1020+
if (ex instanceof ServerApiException) {
10221021
ServerApiException ref = (ServerApiException) ex;
10231022
IdentityProxy uuidproxy = ref.getProxyObject();
10241023
if (uuidproxy != null) {
1025-
apiResponse.setProxyObject(uuidproxy.getTableName(), uuidproxy.getValue());
1024+
apiResponse.setProxyObject(uuidproxy.getTableName(), uuidproxy.getidFieldName(), uuidproxy.getValue());
10261025
}
10271026
} else if (ex instanceof PermissionDeniedException) {
10281027
PermissionDeniedException ref = (PermissionDeniedException) ex;
10291028
IdentityProxy uuidproxy = ref.getProxyObject();
10301029
if (uuidproxy != null) {
1031-
apiResponse.setProxyObject(uuidproxy.getTableName(), uuidproxy.getValue());
1030+
apiResponse.setProxyObject(uuidproxy.getTableName(), uuidproxy.getidFieldName(), uuidproxy.getValue());
10321031
}
10331032
} else if (ex instanceof InvalidParameterValueException) {
10341033
InvalidParameterValueException ref = (InvalidParameterValueException) ex;
10351034
IdentityProxy uuidproxy = ref.getProxyObject();
10361035
if (uuidproxy != null) {
1037-
apiResponse.setProxyObject(uuidproxy.getTableName(), uuidproxy.getValue());
1036+
apiResponse.setProxyObject(uuidproxy.getTableName(), uuidproxy.getidFieldName(), uuidproxy.getValue());
10381037
}
10391038
}
10401039
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public JsonElement serialize(IdentityProxy src, Type srcType, JsonSerializationC
4848
if(uuid == null)
4949
return context.serialize(null);
5050

51-
return new JsonPrimitive(uuid);
51+
return new JsonPrimitive(uuid);
5252
} else {
5353
return new JsonPrimitive(String.valueOf(src.getValue()));
5454
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ public static String toJSONSerializedString(ResponseObject result) {
100100
String jsonErrorText = gson.toJson((ExceptionResponse) result);
101101
jsonErrorText = unescape(jsonErrorText);
102102
sb.append(jsonErrorText);
103+
// Since the IdentityTypeAdapter only converts the uuid, let's append the idFieldName explicitly.
104+
IdentityProxy ref = ((ExceptionResponse) result).getProxyObject();
105+
if (ref != null) {
106+
// bump off the } at the end. We'll re-add it after the idFieldName.
107+
sb.deleteCharAt(sb.length()-1);
108+
String idFieldName = ref.getidFieldName();
109+
if (idFieldName != null) {
110+
sb.append(",\"uuidProperty\":" + "\"" + idFieldName + "\"}");
111+
}
112+
}
103113
} else {
104114
String jsonStr = gson.toJson(result);
105115
if ((jsonStr != null) && !"".equals(jsonStr)) {
@@ -230,6 +240,11 @@ private static void serializeResponseObjFieldsXML(StringBuilder sb, ResponseObje
230240
}
231241
if(id != null && !id.isEmpty())
232242
sb.append("<" + serializedName.value() + ">" + id + "</" + serializedName.value() + ">");
243+
// Append the new idFieldName property also.
244+
String idFieldName = idProxy.getidFieldName();
245+
if (idFieldName != null) {
246+
sb.append("<" + "uuidProperty" + ">" + idFieldName + "</" + "uuidProperty" + ">");
247+
}
233248
} else {
234249
String resultString = escapeSpecialXmlChars(fieldValue.toString());
235250
if (!(obj instanceof ExceptionResponse)) {

0 commit comments

Comments
 (0)