Skip to content
This repository was archived by the owner on Oct 17, 2023. It is now read-only.

Commit 5fc6352

Browse files
authored
Lint and reformat codebase for Java 11 (#107)
* optimize imports * version bump * Automatic inspection and cleanup of code * broad qol improvements * optimized imports * bulk reformat * remove author blocks
1 parent a0f8fbe commit 5fc6352

File tree

107 files changed

+3221
-3439
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+3221
-3439
lines changed

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
group = com.hellosign
22
archivesBaseName = hellosign-java-sdk
3-
version = 5.0.0
3+
version = 5.0.1
44
description = HelloSign Java SDK
55

66
sourceCompatibility = 11
7-
targetCompatibility = 11
7+
targetCompatibility = 11

src/main/java/com/hellosign/sdk/HelloSignClient.java

Lines changed: 181 additions & 226 deletions
Large diffs are not rendered by default.

src/main/java/com/hellosign/sdk/HelloSignException.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.hellosign.sdk;
22

33
/**
4-
* This class wraps all hellosign-java-sdk exceptions. This allows a developer
5-
* to determine where the exception is coming from. It also allows us to store
6-
* the HTTP code and error type from API calls.
7-
*
8-
* @author "Chris Paul (chris@hellosign.com)"
4+
* This class wraps all hellosign-java-sdk exceptions. This allows a developer to determine where
5+
* the exception is coming from. It also allows us to store the HTTP code and error type from API
6+
* calls.
97
*/
108
public class HelloSignException extends Exception {
119

@@ -41,8 +39,8 @@ public HelloSignException(String message, Integer httpCode, String type, Excepti
4139
}
4240

4341
/**
44-
* Returns the HTTP code associated with a HelloSign API call. This may be
45-
* null if the exception does not involve an API request.
42+
* Returns the HTTP code associated with a HelloSign API call. This may be null if the exception
43+
* does not involve an API request.
4644
*
4745
* @return Integer or null if an HTTP code does not exist
4846
*/
@@ -51,8 +49,8 @@ public Integer getHttpCode() {
5149
}
5250

5351
/**
54-
* Returns the HelloSign API error type. This may be null if the exception
55-
* does not involve an API request.
52+
* Returns the HelloSign API error type. This may be null if the exception does not involve an
53+
* API request.
5654
*
5755
* @return String or null if the error type does not exist
5856
*/

src/main/java/com/hellosign/sdk/http/AbstractHttpRequest.java

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919

2020
public abstract class AbstractHttpRequest {
2121

22+
public static final String DEFAULT_ENCODING = "UTF-8";
23+
public static final String USER_AGENT = createUserAgent();
2224
private static final Logger logger = LoggerFactory.getLogger(AbstractHttpRequest.class);
23-
24-
public final static String DEFAULT_ENCODING = "UTF-8";
25-
public final static String USER_AGENT = createUserAgent();
26-
2725
// Request variables
2826
protected String url;
2927
protected Authentication auth;
@@ -53,11 +51,44 @@ private static String createUserAgent() {
5351
}
5452

5553
/**
56-
* Executes this HTTP request and preserves the response stream and HTTP
57-
* response code for processing.
54+
* Creates an HTTP connection.
55+
*
56+
* Optionally checks for proxy parameters and creates a proxied connection using the system
57+
* properties: "hellosign.proxy.url" - the URL of the HTTP proxy "hellosign.proxy.port" - the
58+
* port of the HTTP proxy
59+
*
60+
* @param url String URL to connect to
61+
* @return HttpUrlConnection the (proxied) connection to the URL
62+
* @throws MalformedURLException thrown if the URL is invalid
63+
* @throws IOException thrown if IO cannot be established with the URL
64+
*/
65+
protected static HttpURLConnection getProxiedConnection(String url)
66+
throws MalformedURLException, IOException {
67+
HttpURLConnection conn;
68+
Proxy proxy = null;
69+
String proxyUrlStr = System.getProperty("hellosign.proxy.url");
70+
String proxyPortStr = System.getProperty("hellosign.proxy.port");
71+
int proxyPort = 80; // Default to port 80
72+
if (proxyPortStr != null) {
73+
proxyPort = Integer.parseInt(proxyPortStr);
74+
}
75+
if (proxyUrlStr != null) {
76+
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyUrlStr, proxyPort));
77+
}
78+
if (proxy == null) {
79+
conn = (HttpURLConnection) new URL(url).openConnection();
80+
} else {
81+
conn = (HttpURLConnection) new URL(url).openConnection(proxy);
82+
}
83+
return conn;
84+
}
85+
86+
/**
87+
* Executes this HTTP request and preserves the response stream and HTTP response code for
88+
* processing.
5889
*
59-
* @throws HelloSignException Thrown if there is an error while making the
60-
* HTTP request to the HelloSign API.
90+
* @throws HelloSignException Thrown if there is an error while making the HTTP request to the
91+
* HelloSign API.
6192
*/
6293
public void execute() throws HelloSignException {
6394
HttpURLConnection connection = getConnection();
@@ -106,57 +137,23 @@ public String getResponseBody() {
106137
}
107138

108139
/**
109-
* Creates an HTTP connection.
110-
*
111-
* Optionally checks for proxy parameters and creates a proxied connection
112-
* using the system properties: "hellosign.proxy.url" - the URL of the HTTP
113-
* proxy "hellosign.proxy.port" - the port of the HTTP proxy
114-
*
115-
* @param url String URL to connect to
116-
* @return HttpUrlConnection the (proxied) connection to the URL
117-
* @throws MalformedURLException thrown if the URL is invalid
118-
* @throws IOException thrown if IO cannot be established with the URL
119-
*/
120-
protected static HttpURLConnection getProxiedConnection(String url)
121-
throws MalformedURLException, IOException {
122-
HttpURLConnection conn = null;
123-
Proxy proxy = null;
124-
String proxyUrlStr = System.getProperty("hellosign.proxy.url");
125-
String proxyPortStr = System.getProperty("hellosign.proxy.port");
126-
Integer proxyPort = 80; // Default to port 80
127-
if (proxyPortStr != null) {
128-
proxyPort = Integer.parseInt(proxyPortStr);
129-
}
130-
if (proxyUrlStr != null) {
131-
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyUrlStr, proxyPort));
132-
}
133-
if (proxy == null) {
134-
conn = (HttpURLConnection) new URL(url).openConnection();
135-
} else {
136-
conn = (HttpURLConnection) new URL(url).openConnection(proxy);
137-
}
138-
return conn;
139-
}
140-
141-
/**
142-
* The method class will create the appropriate connection with an endpoint,
143-
* parameters, etc.
140+
* The method class will create the appropriate connection with an endpoint, parameters, etc.
144141
*
145142
* @return HttpURLConnection
146143
* @throws HelloSignException Thrown if a connection cannot be created
147144
*/
148-
abstract protected HttpURLConnection getConnection() throws HelloSignException;
145+
protected abstract HttpURLConnection getConnection() throws HelloSignException;
149146

