Skip to content

Commit

Permalink
PB-16057. Add support for custom request header, Add support for read…
Browse files Browse the repository at this point in the history
…ing system properties
  • Loading branch information
john committed Sep 6, 2018
1 parent 64295d0 commit 58715ab
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 48 deletions.
50 changes: 28 additions & 22 deletions sdk/src/main/java/com/silanis/esl/sdk/EslClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -59,15 +60,18 @@ public class EslClient {
private SigningService signingService;
private SignerVerificationService signerVerificationService;

public static final String API_KEY = "apiKey";
public static final String BASE_URL = "baseURL";

/**
* The constructor of the EslClient class
*
* @param apiKey the api key token
* @param baseURL the eSignLive base url
*/
public EslClient(String apiKey, String baseURL) {
Asserts.notNullOrEmpty(apiKey, "apiKey");
Asserts.notNullOrEmpty(baseURL, "baseURL");
Asserts.notNullOrEmpty(apiKey, API_KEY);
Asserts.notNullOrEmpty(baseURL, BASE_URL);
setBaseURL(baseURL);
setWebpageURL(baseURL);
RestClient client = new RestClient(apiKey);
Expand All @@ -86,8 +90,8 @@ public EslClient(String apiKey, String baseURL, String webpageURL) {
}

public EslClient(String apiKey, String baseURL, String webpageURL, boolean allowAllSSLCertificates) {
Asserts.notNullOrEmpty(apiKey, "apiKey");
Asserts.notNullOrEmpty(baseURL, "baseURL");
Asserts.notNullOrEmpty(apiKey, API_KEY);
Asserts.notNullOrEmpty(baseURL, BASE_URL);
Asserts.notNullOrEmpty(webpageURL, "webpageURL");
setBaseURL(baseURL);
this.webpageURL = webpageURL;
Expand All @@ -104,11 +108,16 @@ public EslClient(String apiKey, String baseURL, ProxyConfiguration proxyConfigur
}

public EslClient(String apiKey, String baseURL, boolean allowAllSSLCertificates, ProxyConfiguration proxyConfiguration) {
Asserts.notNullOrEmpty(apiKey, "apiKey");
Asserts.notNullOrEmpty(baseURL, "baseURL");
this(apiKey, baseURL, allowAllSSLCertificates, proxyConfiguration, false, new HashMap<String, String>());
}

public EslClient(String apiKey, String baseURL, boolean allowAllSSLCertificates, ProxyConfiguration proxyConfiguration,
boolean useSystemProperties, Map<String, String> headers) {
Asserts.notNullOrEmpty(apiKey, API_KEY);
Asserts.notNullOrEmpty(baseURL, BASE_URL);
setBaseURL(baseURL);
setWebpageURL(baseURL);
RestClient client = new RestClient(apiKey, allowAllSSLCertificates, proxyConfiguration);
RestClient client = new RestClient(apiKey, allowAllSSLCertificates, proxyConfiguration, useSystemProperties, headers);
init(client);
}

Expand Down Expand Up @@ -282,7 +291,7 @@ public void changePackageStatusToDraft(PackageId packageId) {
* <p>Configure the document visibility.</p>
*
* @param packageId
* @param visibility the document visibility
* @param visibility the document visibility
*/
public void configureDocumentVisibility(PackageId packageId, DocumentVisibility visibility) {
packageService.configureDocumentVisibility(packageId, visibility);
Expand Down Expand Up @@ -320,9 +329,7 @@ public PackageId createPackageOneStep(DocumentPackage documentPackage) {
}
Collection<Document> documents = documentPackage.getDocuments();

PackageId id = packageService.createPackageOneStep(packageToCreate, documents);

return id;
return packageService.createPackageOneStep(packageToCreate, documents);
}

/**
Expand Down Expand Up @@ -520,7 +527,7 @@ public SessionToken createSenderSessionToken() {
* @deprecated Use the {@link com.silanis.esl.sdk.service.AuthenticationTokensService#createSignerAuthenticationToken}.
*/
@Deprecated
public SessionToken createSignerSessionToken(PackageId packageId, String signerId) throws EslException {
public SessionToken createSignerSessionToken(PackageId packageId, String signerId) {
return sessionService.createSessionToken(packageId.getId(), signerId);
}

Expand All @@ -531,12 +538,12 @@ public SessionToken createSignerSessionToken(PackageId packageId, String signerI
*
* @param packageId the package ID
* @param signerId the signer ID
* @throws EslException
* @return the session token
* @throws EslException
* @deprecated Use the {@link com.silanis.esl.sdk.service.AuthenticationTokensService#createSignerAuthenticationToken}.
*/
@Deprecated
public SessionToken createSessionToken(PackageId packageId, String signerId) throws EslException {
public SessionToken createSessionToken(PackageId packageId, String signerId) {
return sessionService.createSessionToken(packageId.getId(), signerId);
}

Expand All @@ -550,19 +557,19 @@ public DocumentPackage getPackage(PackageId packageId) {

/**
* @param packageId The document package identifier
* @param signerId the signer ID
* @param signerId the signer ID
* @return the document list based on document visibility for the specific signer
*/
public List<Document> getDocuments( PackageId packageId, String signerId ) {
public List<Document> getDocuments(PackageId packageId, String signerId) {
return packageService.getDocuments(packageId, signerId);
}

/**
* @param packageId The document package identifier
* @param documentId the document ID
* @param packageId The document package identifier
* @param documentId the document ID
* @return the signer list based on document visibility for the specific document
*/
public List<Signer> getSigners( PackageId packageId, String documentId ) {
public List<Signer> getSigners(PackageId packageId, String documentId) {
return packageService.getSigners(packageId, documentId);
}

Expand Down Expand Up @@ -629,7 +636,7 @@ public List<Document> uploadDocuments(PackageId packageId, Document... documents
}

public List<Document> uploadDocuments(PackageId packageId, List<Document> documents) {
if(documents.isEmpty()) {
if (documents.isEmpty()) {
return Collections.emptyList();
} else {
return packageService.uploadDocuments(packageId.getId(), documents);
Expand Down Expand Up @@ -668,8 +675,7 @@ public void createSignerVerification(PackageId packageId, String roleId, SignerV
public SignerVerification getSignerVerification(PackageId packageId, String roleId) {
Verification verification = signerVerificationService.getSignerVerification(packageId.getId(), roleId);

SignerVerification signerVerification = new SignerVerificationConverter().toSDKSignerVerification(verification);
return signerVerification;
return new SignerVerificationConverter().toSDKSignerVerification(verification);
}

public void updateSignerVerification(PackageId packageId, String roleId, SignerVerification signerVerification) {
Expand Down
63 changes: 37 additions & 26 deletions sdk/src/main/java/com/silanis/esl/sdk/internal/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,21 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import static com.silanis.esl.sdk.internal.HttpUtil.percentDecode;
import static com.silanis.esl.sdk.internal.MimeTypeUtils.getContentTypeByFileName;

public class RestClient {

public static final String CHARSET_UTF_8 = "UTF-8";
private static final int BUFFER_SIZE = 4096;

public static final String ESL_API_VERSION = "11.18";
public static final String ESL_API_VERSION_HEADER = "esl-api-version=" + ESL_API_VERSION;
Expand All @@ -62,6 +61,7 @@ public class RestClient {
public static final String ESL_CONTENT_TYPE_APPLICATION_JSON = CONTENT_TYPE_APPLICATION_JSON + "; " + ESL_API_VERSION_HEADER;

public static final String HEADER_KEY_ACCEPT = "Accept";
public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition";
public static final String ACCEPT_TYPE_APPLICATION_JSON = "application/json";
public static final String ACCEPT_TYPE_APPLICATION_OCTET_STREAM = "application/octet-stream";
public static final String ACCEPT_TYPE_APPLICATION = "*/*";
Expand All @@ -75,6 +75,8 @@ public class RestClient {
private final String apiToken;
private final Support support = new Support();
private final boolean allowAllSSLCertificates;
private final boolean useSystemProperties;
private Map<String, String> additionalHeaders = new HashMap<String, String>();

private ProxyConfiguration proxyConfiguration;

Expand All @@ -91,12 +93,18 @@ public RestClient(String apiToken, ProxyConfiguration proxyConfiguration) {
}

public RestClient(String apiToken, boolean allowAllSSLCertificates, ProxyConfiguration proxyConfiguration) {
this(apiToken, allowAllSSLCertificates, proxyConfiguration, false, new HashMap<String, String>());
}

public RestClient(String apiToken, boolean allowAllSSLCertificates, ProxyConfiguration proxyConfiguration, boolean useSystemProperties, Map<String, String> headers) {
this.apiToken = apiToken;
this.allowAllSSLCertificates = allowAllSSLCertificates;
this.proxyConfiguration = proxyConfiguration;
this.useSystemProperties = useSystemProperties;
this.additionalHeaders = headers;
}

public String post(String path, String jsonPayload) throws IOException, HttpException, URISyntaxException, RequestException {
public String post(String path, String jsonPayload) throws IOException, RequestException {
support.logRequest("POST", path, jsonPayload);

HttpPost post = new HttpPost(path);
Expand All @@ -117,15 +125,15 @@ public String put(String path, String jsonPayload) throws IOException, RequestEx

HttpPut put = new HttpPut(path);
put.addHeader(buildAcceptHeaderForEslApi());
StringEntity body = new StringEntity(jsonPayload, Charset.forName("UTF-8"));
StringEntity body = new StringEntity(jsonPayload, Charset.forName(CHARSET_UTF_8));

body.setContentType(ESL_CONTENT_TYPE_APPLICATION_JSON);
put.setEntity(body);

return execute(put, jsonHandler);
}

public String postMultipartFile(String path, String fileName, byte[] fileBytes, String jsonPayload) throws IOException, HttpException, URISyntaxException, RequestException {
public String postMultipartFile(String path, String fileName, byte[] fileBytes, String jsonPayload) throws IOException, RequestException {
support.logRequest("POST", path, jsonPayload);

final MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
Expand All @@ -139,7 +147,7 @@ public String postMultipartFile(String path, String fileName, byte[] fileBytes,
return execute(post, jsonHandler);
}

public String postMultipartPackage(String path, Collection<Document> documents, String jsonPayload) throws IOException, HttpException, URISyntaxException, RequestException {
public String postMultipartPackage(String path, Collection<Document> documents, String jsonPayload) throws IOException, RequestException {
support.logRequest("POST", path, jsonPayload);

final MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
Expand All @@ -155,6 +163,12 @@ public String postMultipartPackage(String path, Collection<Document> documents,
return execute(post, jsonHandler);
}

private void addAdditionalHeaders(HttpUriRequest request) {
for (Map.Entry<String, String> entry : additionalHeaders.entrySet()) {
request.setHeader(entry.getKey(), entry.getValue());
}
}

private StringBody buildPartForPayload(String jsonPayload) {
return new StringBody(jsonPayload, ContentType.create("text/plain", Consts.UTF_8));
}
Expand Down Expand Up @@ -190,6 +204,7 @@ private <T> T execute(HttpUriRequest request, ResponseHandler<T> handler) throws
}

try {
addAdditionalHeaders(request);
addAuthorizationHeader(request);
support.log(request);
CloseableHttpResponse response = client.execute(request);
Expand All @@ -205,11 +220,11 @@ private <T> T execute(HttpUriRequest request, ResponseHandler<T> handler) throws
return null;
} else {
InputStream bodyContent = response.getEntity().getContent();
if(null != response.getHeaders("Content-Disposition") && response.getHeaders("Content-Disposition").length > 0) {
String fileName = getFilename(response.getHeaders("Content-Disposition")[0].getValue());
if (null != response.getHeaders(HEADER_CONTENT_DISPOSITION) && response.getHeaders(HEADER_CONTENT_DISPOSITION).length > 0) {
String fileName = getFilename(response.getHeaders(HEADER_CONTENT_DISPOSITION)[0].getValue());
DownloadedFile downloadedFile = (DownloadedFile) handler.extract(bodyContent);
downloadedFile.setFilename(fileName);
return (T)downloadedFile;
return (T) downloadedFile;
}
return handler.extract(bodyContent);
}
Expand All @@ -224,7 +239,7 @@ private String getFilename(String disposition) {
String fileNameTitle = "filename*=UTF-8''";
String[] parts = disposition.split(";");

for(String part : parts) {
for (String part : parts) {
int index = part.indexOf(fileNameTitle);
if (index > 0) {
return percentDecode(part.substring(index + fileNameTitle.length(), part.length()));
Expand All @@ -240,6 +255,10 @@ private CloseableHttpClient buildHttpClient() throws HttpException {
httpClientBuilder.setSSLSocketFactory(buildSSLSocketFactory());
}

if (useSystemProperties) {
httpClientBuilder.useSystemProperties();
}

if (proxyConfiguration != null) {
if (proxyConfiguration.hasCredentials()) {
httpClientBuilder.setDefaultCredentialsProvider(buildCredentialsConfiguration(proxyConfiguration));
Expand Down Expand Up @@ -293,11 +312,11 @@ private CredentialsProvider buildCredentialsConfiguration(ProxyConfiguration pro
return credentialsProvider;
}

public String get(String path) throws IOException, HttpException, URISyntaxException, RequestException {
public String get(String path) throws IOException, RequestException {
return get(path, ESL_ACCEPT_TYPE_APPLICATION_JSON);
}

public String get(String path, String acceptType) throws IOException, HttpException, URISyntaxException, RequestException {
public String get(String path, String acceptType) throws IOException, RequestException {
support.logRequest("GET", path);
HttpGet get = new HttpGet(path);
get.addHeader(buildAcceptHeader(acceptType));
Expand All @@ -313,31 +332,23 @@ private Header buildAcceptHeader(String acceptType) {
return new BasicHeader(HEADER_KEY_ACCEPT, acceptType);
}

public DownloadedFile getBytes(String path) throws IOException, HttpException, URISyntaxException, RequestException {
public DownloadedFile getBytes(String path) throws IOException, RequestException {
return getBytes(path, ESL_ACCEPT_TYPE_APPLICATION);
}

public DownloadedFile getBytes(String path, String acceptType) throws IOException, HttpException, URISyntaxException, RequestException {
public DownloadedFile getBytes(String path, String acceptType) throws IOException, RequestException {
support.logRequest("GET", path);
HttpGet get = new HttpGet(path);
get.addHeader(buildAcceptHeader(acceptType));

return execute(get, bytesHandler);
}

public DownloadedFile getBytesAsOctetStream(String path) throws IOException, HttpException, URISyntaxException, RequestException {
return getBytesAsOctetStream(path, ESL_ACCEPT_TYPE_APPLICATION_OCTET_STREAM);
}

public DownloadedFile getBytesAsOctetStream(String path, String acceptType) throws IOException, HttpException, URISyntaxException, RequestException {
support.logRequest("GET", path);
HttpGet get = new HttpGet(path);
get.addHeader(buildAcceptHeader(acceptType));

return execute(get, bytesHandler);
public DownloadedFile getBytesAsOctetStream(String path) throws IOException, RequestException {
return getBytes(path, ESL_ACCEPT_TYPE_APPLICATION_OCTET_STREAM);
}

public String delete(String path) throws HttpException, IOException, URISyntaxException, RequestException {
public String delete(String path) throws IOException, RequestException {
support.logRequest("DELETE", path);
HttpDelete delete = new HttpDelete(path);
delete.addHeader(buildAcceptHeaderForEslApi());
Expand Down

0 comments on commit 58715ab

Please sign in to comment.