Skip to content

Commit ed853c4

Browse files
committed
client: add the support for custom entity parsers
Some API (e.g. glance image show and download) require custom entity parsers. In this patch: * OpenStackClientConnector.request() (renamed from execute) returns now OpenStackResponse objects * the OpenStackResponse interface has now a getEntity method to parse and return the entity * the connectors and responses implementations (resteasy and jersey2) have been adapted to the new interfaces * a new RESTEasyResponse class has been added * OpenStackClient now exposes a request method which returns response objects * the old OpenStackClient execute method is now a wrapper to the request and getEntity methods * the GlanceListImages examples has been updated to test show image Signed-off-by: Federico Simoncelli <fsimonce@redhat.com>
1 parent d70db94 commit ed853c4

File tree

10 files changed

+88
-46
lines changed

10 files changed

+88
-46
lines changed

glance-client/src/main/java/com/woorea/openstack/glance/ImagesResource.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,11 @@ public class Show extends OpenStackRequest<Image> {
113113
public Show(String id) {
114114
super(CLIENT, HttpMethod.HEAD, new StringBuilder("/images/").append(id).toString(), null, Image.class);
115115
}
116-
117-
/* (non-Javadoc)
118-
* @see org.openstack.base.client.OpenStackRequest#execute()
119-
*/
116+
120117
@Override
121118
public Image execute() {
122-
//custom parsing here
123-
OpenStackResponse response = CLIENT.execute(this, OpenStackResponse.class);
124-
return parse(response.headers());
119+
// custom parsing here
120+
return parse(CLIENT.request(this).headers());
125121
}
126122

127123
}
@@ -156,20 +152,15 @@ public Download(String id) {
156152
header("Accept", "application/octet-stream");
157153
}
158154

159-
/* (non-Javadoc)
160-
* @see org.openstack.base.client.OpenStackRequest#execute()
161-
*/
162155
@Override
163156
public ImageDownload execute() {
164-
//custom parsing here
165-
OpenStackResponse response = CLIENT.execute(this, OpenStackResponse.class);
157+
// custom parsing here
158+
OpenStackResponse response = CLIENT.request(this);
166159
ImageDownload imageDownload = new ImageDownload();
167160
imageDownload.setImage(parse(response.headers()));
168161
imageDownload.setInputStream(response.getInputStream());
169162
return imageDownload;
170163
}
171-
172-
173164

174165
}
175166

openstack-client-connectors/jersey2-connector/src/main/java/com/woorea/openstack/connector/JaxRs20Connector.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,24 @@
1414

1515
import com.woorea.openstack.base.client.OpenStackClientConnector;
1616
import com.woorea.openstack.base.client.OpenStackRequest;
17+
import com.woorea.openstack.base.client.OpenStackResponse;
1718
import com.woorea.openstack.base.client.OpenStackResponseException;
1819

