From 356577e51c3f62b2af30e90e616492c22a286bb1 Mon Sep 17 00:00:00 2001 From: Emil Thies Date: Fri, 19 May 2023 15:28:03 +0200 Subject: [PATCH 01/10] try jar creation without signing --- pom.xml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index b45a9ab..91fb57d 100644 --- a/pom.xml +++ b/pom.xml @@ -292,12 +292,13 @@ true - - --> + From 8ec86328d791e03234601ecc9f987ecf8a499110 Mon Sep 17 00:00:00 2001 From: Emil Thies Date: Fri, 19 May 2023 19:30:26 +0200 Subject: [PATCH 02/10] try jar creation without signing --- README.md | 25 ++++++++++++++----- pom.xml | 2 -- .../PhoneNumberAreaLabelImpl.java | 24 +++++++++++++++++- .../PhoneAreaCodeComponentImplTest.groovy | 14 +++++++++++ 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 34a0d9b..a948276 100644 --- a/README.md +++ b/README.md @@ -79,13 +79,26 @@ Reasons are either PhoneLib just make best efforts for formatting and not format ## State of Our Implementation +### Code + As a wrapper we did not change any code of PhoneLib itself, so an upgrade to the newest version should be possible by just updating the version in the dependency POM definition. -For normalizing a phone number you should just use the our PhoneNumberNormalizer either bei Dependency injection or directly with its implementation like: +You could either take the sourcecode directly from the repository or use Maven dependency management by adding: +``` + + de.telekom.phonenumber + normalizer + +``` +to your POM. + +### How to Use + +For normalizing a phone number you should just use our PhoneNumberNormalizer either bei Dependency injection or directly with its implementation like: ``` String number = "20355555"; -String normalizedNumber = PhoneNumberNormalizerImpl().normalizePhoneNumber(number, "DE"); +String normalizedNumber = new PhoneNumberNormalizerImpl().normalizePhoneNumber(number, "DE"); // normalizedNumber -> "20355555" ``` So here the number without NAC does not get E164 format, because it is only valid locally. @@ -93,11 +106,11 @@ So here the number without NAC does not get E164 format, because it is only vali If we know the context of the devices use (where and how it is used), you can create a DeviceContext: ``` -// Device is used on a fixed-line access in Berlin -DeviceContextDto deviceContext = new DeviceContextDto(DeviceContextLineType.FIXEDLINE, "DE", "30") +// Device is used on a fixed-line access in Berlin (NDC for Berlin "30" and CC for Germany "49") +DeviceContextDto deviceContext = new DeviceContextDto(DeviceContextLineType.FIXEDLINE, "49", "30"); String number = "20355555"; -String normalizedNumber = PhoneNumberNormalizerImpl().normalizePhoneNumber(number, deviceContext); +String normalizedNumber = new PhoneNumberNormalizerImpl().normalizePhoneNumber(number, deviceContext); // normalizedNumber -> "+493020355555" ``` @@ -139,7 +152,7 @@ We only use the PhoneLib's GeoCoder in some testcases, to check if ours and thei ``` String normalizedNumber = "+493020355555"; -String label = PhoneNumberAreaLabelImpl().getLocationByE164Number(normalizedNumber); +Optional label = new PhoneNumberAreaLabelImpl().getLocationByE164Number(normalizedNumber); // label -> "Berlin" ``` diff --git a/pom.xml b/pom.xml index 91fb57d..1ef6846 100644 --- a/pom.xml +++ b/pom.xml @@ -298,7 +298,6 @@ maven-gpg-plugin 1.6 - diff --git a/src/main/java/de/telekom/phonenumbernormalizer/PhoneNumberAreaLabelImpl.java b/src/main/java/de/telekom/phonenumbernormalizer/PhoneNumberAreaLabelImpl.java index c80445f..520d5ce 100644 --- a/src/main/java/de/telekom/phonenumbernormalizer/PhoneNumberAreaLabelImpl.java +++ b/src/main/java/de/telekom/phonenumbernormalizer/PhoneNumberAreaLabelImpl.java @@ -25,7 +25,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.stereotype.Component; @@ -92,6 +95,22 @@ public class PhoneNumberAreaLabelImpl implements PhoneNumberAreaLabel { */ @PostConstruct public void initFile() { + + ClassLoader cl = this.getClass().getClassLoader(); + // if no resources are given, the default once are used: + if (countryCodeResource == null) { + countryCodeResource = new ClassPathResource("arealabels/international_country_codes.json", cl); + } + + if (numberPlanResources == null || numberPlanResources.length==0) { + ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl); + try { + numberPlanResources = resolver.getResources("classpath:arealabels/nationallabels/*.json"); + } catch (IOException e) { + e.printStackTrace(); + } + } + try { LOGGER.debug("init code files"); LOGGER.debug("read international country codes"); @@ -116,7 +135,7 @@ public void initFile() { @Override public Optional getLocationByNationalNumberAndRegionCode(String nationalNumber, String regionCode) { regionCode = regionCode.toUpperCase(Locale.ROOT); - if (!this.areaCodes.containsKey(regionCode)) { + if (Objects.nonNull(this.areaCodes) && !this.areaCodes.containsKey(regionCode)) { LOGGER.debug("no number plan for regioncode: {} available", regionCode); return Optional.empty(); } @@ -127,6 +146,9 @@ public Optional getLocationByNationalNumberAndRegionCode(String national @Override public Optional getCountryNameByCountryCode(String countryCode) { + if (Objects.isNull(this.internationalCountryCodes)) { + return Optional.empty(); + } return Optional.ofNullable((String) this.internationalCountryCodes.get(countryCode)); } diff --git a/src/test/groovy/de/telekom/phonenumbernormalizer/PhoneAreaCodeComponentImplTest.groovy b/src/test/groovy/de/telekom/phonenumbernormalizer/PhoneAreaCodeComponentImplTest.groovy index 7aa94e5..4e6b32c 100644 --- a/src/test/groovy/de/telekom/phonenumbernormalizer/PhoneAreaCodeComponentImplTest.groovy +++ b/src/test/groovy/de/telekom/phonenumbernormalizer/PhoneAreaCodeComponentImplTest.groovy @@ -37,6 +37,20 @@ class PhoneAreaCodeComponentImplTest extends Specification { this.phoneAreaCodeComponentImpl.initFile() } + def "default data loaded"() { + given: + def arealabel + + when: + arealabel = new PhoneNumberAreaLabelImpl() + arealabel.initFile() + + then: + arealabel.numberPlanResources != null + + + } + def "get location by unknown area code"() { given: def nationalNumber = "123456789" From 519104a273fbaabfb60aa09a6ae0c98d414da10d Mon Sep 17 00:00:00 2001 From: Emil Thies Date: Fri, 19 May 2023 15:28:03 +0200 Subject: [PATCH 03/10] try jar creation without signing --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 864ff54..ee8acf6 100644 --- a/pom.xml +++ b/pom.xml @@ -416,4 +416,4 @@ https://oss.sonatype.org/service/local/staging/deploy/maven2/ - + \ No newline at end of file From e5bb6e3217faada23e819d54379934562c620c2e Mon Sep 17 00:00:00 2001 From: Emil Thies Date: Fri, 19 May 2023 19:30:26 +0200 Subject: [PATCH 04/10] try jar creation without signing --- README.md | 25 ++++++++++++++----- .../PhoneNumberAreaLabelImpl.java | 24 +++++++++++++++++- .../PhoneAreaCodeComponentImplTest.groovy | 14 +++++++++++ 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index df63720..232f69a 100644 --- a/README.md +++ b/README.md @@ -79,13 +79,26 @@ Reasons are either PhoneLib just make best efforts for formatting and not format ## State of Our Implementation +### Code + As a wrapper we did not change any code of PhoneLib itself, so an upgrade to the newest version should be possible by just updating the version in the dependency POM definition. -For normalizing a phone number you should just use the our PhoneNumberNormalizer either bei Dependency injection or directly with its implementation like: +You could either take the sourcecode directly from the repository or use Maven dependency management by adding: +``` + + de.telekom.phonenumber + normalizer + +``` +to your POM. + +### How to Use + +For normalizing a phone number you should just use our PhoneNumberNormalizer either bei Dependency injection or directly with its implementation like: ``` String number = "20355555"; -String normalizedNumber = PhoneNumberNormalizerImpl().normalizePhoneNumber(number, "DE"); +String normalizedNumber = new PhoneNumberNormalizerImpl().normalizePhoneNumber(number, "DE"); // normalizedNumber -> "20355555" ``` So here the number without NAC does not get E164 format, because it is only valid locally. @@ -93,11 +106,11 @@ So here the number without NAC does not get E164 format, because it is only vali If we know the context of the devices use (where and how it is used), you can create a DeviceContext: ``` -// Device is used on a fixed-line access in Berlin -DeviceContextDto deviceContext = new DeviceContextDto(DeviceContextLineType.FIXEDLINE, "DE", "30") +// Device is used on a fixed-line access in Berlin (NDC for Berlin "30" and CC for Germany "49") +DeviceContextDto deviceContext = new DeviceContextDto(DeviceContextLineType.FIXEDLINE, "49", "30"); String number = "20355555"; -String normalizedNumber = PhoneNumberNormalizerImpl().normalizePhoneNumber(number, deviceContext); +String normalizedNumber = new PhoneNumberNormalizerImpl().normalizePhoneNumber(number, deviceContext); // normalizedNumber -> "+493020355555" ``` @@ -139,7 +152,7 @@ We only use the PhoneLib's GeoCoder in some testcases, to check if ours and thei ``` String normalizedNumber = "+493020355555"; -String label = PhoneNumberAreaLabelImpl().getLocationByE164Number(normalizedNumber); +Optional label = new PhoneNumberAreaLabelImpl().getLocationByE164Number(normalizedNumber); // label -> "Berlin" ``` diff --git a/src/main/java/de/telekom/phonenumbernormalizer/PhoneNumberAreaLabelImpl.java b/src/main/java/de/telekom/phonenumbernormalizer/PhoneNumberAreaLabelImpl.java index c80445f..520d5ce 100644 --- a/src/main/java/de/telekom/phonenumbernormalizer/PhoneNumberAreaLabelImpl.java +++ b/src/main/java/de/telekom/phonenumbernormalizer/PhoneNumberAreaLabelImpl.java @@ -25,7 +25,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.stereotype.Component; @@ -92,6 +95,22 @@ public class PhoneNumberAreaLabelImpl implements PhoneNumberAreaLabel { */ @PostConstruct public void initFile() { + + ClassLoader cl = this.getClass().getClassLoader(); + // if no resources are given, the default once are used: + if (countryCodeResource == null) { + countryCodeResource = new ClassPathResource("arealabels/international_country_codes.json", cl); + } + + if (numberPlanResources == null || numberPlanResources.length==0) { + ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl); + try { + numberPlanResources = resolver.getResources("classpath:arealabels/nationallabels/*.json"); + } catch (IOException e) { + e.printStackTrace(); + } + } + try { LOGGER.debug("init code files"); LOGGER.debug("read international country codes"); @@ -116,7 +135,7 @@ public void initFile() { @Override public Optional getLocationByNationalNumberAndRegionCode(String nationalNumber, String regionCode) { regionCode = regionCode.toUpperCase(Locale.ROOT); - if (!this.areaCodes.containsKey(regionCode)) { + if (Objects.nonNull(this.areaCodes) && !this.areaCodes.containsKey(regionCode)) { LOGGER.debug("no number plan for regioncode: {} available", regionCode); return Optional.empty(); } @@ -127,6 +146,9 @@ public Optional getLocationByNationalNumberAndRegionCode(String national @Override public Optional getCountryNameByCountryCode(String countryCode) { + if (Objects.isNull(this.internationalCountryCodes)) { + return Optional.empty(); + } return Optional.ofNullable((String) this.internationalCountryCodes.get(countryCode)); } diff --git a/src/test/groovy/de/telekom/phonenumbernormalizer/PhoneAreaCodeComponentImplTest.groovy b/src/test/groovy/de/telekom/phonenumbernormalizer/PhoneAreaCodeComponentImplTest.groovy index 7aa94e5..4e6b32c 100644 --- a/src/test/groovy/de/telekom/phonenumbernormalizer/PhoneAreaCodeComponentImplTest.groovy +++ b/src/test/groovy/de/telekom/phonenumbernormalizer/PhoneAreaCodeComponentImplTest.groovy @@ -37,6 +37,20 @@ class PhoneAreaCodeComponentImplTest extends Specification { this.phoneAreaCodeComponentImpl.initFile() } + def "default data loaded"() { + given: + def arealabel + + when: + arealabel = new PhoneNumberAreaLabelImpl() + arealabel.initFile() + + then: + arealabel.numberPlanResources != null + + + } + def "get location by unknown area code"() { given: def nationalNumber = "123456789" From 2415d14798820da99319564a038fa33c64fdd5fc Mon Sep 17 00:00:00 2001 From: Emil Thies Date: Mon, 21 Aug 2023 13:10:13 +0200 Subject: [PATCH 05/10] Housekeeping --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 232f69a..41e742b 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,6 @@ to your POM. ### How to Use For normalizing a phone number you should just use our PhoneNumberNormalizer either bei Dependency injection or directly with its implementation like: - ``` String number = "20355555"; String normalizedNumber = new PhoneNumberNormalizerImpl().normalizePhoneNumber(number, "DE"); From 8e7887e69635a9b412c76aa65c552131740440e4 Mon Sep 17 00:00:00 2001 From: Emil Thies Date: Mon, 21 Aug 2023 13:53:31 +0200 Subject: [PATCH 06/10] Changed titel of updating guide. --- UPDATE_FOR_NEW_PHONELIB.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UPDATE_FOR_NEW_PHONELIB.md b/UPDATE_FOR_NEW_PHONELIB.md index 6b891a8..cb9c478 100644 --- a/UPDATE_FOR_NEW_PHONELIB.md +++ b/UPDATE_FOR_NEW_PHONELIB.md @@ -1,4 +1,4 @@ -# Phone Number Normalizer +# How to adapt to a new Version of PhoneLib If Google updates its [PhoneLib](https://github.com/google/libphonenumber), this project should be updated to use that new version. This file is a step by step instruction how to do this: From d26901005c2bf4b9fe28d5de711b63451f74f9e2 Mon Sep 17 00:00:00 2001 From: Emil Thies Date: Fri, 19 May 2023 15:28:03 +0200 Subject: [PATCH 07/10] try jar creation without signing --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6166b45..1d9b5d2 100644 --- a/pom.xml +++ b/pom.xml @@ -416,4 +416,4 @@ https://oss.sonatype.org/service/local/staging/deploy/maven2/ - + \ No newline at end of file From b2ebe1d01dceeab474ac9c55a2343b065b3defc0 Mon Sep 17 00:00:00 2001 From: Emil Thies Date: Fri, 19 May 2023 19:30:26 +0200 Subject: [PATCH 08/10] try jar creation without signing --- README.md | 25 ++++++++++++++----- .../PhoneNumberAreaLabelImpl.java | 24 +++++++++++++++++- .../PhoneAreaCodeComponentImplTest.groovy | 14 +++++++++++ 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index df63720..232f69a 100644 --- a/README.md +++ b/README.md @@ -79,13 +79,26 @@ Reasons are either PhoneLib just make best efforts for formatting and not format ## State of Our Implementation +### Code + As a wrapper we did not change any code of PhoneLib itself, so an upgrade to the newest version should be possible by just updating the version in the dependency POM definition. -For normalizing a phone number you should just use the our PhoneNumberNormalizer either bei Dependency injection or directly with its implementation like: +You could either take the sourcecode directly from the repository or use Maven dependency management by adding: +``` + + de.telekom.phonenumber + normalizer + +``` +to your POM. + +### How to Use + +For normalizing a phone number you should just use our PhoneNumberNormalizer either bei Dependency injection or directly with its implementation like: ``` String number = "20355555"; -String normalizedNumber = PhoneNumberNormalizerImpl().normalizePhoneNumber(number, "DE"); +String normalizedNumber = new PhoneNumberNormalizerImpl().normalizePhoneNumber(number, "DE"); // normalizedNumber -> "20355555" ``` So here the number without NAC does not get E164 format, because it is only valid locally. @@ -93,11 +106,11 @@ So here the number without NAC does not get E164 format, because it is only vali If we know the context of the devices use (where and how it is used), you can create a DeviceContext: ``` -// Device is used on a fixed-line access in Berlin -DeviceContextDto deviceContext = new DeviceContextDto(DeviceContextLineType.FIXEDLINE, "DE", "30") +// Device is used on a fixed-line access in Berlin (NDC for Berlin "30" and CC for Germany "49") +DeviceContextDto deviceContext = new DeviceContextDto(DeviceContextLineType.FIXEDLINE, "49", "30"); String number = "20355555"; -String normalizedNumber = PhoneNumberNormalizerImpl().normalizePhoneNumber(number, deviceContext); +String normalizedNumber = new PhoneNumberNormalizerImpl().normalizePhoneNumber(number, deviceContext); // normalizedNumber -> "+493020355555" ``` @@ -139,7 +152,7 @@ We only use the PhoneLib's GeoCoder in some testcases, to check if ours and thei ``` String normalizedNumber = "+493020355555"; -String label = PhoneNumberAreaLabelImpl().getLocationByE164Number(normalizedNumber); +Optional label = new PhoneNumberAreaLabelImpl().getLocationByE164Number(normalizedNumber); // label -> "Berlin" ``` diff --git a/src/main/java/de/telekom/phonenumbernormalizer/PhoneNumberAreaLabelImpl.java b/src/main/java/de/telekom/phonenumbernormalizer/PhoneNumberAreaLabelImpl.java index c80445f..520d5ce 100644 --- a/src/main/java/de/telekom/phonenumbernormalizer/PhoneNumberAreaLabelImpl.java +++ b/src/main/java/de/telekom/phonenumbernormalizer/PhoneNumberAreaLabelImpl.java @@ -25,7 +25,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.stereotype.Component; @@ -92,6 +95,22 @@ public class PhoneNumberAreaLabelImpl implements PhoneNumberAreaLabel { */ @PostConstruct public void initFile() { + + ClassLoader cl = this.getClass().getClassLoader(); + // if no resources are given, the default once are used: + if (countryCodeResource == null) { + countryCodeResource = new ClassPathResource("arealabels/international_country_codes.json", cl); + } + + if (numberPlanResources == null || numberPlanResources.length==0) { + ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl); + try { + numberPlanResources = resolver.getResources("classpath:arealabels/nationallabels/*.json"); + } catch (IOException e) { + e.printStackTrace(); + } + } + try { LOGGER.debug("init code files"); LOGGER.debug("read international country codes"); @@ -116,7 +135,7 @@ public void initFile() { @Override public Optional getLocationByNationalNumberAndRegionCode(String nationalNumber, String regionCode) { regionCode = regionCode.toUpperCase(Locale.ROOT); - if (!this.areaCodes.containsKey(regionCode)) { + if (Objects.nonNull(this.areaCodes) && !this.areaCodes.containsKey(regionCode)) { LOGGER.debug("no number plan for regioncode: {} available", regionCode); return Optional.empty(); } @@ -127,6 +146,9 @@ public Optional getLocationByNationalNumberAndRegionCode(String national @Override public Optional getCountryNameByCountryCode(String countryCode) { + if (Objects.isNull(this.internationalCountryCodes)) { + return Optional.empty(); + } return Optional.ofNullable((String) this.internationalCountryCodes.get(countryCode)); } diff --git a/src/test/groovy/de/telekom/phonenumbernormalizer/PhoneAreaCodeComponentImplTest.groovy b/src/test/groovy/de/telekom/phonenumbernormalizer/PhoneAreaCodeComponentImplTest.groovy index 7aa94e5..4e6b32c 100644 --- a/src/test/groovy/de/telekom/phonenumbernormalizer/PhoneAreaCodeComponentImplTest.groovy +++ b/src/test/groovy/de/telekom/phonenumbernormalizer/PhoneAreaCodeComponentImplTest.groovy @@ -37,6 +37,20 @@ class PhoneAreaCodeComponentImplTest extends Specification { this.phoneAreaCodeComponentImpl.initFile() } + def "default data loaded"() { + given: + def arealabel + + when: + arealabel = new PhoneNumberAreaLabelImpl() + arealabel.initFile() + + then: + arealabel.numberPlanResources != null + + + } + def "get location by unknown area code"() { given: def nationalNumber = "123456789" From 77484ecbe40fd3a4fa38b1f2c1634a996c7eb63a Mon Sep 17 00:00:00 2001 From: Emil Thies Date: Mon, 21 Aug 2023 13:10:13 +0200 Subject: [PATCH 09/10] Housekeeping --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 232f69a..41e742b 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,6 @@ to your POM. ### How to Use For normalizing a phone number you should just use our PhoneNumberNormalizer either bei Dependency injection or directly with its implementation like: - ``` String number = "20355555"; String normalizedNumber = new PhoneNumberNormalizerImpl().normalizePhoneNumber(number, "DE"); From 124b1731eeadeca0b00f3aa1b7e51b19ab1fb0cc Mon Sep 17 00:00:00 2001 From: Emil Thies Date: Mon, 21 Aug 2023 13:53:31 +0200 Subject: [PATCH 10/10] Changed titel of updating guide. --- UPDATE_FOR_NEW_PHONELIB.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UPDATE_FOR_NEW_PHONELIB.md b/UPDATE_FOR_NEW_PHONELIB.md index 6b891a8..cb9c478 100644 --- a/UPDATE_FOR_NEW_PHONELIB.md +++ b/UPDATE_FOR_NEW_PHONELIB.md @@ -1,4 +1,4 @@ -# Phone Number Normalizer +# How to adapt to a new Version of PhoneLib If Google updates its [PhoneLib](https://github.com/google/libphonenumber), this project should be updated to use that new version. This file is a step by step instruction how to do this: