Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Open sidebar
Whirlpool
whirlpool-client-cli
Commits
79e2e904
Commit
79e2e904
authored
Jul 10, 2019
by
zeroleak
Browse files
optimize httpClient: keep proxy connexion opened
parent
c46ff431
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
9 deletions
+69
-9
src/main/java/com/samourai/http/client/JavaHttpClient.java
src/main/java/com/samourai/http/client/JavaHttpClient.java
+64
-8
src/main/java/com/samourai/whirlpool/cli/utils/CliUtils.java
src/main/java/com/samourai/whirlpool/cli/utils/CliUtils.java
+5
-1
No files found.
src/main/java/com/samourai/http/client/JavaHttpClient.java
View file @
79e2e904
...
...
@@ -25,25 +25,31 @@ public class JavaHttpClient implements IHttpClient {
private
CliTorClientService
torClientService
;
private
CliConfig
cliConfig
;
private
ObjectMapper
objectMapper
;
private
HttpClient
httpClientShared
;
private
HttpClient
httpClientRegOut
;
public
JavaHttpClient
(
CliTorClientService
torClientService
,
CliConfig
cliConfig
)
{
this
.
torClientService
=
torClientService
;
this
.
cliConfig
=
cliConfig
;
this
.
objectMapper
=
new
ObjectMapper
();
objectMapper
.
disable
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
);
httpClientShared
=
null
;
httpClientRegOut
=
null
;
}
@Override
public
<
T
>
T
getJson
(
String
urlStr
,
Class
<
T
>
responseType
)
throws
HttpException
{
public
synchronized
<
T
>
T
getJson
(
String
urlStr
,
Class
<
T
>
responseType
)
throws
HttpException
{
final
boolean
isRegOut
=
false
;
try
{
HttpClient
httpClient
=
compute
HttpClient
(
false
);
HttpClient
httpClient
=
get
HttpClient
(
isRegOut
);
ContentResponse
response
=
httpClient
.
GET
(
urlStr
);
T
result
=
parseResponse
(
response
,
responseType
);
return
result
;
}
catch
(
Exception
e
)
{
clearHttpClient
(
isRegOut
);
if
(
log
.
isDebugEnabled
())
{
log
.
error
(
"getJson failed: "
+
urlStr
,
e
);
log
.
error
(
"getJson failed: "
+
urlStr
+
":"
+
e
.
getMessage
()
);
}
if
(!(
e
instanceof
HttpException
))
{
e
=
new
HttpException
(
e
,
null
);
...
...
@@ -53,10 +59,11 @@ public class JavaHttpClient implements IHttpClient {
}
@Override
public
<
T
>
T
postJsonOverTor
(
String
urlStr
,
Class
<
T
>
responseType
,
Object
bodyObj
)
public
synchronized
<
T
>
T
postJsonOverTor
(
String
urlStr
,
Class
<
T
>
responseType
,
Object
bodyObj
)
throws
HttpException
{
final
boolean
isRegOut
=
true
;
try
{
HttpClient
httpClient
=
compute
HttpClient
(
true
);
HttpClient
httpClient
=
get
HttpClient
(
isRegOut
);
Request
request
=
httpClient
.
POST
(
urlStr
);
String
jsonBody
=
objectMapper
.
writeValueAsString
(
bodyObj
);
...
...
@@ -68,6 +75,7 @@ public class JavaHttpClient implements IHttpClient {
T
result
=
parseResponse
(
response
,
responseType
);
return
result
;
}
catch
(
Exception
e
)
{
clearHttpClient
(
isRegOut
);
if
(
log
.
isDebugEnabled
())
{
log
.
error
(
"postJsonOverTor failed: "
+
urlStr
,
e
);
}
...
...
@@ -79,10 +87,11 @@ public class JavaHttpClient implements IHttpClient {
}
@Override
public
<
T
>
T
postUrlEncoded
(
String
urlStr
,
Class
<
T
>
responseType
,
Map
<
String
,
String
>
body
)
throws
HttpException
{
public
synchronized
<
T
>
T
postUrlEncoded
(
String
urlStr
,
Class
<
T
>
responseType
,
Map
<
String
,
String
>
body
)
throws
HttpException
{
final
boolean
isRegOut
=
false
;
try
{
HttpClient
httpClient
=
compute
HttpClient
(
false
);
HttpClient
httpClient
=
get
HttpClient
(
isRegOut
);
Request
request
=
httpClient
.
POST
(
urlStr
);
request
.
content
(
new
FormContentProvider
(
computeBodyFields
(
body
)));
...
...
@@ -91,6 +100,7 @@ public class JavaHttpClient implements IHttpClient {
T
result
=
parseResponse
(
response
,
responseType
);
return
result
;
}
catch
(
Exception
e
)
{
clearHttpClient
(
isRegOut
);
if
(
log
.
isDebugEnabled
())
{
log
.
error
(
"postUrlEncoded failed: "
+
urlStr
,
e
);
}
...
...
@@ -117,10 +127,56 @@ public class JavaHttpClient implements IHttpClient {
return
result
;
}
private
HttpClient
getHttpClient
(
boolean
isRegisterOutput
)
throws
Exception
{
if
(!
isRegisterOutput
)
{
if
(
httpClientShared
==
null
)
{
if
(
log
.
isDebugEnabled
())
{
log
.
debug
(
"+httpClientShared"
);
}
httpClientShared
=
computeHttpClient
(
isRegisterOutput
);
}
return
httpClientShared
;
}
else
{
if
(
httpClientRegOut
==
null
)
{
if
(
log
.
isDebugEnabled
())
{
log
.
debug
(
"+httpClientRegOut"
);
}
httpClientRegOut
=
computeHttpClient
(
isRegisterOutput
);
}
return
httpClientRegOut
;
}
}
private
HttpClient
computeHttpClient
(
boolean
isRegisterOutput
)
throws
Exception
{
HttpClient
httpClient
=
CliUtils
.
computeHttpClient
(
isRegisterOutput
,
torClientService
,
cliConfig
.
getCliProxy
());
httpClient
.
start
();
return
httpClient
;
}
private
void
clearHttpClient
(
boolean
isRegisterOutput
)
{
if
(!
isRegisterOutput
)
{
if
(
httpClientShared
!=
null
)
{
try
{
httpClientShared
.
stop
();
}
catch
(
Exception
e
)
{
}
if
(
log
.
isDebugEnabled
())
{
log
.
debug
(
"clear httpClientShared"
);
}
httpClientShared
=
null
;
}
}
else
{
if
(
httpClientRegOut
!=
null
)
{
try
{
httpClientRegOut
.
stop
();
}
catch
(
Exception
e
)
{
}
if
(
log
.
isDebugEnabled
())
{
log
.
debug
(
"clear httpClientRegOut"
);
}
httpClientRegOut
=
null
;
}
}
}
}
src/main/java/com/samourai/whirlpool/cli/utils/CliUtils.java
View file @
79e2e904
...
...
@@ -166,10 +166,14 @@ public class CliUtils {
if
(
cliProxyOptional
.
isPresent
())
{
CliProxy
cliProxy
=
cliProxyOptional
.
get
();
if
(
log
.
isDebugEnabled
())
{
log
.
debug
(
"httpClient
.
proxy="
+
cliProxy
);
log
.
debug
(
"
+
httpClient
:
proxy="
+
cliProxy
);
}
ProxyConfiguration
.
Proxy
jettyProxy
=
cliProxy
.
computeJettyProxy
();
jettyHttpClient
.
getProxyConfiguration
().
getProxies
().
add
(
jettyProxy
);
}
else
{
if
(
log
.
isDebugEnabled
())
{
log
.
debug
(
"+httpClient: no proxy"
);
}
}
return
jettyHttpClient
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment