The Client object contains all information necessary to perform an HTTP request against a remote API. Typically, an application will have a client that makes requests to a Chain Core, and a separate Client that makes requests to an HSM server.
| 45 | * to an HSM server. |
| 46 | */ |
| 47 | public class Client { |
| 48 | private AtomicInteger urlIndex; |
| 49 | private List<URL> urls; |
| 50 | private String accessToken; |
| 51 | private OkHttpClient httpClient; |
| 52 | |
| 53 | // Used to create empty, in-memory key stores. |
| 54 | private static final char[] DEFAULT_KEYSTORE_PASSWORD = "password".toCharArray(); |
| 55 | private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); |
| 56 | private static String version = "dev"; // updated in the static initializer |
| 57 | |
| 58 | private static class BuildProperties { |
| 59 | public String version; |
| 60 | } |
| 61 | |
| 62 | static { |
| 63 | InputStream in = Client.class.getClassLoader().getResourceAsStream("properties.json"); |
| 64 | if (in != null) { |
| 65 | InputStreamReader inr = new InputStreamReader(in); |
| 66 | version = Utils.serializer.fromJson(inr, BuildProperties.class).version; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public Client(Builder builder) throws ConfigurationException { |
| 71 | List<URL> urls = new ArrayList<>(builder.urls); |
| 72 | if (urls.isEmpty()) { |
| 73 | try { |
| 74 | urls.add(new URL("http://localhost:1999")); |
| 75 | } catch (MalformedURLException e) { |
| 76 | throw new RuntimeException("invalid default development URL", e); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | this.urlIndex = new AtomicInteger(0); |
| 81 | this.urls = urls; |
| 82 | this.accessToken = builder.accessToken; |
| 83 | this.httpClient = buildHttpClient(builder); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Create a new http Client object using the default development host URL. |
| 88 | */ |
| 89 | public Client() throws ChainException { |
| 90 | this(new Builder()); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Create a new http Client object |
| 95 | * |
| 96 | * @param url the URL of the Chain Core or HSM |
| 97 | */ |
| 98 | public Client(String url) throws ChainException { |
| 99 | this(new Builder().setURL(url)); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Create a new http Client object |
| 104 | * |