private String discover ( final String url ) throws TimezonesException { /* For the moment we'll try to find it via .well-known. We may have to * use DNS SRV lookups */ // String domain = hi.getHostname(); // int lpos = domain.lastIndexOf("."); //int lpos2 = domain.lastIndexOf(".", lpos - 1); // if (lpos2 > 0) { // domain = domain.substring(lpos2 + 1); //} String realUrl ; try { /* See if it's a real url */ new URL ( url ) ; realUrl = url ; } catch ( final Throwable t ) { realUrl = "https://" + url + "/.well-known/timezone" ; } for ( int redirects = 0 ; redirects < 10 ; redirects ++ ) { try ( CloseableHttpResponse hresp = doCall ( realUrl , "capabilities" , null , null ) ) { if ( ( status == HttpServletResponse . SC_MOVED_PERMANENTLY ) || ( status == HttpServletResponse . SC_MOVED_TEMPORARILY ) || ( status == HttpServletResponse . SC_TEMPORARY_REDIRECT ) ) { //boolean permanent = rcode == HttpServletResponse.SC_MOVED_PERMANENTLY; final String newLoc = HttpUtil . getFirstHeaderValue ( hresp , "location" ) ; if ( newLoc != null ) { if ( debug ( ) ) { debug ( "Got redirected to " + newLoc + " from " + url ) ; } final int qpos = newLoc . indexOf ( "?" ) ; if ( qpos < 0 ) { realUrl = newLoc ; } else { realUrl = newLoc . substring ( 0 , qpos ) ; } // Try again continue ; } } if ( status != HttpServletResponse . SC_OK ) { // The response is invalid and did not provide the new location for // the resource. Report an error or possibly handle the response // like a 404 Not Found error. error ( "================================================" ) ; error ( "================================================" ) ; error ( "================================================" ) ; error ( "Got response " + status + ", from " + realUrl ) ; error ( "================================================" ) ; error ( "================================================" ) ; error ( "================================================" ) ; throw new TimezonesException ( TimezonesException . noPrimary , "Got response " + status + ", from " + realUrl ) ; } /* Should have a capabilities record. */ try { capabilities = om . readValue ( hresp . getEntity ( ) . getContent ( ) , CapabilitiesType . class ) ; } catch ( final Throwable t ) { // Bad data - we'll just go with the url for the moment? error ( t ) ; } return realUrl ; } catch ( final TimezonesException tze ) { throw tze ; } catch ( final Throwable t ) { if ( debug ( ) ) { error ( t ) ; } throw new TimezonesException ( t ) ; } } if ( debug ( ) ) { error ( "Too many redirects: Got response " + status + ", from " + realUrl ) ; } throw new TimezonesException ( "Too many redirects on " + realUrl ) ; }
See if we have a url for the service . If not discover the real one .