Connect a proxy server with Groovy script

Connect a proxy server with Groovy script

A proxy server is commonly a computer system (or one can say mediator) which acts as an intermediary in the network communication between the server and the client [1]; screening all incoming and outgoing traffic [2].

In this post I will show how you can connect proxy server with the Groovy script:

import java.net.*
import java.io.*
import java.net.HttpURLConnection

def proxyHost = ""
def proxyPort = ""
baseURL = "https://jsonplaceholder.typicode.com/todos"

/** Proxy settings
*/
System.getProperties().put("proxySet", "true")
System.getProperties().put("proxyHost", proxyHost)
System.getProperties().put("proxyPort", proxyPort)

def getTodos() {

    URL url = new URL(baseURL)
    HttpURLConnection con = (HttpURLConnection) url.openConnection()
    con.setRequestMethod("GET")
    int responseCode = con.getResponseCode()
    return responseCode
}

getTodos()

Listing 1. Groovy script based on [3]

(Optional) The following section is for ForgeRock IDM.

Use the following cURL to run the above groovy script from ForgeRock IDM

curl --insecure \
--header "X-OpenIDM-Username: xxx" \
--header "X-OpenIDM-Password: xxx" \
--header "Accept-API-Version: resource=1.0" \
--header "Content-Type: application/json" \
--data '{
    "type" : "groovy",
    "file" : "script/<fileName>.groovy"
}'  \
--request POST "https:xxx/openidm/script?_action=eval" | jq

Other Articles by the Author

References

[1] M. Sysel and O. Doležal, “An educational HTTP proxy server,” Procedia Eng., vol. 69, pp. 128–132, 2014, doi: 10.1016/j.proeng.2014.02.212. Available: https://doi.org/10.1016/j.proeng.2014.02.212

[2] S. Classroom, “Proxy server.” https://www.youtube.com/watch?v=f-oCTcf5k_0

[3] michalbcz, “groovy - connect to URL through proxy.” https://gist.github.com/michalbcz/4167914 (accessed Jan. 26, 2023).

Leave a Reply

Your email address will not be published. Required fields are marked *


© 2024 A. Maharjan