1920
public class JaxRs20Connector implements OpenStackClientConnector {
2021

2122
protected Client client = OpenStack.CLIENT;
2223

2324
@Override
24-
public <T> T execute(OpenStackRequest<T> request) {
25+
public <T> OpenStackResponse request(OpenStackRequest<T> request) {
2526
WebTarget target = client.target(request.endpoint()).path(request.path());
27+
2628
for(Map.Entry<String, Object> entry : request.queryParams().entrySet()) {
2729
target = target.queryParam(entry.getKey(), entry.getValue());
2830
}
29-
target.register(new LoggingFilter(Logger.getLogger("os"),10000));
31+
32+
target.register(new LoggingFilter(Logger.getLogger("os"), 10000));
3033
Invocation.Builder invocation = target.request();
34+
3135
for(Map.Entry<String, List<Object>> h : request.headers().entrySet()) {
3236
StringBuilder sb = new StringBuilder();
3337
for(Object v : h.getValue()) {
@@ -36,30 +40,19 @@ public <T> T execute(OpenStackRequest<T> request) {
3640
invocation.header(h.getKey(), sb);
3741
}
3842

39-
Entity<?> entity = (request.entity() == null) ? null : Entity
40-
.entity(request.entity().getEntity(), request.entity()
41-
.getContentType());
43+
Entity<?> entity = (request.entity() == null) ? null :
44+
Entity.entity(request.entity().getEntity(), request.entity().getContentType());
4245

4346
try {
4447
if (entity != null) {
45-
if (request.returnType() == null || request.returnType() == Void.class) {
46-
invocation.method(request.method().name(), entity);
47-
} else {
48-
return invocation.method(request.method().name(), entity, request.returnType());
49-
}
48+
return new JaxRs20Response(invocation.method(request.method().name(), entity));
5049
} else {
51-
if (request.returnType() == null || request.returnType() == Void.class) {
52-
invocation.method(request.method().name());
53-
} else {
54-
return invocation.method(request.method().name(), request.returnType());
55-
}
50+
return new JaxRs20Response(invocation.method(request.method().name()));
5651
}
5752
} catch (ClientErrorException e) {
5853
throw new OpenStackResponseException(e.getResponse()
5954
.getStatusInfo().toString(), e.getResponse().getStatus());
6055
}
61-
62-
return null;
6356
}
6457

6558
}

openstack-client-connectors/jersey2-connector/src/main/java/com/woorea/openstack/connector/JaxRs20Response.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public JaxRs20Response(Response response) {
1616
this.response = response;
1717
}
1818

19+
@Override
20+
public <T> T getEntity(Class<T> returnType) {
21+
return response.readEntity(returnType);
22+
}
23+
1924
@Override
2025
public InputStream getInputStream() {
2126
return (InputStream) response.getEntity();

openstack-client-connectors/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<profile>
1515
<id>jersey</id>
1616
<activation>
17-
<activeByDefault>true</activeByDefault>
17+
<activeByDefault>false</activeByDefault>
1818
</activation>
1919
<modules>
2020
<module>jersey-connector</module>

openstack-client-connectors/resteasy-connector/src/main/java/com/woorea/openstack/connector/RESTEasyConnector.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.woorea.openstack.base.client.OpenStackClientConnector;
2222
import com.woorea.openstack.base.client.OpenStackRequest;
23+
import com.woorea.openstack.base.client.OpenStackResponse;
2324
import com.woorea.openstack.base.client.OpenStackResponseException;
2425

2526
public class RESTEasyConnector implements OpenStackClientConnector {
@@ -59,12 +60,13 @@ public ObjectMapper getContext(Class<?> type) {
5960
CLIENT_FACTORY = new ClientRequestFactory(ClientRequest.getDefaultExecutor(), providerFactory);
6061
}
6162

62-
@Override
63-
public <T> T execute(OpenStackRequest<T> request) {
63+
public <T> OpenStackResponse request(OpenStackRequest<T> request) {
6464
ClientRequest client = CLIENT_FACTORY.createRequest(request.endpoint() + "/" + request.path());
65+
6566
for(Map.Entry<String, Object> entry : request.queryParams().entrySet()) {
6667
client = client.queryParameter(entry.getKey(), String.valueOf(entry.getValue()));
6768
}
69+
6870
for (Entry<String, List<Object>> h : request.headers().entrySet()) {
6971
StringBuilder sb = new StringBuilder();
7072
for (Object v : h.getValue()) {
@@ -86,9 +88,9 @@ public <T> T execute(OpenStackRequest<T> request) {
8688
}
8789

8890
if (response.getStatus() == HttpStatus.SC_OK
89-
|| response.getStatus() == HttpStatus.SC_CREATED
90-
|| response.getStatus() == HttpStatus.SC_NO_CONTENT) {
91-
return response.getEntity(request.returnType());
91+
|| response.getStatus() == HttpStatus.SC_CREATED
92+
|| response.getStatus() == HttpStatus.SC_NO_CONTENT) {
93+
return new RESTEasyResponse(response);
9294
}
9395

9496
response.releaseConnection();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.woorea.openstack.connector;
2+
3+
import org.jboss.resteasy.client.ClientResponse;
4+
import com.woorea.openstack.base.client.OpenStackResponse;
5+
6+
import javax.ws.rs.core.MultivaluedMap;
7+
import java.io.InputStream;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
public class RESTEasyResponse implements OpenStackResponse {
12+
13+
private ClientResponse response;
14+
15+
public RESTEasyResponse(ClientResponse response) {
16+
this.response = response;
17+
}
18+
19+
@Override
20+
public <T> T getEntity(Class<T> returnType) {
21+
return (T) response.getEntity(returnType);
22+
}
23+
24+
@Override
25+
public InputStream getInputStream() {
26+
return (InputStream) response.getEntity();
27+
}
28+
29+
@Override
30+
public String header(String name) {
31+
@SuppressWarnings("unchecked")
32+
MultivaluedMap<String, String> responseHeaders = response.getHeaders();
33+
return responseHeaders.getFirst(name);
34+
}
35+
36+
@Override
37+
public Map<String, String> headers() {
38+
Map<String, String> headers = new HashMap<String, String>();
39+
40+
@SuppressWarnings("unchecked")
41+
MultivaluedMap<String, String> responseHeaders = response.getHeaders();
42+
for (String key : responseHeaders.keySet()) {
43+
headers.put(key, responseHeaders.getFirst(key));
44+
}
45+
46+
return headers;
47+
}
48+
49+
}

openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackClient.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public OpenStackClient(String endpoint, OpenStackClientConnector connector) {
3636
this.endpoint = endpoint;
3737
this.connector = (connector == null) ? DEFAULT_CONNECTOR : connector;
3838
}
39-
40-
public <T> T execute(OpenStackRequest<T> request) {
39+
40+
public <T> OpenStackResponse request(OpenStackRequest<T> request) {
4141
OpenStackResponseException authException = null;
4242

4343
for (int i = 0; i <= AUTHENTICATION_RETRIES; i++) {
@@ -48,7 +48,7 @@ public <T> T execute(OpenStackRequest<T> request) {
4848
}
4949

5050
try {
51-
return (T) connector.execute(request);
51+
return connector.request(request);
5252
} catch (OpenStackResponseException e) {
5353
if (e.getStatus() != OpenStackResponseStatus.NOT_AUTHORIZED
5454
|| tokenProvider == null) {
@@ -61,10 +61,10 @@ public <T> T execute(OpenStackRequest<T> request) {
6161

6262
throw authException;
6363
}
64-
65-
//Allow to change the response type from the request
66-
public <R, T> T execute(OpenStackRequest<R> request, Class<T> type) {
67-
return null;
64+
65+
public <T> T execute(OpenStackRequest<T> request) {
66+
OpenStackResponse response = request(request);
67+
return request.returnType() != null ? response.getEntity(request.returnType()) : null;
6868
}
6969

7070
public void property(String property, String value) {

openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackClientConnector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
public interface OpenStackClientConnector {
55

6-
public <T> T execute(OpenStackRequest<T> request);
6+
public <T> OpenStackResponse request(OpenStackRequest<T> request);
77

88
}

openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackResponse.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
public interface OpenStackResponse {
77

8+
public <T> T getEntity(Class<T> returnType);
9+
810
public InputStream getInputStream();
911

1012
public String header(String name);

openstack-examples/src/main/java/com/woorea/openstack/examples/glance/GlanceListImages.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static void main(String[] args) {
4444
Images images = glance.images().list(false).execute();
4545

4646
for (Image image : images) {
47-
System.out.println(image);
47+
System.out.println(glance.images().show(image.getId()).execute());
4848
}
4949
}
5050
}

0 commit comments

Comments
 (0)