Skip to content

Commit 488cfb6

Browse files
committed
Made ssl certificates configurable
1 parent 0866fa1 commit 488cfb6

File tree

2 files changed

+127
-105
lines changed

2 files changed

+127
-105
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ allprojects {
2424
}
2525

2626
project.ext {
27-
sdkVersion='0.8.6'
27+
sdkVersion='0.8.7'
2828
versionCode=1
2929

3030
compileSdkVersion=28

core/src/main/java/io/snabble/sdk/SnabbleSdk.java

Lines changed: 126 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import java.util.concurrent.CopyOnWriteArrayList;
2020
import java.util.concurrent.CountDownLatch;
2121

22+
import javax.net.ssl.SSLSocketFactory;
23+
import javax.net.ssl.X509TrustManager;
24+
2225
import io.snabble.sdk.utils.Downloader;
2326
import io.snabble.sdk.utils.JsonUtils;
2427
import io.snabble.sdk.utils.Logger;
@@ -65,13 +68,12 @@ public class SnabbleSdk {
6568
private String encodedCodesSeperator = null;
6669
private String encodedCodesSuffix = null;
6770

68-
6971
private void init(final Application app,
7072
final Config config,
7173
final SetupCompletionListener setupCompletionListener) {
7274
application = app;
7375

74-
createOkHttpClient(config.clientToken);
76+
createOkHttpClient(config.clientToken, config.sslSocketFactory, config.x509TrustManager);
7577
internalStorageDirectory = new File(app.getFilesDir(), "snabble/" + config.projectId + "/");
7678
//noinspection ResultOfMethodCallIgnored
7779
internalStorageDirectory.mkdirs();
@@ -147,6 +149,38 @@ protected void onError() {
147149
}
148150
}
149151

152+
private void createOkHttpClient(String clientToken,
153+
SSLSocketFactory sslSocketFactory,
154+
X509TrustManager x509TrustManager) {
155+
OkHttpClient.Builder builder = new OkHttpClient.Builder();
156+
157+
builder.cache(new Cache(application.getCacheDir(), 10485760)); //10 MB
158+
159+
builder.retryOnConnectionFailure(true);
160+
161+
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(
162+
new HttpLoggingInterceptor.Logger() {
163+
@Override
164+
public void log(String message) {
165+
Logger.i(message);
166+
}
167+
});
168+
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
169+
builder.addInterceptor(logging);
170+
171+
if (clientToken != null) {
172+
builder.addInterceptor(new SnabbleAuthorizationInterceptor(this, clientToken));
173+
}
174+
175+
builder.addInterceptor(new UserAgentInterceptor(application));
176+
177+
if(sslSocketFactory != null && x509TrustManager != null) {
178+
builder.sslSocketFactory(sslSocketFactory, x509TrustManager);
179+
}
180+
181+
okHttpClient = builder.build();
182+
}
183+
150184
private SnabbleSdk() {
151185

152186
}
@@ -313,108 +347,6 @@ private void setupError(final SetupCompletionListener setupCompletionListener,
313347
}
314348
}
315349

316-
private void createOkHttpClient(String clientToken) {
317-
OkHttpClient.Builder builder = new OkHttpClient.Builder();
318-
319-
builder.cache(new Cache(application.getCacheDir(), 10485760)); //10 MB
320-
321-
builder.retryOnConnectionFailure(true);
322-
323-
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(
324-
new HttpLoggingInterceptor.Logger() {
325-
@Override
326-
public void log(String message) {
327-
Logger.i(message);
328-
}
329-
});
330-
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
331-
builder.addInterceptor(logging);
332-
333-
if (clientToken != null) {
334-
builder.addInterceptor(new SnabbleAuthorizationInterceptor(this, clientToken));
335-
}
336-
337-
builder.addInterceptor(new UserAgentInterceptor(application));
338-
339-
okHttpClient = builder.build();
340-
}
341-
342-
File getInternalStorageDirectory() {
343-
return internalStorageDirectory;
344-
}
345-
346-
public String getEndpointBaseUrl() {
347-
return endpointBaseUrl;
348-
}
349-
350-
public String getProjectId() {
351-
return projectId;
352-
}
353-
354-
OkHttpClient getOkHttpClient() {
355-
return okHttpClient;
356-
}
357-
358-
Application getApplication() {
359-
return application;
360-
}
361-
362-
String getEventsUrl() {
363-
return metadataDownloader.getUrls().get("appEvents");
364-
}
365-
366-
String getAppDbUrl() {
367-
return metadataDownloader.getUrls().get("appdb");
368-
}
369-
370-
String getCheckoutUrl() {
371-
return metadataDownloader.getUrls().get("checkoutInfo");
372-
}
373-
374-
String getMetadataUrl() {
375-
if (loyaltyCardId != null) {
376-
return metadataUrl + "?loyaltyCard=" + loyaltyCardId;
377-
} else {
378-
return metadataUrl;
379-
}
380-
}
381-
382-
String getProductBySkuUrl() {
383-
return metadataDownloader.getUrls().get("productBySku");
384-
}
385-
386-
String getProductByCodeUrl() {
387-
return metadataDownloader.getUrls().get("productByCode");
388-
}
389-
390-
String getProductByWeighItemIdUrl() {
391-
return metadataDownloader.getUrls().get("productByWeighItemId");
392-
}
393-
394-
public String[] getPricePrefixes() {
395-
return pricePrefixes;
396-
}
397-
398-
public String[] getWeighPrefixes() {
399-
return weighPrefixes;
400-
}
401-
402-
public String[] getUnitPrefixes() {
403-
return unitPrefixes;
404-
}
405-
406-
public String getEncodedCodesPrefix() {
407-
return encodedCodesPrefix;
408-
}
409-
410-
public String getEncodedCodesSeperator() {
411-
return encodedCodesSeperator;
412-
}
413-
414-
public String getEncodedCodesSuffix() {
415-
return encodedCodesSuffix;
416-
}
417-
418350
public static class Config {
419351
/**
420352
* The endpoint url of the snabble backend. For example "snabble.io" for the Production environment.
@@ -507,13 +439,103 @@ public static class Config {
507439
*/
508440
public boolean productDbDownloadIfMissing = true;
509441

442+
/**
443+
* Optional SSLSocketFactory that gets used for HTTP requests.
444+
*
445+
* Requires also x509TrustManager to be set.
446+
*/
447+
public SSLSocketFactory sslSocketFactory = null;
448+
449+
/**
450+
* Optional X509TrustManager that gets used for HTTP requests.
451+
*
452+
* Requires also sslSocketFactory to be set.
453+
*/
454+
public X509TrustManager x509TrustManager = null;
455+
510456
public boolean useGermanPrintPrefix = false;
511457

512458
public String encodedCodesPrefix = null;
513459
public String encodedCodesSeperator = null;
514460
public String encodedCodesSuffix = null;
515461
}
516462

463+
File getInternalStorageDirectory() {
464+
return internalStorageDirectory;
465+
}
466+
467+
public String getEndpointBaseUrl() {
468+
return endpointBaseUrl;
469+
}
470+
471+
public String getProjectId() {
472+
return projectId;
473+
}
474+
475+
OkHttpClient getOkHttpClient() {
476+
return okHttpClient;
477+
}
478+
479+
Application getApplication() {
480+
return application;
481+
}
482+
483+
String getEventsUrl() {
484+
return metadataDownloader.getUrls().get("appEvents");
485+
}
486+
487+
String getAppDbUrl() {
488+
return metadataDownloader.getUrls().get("appdb");
489+
}
490+
491+
String getCheckoutUrl() {
492+
return metadataDownloader.getUrls().get("checkoutInfo");
493+
}
494+
495+
String getMetadataUrl() {
496+
if (loyaltyCardId != null) {
497+
return metadataUrl + "?loyaltyCard=" + loyaltyCardId;
498+
} else {
499+
return metadataUrl;
500+
}
501+
}
502+
503+
String getProductBySkuUrl() {
504+
return metadataDownloader.getUrls().get("productBySku");
505+
}
506+
507+
String getProductByCodeUrl() {
508+
return metadataDownloader.getUrls().get("productByCode");
509+
}
510+
511+
String getProductByWeighItemIdUrl() {
512+
return metadataDownloader.getUrls().get("productByWeighItemId");
513+
}
514+
515+
public String[] getPricePrefixes() {
516+
return pricePrefixes;
517+
}
518+
519+
public String[] getWeighPrefixes() {
520+
return weighPrefixes;
521+
}
522+
523+
public String[] getUnitPrefixes() {
524+
return unitPrefixes;
525+
}
526+
527+
public String getEncodedCodesPrefix() {
528+
return encodedCodesPrefix;
529+
}
530+
531+
public String getEncodedCodesSeperator() {
532+
return encodedCodesSeperator;
533+
}
534+
535+
public String getEncodedCodesSuffix() {
536+
return encodedCodesSuffix;
537+
}
538+
517539
String absoluteUrl(String url) {
518540
if (url.startsWith("http")) {
519541
return url;

0 commit comments

Comments
 (0)