public Boolean verifySubscriberRequestedSubscription ( SubscriptionRequest sr ) throws SubscriptionOriginVerificationException { LOG . info ( "(Hub) -> Subscriber, sending notification to verify the origin of the subscription {}." , sr . getCallback ( ) ) ; SubscriptionConfirmationRequest sc = new SubscriptionConfirmationRequest ( sr . getMode ( ) , sr . getTopic ( ) , "challenge" , "0" ) ; URI uri ; try { uri = new URIBuilder ( sr . getCallback ( ) ) . setParameters ( sc . toRequestParameters ( ) ) . build ( ) ; } catch ( URISyntaxException e ) { throw new SubscriptionOriginVerificationException ( "URISyntaxException while sending a confirmation of subscription" , e ) ; } HttpGet httpGet = new HttpGet ( uri ) ; CloseableHttpClient httpclient = HttpClients . createDefault ( ) ; CloseableHttpResponse response ; try { response = httpclient . execute ( httpGet ) ; } catch ( IOException e ) { throw new SubscriptionOriginVerificationException ( "IOException while sending a confirmation of subscription" , e ) ; } LOG . info ( "Subscriber replied with the http code {}." , response . getStatusLine ( ) . getStatusCode ( ) ) ; Integer returnedCode = response . getStatusLine ( ) . getStatusCode ( ) ; // if code is a success code return true, else false return ( 199 < returnedCode ) && ( returnedCode < 300 ) ; }
Output method that sends a subscription confirmation for the subscriber to avoid DoS attacks or false subscription .