001package com.thetransactioncompany.jsonrpc2.examples; 002 003 004import java.util.*; 005 006import com.thetransactioncompany.jsonrpc2.*; 007 008 009/** 010 * This example illustrates the typical life-cycle of a JSON-RPC 2.0 request. 011 * 012 * <ul> 013 * <li>Client side: Create new request 014 * <li>Client side: Serialise request to string and send 015 * <li>Server side: Parse received string back to request object 016 * <li>Server side: Get the request data 017 * <li>Server side: Create a response 018 * <li>Server side: Serialise response to string and send back 019 * <li>Client side: Parse received string back to response object 020 * <li>Client side: Check the response for success, get the result / error 021 * </ul> 022 * 023 * @author Vladimir Dzhuvinov 024 */ 025public class Example1 { 026 027 028 public static void main(String[] args) { 029 030 // The remote method to call 031 String method = "makePayment"; 032 033 // The required parameters to pass 034 Map<String,Object> params = new HashMap<String,Object>(); 035 params.put("recipient", "Penny Adams"); 036 params.put("amount", 175.05); 037 038 // The mandatory request ID 039 String id = "req-001"; 040 041 System.out.println("Creating new request with properties :"); 042 System.out.println("\tmethod : " + method); 043 System.out.println("\tparameters : " + params); 044 System.out.println("\tid : " + id + "\n\n"); 045 046 // Create request 047 JSONRPC2Request reqOut = new JSONRPC2Request(method, params, id); 048 049 050 // Serialise request to JSON-encoded string 051 String jsonString = reqOut.toString(); 052 053 System.out.println("Serialised request to JSON-encoded string :"); 054 System.out.println("\t" + jsonString + "\n\n"); 055 056 057 // The string can now be dispatched to the server... 058 059 060 // Parse request string 061 JSONRPC2Request reqIn; 062 063 try { 064 reqIn = JSONRPC2Request.parse(jsonString); 065 066 } catch (JSONRPC2ParseException e) { 067 System.out.println(e.getMessage()); 068 return; 069 } 070 071 // Extract the request data 072 System.out.println("Parsed request with properties :"); 073 System.out.println("\tmethod : " + reqIn.getMethod()); 074 System.out.println("\tparameters : " + reqIn.getNamedParams()); 075 System.out.println("\tid : " + reqIn.getID() + "\n\n"); 076 077 078 // Process request... 079 080 081 // The request result 082 String result = "payment-id-001"; 083 084 System.out.println("Creating response with properties : "); 085 System.out.println("\tresult : " + result); 086 System.out.println("\tid : " + reqIn.getID() + "\n\n"); // ID must be echoed back 087 088 089 // Create response 090 JSONRPC2Response respOut = new JSONRPC2Response(result, reqIn.getID()); 091 092 093 // Serialise response to JSON-encoded string 094 jsonString = respOut.toString(); 095 096 System.out.println("Serialised response to JSON-encoded string :"); 097 System.out.println("\t" + jsonString + "\n\n"); 098 099 100 // The response can now be sent back to the client... 101 102 103 // Parse response string 104 JSONRPC2Response respIn; 105 106 try { 107 respIn = JSONRPC2Response.parse(jsonString); 108 109 } catch (JSONRPC2ParseException e) { 110 System.out.println(e.getMessage()); 111 return; 112 } 113 114 115 // Check for success or error 116 if (respIn.indicatesSuccess()) { 117 System.out.println("The request succeeded :"); 118 119 System.out.println("\tresult : " + respIn.getResult()); 120 System.out.println("\tid : " + respIn.getID()); 121 122 } else { 123 System.out.println("The request failed :"); 124 125 JSONRPC2Error err = respIn.getError(); 126 127 System.out.println("\terror.code : " + err.getCode()); 128 System.out.println("\terror.message : " + err.getMessage()); 129 System.out.println("\terror.data : " + err.getData()); 130 } 131 } 132}