Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HCX-475 Save http client in restProcessor #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 43 additions & 37 deletions src/org/camunda/latera/bss/http/HTTPRestProcessor.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ class HTTPRestProcessor {
Boolean supressRequestBodyLog
Boolean supressResponseBodyLog

private static final Map<String, OkHttpBuilder> cachedBuilders = [:]

HTTPRestProcessor(Map params) {
this.logger = new SimpleLogger(params.execution)
def ENV = System.getenv()
def ENV = System.getenv()

this.testEnv = ENV['CAMUNDA_EXT_ENV'] == 'test'
this.baseUrl = params.baseUrl
Expand Down Expand Up @@ -51,51 +53,56 @@ class HTTPRestProcessor {
params.remove('supressResponseBodyLog')
params.remove('execution')

this.httpClient = OkHttpBuilder.configure {
request.uri = this.baseUrl.toString()
params.remove('baseUrl')

request.contentType = 'application/json'
response.success responseBlock(false, this.supressRequestBodyLog)
response.failure responseBlock(true, this.supressResponseBodyLog)

client.clientCustomizer {
it.followRedirects = true
it.connectTimeout(ENV['HTTP_CONNECT_TIMEOUT_SEC'].toInteger(), TimeUnit.SECONDS)
it.readTimeout(ENV['HTTP_READ_TIMEOUT_SEC'].toInteger(), TimeUnit.SECONDS)
it.writeTimeout(ENV['HTTP_WRITE_TIMEOUT_SEC'].toInteger(), TimeUnit.SECONDS)
}
if (cachedBuilders.containsKey(this.baseUrl)) {
this.httpClient = cachedBuilders[this.baseUrl]
} else {
this.httpClient = OkHttpBuilder.configure {
request.uri = this.baseUrl.toString()
params.remove('baseUrl')

request.contentType = 'application/json'
response.success responseBlock(false, this.supressRequestBodyLog)
response.failure responseBlock(true, this.supressResponseBodyLog)

client.clientCustomizer {
it.followRedirects = true
it.connectTimeout(ENV['HTTP_CONNECT_TIMEOUT_SEC'].toInteger(), TimeUnit.SECONDS)
it.readTimeout(ENV['HTTP_READ_TIMEOUT_SEC'].toInteger(), TimeUnit.SECONDS)
it.writeTimeout(ENV['HTTP_WRITE_TIMEOUT_SEC'].toInteger(), TimeUnit.SECONDS)
}

!ENV['HTTP_USE_SSL'].toBoolean() && ignoreSslIssues(execution)
!ENV['HTTP_USE_SSL'].toBoolean() && ignoreSslIssues(execution)

if (notEmpty(params.user) && notEmpty(params.password)) {
request.auth.basic(params.user, params.password)
params.remove('user')
params.remove('password')
}
if (notEmpty(params.user) && notEmpty(params.password)) {
request.auth.basic(params.user, params.password)
params.remove('user')
params.remove('password')
}

if (params) {
if (params.client) {
params.client.each { k,v ->
client."${k}" = v
if (params) {
if (params.client) {
params.client.each { k,v ->
client."${k}" = v
}
params.remove('client')
}
params.remove('client')
}
if (params.headers) {
params.headers.each { k,v ->
request.headers."${k}" = v
if (params.headers) {
params.headers.each { k,v ->
request.headers."${k}" = v
}
params.remove('headers')
}
params.each { k,v ->
request."${k}" = v
}
params.remove('headers')
}
params.each { k,v ->
request."${k}" = v
}
}
cachedBuilders[this.baseUrl] = this.httpClient
}
}

def private responseBlock(Boolean failure = false, Boolean supress = false) {
{FromServer response, Object data ->
{ FromServer response, Object data ->
logger.debug("Response status: ${response.statusCode}")
logger.debug("Content-Type: ${response.contentType}")
logger.debug("Response data: -----")
Expand Down Expand Up @@ -185,8 +192,7 @@ class HTTPRestProcessor {
}

private String parsePath(CharSequence rawUrl) {
// baseUrl http://homs:3000/api
// return /api
// baseUrl http://homs:3000/api -> возвращает /api
URL absolute = new URL(rawUrl.toString())
return absolute.getPath()
}
Expand Down