Skip to content

Commit 36d1dd1

Browse files
CA-392453: Misc fixes to Java SDK
- Use `Types.checkError` instead of throwing a generic `XenAPIException`. This ensures the `Types.XYZ` family of exceptions are being used - Use `@JsonValue` to ensure base class objects are deserialised as a simple opaque_ref string, as opposed to a mapping of each field. This ensures the API's behaviour is unchanged. - Parse the results of `task.getResult` calls. The jsonrpc method returns value payloads of the form `"value" : "<value>OpaqueRef:XYZ</value>"` so we need to strip the surrounding XML Signed-off-by: Danilo Del Busso <[email protected]>
1 parent 8de2308 commit 36d1dd1

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

ocaml/sdk-gen/java/autogen/xen-api/src/main/java/com/xensource/xenapi/Connection.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,8 @@ public String getSessionReference() {
259259
public <T> T dispatch(String methodCall, Object[] methodParameters, TypeReference<T> responseTypeReference) throws XenAPIException, IOException {
260260
var result = client.sendRequest(methodCall, methodParameters, responseTypeReference);
261261
if (result.error != null) {
262-
throw new XenAPIException(String.valueOf(result.error));
263-
}
264-
265-
if (methodCall.equals("session.login_with_password")) {
262+
Types.checkError(result.error);
263+
} else if (methodCall.equals("session.login_with_password")) {
266264
var session = ((Session) result.result);
267265
sessionReference = session.ref;
268266
setAPIVersion();

ocaml/sdk-gen/java/main.ml

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ let gen_class cls folder =
573573
fprintf file
574574
{|package com.xensource.xenapi;
575575
import com.fasterxml.jackson.annotation.JsonProperty;
576+
import com.fasterxml.jackson.annotation.JsonValue;
576577
import com.fasterxml.jackson.core.type.TypeReference;
577578
import com.xensource.xenapi.Types.BadServerResponse;
578579
import com.xensource.xenapi.Types.XenAPIException;
@@ -592,7 +593,7 @@ import java.io.IOException;
592593

593594
if class_is_empty cls then
594595
fprintf file
595-
"\n public String toWireString() {\n return null;\n }\n\n"
596+
" @JsonValue\n public String toWireString() {\n return null;\n }\n\n"
596597
else (
597598
fprintf file " /**\n" ;
598599
fprintf file " * The XenAPI reference (OpaqueRef) to this object.\n" ;
@@ -608,6 +609,7 @@ import java.io.IOException;
608609
fprintf file
609610
" * @return The XenAPI reference (OpaqueRef) to this object.\n" ;
610611
fprintf file " */\n" ;
612+
fprintf file " @JsonValue\n" ;
611613
fprintf file " public String toWireString() {\n" ;
612614
fprintf file " return this.ref;\n" ;
613615
fprintf file " }\n\n"
@@ -719,7 +721,7 @@ let generate_reference_task_result_func file clstr =
719721
" public static %s to%s(Task task, Connection connection) throws \
720722
IOException {\n"
721723
clstr clstr ;
722-
fprintf file " return Types.to%s(task.getResult(connection));\n" clstr ;
724+
fprintf file " return Types.to%s(parseResult(task.getResult(connection)));\n" clstr ;
723725
fprintf file " }\n" ;
724726
fprintf file "\n"
725727

@@ -948,6 +950,8 @@ import java.util.*;
948950
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
949951
import com.fasterxml.jackson.annotation.JsonProperty;
950952
import java.io.IOException;
953+
import java.util.regex.Matcher;
954+
import java.util.regex.Pattern;
951955

952956
/**
953957
* This class holds enum types and exceptions.
@@ -1030,7 +1034,7 @@ public class Types
10301034
if(errorData.length == 0){
10311035
throw new BadServerResponse(response);
10321036
}
1033-
var errorName = errorData[0];
1037+
var errorName = response.message;
10341038
|} ;
10351039

10361040
Hashtbl.iter (gen_method_error_throw file) Datamodel.errors ;
@@ -1052,6 +1056,29 @@ public class Types
10521056
TypeSet.iter (gen_task_result_func file) !types ;
10531057
fprintf file
10541058
{|
1059+
1060+
public static class BadAsyncResult extends XenAPIException
1061+
{
1062+
public final String result;
1063+
1064+
public BadAsyncResult(String result)
1065+
{
1066+
super(result);
1067+
this.result = result;
1068+
}
1069+
}
1070+
1071+
private static String parseResult(String result) throws BadAsyncResult
1072+
{
1073+
Pattern pattern = Pattern.compile("<value>(.*)</value>");
1074+
Matcher matcher = pattern.matcher(result);
1075+
if (!matcher.find() || matcher.groupCount() != 1) {
1076+
throw new Types.BadAsyncResult("Can't interpret: " + result);
1077+
}
1078+
1079+
return matcher.group(1);
1080+
}
1081+
10551082
public static EventBatch toEventBatch(Object object) {
10561083
if (object == null) {
10571084
return null;

0 commit comments

Comments
 (0)