@@ -44,93 +44,100 @@ public class OrganizationUtil {
4444
4545 private static Logger logger = Logger .getLogger (OrganizationUtil .class );
4646
47+
48+ public static MostOptimalNumberResponse getMostOptimalIncomingPhoneNumber (DaoManager storage , SipServletRequest request , String phone ,
49+ Sid sourceOrganizationSid ) {
50+ MostOptimalNumberResponse res = null ;
51+ Sid destinationOrganizationSid = getOrganizationSidBySipURIHost (storage , (SipURI ) request .getRequestURI ());
52+ // try to get destinationOrganizationSid from toUril
53+ destinationOrganizationSid = destinationOrganizationSid != null ? destinationOrganizationSid : getOrganizationSidBySipURIHost (storage , (SipURI ) request .getTo ().getURI ());
54+ if (destinationOrganizationSid == null ) {
55+ logger .error ("destinationOrganizationSid is NULL: request Uri is: " + (SipURI ) request .getRequestURI () + " To Uri is: " + (SipURI ) request .getTo ().getURI ());
56+ res = new MostOptimalNumberResponse (null , false );
57+ } else {
58+ if (logger .isDebugEnabled ()) {
59+ logger .debug ("getMostOptimalIncomingPhoneNumber: sourceOrganizationSid: " + sourceOrganizationSid + " : destinationOrganizationSid: " + destinationOrganizationSid + " request Uri is: " + (SipURI ) request .getRequestURI () + " To Uri is: " + (SipURI ) request .getTo ().getURI ());
60+ }
61+ res = getMostOptimalIncomingPhoneNumber (storage , destinationOrganizationSid , phone , sourceOrganizationSid );
62+ }
63+ return res ;
64+ }
65+
4766 /**
4867 * @param storage DaoManager
4968 * @param request SipServletRequest
5069 * @param phone
5170 * @param sourceOrganizationSid organization Sid of request initiator
5271 * @return
5372 */
54- public static MostOptimalNumberResponse getMostOptimalIncomingPhoneNumber (DaoManager storage , SipServletRequest request , String phone ,
73+ public static MostOptimalNumberResponse getMostOptimalIncomingPhoneNumber (DaoManager storage , Sid destinationOrganizationSid , String phone ,
5574 Sid sourceOrganizationSid ) {
56- if (logger .isDebugEnabled ())
57- logger .debug ("getMostOptimalIncomingPhoneNumber: " +phone );
5875
5976 IncomingPhoneNumber number = null ;
6077 boolean failCall = false ;
6178 List <IncomingPhoneNumber > numbers = new ArrayList <IncomingPhoneNumber >();
6279 final IncomingPhoneNumbersDao numbersDao = storage .getIncomingPhoneNumbersDao ();
6380 try {
64- Sid destinationOrganizationSid = getOrganizationSidBySipURIHost (storage , (SipURI )request .getRequestURI ());
65- // try to get destinationOrganizationSid from toUril
66- destinationOrganizationSid = destinationOrganizationSid != null ? destinationOrganizationSid : getOrganizationSidBySipURIHost (storage , (SipURI )request .getTo ().getURI ());
67- if (destinationOrganizationSid == null ){
68- logger .error ("destinationOrganizationSid is NULL: request Uri is: " +(SipURI )request .getRequestURI ()+ " To Uri is: " +(SipURI )request .getTo ().getURI ());
69- }else {
70- if (logger .isDebugEnabled ())
71- logger .debug ("getMostOptimalIncomingPhoneNumber: sourceOrganizationSid: " +sourceOrganizationSid +" : destinationOrganizationSid: " +destinationOrganizationSid +" request Uri is: " +(SipURI )request .getRequestURI ()+ " To Uri is: " +(SipURI )request .getTo ().getURI ());
72-
73- // Format the destination to an E.164 phone number.
74- final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil .getInstance ();
75- String formatedPhone = null ;
76- if (!(phone .contains ("*" ) || phone .contains ("#" ))) {
77- try {
78- formatedPhone = phoneNumberUtil .format (phoneNumberUtil .parse (phone , "US" ), PhoneNumberFormat .E164 );
79- } catch (NumberParseException e ) {
80- //logger.error("Exception when try to format : " + e);
81- }
82- }
83- if (formatedPhone == null ) {
84- //Don't format to E.164 if phone contains # or * as this is
85- //for a Regex or USSD short number
86- formatedPhone = phone ;
87- }else {
88- //get all number with same number, by both formatedPhone and unformatedPhone
89- numbers = OrganizationUtil .searchAndAddNumberToTheList (numbers , formatedPhone , numbersDao );
81+ // Format the destination to an E.164 phone number.
82+ final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil .getInstance ();
83+ String formatedPhone = null ;
84+ if (!(phone .contains ("*" ) || phone .contains ("#" ))) {
85+ try {
86+ formatedPhone = phoneNumberUtil .format (phoneNumberUtil .parse (phone , "US" ), PhoneNumberFormat .E164 );
87+ } catch (NumberParseException e ) {
88+ //logger.error("Exception when try to format : " + e);
9089 }
90+ }
91+ if (formatedPhone == null ) {
92+ //Don't format to E.164 if phone contains # or * as this is
93+ //for a Regex or USSD short number
94+ formatedPhone = phone ;
95+ }else {
96+ //get all number with same number, by both formatedPhone and unformatedPhone
97+ numbers = OrganizationUtil .searchAndAddNumberToTheList (numbers , formatedPhone , numbersDao );
98+ }
99+ numbers = OrganizationUtil .searchAndAddNumberToTheList (numbers , phone , numbersDao );
100+ if (phone .startsWith ("+" )) {
101+ //remove the (+) and check if exists
102+ phone = phone .replaceFirst ("\\ +" ,"" );
91103 numbers = OrganizationUtil .searchAndAddNumberToTheList (numbers , phone , numbersDao );
92- if (phone .startsWith ("+" )) {
93- //remove the (+) and check if exists
94- phone = phone .replaceFirst ("\\ +" ,"" );
95- numbers = OrganizationUtil .searchAndAddNumberToTheList (numbers , phone , numbersDao );
96- } else {
97- //Add "+" add check if number exists
98- phone = "+" .concat (phone );
99- numbers = OrganizationUtil .searchAndAddNumberToTheList (numbers , phone , numbersDao );
100- }
101- if (numbers == null || numbers .isEmpty ()){
102- // https://github.com/Mobicents/RestComm/issues/84 using wildcard as default application
103- numbers = OrganizationUtil .searchAndAddNumberToTheList (numbers , "*" , numbersDao );
104- }
105- if (numbers != null && !numbers .isEmpty ()){
106- // find number in same organization
107- for (IncomingPhoneNumber n : numbers ){
108- if (n != null ){
109- if (logger .isDebugEnabled ())
110- logger .debug (String .format ("getMostOptimalIncomingPhoneNumber: Got a similar number from DB: Analysis report: Number:Sid = %s : %s | Is Number pure sip? %s | Number's organizations: %s" , n .getPhoneNumber (), n .getSid (), n .isPureSip (), n .getOrganizationSid ()));
111- if (n .getOrganizationSid ().equals (destinationOrganizationSid )){
112- /*
113- * check if request is coming from same org
114- * if not then only allow provider numbers
115- */
116- if ((sourceOrganizationSid != null && sourceOrganizationSid .equals (destinationOrganizationSid )) || (sourceOrganizationSid == null ) || !n .isPureSip ()){
117- number = n ;
118- if (logger .isInfoEnabled ())
119- logger .info (String .format ("Found most optimal phone number: Number:Sid = %s : %s" , n .getPhoneNumber (), n .getSid ()));
120- }else {
121- if (logger .isDebugEnabled ())
122- logger .debug ("not allowed to call this number due to organizational restrictions" );
123- }
104+ } else {
105+ //Add "+" add check if number exists
106+ phone = "+" .concat (phone );
107+ numbers = OrganizationUtil .searchAndAddNumberToTheList (numbers , phone , numbersDao );
108+ }
109+ if (numbers == null || numbers .isEmpty ()){
110+ // https://github.com/Mobicents/RestComm/issues/84 using wildcard as default application
111+ numbers = OrganizationUtil .searchAndAddNumberToTheList (numbers , "*" , numbersDao );
112+ }
113+ if (numbers != null && !numbers .isEmpty ()){
114+ // find number in same organization
115+ for (IncomingPhoneNumber n : numbers ){
116+ if (n != null ){
117+ if (logger .isDebugEnabled ())
118+ logger .debug (String .format ("getMostOptimalIncomingPhoneNumber: Got a similar number from DB: Analysis report: Number:Sid = %s : %s | Is Number pure sip? %s | Number's organizations: %s" , n .getPhoneNumber (), n .getSid (), n .isPureSip (), n .getOrganizationSid ()));
119+ if (n .getOrganizationSid ().equals (destinationOrganizationSid )){
120+ /*
121+ * check if request is coming from same org
122+ * if not then only allow provider numbers
123+ */
124+ if ((sourceOrganizationSid != null && sourceOrganizationSid .equals (destinationOrganizationSid )) || (sourceOrganizationSid == null ) || !n .isPureSip ()){
125+ number = n ;
126+ if (logger .isInfoEnabled ())
127+ logger .info (String .format ("Found most optimal phone number: Number:Sid = %s : %s" , n .getPhoneNumber (), n .getSid ()));
124128 }else {
125129 if (logger .isDebugEnabled ())
126- logger .debug (String . format ( "getMostOptimalIncomingPhoneNumber: Number:Sid = %s : %s does not belong to requested/destination organization: %s" , n . getPhoneNumber (), n . getSid (), destinationOrganizationSid ) );
130+ logger .debug ("not allowed to call this number due to organizational restrictions" );
127131 }
128- if (number != null )
129- break ;
132+ }else {
133+ if (logger .isDebugEnabled ())
134+ logger .debug (String .format ("getMostOptimalIncomingPhoneNumber: Number:Sid = %s : %s does not belong to requested/destination organization: %s" , n .getPhoneNumber (), n .getSid (), destinationOrganizationSid ));
130135 }
136+ if (number != null )
137+ break ;
131138 }
132- failCall = number == null ;
133139 }
140+ failCall = number == null ;
134141 }
135142 }catch (Exception e ){
136143 logger .error ("Error while trying to retrive getMostOptimalIncomingPhoneNumber: " , e );
0 commit comments