diff --git a/src/org/camunda/latera/bss/connectors/Hoper.groovy b/src/org/camunda/latera/bss/connectors/Hoper.groovy index 893017a6..5d183111 100644 --- a/src/org/camunda/latera/bss/connectors/Hoper.groovy +++ b/src/org/camunda/latera/bss/connectors/Hoper.groovy @@ -2,16 +2,17 @@ package org.camunda.latera.bss.connectors import org.camunda.latera.bss.http.HTTPRestProcessor import org.camunda.latera.bss.logging.SimpleLogger +import org.camunda.latera.bss.utils.Base64Converter import org.camunda.bpm.engine.delegate.DelegateExecution class Hoper { - HTTPRestProcessor processor - LinkedHashMap headers - DelegateExecution execution - SimpleLogger logger String url + private String user + private String password String version - LinkedHashMap auth + HTTPRestProcessor http + DelegateExecution execution + SimpleLogger logger Hoper(DelegateExecution execution) { this.execution = execution @@ -19,24 +20,47 @@ class Hoper { this.url = execution.getVariable("hoperUrl") this.version = execution.getVariable("hoperVersion") ?: '2' - this.auth = [ - login: execution.getVariable("hydraUser"), - password: execution.getVariable("hydraPassword") + this.user = execution.getVariable("hydraUser") + this.password = execution.getVariable("hydraPassword") + this.http = new HTTPRestProcessor(baseUrl : this.url, + execution : execution) + } + + private def authToken() { + def auth = [ + session: [ + login : user, + password : password + ] ] - this.processor = new HTTPRestProcessor(baseUrl : this.url, - execution : execution) + return http.sendRequest('get', + path : "/rest/v${this.version}/login", + body : auth, + supressRequestBodyLog : true, + supressResponseBodyLog : true)?.session?.token + } + + private def authBasic() { + def auth = "${user}:${password}" + return Base64Converter.to(auth) + } + + private def authHeader() { + if (version.toString() == '1') { + return ['Authorization': "Basic ${authBasic}"] + } + if (version.toString() == '2') { + return ['Authorization': "Token token=\"${authToken}\""] + } + return [] } def sendRequest(LinkedHashMap input, String method = 'get') { - def token = this.processor.sendRequest(path: "/rest/v${this.version}/login", body: [session: this.auth], supressRequestBodyLog: true, supressResponseBodyLog: true, 'get')?.session?.token - def headers = [ - 'Authorization': "Token token=\"${token}\"" - ] if (!input.headers) { input.headers = [:] } - input.headers += headers - input.path = "/rest/v${this.version}/${input.path}".toString() - return this.processor.sendRequest(input, method) + input.headers += authHeader + input.path = "/rest/v${version}/${input.path}".toString() + return http.sendRequest(input, method) } } \ No newline at end of file