- Saved searches
- Use saved searches to filter your results more quickly
- FedericoMartinese/JSONRPC
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- JSON-RPC 2.0 Client
- 2. Requirements
- 3. Installation
- 4. Example
- 5. Options
- 6. Setting custom HTTP URL connection properties
- 8. JavaDoc documentation
- 9. Download
- 10. Change log
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Project for Software Engineering at Polimi. Java library for use of JSON-RPC 2.0 communication protocol.
FedericoMartinese/JSONRPC
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Project for Software Engineering at Polimi. Java library for use of JSON-RPC 2.0 communication protocol.
This module is part of a bigger project that uses this library for remote communication between clients and a server in order to request and give authorizations to specific users for specific resources.
This library implements JSON-RPC 2.0 communication protocol (spec: https://www.jsonrpc.org/specification) over sockets, using ZeroMQ.
The server and the client are the actors. Receiving and sending requests, notifications and responses are the goals of the serves and the client. These goals are reached thanks to the showed tasks.
The diagram was required to be in Italian.
JSON-RPC 2.0 Client
This Java library provides a simple class for establishing client sessions to JSON-RPC 2.0 servers.
- Create a new session to the desired JSON-RPC 2.0 server URL.
- Use the session object to send JSON-RPC 2.0 requests to the server and receive the corresponding responses. Sending of JSON-RPC 2.0 notifications is also supported.
The JSON-RPC 2.0 messages are transported with HTTP(S) POST.
Apart from the basic functionality — sending JSON-RPC 2.0 requests + notifications and receiving responses, this library also has a number of configuration options:
- Customise the «Content-Type» header in HTTP POST requests.
- Set an «Origin» header in HTTP POST requests to simulate Cross-Origin Resource Sharing (CORS) requests from a browser.
- Accept HTTP cookies, for JSON-RPC services that use this out-of-channel method for keeping track of user sessions.
- Customise the allowable «Content-Type» header values in HTTP POST responses.
- Set an HTTP proxy.
- Enable HTTP response compression using GZIP and DEFLATE content encoding.
- Preserve the parse order of JSON object members in JSON-RPC 2.0 response results (for human facing clients, such as the JSON-RPC 2.0 Shell ).
- Ignore version 2.0 checks when parsing responses to allow client sessions to older JSON-RPC (1.0) servers.
- Parse non-standard attributes in JSON-RPC 2.0 responses.
- Set timeouts for HTTP server connect and read operations.
- Trust all X.509 server certificates (for secure HTTPS connections), including self-signed certificates.
2. Requirements
- Supported JSON-RPC protocol version: 2.0
- JSON-RPC 2.0 transport mechanism: HTTP (POST)
- Java Runtime: 1.5+
- Package dependencies:
- JSON-RPC 2.0 Base for creating, serialising and parsing JSON-RPC 2.0 messages (also available on this web site, JAR included in the download package for convenience).
- JSON Smart for encoding and decoding JSON text (JAR included in the download package for convenience).
3. Installation
Place the downloaded jsonrpc2-client-.jar file as well as the required jsonrpc2-base-.jar and json-smart-.jar dependencies in your Java CLASSPATH.
If you use Maven to build your project:
com.thetransactioncompany jsonrpc2-client 4. Example
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.*;
Then, create a new session to a JSON-RPC 2.0 web service at a specified URL, e.g. http://jsonrpc.example.com:8080 :
// 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());
The complete Java example can be downloaded from here.
5. Options
The JSON-RPC 2.0 client session options are handled by the JSONRPC2SessionOptions class.
Setting a custom «Content-Type» header in HTTP POST requests (the default is application/json ):
mySession.getOptions().setRequestContentType("application/json+rpc");
Setting an «Origin» header to simulate a CORS request from a browser:
mySession.getOptions().setOrigin("http://my-domain.com");
Turning on support for HTTP cookies, to accomodate, for example, JSON-RPC servers that use cookies for handling the user sessions:
mySession.getOptions().acceptCookies(true);
Changing the allowable «Content-Type» header values in HTTP POST responses (the default allowable are «application/json» and «text/plain»):
mySession.getOptions().setAllowedResponseContentTypes(new String[]);
To preserve the parse order of JSON object members in JSON-RPC 2.0 response results (may slow down performance a bit):
mySession.getOptions().preserveParseOrder(true);
To ignore JSON-RPC 2.0 version checking:
mySession.getOptions().ignoreVersion(true);
To parse non-standard attributes (such as meta or debugging fields) included in JSON-RPC 2.0 responses:
mySession.getOptions().parseNonStdAttributes(true);
To set specific timeouts (in milliseconds) for HTTP connect and read operations to the remote JSON-RPC 2.0 server:
mySession.getOptions().setConnectTimeout(3000); // 3 seconds mySession.getOptions().setReadTimeout(1000); // second
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 8080)); mySession.getOptions().setProxy(proxy);
To enable HTTP response compression using GZIP or DEFLATE content encoding (if the web server doesn’t support compression this setting will have no effect):
mySession().getOptions().enableCompression(true);
To trust self-signed certificates presented by the JSON-RPC 2.0 server (for secure HTTPS connections; note: use only for testing and development purposes!):
mySession.getOptions().trustAllCerts(true);
6. Setting custom HTTP URL connection properties
The client library also supports a mechanism to configure the underlying HttpURLConnection objects before the actual connections are established. You may need this to set custom HTTP request properties such as
To do this implement a ConnectionConfigurator and pass it to the JSONRPC2Session class. Your connection configurator will be called after the session options are applied and before establishing the actual HTTP connection.
Example custom configurator to set HTTP basic authentication:
import com.thetransactioncompany.jsonrpc2.client.ConnectionConfigurator; public class BasicAuthenticator implements ConnectionConfigurator < public void configure(HttpURLConnection connection) < // add custom HTTP header connection.addRequestProperty("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ= section-7">7. Inspecting the raw HTTP responses
For client applications that need to access specific headers or the raw content (e.g. for debugging purposes) of the HTTP response to a JSON-RPC 2.0 request or notification there is a dedicated inspection interface.
import com.thetransactioncompany.jsonrpc2.client.RawResponse; import com.thetransactioncompany.jsonrpc2.client.RawResponseInspector; public class MyInspector implements RawResponseInspector < public void inspect(RawResponse response) < // print the HTTP status code System.out.println("HTTP status: " + response.getStatusCode()); // print the value of the "Date" HTTP header System.out.println("Date: " + response.getHeaderField("Date")); >>
Configuring the JSON-RPC 2.0 session object to invoke the inspector every time when a HTTP response is received:
JSONRPC2Session session = new JSONRPC2Session(. ); session.setRawResponseInspector(new MyInspector());
8. JavaDoc documentation
The JSON-RPC 2.0 Client source comes with rich documentation in the form of JavaDocs. A copy of the docs is available for browsing online.
9. Download
The JSON-RPC 2.0 Client package is offered with an Apache 2.0 open source license.
Get in touch with me if you have questions or comments about the JSON-RPC 2.0 Server software. For general questions go to the JSON-RPC forum.
You may also want to have a look at the companion JSON-RPC 2.0 Shell product, a must have tool for developers undertaking serious JSON-RPC 2.0 work.
10. Change log
- version 1.0 (2010-11-09)
- First release.
- Major refactoring and API change.
- Adds optional setting for custom "Content-Type" header in HTTP POST requests.
- Adds optional setting to customise the allowable "Content-Type" header values in HTTP POST responses.
- Adds optional setting to disable strict JSON-RPC 2.0 parsing of responses to allow sessions with older JSON-RPC (1.0) servers.
- Adds optional setting to trust self-signed server sertificates for secure HTTPS connections.
- Skips ID match checking for JSON-RPC 2.0 responses indicating errors -32700 (parse error), -32600 (invalid request) and -32603 (internal error).
- Raises minimal JSON-RPC 2.0 Base requirement to version 1.19.
- Deprecates JSONRPC2Session.disableStrictParsing() in favour of JSONRPC2Session.ignoreVersion().
- Deprecates JSONRPC2Session.strictParsingDisabled() in favour of JSONRPC2Session.ignoresVersion().
- Adds option to parse non-standard attributes appended to JSON-RPC 2.0 responses.
- Moves all optional session properties and methods into a separate JSONPRC2SessionOptions class.
- Adds a JUnit test for the JSONRPC2SessionOptions class.
- Adds an interface for specifying a custom HTTP URL connection configurator.
- Adds an interface and class for inspecting the raw HTTP response.
- Adds a configuration option to accept HTTP cookies.
- Provides additional JSON-RPC 2.0 session exception cause types.
- Updates JSON-RPC 2.0 Base JAR to 1.25.1 (JSON Smart 1.1.1).
- Updates JSON-RPC 2.0 Base JAR to 1.27.
- Introduces option for controlling HTTP connection and read timeouts.
- Upgrades JSON-RPC 2.0 Base JAR to 1.30.
- Adds HTTP proxy support.
- Fixes JSONRPC2SessionException message on missing Content-Type header in HTTP response.
- Upgrades JSON-RPC 2.0 Base JAR to 1.31.
- Adds support for HTTP response compression using GZIP and DEFLATE content encoding.
- Switches cookie handling to java.net.CookieManager to add support for cookie replacement and expiration.
- Forces UTF-8 output for POSTed JSON-RPC 2.0 strings.
- Switches project build from Ant to Maven.
- Expects UTF-8 input for HTTP responses.
- Upgrades to JSON-RPC 2.0 Base 1.34.
- Updates Maven pom.xml.
- Fixes bug in assembly-dist.xml.
- Publishes library to Maven Central.
- Fixes exception reporting on request / response ID mismatch.
- Cleans up code.
- Upgrades to JSON-RPC 2.0 Base 1.36.
- Upgrades to JSON Smart 1.3.1.