0

I have a groovy script using HTTPBuilder to make a RestAPI call to an SSL site. To connect I have imported the certs into a new keystore and use the recommended code in the HTTPBuilder support pages https://github.com/jgritman/httpbuilder/wiki/SSL

Here is the code:

def url = 'https://1.1.1.1'
def uri = new URIBuilder(url)

uri.path = '/rest/com/session'

def httpLOGIN = new HTTPBuilder(uri)
def keyStore = KeyStore.getInstance(KeyStore.defaultType)

getClass().getResource("\keystore.jks").withInputStream {
keyStore.load( it, "password".toCharArray())
}

String TempBasicAuth = "username:password".bytes.encodeBase64().toString()

httpLOGIN.client.connectionManager.schemeRegistry.register(
    new Scheme("https", 443, new SSLSocketFactory(keyStore))
)

httpLOGIN.request(POST,JSON) { req ->
headers.'Authorization' = "Basic $TempBasicAuth"

response.failure = { resp, json ->
    println "POST Failure. LOGIN: ${resp.statusLine}"
}
response.success = {// resp, json ->
    println "POST Success. LOGIN: ${resp.statusLine}"*/
}

}

The response I get is

Caught: java.lang.NullPointerException: Cannot invoke method withInputStream() on null object
java.lang.NullPointerException: Cannot invoke method withInputStream() on null object

I've tried providing the full path to the keystore but get the same result. Is it that I have not supplied the path correctly (I've tried multiple iterations) or is there another issue?

Mondo
  • 163
  • 13
  • I suspect you don't want to be escaping the 'k' in `"\keystore.jks"`, so use `\\\` to escape the backslash if you indeed want a backslash. – doelleri Jan 11 '18 at 07:27

1 Answers1

0

The issue was explained in this question -

Loading resources using getClass().getResource()

getResource is looking for the path relative to the class / script path and not the file path.

Mondo
  • 163
  • 13