FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
AEM-REST-Integration/HttpClientFactoryImpl.java at master · kiransg89/AEM-REST-Integration · GitHub
kiransg89
/
AEM-REST-Integration
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
AEM-REST-Integration
/
HttpClientFactoryImpl.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
229 lines (186 loc) · 8.11 KB
Breadcrumbs
AEM-REST-Integration
/
HttpClientFactoryImpl.java
Copy path
File metadata and controls
229 lines (186 loc) · 8.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package
com
.
example
.
core
.
services
.
impl
;
import
java
.
io
.
IOException
;
import
java
.
security
.
KeyManagementException
;
import
java
.
security
.
KeyStoreException
;
import
java
.
security
.
NoSuchAlgorithmException
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
import
java
.
util
.
concurrent
.
TimeUnit
;
import
org
.
apache
.
commons
.
lang3
.
StringUtils
;
import
org
.
apache
.
http
.
Header
;
import
org
.
apache
.
http
.
HttpResponse
;
import
org
.
apache
.
http
.
client
.
config
.
RequestConfig
;
import
org
.
apache
.
http
.
client
.
fluent
.
Executor
;
import
org
.
apache
.
http
.
client
.
fluent
.
Request
;
import
org
.
apache
.
http
.
config
.
Registry
;
import
org
.
apache
.
http
.
config
.
RegistryBuilder
;
import
org
.
apache
.
http
.
conn
.
ConnectionKeepAliveStrategy
;
import
org
.
apache
.
http
.
conn
.
socket
.
ConnectionSocketFactory
;
import
org
.
apache
.
http
.
conn
.
socket
.
PlainConnectionSocketFactory
;
import
org
.
apache
.
http
.
conn
.
ssl
.
NoopHostnameVerifier
;
import
org
.
apache
.
http
.
conn
.
ssl
.
SSLConnectionSocketFactory
;
import
org
.
apache
.
http
.
conn
.
ssl
.
TrustAllStrategy
;
import
org
.
apache
.
http
.
impl
.
client
.
CloseableHttpClient
;
import
org
.
apache
.
http
.
impl
.
client
.
HttpClientBuilder
;
import
org
.
apache
.
http
.
impl
.
conn
.
PoolingHttpClientConnectionManager
;
import
org
.
apache
.
http
.
message
.
BasicHeader
;
import
org
.
apache
.
http
.
osgi
.
services
.
HttpClientBuilderFactory
;
import
org
.
apache
.
http
.
protocol
.
HttpContext
;
import
org
.
apache
.
http
.
ssl
.
SSLContextBuilder
;
import
org
.
osgi
.
service
.
component
.
annotations
.
Activate
;
import
org
.
osgi
.
service
.
component
.
annotations
.
Component
;
import
org
.
osgi
.
service
.
component
.
annotations
.
Deactivate
;
import
org
.
osgi
.
service
.
component
.
annotations
.
Modified
;
import
org
.
osgi
.
service
.
component
.
annotations
.
Reference
;
import
org
.
osgi
.
service
.
metatype
.
annotations
.
Designate
;
import
org
.
slf4j
.
Logger
;
import
org
.
slf4j
.
LoggerFactory
;
import
com
.
example
.
core
.
services
.
HttpClientFactory
;
import
com
.
example
.
core
.
services
.
config
.
HttpClientFactoryConfig
;
/**
* Implementation of @{@link HttpClientFactory}.
* <p>
* HttpClientFactory provides service to handle API connection and executor.
*/
@
Component
(
service
=
HttpClientFactory
.
class
)
@
Designate
(
ocd
=
HttpClientFactoryConfig
.
class
)
public
class
HttpClientFactoryImpl
implements
HttpClientFactory
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
HttpClientFactoryImpl
.
class
);
private
Executor
executor
;
private
String
baseUrl
;
private
String
uriExternalType
;
private
String
apiStoreLocatorHostName
;
private
CloseableHttpClient
httpClient
;
private
HttpClientFactoryConfig
config
;
@
Reference
private
HttpClientBuilderFactory
httpClientBuilderFactory
;
@
Activate
@
Modified
protected
void
activate
(
HttpClientFactoryConfig
config
)
throws
Exception
{
log
.
info
(
"########### OSGi Configs Start ###############"
);
log
.
info
(
"API Host Name : {}"
,
config
.
apiHostName
());
log
.
info
(
"URI Type: {}"
,
config
.
uriType
());
log
.
info
(
"########### OSGi Configs End ###############"
);
closeHttpConnection
();
this
.
config
=
config
;
if
(
this
.
config
.
apiHostName
() ==
null
) {
log
.
debug
(
"Configuration is not valid. Both hostname is mandatory."
);
throw
new
IllegalArgumentException
(
"Configuration is not valid. Both hostname is mandatory."
);
}
this
.
uriExternalType
=
this
.
config
.
uriExternalType
();
this
.
apiStoreLocatorHostName
=
this
.
config
.
apiStoreLocatorHostName
();
this
.
baseUrl
=
StringUtils
.
join
(
this
.
config
.
apiHostName
(),
config
.
uriType
());
initExecutor
();
}
private
void
initExecutor
()
throws
Exception
{
PoolingHttpClientConnectionManager
connMgr
=
null
;
RequestConfig
requestConfig
=
initRequestConfig
();
HttpClientBuilder
builder
=
httpClientBuilderFactory
.
newBuilder
();
builder
.
setDefaultRequestConfig
(
requestConfig
);
if
(
config
.
relaxedSSL
()) {
connMgr
=
initPoolingConnectionManagerWithRelaxedSSL
();
}
else
{
connMgr
=
new
PoolingHttpClientConnectionManager
();
}
connMgr
.
closeExpiredConnections
();
connMgr
.
setMaxTotal
(
config
.
maxTotalOpenConnections
());
connMgr
.
setDefaultMaxPerRoute
(
config
.
maxConcurrentConnectionPerRoute
());
builder
.
setConnectionManager
(
connMgr
);
List
<
Header
>
headers
=
new
ArrayList
<>();
headers
.
add
(
new
BasicHeader
(
"Accept"
,
"application/json"
));
builder
.
setDefaultHeaders
(
headers
);
builder
.
setKeepAliveStrategy
(
keepAliveStratey
);
httpClient
=
builder
.
build
();
executor
=
Executor
.
newInstance
(
httpClient
);
}
private
PoolingHttpClientConnectionManager
initPoolingConnectionManagerWithRelaxedSSL
()
throws
NoSuchAlgorithmException
,
KeyStoreException
,
KeyManagementException
{
PoolingHttpClientConnectionManager
connMgr
;
SSLContextBuilder
sslbuilder
=
new
SSLContextBuilder
();
sslbuilder
.
loadTrustMaterial
(
new
TrustAllStrategy
());
SSLConnectionSocketFactory
sslsf
=
new
SSLConnectionSocketFactory
(
sslbuilder
.
build
(),
NoopHostnameVerifier
.
INSTANCE
);
Registry
<
ConnectionSocketFactory
>
socketFactoryRegistry
=
RegistryBuilder
.<
ConnectionSocketFactory
>
create
()
.
register
(
"http"
,
PlainConnectionSocketFactory
.
getSocketFactory
()).
register
(
"https"
,
sslsf
).
build
();
connMgr
=
new
PoolingHttpClientConnectionManager
(
socketFactoryRegistry
);
return
connMgr
;
}
private
RequestConfig
initRequestConfig
() {
return
RequestConfig
.
custom
()
.
setConnectTimeout
(
Math
.
toIntExact
(
TimeUnit
.
SECONDS
.
toMillis
(
config
.
defaultConnectionTimeout
())))
.
setSocketTimeout
(
Math
.
toIntExact
(
TimeUnit
.
SECONDS
.
toMillis
(
config
.
defaultSocketTimeout
())))
.
setConnectionRequestTimeout
(
Math
.
toIntExact
(
TimeUnit
.
SECONDS
.
toMillis
(
config
.
defaultConnectionRequestTimeout
())))
.
build
();
}
@
Deactivate
protected
void
deactivate
() {
closeHttpConnection
();
}
private
void
closeHttpConnection
() {
if
(
null
!=
httpClient
) {
try
{
httpClient
.
close
();
}
catch
(
final
IOException
exception
) {
log
.
debug
(
"IOException while clossing API, {}"
,
exception
.
getMessage
());
}
}
}
@
Override
public
Executor
getExecutor
() {
return
executor
;
}
@
Override
public
Request
get
(
String
partialUrl
) {
String
url
=
baseUrl
+
partialUrl
;
return
Request
.
Get
(
url
);
}
@
Override
public
Request
post
(
String
partialUrl
) {
String
url
=
baseUrl
+
partialUrl
;
return
Request
.
Post
(
url
);
}
@
Override
public
Request
postWithAbsolute
(
String
absolutelUrl
) {
return
Request
.
Post
(
absolutelUrl
);
}
@
Override
public
Request
put
(
String
partialUrl
) {
String
url
=
baseUrl
+
partialUrl
;
return
Request
.
Put
(
url
);
}
@
Override
public
Request
delete
(
String
partialUrl
) {
String
url
=
baseUrl
+
partialUrl
;
return
Request
.
Delete
(
url
);
}
@
Override
public
Request
options
(
String
partialUrl
) {
String
url
=
baseUrl
+
partialUrl
;
return
Request
.
Options
(
url
);
}
@
Override
public
String
getExternalURIType
() {
return
uriExternalType
;
}
@
Override
public
String
getApiStoreLocatorHostName
() {
return
apiStoreLocatorHostName
;
}
ConnectionKeepAliveStrategy
keepAliveStratey
=
new
ConnectionKeepAliveStrategy
() {
@
Override
public
long
getKeepAliveDuration
(
HttpResponse
response
,
HttpContext
context
) {
/*
* HeaderElementIterator headerElementIterator = new BasicHeaderElementIterator(
* response.headerIterator(HTTP.CONN_KEEP_ALIVE));
*
* while (headerElementIterator.hasNext()) { HeaderElement headerElement =
* headerElementIterator.nextElement(); String param = headerElement.getName();
* String value = headerElement.getValue(); if (value != null &&
* param.equalsIgnoreCase("timeout")) { return
* TimeUnit.SECONDS.toMillis(Long.parseLong(value)); } }
*/
return
TimeUnit
.
SECONDS
.
toMillis
(
config
.
defaultKeepAliveconnection
());
}
};
}
Back
|
FazBrowse Home
|
New Git URL