1
1
package org .openstack4j .connectors .resteasy ;
2
2
3
- import java .util .List ;
4
- import java .util .Map ;
5
-
6
- import javax .ws .rs .core .UriBuilder ;
7
-
8
- import org .jboss .resteasy .client .ClientRequest ;
9
- import org .jboss .resteasy .client .ClientResponse ;
10
- import org .openstack4j .connectors .resteasy .executors .ApacheHttpClientExecutor ;
3
+ import org .jboss .resteasy .client .jaxrs .ResteasyClientBuilder ;
4
+ import org .jboss .resteasy .client .jaxrs .ResteasyWebTarget ;
5
+ import org .openstack4j .connectors .resteasy .executors .ApacheHttpClientEngine ;
11
6
import org .openstack4j .core .transport .ClientConstants ;
12
7
import org .openstack4j .core .transport .HttpRequest ;
13
8
import org .openstack4j .core .transport .functions .EndpointURIFromRequestFunction ;
14
9
10
+ import javax .ws .rs .client .Entity ;
11
+ import javax .ws .rs .client .Invocation ;
12
+ import javax .ws .rs .core .Response ;
13
+ import javax .ws .rs .core .UriBuilder ;
14
+ import java .util .List ;
15
+ import java .util .Map ;
16
+
15
17
/**
16
- * HttpCommand is responsible for executing the actual request driven by the HttpExecutor.
17
- *
18
+ * HttpCommand is responsible for executing the actual request driven by the HttpExecutor.
19
+ *
18
20
* @param <R>
19
21
*/
20
22
public final class HttpCommand <R > {
21
23
22
24
private HttpRequest <R > request ;
23
- private ClientRequest client ;
25
+ private ResteasyWebTarget resteasyWebTarget ;
24
26
private int retries ;
27
+ private Invocation .Builder resteasyRequest ;
25
28
26
29
private HttpCommand (HttpRequest <R > request ) {
27
30
this .request = request ;
@@ -33,37 +36,39 @@ private HttpCommand(HttpRequest<R> request) {
33
36
* @return the command
34
37
*/
35
38
public static <R > HttpCommand <R > create (HttpRequest <R > request ) {
36
- HttpCommand <R > command = new HttpCommand <R >(request );
39
+ HttpCommand <R > command = new HttpCommand <>(request );
37
40
command .initialize ();
38
41
return command ;
39
42
}
40
43
41
44
private void initialize () {
42
- client = new ClientRequest ( UriBuilder . fromUri ( new EndpointURIFromRequestFunction (). apply ( request )),
43
- ApacheHttpClientExecutor . create (request .getConfig ()), ResteasyClientFactory . getInstance ());
44
-
45
- client . followRedirects ( true );
46
-
45
+
46
+ resteasyWebTarget = new ResteasyClientBuilder (). httpEngine ( ApacheHttpClientEngine . create (request .getConfig ()))
47
+ . providerFactory ( ResteasyClientFactory . getInstance ()). build ()
48
+ . target ( UriBuilder . fromUri ( new EndpointURIFromRequestFunction (). apply ( request )) );
49
+
47
50
populateQueryParams (request );
51
+ resteasyRequest = resteasyWebTarget .request ();
48
52
populateHeaders (request );
49
53
}
50
54
51
55
/**
52
56
* Executes the command and returns the Response
53
- *
57
+ *
54
58
* @return the response
55
- * @throws Exception
56
59
*/
57
- public ClientResponse <R > execute () throws Exception {
58
-
60
+ public Response execute (){
61
+
62
+ Invocation webRequest ;
59
63
if (request .getEntity () != null ) {
60
- client .body (request .getContentType (), request .getEntity ());
64
+ webRequest = resteasyRequest .build (request .getMethod ().name (), Entity .entity (request .getEntity (), request .getContentType ()));
65
+ } else if (request .hasJson ()) {
66
+ webRequest = resteasyRequest .build (request .getMethod ().name () , Entity .entity (request .getJson (),ClientConstants .CONTENT_TYPE_JSON ));
67
+ }else {
68
+ webRequest = resteasyRequest .build (request .getMethod ().name ());
61
69
}
62
- else if (request .hasJson ()) {
63
- client .body (ClientConstants .CONTENT_TYPE_JSON , request .getJson ());
64
- }
65
- ClientResponse <R > response = client .httpMethod (request .getMethod ().name (), request .getReturnType ());
66
- return response ;
70
+
71
+ return webRequest .invoke ();
67
72
}
68
73
69
74
/**
@@ -72,21 +77,21 @@ else if(request.hasJson()) {
72
77
public boolean hasEntity () {
73
78
return request .getEntity () != null ;
74
79
}
75
-
80
+
76
81
/**
77
82
* @return current retry execution count for this command
78
83
*/
79
84
public int getRetries () {
80
85
return retries ;
81
86
}
82
-
87
+
83
88
/**
84
89
* @return incremement's the retry count and returns self
85
90
*/
86
91
public HttpCommand <R > incrementRetriesAndReturn () {
87
- initialize ();
88
- retries ++;
89
- return this ;
92
+ initialize ();
93
+ retries ++;
94
+ return this ;
90
95
}
91
96
92
97
public HttpRequest <R > getRequest () {
@@ -97,9 +102,9 @@ private void populateQueryParams(HttpRequest<R> request) {
97
102
98
103
if (!request .hasQueryParams ()) return ;
99
104
100
- for (Map .Entry <String , List <Object > > entry : request .getQueryParams ().entrySet ()) {
105
+ for (Map .Entry <String , List <Object >> entry : request .getQueryParams ().entrySet ()) {
101
106
for (Object o : entry .getValue ()) {
102
- client = client . queryParameter (entry .getKey (), o );
107
+ resteasyWebTarget = resteasyWebTarget . queryParam (entry .getKey (), o );
103
108
}
104
109
}
105
110
}
@@ -108,8 +113,8 @@ private void populateHeaders(HttpRequest<R> request) {
108
113
109
114
if (!request .hasHeaders ()) return ;
110
115
111
- for (Map .Entry <String , Object > h : request .getHeaders ().entrySet ()) {
112
- client .header (h .getKey (), h .getValue ());
116
+ for (Map .Entry <String , Object > h : request .getHeaders ().entrySet ()) {
117
+ resteasyRequest .header (h .getKey (), h .getValue ());
113
118
}
114
119
}
115
120
}
0 commit comments