public static String modifyURIAuthority ( String uri , String newAuthority ) { try { URI uriObj = new URI ( uri ) ; // code below modifies new authority considering also network interface syntax Pattern pattern = Pattern . compile ( NETWORK_INTERFACE_AUTHORITY ) ; Matcher matcher = pattern . matcher ( newAuthority ) ; String matchedToken = MOCK_HOST ; // if newAuthority corresponds to NetworkInterfaceURI syntax if ( matcher . find ( ) ) { matchedToken = matcher . group ( 0 ) ; newAuthority = newAuthority . replace ( matchedToken , MOCK_HOST ) ; } URI modifiedURIAuthority = URLUtils . modifyURIAuthority ( uriObj , newAuthority ) ; String uriWithModifiedAuthority = URIUtils . uriToString ( modifiedURIAuthority ) . replace ( MOCK_HOST , matchedToken ) ; return uriWithModifiedAuthority ; } catch ( URISyntaxException e ) { try { return ( new NetworkInterfaceURI ( uri ) ) . modifyURIAuthority ( newAuthority ) ; } catch ( IllegalArgumentException ne ) { throw new IllegalArgumentException ( ne . getMessage ( ) , ne ) ; } } }
Helper method for modifying URI authority