Package eliasstar.jsonrpc
Class Connection
- java.lang.Object
-
- eliasstar.jsonrpc.Connection
-
public class Connection extends Object
This class is the primary way of interacting with a JSON-RPC service.Use it to
call a remote procedureorsend aNotification.- Since:
- 0.1.0
- Author:
- Elias*
-
-
Field Summary
Fields Modifier and Type Field Description protected HttpClientclientThe client used by thisConnection.protected com.google.gson.GsongsonThe gson instance used for serialization and deserialization.protected Optional<String>idThe optional id of thisConnection.protected HttpRequest.BuilderreqBuilderThe request builder used for new requests.protected intrequestIdThe next request id.
-
Constructor Summary
Constructors Modifier Constructor Description protectedConnection(String id, HttpClient client, HttpRequest.Builder reqBuilder, com.google.gson.Gson gson)Used byConnectionBuilder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description com.google.gson.JsonElementcallRemoteProcedure(String method)Calls a remote procedure.com.google.gson.JsonElementcallRemoteProcedure(String method, com.google.gson.JsonArray params)Calls a remote procedure.com.google.gson.JsonElementcallRemoteProcedure(String method, com.google.gson.JsonObject params)Calls a remote procedure.protected com.google.gson.JsonElementcheckResponse(Request req, Response res)Checks the response if the ids match and no error is received.Optional<Response[]>sendBatchRequest(Request... requests)Sends an array ofRequestobjects to the server.voidsendNotification(String method)Sends aNotificationwith the specified method.voidsendNotification(String method, com.google.gson.JsonArray params)Sends aNotificationwith the specified method and parameters.voidsendNotification(String method, com.google.gson.JsonObject params)Sends aNotificationwith the specified method and parameters.Optional<Response>sendRequest(Request req)Sends aRequestobject to the server.
-
-
-
Field Detail
-
client
protected final HttpClient client
The client used by thisConnection.
-
reqBuilder
protected final HttpRequest.Builder reqBuilder
The request builder used for new requests.
-
gson
protected final com.google.gson.Gson gson
The gson instance used for serialization and deserialization.
-
id
protected final Optional<String> id
The optional id of thisConnection.
-
requestId
protected int requestId
The next request id.
-
-
Constructor Detail
-
Connection
protected Connection(String id, HttpClient client, HttpRequest.Builder reqBuilder, com.google.gson.Gson gson)
Used byConnectionBuilder.- Parameters:
id- The id prefix used for requestsclient- The client used for requestsreqBuilder- The request builder used for new requestsgson- The gson instance used for serialization and deserialization.
-
-
Method Detail
-
sendRequest
public Optional<Response> sendRequest(Request req) throws ConnectionException
Sends aRequestobject to the server.If the request is a
Notificationthe returnedOptionalwill be empty.- Parameters:
req- TheRequestorNotification- Returns:
- A optional
Response - Throws:
ConnectionException- If sending fails
-
callRemoteProcedure
public com.google.gson.JsonElement callRemoteProcedure(String method) throws ConnectionException, ErrorResponseException, IdMismatchException
Calls a remote procedure.Sends a
Requestwith the specified method and checks the response.- Parameters:
method- The method to invoke- Returns:
- The result of the operation
- Throws:
ConnectionException- If sending failsErrorResponseException- If aResponsewith anErroris receivedIdMismatchException- If theResponsedoes not have the same id as theRequest.
-
callRemoteProcedure
public com.google.gson.JsonElement callRemoteProcedure(String method, com.google.gson.JsonArray params) throws ConnectionException, ErrorResponseException, IdMismatchException
Calls a remote procedure.Sends a
Requestwith the specified method and params and checks the response.- Parameters:
method- The method to invokeparams- The parameters of the method- Returns:
- The result of the operation
- Throws:
ConnectionException- If sending failsErrorResponseException- If aResponsewith anErroris receivedIdMismatchException- If theResponsedoes not have the same id as theRequest.
-
callRemoteProcedure
public com.google.gson.JsonElement callRemoteProcedure(String method, com.google.gson.JsonObject params) throws ConnectionException, ErrorResponseException, IdMismatchException
Calls a remote procedure.Sends a
Requestwith the specified method and params and checks the response.- Parameters:
method- The method to invokeparams- The parameters of the method- Returns:
- The result of the operation
- Throws:
ConnectionException- If sending failsErrorResponseException- If aResponsewith anErroris receivedIdMismatchException- If theResponsedoes not have the same id as theRequest.
-
sendNotification
public void sendNotification(String method) throws ConnectionException
Sends aNotificationwith the specified method.- Parameters:
method- The method to invoke- Throws:
ConnectionException- If sending fails
-
sendNotification
public void sendNotification(String method, com.google.gson.JsonArray params) throws ConnectionException
Sends aNotificationwith the specified method and parameters.- Parameters:
method- The method to invokeparams- The parameters of the method- Throws:
ConnectionException- If sending fails
-
sendNotification
public void sendNotification(String method, com.google.gson.JsonObject params) throws ConnectionException
Sends aNotificationwith the specified method and parameters.- Parameters:
method- The method to invokeparams- The parameters of the method- Throws:
ConnectionException- If sending fails
-
sendBatchRequest
public Optional<Response[]> sendBatchRequest(Request... requests) throws ConnectionException
Sends an array ofRequestobjects to the server.If all requests are
Notificationsthe returnedOptionalwill be empty.- Parameters:
requests- TheRequestsorNotifications- Returns:
- A optional
Responsearray - Throws:
ConnectionException- If sending fails
-
checkResponse
protected com.google.gson.JsonElement checkResponse(Request req, Response res) throws ErrorResponseException, IdMismatchException
Checks the response if the ids match and no error is received.- Parameters:
req- The corespondingRequestres- The response to be checked- Returns:
- The result of the
Response - Throws:
ErrorResponseException- If instead of a result a error was receivedIdMismatchException- If the ids do not match
-
-