Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.protocol.http.ctl; |
| 18 | |
Michal Mach | bcd58c7 | 2017-06-19 17:12:34 +0200 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 20 | import org.apache.commons.io.IOUtils; |
| 21 | import org.apache.http.client.methods.HttpPatch; |
| 22 | import org.apache.http.conn.ssl.AllowAllHostnameVerifier; |
| 23 | import org.apache.http.entity.StringEntity; |
| 24 | import org.apache.http.impl.client.CloseableHttpClient; |
| 25 | import org.apache.http.impl.client.HttpClients; |
| 26 | import org.apache.http.ssl.SSLContextBuilder; |
| 27 | import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 28 | import org.glassfish.jersey.client.oauth2.OAuth2ClientSupport; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 29 | import org.onlab.packet.IpAddress; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.protocol.http.HttpSBController; |
| 32 | import org.onosproject.protocol.rest.RestSBDevice; |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 33 | import org.onosproject.protocol.rest.RestSBDevice.AuthenticationScheme; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
Michal Mach | bcd58c7 | 2017-06-19 17:12:34 +0200 | [diff] [blame] | 37 | import javax.net.ssl.SSLContext; |
| 38 | import javax.net.ssl.TrustManager; |
| 39 | import javax.net.ssl.X509TrustManager; |
| 40 | import javax.ws.rs.client.Client; |
| 41 | import javax.ws.rs.client.ClientBuilder; |
| 42 | import javax.ws.rs.client.Entity; |
| 43 | import javax.ws.rs.client.WebTarget; |
| 44 | import javax.ws.rs.core.MediaType; |
| 45 | import javax.ws.rs.core.Response; |
| 46 | import javax.ws.rs.core.Response.Status; |
| 47 | import java.io.ByteArrayInputStream; |
| 48 | import java.io.IOException; |
| 49 | import java.io.InputStream; |
| 50 | import java.nio.charset.StandardCharsets; |
| 51 | import java.security.KeyManagementException; |
| 52 | import java.security.KeyStoreException; |
| 53 | import java.security.NoSuchAlgorithmException; |
| 54 | import java.security.cert.CertificateException; |
| 55 | import java.security.cert.X509Certificate; |
| 56 | import java.util.Base64; |
| 57 | import java.util.Map; |
| 58 | import java.util.concurrent.ConcurrentHashMap; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 59 | |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 60 | import static com.google.common.base.Preconditions.checkNotNull; |
| 61 | |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 62 | /** |
| 63 | * The implementation of HttpSBController. |
| 64 | */ |
| 65 | public class HttpSBControllerImpl implements HttpSBController { |
| 66 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 67 | private static final Logger log = LoggerFactory.getLogger(HttpSBControllerImpl.class); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 68 | private static final String XML = "xml"; |
| 69 | private static final String JSON = "json"; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 70 | protected static final String DOUBLESLASH = "//"; |
| 71 | protected static final String COLON = ":"; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 72 | private static final int STATUS_OK = Response.Status.OK.getStatusCode(); |
| 73 | private static final int STATUS_CREATED = Response.Status.CREATED.getStatusCode(); |
| 74 | private static final int STATUS_ACCEPTED = Response.Status.ACCEPTED.getStatusCode(); |
| 75 | private static final String HTTPS = "https"; |
| 76 | private static final String AUTHORIZATION_PROPERTY = "authorization"; |
| 77 | private static final String BASIC_AUTH_PREFIX = "Basic "; |
fahadnaeemkhan | 2675a27 | 2017-12-13 13:17:23 -0800 | [diff] [blame] | 78 | private static final String OAUTH2_BEARER_AUTH_PREFIX = "Bearer "; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 79 | |
| 80 | private final Map<DeviceId, RestSBDevice> deviceMap = new ConcurrentHashMap<>(); |
| 81 | private final Map<DeviceId, Client> clientMap = new ConcurrentHashMap<>(); |
| 82 | |
| 83 | public Map<DeviceId, RestSBDevice> getDeviceMap() { |
| 84 | return deviceMap; |
| 85 | } |
| 86 | |
| 87 | public Map<DeviceId, Client> getClientMap() { |
| 88 | return clientMap; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Map<DeviceId, RestSBDevice> getDevices() { |
| 93 | return ImmutableMap.copyOf(deviceMap); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public RestSBDevice getDevice(DeviceId deviceInfo) { |
| 98 | return deviceMap.get(deviceInfo); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public RestSBDevice getDevice(IpAddress ip, int port) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 103 | return deviceMap.values().stream().filter(v -> v.ip().equals(ip) && v.port() == port).findFirst().get(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void addDevice(RestSBDevice device) { |
| 108 | if (!deviceMap.containsKey(device.deviceId())) { |
| 109 | Client client = ignoreSslClient(); |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 110 | authenticate(client, device); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 111 | clientMap.put(device.deviceId(), client); |
| 112 | deviceMap.put(device.deviceId(), device); |
| 113 | } else { |
| 114 | log.warn("Trying to add a device that is already existing {}", device.deviceId()); |
| 115 | } |
| 116 | |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void removeDevice(DeviceId deviceId) { |
| 121 | clientMap.remove(deviceId); |
| 122 | deviceMap.remove(deviceId); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean post(DeviceId device, String request, InputStream payload, String mediaType) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 127 | return checkStatusCode(post(device, request, payload, typeOfMediaType(mediaType))); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 131 | public int post(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
| 132 | Response response = getResponse(device, request, payload, mediaType); |
| 133 | if (response == null) { |
| 134 | return Status.NO_CONTENT.getStatusCode(); |
| 135 | } |
| 136 | return response.getStatus(); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public <T> T post(DeviceId device, String request, InputStream payload, String mediaType, Class<T> responseClass) { |
| 141 | return post(device, request, payload, typeOfMediaType(mediaType), responseClass); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public <T> T post(DeviceId device, String request, InputStream payload, MediaType mediaType, |
| 146 | Class<T> responseClass) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 147 | Response response = getResponse(device, request, payload, mediaType); |
Palash Kala | da4798d | 2017-05-23 20:16:55 +0900 | [diff] [blame] | 148 | if (response != null && response.hasEntity()) { |
Hesam Rahimi | 9630554 | 2017-06-07 13:59:48 -0400 | [diff] [blame] | 149 | // Do not read the entity if the responseClass is of type Response. This would allow the |
| 150 | // caller to receive the Response directly and try to read its appropriate entity locally. |
| 151 | return responseClass == Response.class ? (T) response : response.readEntity(responseClass); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 152 | } |
| 153 | log.error("Response from device {} for request {} contains no entity", device, request); |
| 154 | return null; |
| 155 | } |
| 156 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 157 | private Response getResponse(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 158 | |
| 159 | WebTarget wt = getWebTarget(device, request); |
| 160 | |
| 161 | Response response = null; |
| 162 | if (payload != null) { |
| 163 | try { |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 164 | response = wt.request(mediaType) |
| 165 | .post(Entity.entity(IOUtils.toString(payload, StandardCharsets.UTF_8), mediaType)); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 166 | } catch (IOException e) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 167 | log.error("Cannot do POST {} request on device {} because can't read payload", request, device); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 168 | } |
| 169 | } else { |
Georgios Katsikas | 186b958 | 2017-05-31 17:25:54 +0200 | [diff] [blame] | 170 | response = wt.request(mediaType).post(Entity.entity(null, mediaType)); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 171 | } |
| 172 | return response; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public boolean put(DeviceId device, String request, InputStream payload, String mediaType) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 177 | return checkStatusCode(put(device, request, payload, typeOfMediaType(mediaType))); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public int put(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 182 | |
| 183 | WebTarget wt = getWebTarget(device, request); |
| 184 | |
| 185 | Response response = null; |
| 186 | if (payload != null) { |
| 187 | try { |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 188 | response = wt.request(mediaType).put(Entity.entity(IOUtils. |
| 189 | toString(payload, StandardCharsets.UTF_8), mediaType)); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 190 | } catch (IOException e) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 191 | log.error("Cannot do PUT {} request on device {} because can't read payload", request, device); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 192 | } |
| 193 | } else { |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 194 | response = wt.request(mediaType).put(Entity.entity(null, mediaType)); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 195 | } |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 196 | |
| 197 | if (response == null) { |
| 198 | return Status.NO_CONTENT.getStatusCode(); |
| 199 | } |
| 200 | return response.getStatus(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public InputStream get(DeviceId device, String request, String mediaType) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 205 | return get(device, request, typeOfMediaType(mediaType)); |
| 206 | } |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 207 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 208 | @Override |
| 209 | public InputStream get(DeviceId device, String request, MediaType mediaType) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 210 | WebTarget wt = getWebTarget(device, request); |
| 211 | |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 212 | Response s = wt.request(mediaType).get(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 213 | |
| 214 | if (checkReply(s)) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 215 | return new ByteArrayInputStream(s.readEntity((String.class)).getBytes(StandardCharsets.UTF_8)); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 216 | } |
| 217 | return null; |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public boolean patch(DeviceId device, String request, InputStream payload, String mediaType) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 222 | return checkStatusCode(patch(device, request, payload, typeOfMediaType(mediaType))); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public int patch(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 227 | |
| 228 | try { |
| 229 | log.debug("Url request {} ", getUrlString(device, request)); |
| 230 | HttpPatch httprequest = new HttpPatch(getUrlString(device, request)); |
fahadnaeemkhan | 2675a27 | 2017-12-13 13:17:23 -0800 | [diff] [blame] | 231 | if (deviceMap.get(device).authentication() == AuthenticationScheme.BASIC) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 232 | String pwd = deviceMap.get(device).password() == null ? "" : COLON + deviceMap.get(device).password(); |
| 233 | String userPassword = deviceMap.get(device).username() + pwd; |
| 234 | String base64string = Base64.getEncoder().encodeToString(userPassword.getBytes(StandardCharsets.UTF_8)); |
| 235 | httprequest.addHeader(AUTHORIZATION_PROPERTY, BASIC_AUTH_PREFIX + base64string); |
fahadnaeemkhan | 2675a27 | 2017-12-13 13:17:23 -0800 | [diff] [blame] | 236 | } else if (deviceMap.get(device).authentication() == AuthenticationScheme.OAUTH2) { |
| 237 | String token = deviceMap.get(device).token(); |
| 238 | // TODO: support token types other then bearer of OAuth2 authentication |
| 239 | httprequest.addHeader(AUTHORIZATION_PROPERTY, OAUTH2_BEARER_AUTH_PREFIX + token); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 240 | } |
| 241 | if (payload != null) { |
| 242 | StringEntity input = new StringEntity(IOUtils.toString(payload, StandardCharsets.UTF_8)); |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 243 | input.setContentType(mediaType.toString()); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 244 | httprequest.setEntity(input); |
| 245 | } |
| 246 | CloseableHttpClient httpClient; |
| 247 | if (deviceMap.containsKey(device) && deviceMap.get(device).protocol().equals(HTTPS)) { |
| 248 | httpClient = getApacheSslBypassClient(); |
| 249 | } else { |
| 250 | httpClient = HttpClients.createDefault(); |
| 251 | } |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 252 | return httpClient.execute(httprequest).getStatusLine().getStatusCode(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 253 | } catch (IOException | NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 254 | log.error("Cannot do PATCH {} request on device {}", request, device, e); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 255 | } |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 256 | return Status.BAD_REQUEST.getStatusCode(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public boolean delete(DeviceId device, String request, InputStream payload, String mediaType) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 261 | return checkStatusCode(delete(device, request, payload, typeOfMediaType(mediaType))); |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public int delete(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 266 | |
| 267 | WebTarget wt = getWebTarget(device, request); |
| 268 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 269 | // FIXME: do we need to delete an entry by enclosing data in DELETE |
| 270 | // request? |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 271 | // wouldn't it be nice to use PUT to implement the similar concept? |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 272 | Response response = wt.request(mediaType).delete(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 273 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 274 | return response.getStatus(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 277 | private MediaType typeOfMediaType(String type) { |
| 278 | switch (type) { |
| 279 | case XML: |
| 280 | return MediaType.APPLICATION_XML_TYPE; |
| 281 | case JSON: |
| 282 | return MediaType.APPLICATION_JSON_TYPE; |
Michal Mach | f0ce45e | 2017-06-20 11:54:08 +0200 | [diff] [blame] | 283 | case MediaType.WILDCARD: |
| 284 | return MediaType.WILDCARD_TYPE; |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 285 | default: |
| 286 | throw new IllegalArgumentException("Unsupported media type " + type); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 287 | |
| 288 | } |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 289 | } |
| 290 | |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 291 | private void authenticate(Client client, RestSBDevice device) { |
| 292 | AuthenticationScheme authScheme = device.authentication(); |
| 293 | if (authScheme == AuthenticationScheme.NO_AUTHENTICATION) { |
| 294 | log.debug("{} scheme is specified, ignoring authentication", authScheme); |
| 295 | return; |
| 296 | } else if (authScheme == AuthenticationScheme.OAUTH2) { |
| 297 | String token = checkNotNull(device.token()); |
| 298 | client.register(OAuth2ClientSupport.feature(token)); |
| 299 | } else if (authScheme == AuthenticationScheme.BASIC) { |
| 300 | String username = device.username(); |
| 301 | String password = device.password() == null ? "" : device.password(); |
| 302 | client.register(HttpAuthenticationFeature.basic(username, password)); |
| 303 | } else { |
| 304 | // TODO: Add support for other authentication schemes here. |
| 305 | throw new IllegalArgumentException(String.format("Unsupported authentication scheme: %s", |
| 306 | authScheme.name())); |
| 307 | } |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | protected WebTarget getWebTarget(DeviceId device, String request) { |
| 311 | log.debug("Sending request to URL {} ", getUrlString(device, request)); |
| 312 | return clientMap.get(device).target(getUrlString(device, request)); |
| 313 | } |
| 314 | |
| 315 | //FIXME security issue: this trusts every SSL certificate, even if is self-signed. Also deprecated methods. |
| 316 | private CloseableHttpClient getApacheSslBypassClient() throws NoSuchAlgorithmException, |
| 317 | KeyManagementException, KeyStoreException { |
| 318 | return HttpClients.custom(). |
| 319 | setHostnameVerifier(new AllowAllHostnameVerifier()). |
| 320 | setSslcontext(new SSLContextBuilder() |
| 321 | .loadTrustMaterial(null, (arg0, arg1) -> true) |
| 322 | .build()).build(); |
| 323 | } |
| 324 | |
Michal Mach | bcd58c7 | 2017-06-19 17:12:34 +0200 | [diff] [blame] | 325 | protected String getUrlString(DeviceId deviceId, String request) { |
| 326 | RestSBDevice restSBDevice = deviceMap.get(deviceId); |
| 327 | if (restSBDevice == null) { |
| 328 | log.warn("restSbDevice cannot be NULL!"); |
| 329 | return ""; |
| 330 | } |
| 331 | if (restSBDevice.url() != null) { |
| 332 | return restSBDevice.protocol() + COLON + DOUBLESLASH + restSBDevice.url() + request; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 333 | } else { |
Michal Mach | bcd58c7 | 2017-06-19 17:12:34 +0200 | [diff] [blame] | 334 | return restSBDevice.protocol() + COLON + DOUBLESLASH + restSBDevice.ip().toString() |
| 335 | + COLON + restSBDevice.port() + request; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | private boolean checkReply(Response response) { |
| 340 | if (response != null) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 341 | boolean statusCode = checkStatusCode(response.getStatus()); |
| 342 | if (!statusCode && response.hasEntity()) { |
| 343 | log.error("Failed request, HTTP error msg : " + response.readEntity(String.class)); |
| 344 | } |
| 345 | return statusCode; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 346 | } |
| 347 | log.error("Null reply from device"); |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | private boolean checkStatusCode(int statusCode) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 352 | if (statusCode == STATUS_OK || statusCode == STATUS_CREATED || statusCode == STATUS_ACCEPTED) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 353 | return true; |
| 354 | } else { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 355 | log.error("Failed request, HTTP error code : " + statusCode); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 356 | return false; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | private Client ignoreSslClient() { |
| 361 | SSLContext sslcontext = null; |
| 362 | |
| 363 | try { |
| 364 | sslcontext = SSLContext.getInstance("TLS"); |
| 365 | sslcontext.init(null, new TrustManager[]{new X509TrustManager() { |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 366 | @Override |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 367 | public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { |
| 368 | } |
| 369 | |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 370 | @Override |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 371 | public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { |
| 372 | } |
| 373 | |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 374 | @Override |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 375 | public X509Certificate[] getAcceptedIssuers() { |
| 376 | return new X509Certificate[0]; |
| 377 | } |
| 378 | } }, new java.security.SecureRandom()); |
| 379 | } catch (NoSuchAlgorithmException | KeyManagementException e) { |
| 380 | e.printStackTrace(); |
| 381 | } |
| 382 | |
| 383 | return ClientBuilder.newBuilder().sslContext(sslcontext).hostnameVerifier((s1, s2) -> true).build(); |
| 384 | } |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 385 | |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 386 | } |