11package io .a2a .grpc .utils ;
22
3+
34import com .fasterxml .jackson .core .JsonProcessingException ;
45import com .fasterxml .jackson .databind .JsonMappingException ;
56import com .google .gson .Gson ;
89import com .google .gson .JsonObject ;
910import com .google .gson .JsonParser ;
1011import com .google .gson .Strictness ;
12+ import com .google .gson .stream .JsonWriter ;
1113import com .google .protobuf .InvalidProtocolBufferException ;
1214import com .google .protobuf .util .JsonFormat ;
1315import io .a2a .spec .CancelTaskRequest ;
16+ import io .a2a .spec .CancelTaskResponse ;
1417import io .a2a .spec .DeleteTaskPushNotificationConfigRequest ;
18+ import io .a2a .spec .DeleteTaskPushNotificationConfigResponse ;
1519import io .a2a .spec .GetAuthenticatedExtendedCardRequest ;
1620import io .a2a .spec .GetTaskPushNotificationConfigRequest ;
21+ import io .a2a .spec .GetTaskPushNotificationConfigResponse ;
1722import io .a2a .spec .GetTaskRequest ;
23+ import io .a2a .spec .GetTaskResponse ;
1824import io .a2a .spec .IdJsonMappingException ;
1925import io .a2a .spec .InvalidParamsError ;
2026import io .a2a .spec .JSONRPCError ;
2127import io .a2a .spec .JSONRPCMessage ;
2228import io .a2a .spec .JSONRPCRequest ;
29+ import io .a2a .spec .JSONRPCResponse ;
2330import io .a2a .spec .ListTaskPushNotificationConfigRequest ;
31+ import io .a2a .spec .ListTaskPushNotificationConfigResponse ;
2432import io .a2a .spec .ListTasksRequest ;
33+ import io .a2a .spec .ListTasksResponse ;
2534import io .a2a .spec .MethodNotFoundJsonMappingException ;
2635import io .a2a .spec .SendMessageRequest ;
36+ import io .a2a .spec .SendMessageResponse ;
2737import io .a2a .spec .SendStreamingMessageRequest ;
2838import io .a2a .spec .SetTaskPushNotificationConfigRequest ;
39+ import io .a2a .spec .SetTaskPushNotificationConfigResponse ;
2940import io .a2a .spec .SubscribeToTaskRequest ;
3041import java .io .IOException ;
3142import java .io .StringWriter ;
3243import java .io .Writer ;
44+ import java .util .UUID ;
3345import java .util .logging .Level ;
3446import java .util .logging .Logger ;
3547
@@ -38,15 +50,15 @@ public class JSONRPCUtils {
3850 private static final Logger log = Logger .getLogger (JSONRPCUtils .class .getName ());
3951 private static final Gson GSON = new GsonBuilder ().setStrictness (Strictness .STRICT ).create ();
4052
41- public static JSONRPCRequest <?> parseBody (String body ) throws JsonProcessingException {
53+ public static JSONRPCRequest <?> parseRequestBody (String body ) throws JsonProcessingException {
4254 JsonElement jelement = JsonParser .parseString (body );
4355 JsonObject jsonRpc = jelement .getAsJsonObject ();
4456 if (!jsonRpc .has ("method" )) {
4557 throw new IdJsonMappingException ("Missing method" , getIdIfPossible (jsonRpc ));
4658 }
4759 String version = getAndValidateJsonrpc (jsonRpc );
48- String method = jsonRpc .get ("method" ).getAsString ();
4960 Object id = getAndValidateId (jsonRpc );
61+ String method = jsonRpc .get ("method" ).getAsString ();
5062 JsonElement paramsNode = jsonRpc .get ("params" );
5163
5264 switch (method ) {
@@ -93,24 +105,78 @@ public static JSONRPCRequest<?> parseBody(String body) throws JsonProcessingExce
93105 case GetAuthenticatedExtendedCardRequest .METHOD -> {
94106 return new GetAuthenticatedExtendedCardRequest (version , id , method , null );
95107 }
96- case SendStreamingMessageRequest .METHOD -> {
108+ case SendStreamingMessageRequest .METHOD -> {
97109 io .a2a .grpc .SendMessageRequest .Builder builder = io .a2a .grpc .SendMessageRequest .newBuilder ();
98110 parseRequestBody (paramsNode , builder );
99111 return new SendStreamingMessageRequest (version , id , method , ProtoUtils .FromProto .messageSendParams (builder ));
100112 }
101- case SubscribeToTaskRequest .METHOD -> {
113+ case SubscribeToTaskRequest .METHOD -> {
102114 io .a2a .grpc .SubscribeToTaskRequest .Builder builder = io .a2a .grpc .SubscribeToTaskRequest .newBuilder ();
103115 parseRequestBody (paramsNode , builder );
104116 return new SubscribeToTaskRequest (version , id , method , ProtoUtils .FromProto .taskIdParams (builder ));
105117 }
106- default -> throw new MethodNotFoundJsonMappingException ("Invalid method" , getIdIfPossible (jsonRpc ));
118+ default ->
119+ throw new MethodNotFoundJsonMappingException ("Invalid method" , getIdIfPossible (jsonRpc ));
120+ }
121+ }
122+
123+ public static JSONRPCResponse <?> parseResponseBody (String body , String method ) throws JsonProcessingException {
124+ JsonElement jelement = JsonParser .parseString (body );
125+ JsonObject jsonRpc = jelement .getAsJsonObject ();
126+ String version = getAndValidateJsonrpc (jsonRpc );
127+ Object id = getAndValidateId (jsonRpc );
128+ JsonElement paramsNode = jsonRpc .get ("result" );
129+ switch (method ) {
130+ case GetTaskRequest .METHOD -> {
131+ io .a2a .grpc .Task .Builder builder = io .a2a .grpc .Task .newBuilder ();
132+ parseRequestBody (paramsNode , builder );
133+ return new GetTaskResponse (id , ProtoUtils .FromProto .task (builder ));
134+ }
135+ case CancelTaskRequest .METHOD -> {
136+ io .a2a .grpc .Task .Builder builder = io .a2a .grpc .Task .newBuilder ();
137+ parseRequestBody (paramsNode , builder );
138+ return new CancelTaskResponse (id , ProtoUtils .FromProto .task (builder ));
139+ }
140+ case ListTasksRequest .METHOD -> {
141+ io .a2a .grpc .ListTasksResponse .Builder builder = io .a2a .grpc .ListTasksResponse .newBuilder ();
142+ parseRequestBody (paramsNode , builder );
143+ return new ListTasksResponse (id , ProtoUtils .FromProto .listTasksResult (builder ));
144+ }
145+ case SetTaskPushNotificationConfigRequest .METHOD -> {
146+ io .a2a .grpc .TaskPushNotificationConfig .Builder builder = io .a2a .grpc .TaskPushNotificationConfig .newBuilder ();
147+ parseRequestBody (paramsNode , builder );
148+ return new SetTaskPushNotificationConfigResponse (id , ProtoUtils .FromProto .taskPushNotificationConfig (builder ));
149+ }
150+ case GetTaskPushNotificationConfigRequest .METHOD -> {
151+ io .a2a .grpc .TaskPushNotificationConfig .Builder builder = io .a2a .grpc .TaskPushNotificationConfig .newBuilder ();
152+ parseRequestBody (paramsNode , builder );
153+ return new GetTaskPushNotificationConfigResponse (id , ProtoUtils .FromProto .taskPushNotificationConfig (builder ));
154+ }
155+ case SendMessageRequest .METHOD -> {
156+ io .a2a .grpc .SendMessageResponse .Builder builder = io .a2a .grpc .SendMessageResponse .newBuilder ();
157+ parseRequestBody (paramsNode , builder );
158+ if (builder .hasMsg ()) {
159+ return new SendMessageResponse (id , ProtoUtils .FromProto .message (builder .getMsg ()));
160+ }
161+ return new SendMessageResponse (id , ProtoUtils .FromProto .task (builder .getTask ()));
162+ }
163+ case ListTaskPushNotificationConfigRequest .METHOD -> {
164+ io .a2a .grpc .ListTaskPushNotificationConfigResponse .Builder builder = io .a2a .grpc .ListTaskPushNotificationConfigResponse .newBuilder ();
165+ parseRequestBody (paramsNode , builder );
166+ return new ListTaskPushNotificationConfigResponse (id , ProtoUtils .FromProto .listTaskPushNotificationConfigParams (builder ));
167+ }
168+ case DeleteTaskPushNotificationConfigRequest .METHOD -> {
169+ return new DeleteTaskPushNotificationConfigResponse (id );
170+ }
171+ default ->
172+ throw new MethodNotFoundJsonMappingException ("Invalid method" , getIdIfPossible (jsonRpc ));
107173 }
108174 }
109175
110176 protected static void parseRequestBody (JsonElement jsonRpc , com .google .protobuf .Message .Builder builder ) throws JSONRPCError {
111177 try (Writer writer = new StringWriter ()) {
112178 GSON .toJson (jsonRpc , writer );
113- parseRequestBody (writer .toString (), builder );
179+ JSONRPCUtils . parseRequestBody (writer .toString (), builder );
114180 } catch (IOException e ) {
115181 log .log (Level .SEVERE , "Error parsing JSON request body: {0}" , jsonRpc );
116182 log .log (Level .SEVERE , "Parse error details" , e );
@@ -160,38 +226,58 @@ protected static Object getAndValidateId(JsonObject jsonRpc) throws JsonProcessi
160226 return id ;
161227 }
162228
163-
164- /**
165-
166- public static String toJsonRPCString(String id, com.google.protobuf.Message.Builder builder) {
167- try (StringWriter result = new StringWriter()) {
168- JsonObjectBuilder json = Json.createObjectBuilder();
169- json.add("jsonrpc", "2.0");
170- json.add("id", id);
171- json.add("result", Json.createObjectBuilder(Json.createParser(new StringReader(JsonFormat.printer().print(builder))).getObject()));
172- Json.createWriter(result).writeObject(json.build());
229+ public static String toJsonRPCString (String requestId , String method , com .google .protobuf .MessageOrBuilder builder ) {
230+ try (StringWriter result = new StringWriter (); JsonWriter output = GSON .newJsonWriter (result ).beginObject ()) {
231+ output .name ("jsonrpc" ).value ("2.0" );
232+ String id = requestId ;
233+ if (requestId == null ) {
234+ id = UUID .randomUUID ().toString ();
235+ }
236+ output .name ("id" ).value (id );
237+ if (method != null ) {
238+ output .name ("method" ).value (method );
239+ }
240+ output .name ("result" ).beginObject ().jsonValue (JsonFormat .printer ().print (builder ));
241+ output .endObject ().endObject ();
173242 return result .toString ();
174243 } catch (IOException ex ) {
175244 throw new RuntimeException (ex );
176245 }
177246 }
178247
179- public static String toJsonRPCString(String id, JSONRPCError error) {
180- try (StringWriter result = new StringWriter()) {
181- JsonObjectBuilder errorBuilder = Json.createObjectBuilder();
182- errorBuilder.add("code", error.getCode());
183- errorBuilder.add("message", error.getMessage());
184- if (error.getData() != null) {
185- errorBuilder.add("data", error.getData().toString());
186- }
187- JsonObjectBuilder json = Json.createObjectBuilder();
188- json.add("jsonrpc", "2.0");
189- json.add("id", id);
190- json.add("error", errorBuilder);
191- Json.createWriter(result).writeObject(json.build());
192- return result.toString();
193- } catch (IOException ex) {
194- throw new RuntimeException(ex);
195- }
196- }*/
248+ /**
249+ *
250+ * public static String toJsonRPCString(String id, com.google.protobuf.Message.Builder builder) {
251+ * try (StringWriter result = new StringWriter()) {
252+ * JsonObjectBuilder json = Json.createObjectBuilder();
253+ * json.add("jsonrpc", "2.0");
254+ * json.add("id", id);
255+ * json.add("result", Json.createObjectBuilder(Json.createParser(new
256+ * StringReader(JsonFormat.printer().print(builder))).getObject()));
257+ * Json.createWriter(result).writeObject(json.build());
258+ * return result.toString();
259+ * } catch (IOException ex) {
260+ * throw new RuntimeException(ex);
261+ * }
262+ * }
263+ *
264+ * public static String toJsonRPCString(String id, JSONRPCError error) {
265+ * try (StringWriter result = new StringWriter()) {
266+ * JsonObjectBuilder errorBuilder = Json.createObjectBuilder();
267+ * errorBuilder.add("code", error.getCode());
268+ * errorBuilder.add("message", error.getMessage());
269+ * if (error.getData() != null) {
270+ * errorBuilder.add("data", error.getData().toString());
271+ * }
272+ * JsonObjectBuilder json = Json.createObjectBuilder();
273+ * json.add("jsonrpc", "2.0");
274+ * json.add("id", id);
275+ * json.add("error", errorBuilder);
276+ * Json.createWriter(result).writeObject(json.build());
277+ * return result.toString();
278+ * } catch (IOException ex) {
279+ * throw new RuntimeException(ex);
280+ * }
281+ * }
282+ */
197283}
0 commit comments