001package com.thetransactioncompany.jsonrpc2.server.examples; 002 003 004import java.text.*; 005import java.util.*; 006 007import com.thetransactioncompany.jsonrpc2.*; 008import com.thetransactioncompany.jsonrpc2.server.*; 009 010 011/** 012 * Demonstrates use of the JSON-RPC 2.0 Server framework. The request handlers 013 * are implemented as static nested classes for convenience. 014 * 015 * @author Vladimir Dzhuvinov 016 */ 017public class Example { 018 019 020 // Implements a handler for an "echo" JSON-RPC method 021 public static class EchoHandler implements RequestHandler { 022 023 024 // Reports the method names of the handled requests 025 public String[] handledRequests() { 026 027 return new String[]{"echo"}; 028 } 029 030 031 // Processes the requests 032 public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) { 033 034 if (req.getMethod().equals("echo")) { 035 036 // Echo first parameter 037 038 List params = (List)req.getPositionalParams(); 039 040 Object input = params.get(0); 041 042 return new JSONRPC2Response(input, req.getID()); 043 } 044 else { 045 // Method name not supported 046 047 return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID()); 048 } 049 } 050 } 051 052 053 // Implements a handler for "getDate" and "getTime" JSON-RPC methods 054 // that return the current date and time 055 public static class DateTimeHandler implements RequestHandler { 056 057 058 // Reports the method names of the handled requests 059 public String[] handledRequests() { 060 061 return new String[]{"getDate", "getTime"}; 062 } 063 064 065 // Processes the requests 066 public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) { 067 068 if (req.getMethod().equals("getDate")) { 069 070 DateFormat df = DateFormat.getDateInstance(); 071 072 String date = df.format(new Date()); 073 074 return new JSONRPC2Response(date, req.getID()); 075 } 076 else if (req.getMethod().equals("getTime")) { 077 078 DateFormat df = DateFormat.getTimeInstance(); 079 080 String time = df.format(new Date()); 081 082 return new JSONRPC2Response(time, req.getID()); 083 } 084 else { 085 086 // Method name not supported 087 088 return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID()); 089 } 090 } 091 } 092 093 094 public static void main(String[] args) { 095 096 // Create a new JSON-RPC 2.0 request dispatcher 097 Dispatcher dispatcher = new Dispatcher(); 098 099 100 // Register the "echo", "getDate" and "getTime" handlers with it 101 dispatcher.register(new EchoHandler()); 102 dispatcher.register(new DateTimeHandler()); 103 104 // Simulate an "echo" JSON-RPC 2.0 request 105 List<Object> echoParam = new LinkedList<Object>(); 106 echoParam.add("Hello world!"); 107 108 JSONRPC2Request req = new JSONRPC2Request("echo", echoParam, "req-id-01"); 109 System.out.println("Request: \n" + req); 110 111 JSONRPC2Response resp = dispatcher.process(req, null); 112 System.out.println("Response: \n" + resp); 113 114 115 // Simulate a "getDate" JSON-RPC 2.0 request 116 req = new JSONRPC2Request("getDate", "req-id-02"); 117 System.out.println("Request: \n" + req); 118 119 resp = dispatcher.process(req, null); 120 System.out.println("Response: \n" + resp); 121 122 123 // Simulate a "getTime" JSON-RPC 2.0 request 124 req = new JSONRPC2Request("getTime", "req-id-03"); 125 System.out.println("Request: \n" + req); 126 127 resp = dispatcher.process(req, null); 128 System.out.println("Response: \n" + resp); 129 } 130}