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
|
/*
* ONAP-SO
* ============LICENSE_START==========================================
* ===================================================================
* Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
* ===================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END============================================
*
* ECOMP and OpenECOMP are trademarks
* and service marks of AT&T Intellectual Property.
*
*/
package com.woorea.openstack.connector;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.woorea.openstack.base.client.OpenStackClientConnector;
import com.woorea.openstack.base.client.OpenStackConnectException;
import com.woorea.openstack.base.client.OpenStackRequest;
import com.woorea.openstack.base.client.OpenStackResponse;
import com.woorea.openstack.base.client.OpenStackResponseException;
public class HttpClientConnector implements OpenStackClientConnector {
public static ObjectMapper DEFAULT_MAPPER;
public static ObjectMapper WRAPPED_MAPPER;
private static Logger LOGGER = LoggerFactory.getLogger(HttpClientConnector.class);
static {
DEFAULT_MAPPER = new ObjectMapper();
DEFAULT_MAPPER.setSerializationInclusion(Include.NON_NULL);
DEFAULT_MAPPER.disable(SerializationFeature.INDENT_OUTPUT);
DEFAULT_MAPPER.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
DEFAULT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
WRAPPED_MAPPER = new ObjectMapper();
WRAPPED_MAPPER.setSerializationInclusion(Include.NON_NULL);
WRAPPED_MAPPER.disable(SerializationFeature.INDENT_OUTPUT);
WRAPPED_MAPPER.enable(SerializationFeature.WRAP_ROOT_VALUE);
WRAPPED_MAPPER.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
WRAPPED_MAPPER.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
WRAPPED_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
protected static <T> ObjectMapper getObjectMapper (Class<T> type) {
return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;
}
@Override
public <T> OpenStackResponse request(OpenStackRequest<T> request) {
CloseableHttpClient httpClient = null; //HttpClients.createDefault();
httpClient = HttpClients.custom().setRedirectStrategy(new HttpClientRedirectStrategy()).build();
URI uri = null;
// Build the URI with query params
try {
URIBuilder uriBuilder = new URIBuilder(request.endpoint() + request.path());
for(Map.Entry<String, List<Object> > entry : request.queryParams().entrySet()) {
for (Object o : entry.getValue()) {
uriBuilder.setParameter(entry.getKey(), String.valueOf(o));
}
}
uri = uriBuilder.build();
} catch (URISyntaxException e) {
throw new HttpClientException (e);
}
HttpEntity entity = null;
if (request.entity() != null) {
// Flatten the entity to a Json string
try {
// Get appropriate mapper, based on existence of a root element in Entity class
ObjectMapper mapper = getObjectMapper (request.entity().getEntity().getClass());
String entityJson = mapper.writeValueAsString (request.entity().getEntity());
entity = new StringEntity(entityJson, ContentType.create(request.entity().getContentType()));
LOGGER.debug ("Request JSON Body: " + entityJson.replaceAll("\"password\":\"[^\"]*\"", "\"password\":\"***\""));
} catch (JsonProcessingException e) {
throw new HttpClientException ("Json processing error on request entity", e);
} catch (IOException e) {
throw new HttpClientException ("Json IO error on request entity", e);
}
}
// Determine the HttpRequest class based on the method
HttpUriRequest httpRequest;
switch (request.method()) {
case POST:
HttpPost post = new HttpPost(uri);
post.setEntity (entity);
httpRequest = post;
break;
case GET:
httpRequest = new HttpGet(uri);
break;
case PUT:
HttpPut put = new HttpPut(uri);
put.setEntity (entity);
httpRequest = put;
break;
case DELETE:
httpRequest = new HttpDelete(uri);
break;
default:
throw new HttpClientException ("Unrecognized HTTP Method: " + request.method());
}
for (Entry<String, List<Object>> h : request.headers().entrySet()) {
StringBuilder sb = new StringBuilder();
for (Object v : h.getValue()) {
sb.append(String.valueOf(v));
}
httpRequest.addHeader(h.getKey(), sb.toString());
}
LOGGER.debug ("Sending HTTP request: " + httpRequest.toString());
// Get the Response. But don't get the body entity yet, as this response
// will be wrapped in an HttpClientResponse. The HttpClientResponse
// buffers the body in constructor, so can close the response here.
HttpClientResponse httpClientResponse = null;
CloseableHttpResponse httpResponse = null;
// Catch known HttpClient exceptions, and wrap them in OpenStack Client Exceptions
// so calling functions can distinguish. Only RuntimeExceptions are allowed.
try {
httpResponse = httpClient.execute(httpRequest);
LOGGER.debug ("Response status: " + httpResponse.getStatusLine().getStatusCode());
httpClientResponse = new HttpClientResponse (httpResponse);
int status = httpResponse.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED ||
status == HttpStatus.SC_NO_CONTENT || status == HttpStatus.SC_ACCEPTED)
{
return httpClientResponse;
}
}
catch (HttpResponseException e) {
// What exactly does this mean? It does not appear to get thrown for
// non-2XX responses as documented.
throw new OpenStackResponseException(e.getMessage(), e.getStatusCode());
}
catch (UnknownHostException e) {
throw new OpenStackConnectException("Unknown Host: " + e.getMessage());
}
catch (IOException e) {
// Catch all other IOExceptions and throw as OpenStackConnectException
throw new OpenStackConnectException(e.getMessage());
}
catch (Exception e) {
// Catchall for anything else, must throw as a RuntimeException
LOGGER.error ("Unexpected client exception: " +e.getMessage());
throw new RuntimeException("Unexpected client exception", e);
}
finally {
// Have the body. Close the stream
if (httpResponse != null)
try {
httpResponse.close();
} catch (IOException e) {
LOGGER.warn("Unable to close HTTP Response: " + e);
}
}
// Get here on an error response (4XX-5XX)
throw new OpenStackResponseException(httpResponse.getStatusLine().getReasonPhrase(),
httpResponse.getStatusLine().getStatusCode(),
httpClientResponse);
}
}
|