|
19 | 19 | import java.util.concurrent.CopyOnWriteArrayList; |
20 | 20 | import java.util.concurrent.CountDownLatch; |
21 | 21 |
|
| 22 | +import javax.net.ssl.SSLSocketFactory; |
| 23 | +import javax.net.ssl.X509TrustManager; |
| 24 | + |
22 | 25 | import io.snabble.sdk.utils.Downloader; |
23 | 26 | import io.snabble.sdk.utils.JsonUtils; |
24 | 27 | import io.snabble.sdk.utils.Logger; |
@@ -65,13 +68,12 @@ public class SnabbleSdk { |
65 | 68 | private String encodedCodesSeperator = null; |
66 | 69 | private String encodedCodesSuffix = null; |
67 | 70 |
|
68 | | - |
69 | 71 | private void init(final Application app, |
70 | 72 | final Config config, |
71 | 73 | final SetupCompletionListener setupCompletionListener) { |
72 | 74 | application = app; |
73 | 75 |
|
74 | | - createOkHttpClient(config.clientToken); |
| 76 | + createOkHttpClient(config.clientToken, config.sslSocketFactory, config.x509TrustManager); |
75 | 77 | internalStorageDirectory = new File(app.getFilesDir(), "snabble/" + config.projectId + "/"); |
76 | 78 | //noinspection ResultOfMethodCallIgnored |
77 | 79 | internalStorageDirectory.mkdirs(); |
@@ -147,6 +149,38 @@ protected void onError() { |
147 | 149 | } |
148 | 150 | } |
149 | 151 |
|
| 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 | + |
150 | 184 | private SnabbleSdk() { |
151 | 185 |
|
152 | 186 | } |
@@ -313,108 +347,6 @@ private void setupError(final SetupCompletionListener setupCompletionListener, |
313 | 347 | } |
314 | 348 | } |
315 | 349 |
|
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 | | - |
418 | 350 | public static class Config { |
419 | 351 | /** |
420 | 352 | * The endpoint url of the snabble backend. For example "snabble.io" for the Production environment. |
@@ -507,13 +439,103 @@ public static class Config { |
507 | 439 | */ |
508 | 440 | public boolean productDbDownloadIfMissing = true; |
509 | 441 |
|
| 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 | + |
510 | 456 | public boolean useGermanPrintPrefix = false; |
511 | 457 |
|
512 | 458 | public String encodedCodesPrefix = null; |
513 | 459 | public String encodedCodesSeperator = null; |
514 | 460 | public String encodedCodesSuffix = null; |
515 | 461 | } |
516 | 462 |
|
| 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 | + |
517 | 539 | String absoluteUrl(String url) { |
518 | 540 | if (url.startsWith("http")) { |
519 | 541 | return url; |
|
0 commit comments