150147
/**
151148
* Write the last response to a file.
152149
*
153150
* @param f File
154151
* @return long bytes written
155-
* @throws HelloSignException Thrown if an exception occurs during the copy
156-
* of the response stream to the given file.
152+
* @throws HelloSignException Thrown if an exception occurs during the copy of the response
153+
* stream to the given file.
157154
*/
158155
public long getResponseAsFile(File f) throws HelloSignException {
159-
long bytesWritten = 0;
156+
long bytesWritten;
160157
try {
161158
bytesWritten = Files
162159
.copy(lastResponseStream, f.toPath(), StandardCopyOption.REPLACE_EXISTING);

src/main/java/com/hellosign/sdk/http/Authentication.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,18 @@
77
import org.slf4j.LoggerFactory;
88

99
/**
10-
* This class provides convenience methods for handling authentication
11-
* information for a HelloSignClient instance. We usually only want to use one
12-
* of these methods, but don't necessarily want to have to pass around all of
13-
* the information in method parameters.
14-
*
15-
* @author Chris Paul (chris@hellosign.com)
10+
* This class provides convenience methods for handling authentication information for a
11+
* HelloSignClient instance. We usually only want to use one of these methods, but don't necessarily
12+
* want to have to pass around all of the information in method parameters.
1613
*/
1714
public class Authentication {
1815

1916
private static final Logger logger = LoggerFactory.getLogger(Authentication.class);
20-
21-
private String apiKey = new String();
22-
private String accessToken = new String();
23-
private String accessTokenType = new String();
24-
2517
private static final String[] allowedOauthOps = {"account", "signature_request",
2618
"reusable_form", "template"};
19+
private String apiKey = "";
20+
private String accessToken = "";
21+
private String accessTokenType = "";
2722

2823
public Authentication() {
2924
}
@@ -47,7 +42,7 @@ public Authentication(Authentication clone) throws HelloSignException {
4742
* @return String API key
4843
*/
4944
public String getApiKey() {
50-
return new String(apiKey);
45+
return apiKey;
5146
}
5247

5348
/**
@@ -59,7 +54,7 @@ public void setApiKey(String apiKey) {
5954
if (apiKey == null) {
6055
return;
6156
}
62-
this.apiKey = new String(apiKey);
57+
this.apiKey = apiKey;
6358
}
6459

6560
/**
@@ -77,7 +72,7 @@ public boolean hasApiKey() {
7772
* @return String access token
7873
*/
7974
public String getAccessToken() {
80-
return new String(accessToken);
75+
return accessToken;
8176
}
8277

8378
/**
@@ -86,16 +81,15 @@ public String getAccessToken() {
8681
* @return String access token type
8782
*/
8883
public String getAccessTokenType() {
89-
return new String(accessTokenType);
84+
return accessTokenType;
9085
}
9186

9287
/**
9388
* Sets the access token for the HelloSign client authentication.
9489
*
9590
* @param accessToken String
9691
* @param tokenType String
97-
* @throws HelloSignException if either the accessToken or tokenType are
98-
* null
92+
* @throws HelloSignException if either the accessToken or tokenType are null
9993
*/
10094
public void setAccessToken(String accessToken, String tokenType) throws HelloSignException {
10195
if (accessToken == null) {
@@ -104,8 +98,8 @@ public void setAccessToken(String accessToken, String tokenType) throws HelloSig
10498
if (tokenType == null) {
10599
throw new HelloSignException("Token Type cannot be null");
106100
}
107-
this.accessToken = new String(accessToken);
108-
this.accessTokenType = new String(tokenType);
101+
this.accessToken = accessToken;
102+
this.accessTokenType = tokenType;
109103
}
110104

111105
/**

src/main/java/com/hellosign/sdk/http/HttpClient.java

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import javax.net.ssl.HostnameVerifier;
1212
import javax.net.ssl.HttpsURLConnection;
1313
import javax.net.ssl.SSLContext;
14-
import javax.net.ssl.SSLSession;
1514
import javax.net.ssl.TrustManager;
1615
import javax.net.ssl.X509TrustManager;
1716
import org.json.JSONException;
@@ -35,7 +34,7 @@ public class HttpClient {
3534

3635
public HttpClient() {
3736
String disableSslCheck = System.getProperty("hellosign.disable.ssl");
38-
if (disableSslCheck != null && "true".equalsIgnoreCase(disableSslCheck)) {
37+
if ("true".equalsIgnoreCase(disableSslCheck)) {
3938
disableStrictSSL();
4039
}
4140
}
@@ -47,7 +46,7 @@ public HttpClient withAuth(Authentication auth) {
4746

4847
public HttpClient withGetParam(String key, String value) {
4948
if (getParams == null) {
50-
getParams = new HashMap<String, String>();
49+
getParams = new HashMap<>();
5150
}
5251
getParams.put(key, value);
5352
return this;
@@ -60,7 +59,7 @@ public HttpClient withGetParams(Map<String, String> params) {
6059

6160
public HttpClient withPostField(String key, Serializable value) {
6261
if (postFields == null) {
63-
postFields = new HashMap<String, Serializable>();
62+
postFields = new HashMap<>();
6463
}
6564
postFields.put(key, value);
6665
return this;
@@ -100,21 +99,18 @@ public Integer getLastResponseCode() {
10099
*
101100
* @param f File that should contain the response
102101
* @return long bytes written
103-
* @throws HelloSignException thrown if there is a problem writing to the
104-
* file or reading the response stream
102+
* @throws HelloSignException thrown if there is a problem writing to the file or reading the
103+
* response stream
105104
*/
106105
public long getLastResponseAsFile(File f) throws HelloSignException {
107106
return request.getResponseAsFile(f);
108107
}
109108

110109
/**
111-
* Inspects the JSONObject response for errors and throws an exception if
112-
* found.
110+
* Inspects the JSONObject response for errors and throws an exception if found.
113111
*
114112
* @param json JSONObject response
115-
* @param code HTTP response code
116-
* @throws HelloSignException thrown if an error is reported from the API
117-
* call
113+
* @throws HelloSignException thrown if an error is reported from the API call
118114
*/
119115
private void validate(JSONObject json) throws HelloSignException {
120116
if (json.has("error")) {
@@ -143,11 +139,10 @@ private void reset() {
143139
* Executes the request and returns the response as a JSONObject.
144140
*
145141
* @return JSONObject response
146-
* @throws HelloSignException thrown if there is a problem executing the
147-
* request
142+
* @throws HelloSignException thrown if there is a problem executing the request
148143
*/
149144
public JSONObject asJson() throws HelloSignException {
150-
JSONObject json = null;
145+
JSONObject json;
151146
String response = getLastResponse();
152147
logger.debug("Response body: " + response);
153148
try {
@@ -168,12 +163,11 @@ public JSONObject asJson() throws HelloSignException {
168163
*
169164
* @param fileName String name of destination file
170165
* @return File response
171-
* @throws HelloSignException thrown if there is a problem executing the
172-
* request
166+
* @throws HelloSignException thrown if there is a problem executing the request
173167
*/
174168
public File asFile(String fileName) throws HelloSignException {
175169
Integer lastResponseCode = getLastResponseCode();
176-
File f = null;
170+
File f;
177171
try {
178172
if (lastResponseCode != null && lastResponseCode != HttpURLConnection.HTTP_OK) {
179173
this.asJson(); // Will validate response
@@ -199,11 +193,11 @@ public File asFile(String fileName) throws HelloSignException {
199193
*/
200194
private File createTemporaryFile(String filename) throws HelloSignException {
201195
String prefix = filename.substring(0, filename.indexOf("."));
202-
String postfix = filename.substring(filename.indexOf(".") + 1, filename.length());
203-
if (prefix == null || postfix == null) {
196+
String postfix = filename.substring(filename.indexOf(".") + 1);
197+
if (prefix.isBlank() || postfix.isBlank()) {
204198
throw new HelloSignException("Invalid file name: " + prefix + "." + postfix);
205199
}
206-
File f = null;
200+
File f;
207201
try {
208202
f = File.createTempFile(prefix, "." + postfix);
209203
} catch (Exception ex) {
@@ -302,8 +296,7 @@ public HttpClient options(String url) throws HelloSignException {
302296
}
303297

304298
/**
305-
* Helper method that allows this client to ignore SSL certificates when
306-
* making API requests.
299+
* Helper method that allows this client to ignore SSL certificates when making API requests.
307300
*/
308301
private void disableStrictSSL() {
309302
// Create a trust manager that does not validate certificate chains
@@ -320,11 +313,7 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
320313
}};
321314

322315
// Ignore differences between given hostname and certificate hostname
323-
HostnameVerifier hv = new HostnameVerifier() {
324-
public boolean verify(String hostname, SSLSession session) {
325-
return true;
326-
}
327-
};
316+
HostnameVerifier hv = (hostname, session) -> true;
328317

329318
// Install the all-trusting trust manager
330319
try {

0 commit comments

Comments
 (0)