import java.net.HttpURLConnection
import java.net.URL
import groovy.transform.CompileStatic
import java.util.concurrent.FutureTask
@CompileStatic
def async (Closure close) {
def task = new FutureTask(close)
new Thread(task).start()
return task
} //Tell Groovy to use static type checking, and define a function that we use to create new async requests
String username = "<username>"
String password = "<password>"
//Define the credentials that we'll use to authenticate against the server/DC version of Jira or Confluence
//If we want to authenticate against Jira or Confluence Cloud, we'd need to replace the password with an API token, and replace the username with an email address
// Define a list of URLs to fetch
def urls = [
"<URL>",
"<URL>",
"<URL>",
"<URL>",
"<URL>",
]
// Define a list to hold the async requests
def asyncResponses = []
def sb = []
// Loop over the list of URLs and make an async request for each one
urls.each {
url ->
//For each URL in the array
def asyncRequest = {
//Define a new async request object
HttpURLConnection connection = null
//Define a new HTTP URL Connection, but make it null
try {
// Create a connection to the URL
URL u = new URL(url)
connection = (HttpURLConnection) u.openConnection()
connection.setRequestMethod("GET")
//Create a new HTTP connection with the current URL, and set the request method as GET
connection.setConnectTimeout(5000)
connection.setReadTimeout(5000)
//Set the connection parameters
String authString = "${username}:${password}"
String authStringEncoded = authString.bytes.encodeBase64().toString()
connection.setRequestProperty("Authorization", "Basic ${authStringEncoded}")
//Set the authentication parameters
// Read the response and log the results
def responseCode = connection.getResponseCode()
def responseBody = connection.getInputStream().getText()
logger.warn("Response status code for ${url}: ${responseCode}")
sb.add("Response body for ${url}: ${responseBody}")
} catch (Exception e) {
// Catch any errors
logger.warn("Error fetching ${url}: ${e.getMessage()}")
} finally {
// Terminate the connection
if (connection != null) {
connection.disconnect()
}
}
}
asyncResponses.add(async (asyncRequest))
}
// Wait for all async responses to complete
asyncResponses.each {
asyncResponse ->
asyncResponse.get()
}
return sb