From d5888d27360708afce681d174b1a5181565ec0df Mon Sep 17 00:00:00 2001 From: roshan Date: Thu, 29 Mar 2018 18:43:02 +0530 Subject: [PATCH] MSISDNUtil updated for resourceInMsisdnFormat --- .../core/msisdnvalidator/MSISDNUtil.java | 99 +++++++++++-------- 1 file changed, 56 insertions(+), 43 deletions(-) diff --git a/components/msisdn-validator/src/main/java/com/wso2telco/core/msisdnvalidator/MSISDNUtil.java b/components/msisdn-validator/src/main/java/com/wso2telco/core/msisdnvalidator/MSISDNUtil.java index 4ecbffb8..a381a032 100644 --- a/components/msisdn-validator/src/main/java/com/wso2telco/core/msisdnvalidator/MSISDNUtil.java +++ b/components/msisdn-validator/src/main/java/com/wso2telco/core/msisdnvalidator/MSISDNUtil.java @@ -3,54 +3,67 @@ import com.google.i18n.phonenumbers.NumberParseException; import com.google.i18n.phonenumbers.PhoneNumberUtil; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Wrap the google phonenumber utility * */ public class MSISDNUtil { - PhoneNumberUtil util = null; - { - util = PhoneNumberUtil.getInstance(); - } - - /** - * parse the given text into phone number using google phonenumber lib. - * it is mandatory to have + sign in the rowmsisdn string. - * @param rowmsisdn - * @return MSISDN - * @throws InvalidMSISDNException - */ - public MSISDN parse(String rowmsisdn) throws InvalidMSISDNException { - try { - rowmsisdn = appendPlusIfNotExist(rowmsisdn); - PhoneNumber number = util.parse(rowmsisdn, null); - MSISDN msisdn = new MSISDN(number.getCountryCode(), number.getNationalNumber()); - return msisdn; - } catch (NumberParseException e) { - throw new InvalidMSISDNException(Google2InternalErrorMapper.mapErrorType(e.getErrorType()) ); - } - } - - private String appendPlusIfNotExist(String rawmsisdn) { - String formattedNumber = null; - String validationRegex = "tel\\:[a-zA-Z0-9]+"; - - if (rawmsisdn != null && rawmsisdn.matches(validationRegex)) { - StringBuilder builder = new StringBuilder(rawmsisdn); - builder.insert(4, "+"); - formattedNumber = builder.toString(); - } else if (rawmsisdn.contains("etel:")) { - String[] msidn = rawmsisdn.split(":"); - formattedNumber = msidn[0] + "+" + msidn[1]; - } else { - formattedNumber = rawmsisdn; - } - - return formattedNumber; - } - - public boolean resourceInMsisdnFormat(String msisdnResurce) { - return (appendPlusIfNotExist(msisdnResurce) != null ); + + PhoneNumberUtil util = null; + + { + util = PhoneNumberUtil.getInstance(); + } + + /** + * parse the given text into phone number using google phonenumber lib. it + * is mandatory to have + sign in the rowmsisdn string. + * + * @param rowmsisdn + * @return MSISDN + * @throws InvalidMSISDNException + */ + public MSISDN parse(String rowmsisdn) throws InvalidMSISDNException { + try { + rowmsisdn = appendPlusIfNotExist(rowmsisdn); + PhoneNumber number = util.parse(rowmsisdn, null); + MSISDN msisdn = new MSISDN(number.getCountryCode(), number.getNationalNumber()); + return msisdn; + } catch (NumberParseException e) { + throw new InvalidMSISDNException(Google2InternalErrorMapper.mapErrorType(e.getErrorType())); + } + } + + private String appendPlusIfNotExist(String rawmsisdn) { + String formattedNumber = null; + String validationRegex = "tel\\:[a-zA-Z0-9]+"; + + if (rawmsisdn != null && rawmsisdn.matches(validationRegex)) { + StringBuilder builder = new StringBuilder(rawmsisdn); + builder.insert(4, "+"); + formattedNumber = builder.toString(); + } else if (rawmsisdn.contains("etel:")) { + String[] msidn = rawmsisdn.split(":"); + formattedNumber = msidn[0] + "+" + msidn[1]; + } else { + formattedNumber = rawmsisdn; + } + + return formattedNumber; + } + + public boolean resourceInMsisdnFormat(String msisdnResurce) { + + boolean isMsisdnFormat = false; + try { + MSISDN msisdn = parse(msisdnResurce); + isMsisdnFormat = true; + } catch (InvalidMSISDNException ex) { + //Logger.getLogger(MSISDNUtil.class.getName()).log(Level.SEVERE, null, ex); } + return isMsisdnFormat; + } }