public class JSONRPC2Session extends Object
The client-session class has a number of optional settings
. To change them pass a modified options instance to the
setOptions()
method.
Example JSON-RPC 2.0 client session:
// First, import the required packages: // The Client sessions package import com.thetransactioncompany.jsonrpc2.client.*; // The Base package for representing JSON-RPC 2.0 messages import com.thetransactioncompany.jsonrpc2.*; // The JSON Smart package for JSON encoding/decoding (optional) import net.minidev.json.*; // For creating URLs import java.net.*; // ... // Creating a new session to a JSON-RPC 2.0 web service at a specified URL // The JSON-RPC 2.0 server URL URL serverURL = null; try { serverURL = new URL("http://jsonrpc.example.com:8080"); } catch (MalformedURLException e) { // handle exception... } // Create new JSON-RPC 2.0 client session JSONRPC2Session mySession = new JSONRPC2Session(serverURL); // Once the client session object is created, you can use to send a series // of JSON-RPC 2.0 requests and notifications to it. // Sending an example "getServerTime" request: // Construct new request String method = "getServerTime"; int requestID = 0; JSONRPC2Request request = new JSONRPC2Request(method, requestID); // Send request JSONRPC2Response response = null; try { response = mySession.send(request); } catch (JSONRPC2SessionException e) { System.err.println(e.getMessage()); // handle exception... } // Print response result / error if (response.indicatesSuccess()) System.out.println(response.getResult()); else System.out.println(response.getError().getMessage());
Constructor and Description |
---|
JSONRPC2Session(URL url)
Creates a new client session to a JSON-RPC 2.0 server at the
specified URL.
|
Modifier and Type | Method and Description |
---|---|
static SSLSocketFactory |
createTrustAllSocketFactory()
Creates a trust-all-certificates SSL socket factory.
|
ConnectionConfigurator |
getConnectionConfigurator()
Gets the custom HTTP URL connection configurator.
|
List<HttpCookie> |
getCookies()
Gets all non-expired HTTP cookies currently stored in the client.
|
JSONRPC2SessionOptions |
getOptions()
Gets the JSON-RPC 2.0 client session options.
|
RawResponseInspector |
getRawResponseInspector()
Gets the optional inspector for the raw HTTP responses.
|
URL |
getURL()
Gets the JSON-RPC 2.0 server URL.
|
void |
send(com.thetransactioncompany.jsonrpc2.JSONRPC2Notification notification)
Sends a JSON-RPC 2.0 notification using HTTP POST.
|
com.thetransactioncompany.jsonrpc2.JSONRPC2Response |
send(com.thetransactioncompany.jsonrpc2.JSONRPC2Request request)
Sends a JSON-RPC 2.0 request using HTTP POST and returns the server
response.
|
void |
setConnectionConfigurator(ConnectionConfigurator connectionConfigurator)
Specifies a custom HTTP URL connection configurator.
|
void |
setOptions(JSONRPC2SessionOptions options)
Sets the JSON-RPC 2.0 client session options.
|
void |
setRawResponseInspector(RawResponseInspector responseInspector)
Specifies an optional inspector for the raw HTTP responses to
JSON-RPC 2.0 requests and notifications.
|
void |
setURL(URL url)
Sets the JSON-RPC 2.0 server URL.
|
public JSONRPC2Session(URL url)
JSONRPC2SessionOptions
instance.url
- The server URL, e.g. "http://jsonrpc.example.com:8080".
Must not be null
.public static SSLSocketFactory createTrustAllSocketFactory()
public void setURL(URL url)
url
- The server URL. Must not be null
.public JSONRPC2SessionOptions getOptions()
public void setOptions(JSONRPC2SessionOptions options)
options
- The client session options, must not be null
.public ConnectionConfigurator getConnectionConfigurator()
null
if none is set.public void setConnectionConfigurator(ConnectionConfigurator connectionConfigurator)
applied
to each new HTTP
connection after the session options
are applied and before the connection is established.
This method may be used to set custom HTTP request headers, timeouts or other properties.
connectionConfigurator
- A custom HTTP URL connection
configurator, null
to remove
a previously set one.public RawResponseInspector getRawResponseInspector()
null
if none is set.public void setRawResponseInspector(RawResponseInspector responseInspector)
inspect
method will be called
upon reception of a HTTP response.
You can use the RawResponseInspector
interface to
retrieve the unparsed response content and headers.
responseInspector
- An optional inspector for the raw HTTP
responses, null
to remove a
previously set one.public List<HttpCookie> getCookies()
accepted
.public com.thetransactioncompany.jsonrpc2.JSONRPC2Response send(com.thetransactioncompany.jsonrpc2.JSONRPC2Request request) throws JSONRPC2SessionException
request
- The JSON-RPC 2.0 request to send. Must not be
null
.JSONRPC2SessionException
- On a network error, unexpected HTTP
response content type or invalid
JSON-RPC 2.0 response.public void send(com.thetransactioncompany.jsonrpc2.JSONRPC2Notification notification) throws JSONRPC2SessionException
notification
- The JSON-RPC 2.0 notification to send. Must not
be null
.JSONRPC2SessionException
- On a network error.Copyright © 2015 The Transaction Company. All Rights Reserved.