@@ -48,37 +48,69 @@ class SonarApiClient
4848
4949 private static String generateAuthValue() {
5050 return "${username}:${password}".getBytes().encodeBase64().toString()
51- }
52-
53- static boolean postQueryString (String url, String queryString ) {
51+ }
52+
53+ private static HttpURLConnection openPostConnection (String url) {
5454 def connection = new URL(url).openConnection() as HttpURLConnection
55-
55+
5656 connection.setRequestProperty('Accept', 'application/json')
57- connection.setRequestProperty('Authorization', "Basic ${getAuthValue ()}")
57+ connection.setRequestProperty('Authorization', "Basic ${generateAuthValue ()}")
5858 connection.setRequestMethod('POST')
5959 connection.doOutput = true
60-
61- def writer = new OutputStreamWriter(connection.outputStream)
62-
63- try {
64- writer.write(queryString)
65- writer.flush()
66-
67- if (connection.responseCode == HttpURLConnection.HTTP_OK ||
68- connection.responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
69- println "Request to ${url} with query string ${queryString} succeeded"
70- } else {
71- println "Request to ${url} with query string ${queryString} failed with status " +
72- "${connection.responseCode} and response ${connection.responseMessage}"
60+
61+ return connection
62+ }
63+
64+ static boolean postQueryString(
65+ String url,
66+ String queryString,
67+ int numberOfTimesToRetry = 60,
68+ int delayBetweenRetriesInMilliseconds = 1000) {
69+
70+ def finalResponseReceived = false
71+
72+ while ((!finalResponseReceived) && (numberOfTimesToRetry-- > 0)) {
73+
74+ def connection
75+ def writer
76+
77+ try {
78+ connection = openPostConnection(url)
79+ writer = new OutputStreamWriter(connection.outputStream)
80+
81+ writer.write(queryString)
82+ writer.flush()
83+
84+ def responseCode = connection.responseMessage
85+
86+ if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
87+ sleep(delayBetweenRetriesInMilliseconds)
88+ println "Waiting ${delayBetweenRetriesInMilliseconds} milliseconds to post to ${url} with " +
89+ "${numberOfTimesToRetry} retries remaining"
90+ } else if (responseCode == HttpURLConnection.HTTP_OK ||
91+ responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
92+ println "Request to ${url} with query string ${queryString} succeeded"
93+ finalResponseReceived = true
94+ } else {
95+ println "Request to ${url} with query string ${queryString} failed with status " +
96+ "${responseCode} and response ${connection.responseMessage}"
97+ finalResponseReceived = true
98+ }
99+ } catch (IOException ioe) {
100+ println "Request failed with error: ${ioe}"
73101 return false
102+ } finally {
103+ if (writer != null) writer.close()
104+ if (connection != null) connection.disconnect()
74105 }
75- } catch (IOException ioe) {
76- println "Request failed with error: ${ioe}"
106+ }
107+
108+ if (!finalResponseReceived) {
109+ println "Timed out waiting for response from ${url}"
77110 return false
78- } finally {
79- writer.close()
80111 }
81-
112+
82113 return true
83114 }
84115}
116+
0 commit comments