for (Field field : fields) {
if ((field.getModifiers() & Modifier.TRANSIENT) != 0) {
continue; // skip transient fields
}
SerializedName serializedName = field.getAnnotation(SerializedName.class);
if (serializedName == null) {
continue; // skip fields w/o serialized name
}
field.setAccessible(true);
Object fieldValue = null;
try {
fieldValue = field.get(obj);
} catch (IllegalArgumentException e) {
throw new CloudRuntimeException("how illegal is it?", e);
} catch (IllegalAccessException e) {
throw new CloudRuntimeException("come on...we set accessible already", e);
}
if (fieldValue != null) {
if (fieldValue instanceof ResponseObject) {
ResponseObject subObj = (ResponseObject) fieldValue;
if (isAsync) {
sb.append("<jobresult>");
}
serializeResponseObjXML(sb, subObj);
if (isAsync) {
sb.append("</jobresult>");
}
} else if (fieldValue instanceof List<?>) {
List<?> subResponseList = (List<Object>) fieldValue;
boolean usedUuidList = false;
for (Object value : subResponseList) {
if (value instanceof ResponseObject) {
ResponseObject subObj = (ResponseObject) value;
if (serializedName != null) {
subObj.setObjectName(serializedName.value());
}
serializeResponseObjXML(sb, subObj);
} else if (value instanceof IdentityProxy) {
// Only exception reponses carry a list of IdentityProxy objects.
IdentityProxy idProxy = (IdentityProxy)value;
String id = (idProxy.getValue() != null ? String.valueOf(idProxy.getValue()) : "");
if(!id.isEmpty()) {
IdentityDao identityDao = new IdentityDaoImpl();
id = identityDao.getIdentityUuid(idProxy.getTableName(), id);
}
if(id != null && !id.isEmpty()) {
// If this is the first IdentityProxy field encountered, put in a uuidList tag.
if (!usedUuidList) {
sb.append("<" + serializedName.value() + ">");
usedUuidList = true;
}
sb.append("<" + "uuid" + ">" + id + "</" + "uuid" + ">");
}
// Append the new idFieldName property also.
String idFieldName = idProxy.getidFieldName();
if (idFieldName != null) {
sb.append("<" + "uuidProperty" + ">" + idFieldName + "</" + "uuidProperty" + ">");
}
}
}
if (usedUuidList) {
// close the uuidList.
sb.append("</" + serializedName.value() + ">");
}
} else if (fieldValue instanceof Date) {
sb.append("<" + serializedName.value() + ">" + BaseCmd.getDateString((Date) fieldValue) + "</" + serializedName.value() + ">");
} else if (fieldValue instanceof IdentityProxy) {
IdentityProxy idProxy = (IdentityProxy)fieldValue;
String id = (idProxy.getValue() != null ? String.valueOf(idProxy.getValue()) : "");
if(!id.isEmpty()) {
IdentityDao identityDao = new IdentityDaoImpl();
if(idProxy.getTableName() != null) {
id = identityDao.getIdentityUuid(idProxy.getTableName(), id);
} else {
s_logger.warn("IdentityProxy sanity check issue, invalid IdentityProxy table name found in class: " + obj.getClass().getName());
}
}
if(id != null && !id.isEmpty())
sb.append("<" + serializedName.value() + ">" + id + "</" + serializedName.value() + ">");
} else {
String resultString = escapeSpecialXmlChars(fieldValue.toString());
if (!(obj instanceof ExceptionResponse)) {
resultString = encodeParam(resultString);
}
sb.append("<" + serializedName.value() + ">" + resultString + "</" + serializedName.value() + ">");
}
}
}
}