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 procedure
orsend a
Notification
.- Since:
- 0.1.0
- Author:
- Elias*
-
-
Field Summary
Fields Modifier and Type Field Description protected HttpClient
client
The client used by thisConnection
.protected com.google.gson.Gson
gson
The gson instance used for serialization and deserialization.protected Optional<String>
id
The optional id of thisConnection
.protected HttpRequest.Builder
reqBuilder
The request builder used for new requests.protected int
requestId
The next request id.
-
Constructor Summary
Constructors Modifier Constructor Description protected
Connection(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.JsonElement
callRemoteProcedure(String method)
Calls a remote procedure.com.google.gson.JsonElement
callRemoteProcedure(String method, com.google.gson.JsonArray params)
Calls a remote procedure.com.google.gson.JsonElement
callRemoteProcedure(String method, com.google.gson.JsonObject params)
Calls a remote procedure.protected com.google.gson.JsonElement
checkResponse(Request req, Response res)
Checks the response if the ids match and no error is received.Optional<Response[]>
sendBatchRequest(Request... requests)
Sends an array ofRequest
objects to the server.void
sendNotification(String method)
Sends aNotification
with the specified method.void
sendNotification(String method, com.google.gson.JsonArray params)
Sends aNotification
with the specified method and parameters.void
sendNotification(String method, com.google.gson.JsonObject params)
Sends aNotification
with the specified method and parameters.Optional<Response>
sendRequest(Request req)
Sends aRequest
object 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 aRequest
object to the server.If the request is a
Notification
the returnedOptional
will be empty.- Parameters:
req
- TheRequest
orNotification
- 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
Request
with 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 aResponse
with anError
is receivedIdMismatchException
- If theResponse
does 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
Request
with 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 aResponse
with anError
is receivedIdMismatchException
- If theResponse
does 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
Request
with 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 aResponse
with anError
is receivedIdMismatchException
- If theResponse
does not have the same id as theRequest
.
-
sendNotification
public void sendNotification(String method) throws ConnectionException
Sends aNotification
with 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 aNotification
with 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 aNotification
with 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 ofRequest
objects to the server.If all requests are
Notifications
the returnedOptional
will be empty.- Parameters:
requests
- TheRequests
orNotifications
- Returns:
- A optional
Response
array - 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 corespondingRequest
res
- 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
-
-