diff --git a/sdk/src/main/java/com/silanis/esl/sdk/EslClient.java b/sdk/src/main/java/com/silanis/esl/sdk/EslClient.java index b70a044b9..56f902717 100644 --- a/sdk/src/main/java/com/silanis/esl/sdk/EslClient.java +++ b/sdk/src/main/java/com/silanis/esl/sdk/EslClient.java @@ -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; @@ -59,6 +60,9 @@ 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 * @@ -66,8 +70,8 @@ public class EslClient { * @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); @@ -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; @@ -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()); + } + + public EslClient(String apiKey, String baseURL, boolean allowAllSSLCertificates, ProxyConfiguration proxyConfiguration, + boolean useSystemProperties, Map 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); } @@ -282,7 +291,7 @@ public void changePackageStatusToDraft(PackageId packageId) { *

Configure the document visibility.

* * @param packageId - * @param visibility the document visibility + * @param visibility the document visibility */ public void configureDocumentVisibility(PackageId packageId, DocumentVisibility visibility) { packageService.configureDocumentVisibility(packageId, visibility); @@ -320,9 +329,7 @@ public PackageId createPackageOneStep(DocumentPackage documentPackage) { } Collection documents = documentPackage.getDocuments(); - PackageId id = packageService.createPackageOneStep(packageToCreate, documents); - - return id; + return packageService.createPackageOneStep(packageToCreate, documents); } /** @@ -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); } @@ -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); } @@ -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 getDocuments( PackageId packageId, String signerId ) { + public List 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 getSigners( PackageId packageId, String documentId ) { + public List getSigners(PackageId packageId, String documentId) { return packageService.getSigners(packageId, documentId); } @@ -629,7 +636,7 @@ public List uploadDocuments(PackageId packageId, Document... documents } public List uploadDocuments(PackageId packageId, List documents) { - if(documents.isEmpty()) { + if (documents.isEmpty()) { return Collections.emptyList(); } else { return packageService.uploadDocuments(packageId.getId(), documents); @@ -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) { diff --git a/sdk/src/main/java/com/silanis/esl/sdk/internal/RestClient.java b/sdk/src/main/java/com/silanis/esl/sdk/internal/RestClient.java index 426e43b00..3c348921c 100644 --- a/sdk/src/main/java/com/silanis/esl/sdk/internal/RestClient.java +++ b/sdk/src/main/java/com/silanis/esl/sdk/internal/RestClient.java @@ -38,14 +38,14 @@ 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; @@ -53,7 +53,6 @@ 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; @@ -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 = "*/*"; @@ -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 additionalHeaders = new HashMap(); private ProxyConfiguration proxyConfiguration; @@ -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()); + } + + public RestClient(String apiToken, boolean allowAllSSLCertificates, ProxyConfiguration proxyConfiguration, boolean useSystemProperties, Map 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); @@ -117,7 +125,7 @@ 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); @@ -125,7 +133,7 @@ public String put(String path, String jsonPayload) throws IOException, RequestEx 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(); @@ -139,7 +147,7 @@ public String postMultipartFile(String path, String fileName, byte[] fileBytes, return execute(post, jsonHandler); } - public String postMultipartPackage(String path, Collection documents, String jsonPayload) throws IOException, HttpException, URISyntaxException, RequestException { + public String postMultipartPackage(String path, Collection documents, String jsonPayload) throws IOException, RequestException { support.logRequest("POST", path, jsonPayload); final MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); @@ -155,6 +163,12 @@ public String postMultipartPackage(String path, Collection documents, return execute(post, jsonHandler); } + private void addAdditionalHeaders(HttpUriRequest request) { + for (Map.Entry 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)); } @@ -190,6 +204,7 @@ private T execute(HttpUriRequest request, ResponseHandler handler) throws } try { + addAdditionalHeaders(request); addAuthorizationHeader(request); support.log(request); CloseableHttpResponse response = client.execute(request); @@ -205,11 +220,11 @@ private T execute(HttpUriRequest request, ResponseHandler 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); } @@ -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())); @@ -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)); @@ -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)); @@ -313,11 +332,11 @@ 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)); @@ -325,19 +344,11 @@ public DownloadedFile getBytes(String path, String acceptType) throws IOExceptio 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());