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; |
| 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.protocol.http.HttpSBController; |
| 31 | import org.onosproject.protocol.rest.RestSBDevice; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
Michal Mach | bcd58c7 | 2017-06-19 17:12:34 +0200 | [diff] [blame] | 35 | import javax.net.ssl.SSLContext; |
| 36 | import javax.net.ssl.TrustManager; |
| 37 | import javax.net.ssl.X509TrustManager; |
| 38 | import javax.ws.rs.client.Client; |
| 39 | import javax.ws.rs.client.ClientBuilder; |
| 40 | import javax.ws.rs.client.Entity; |
| 41 | import javax.ws.rs.client.WebTarget; |
| 42 | import javax.ws.rs.core.MediaType; |
| 43 | import javax.ws.rs.core.Response; |
| 44 | import javax.ws.rs.core.Response.Status; |
| 45 | import java.io.ByteArrayInputStream; |
| 46 | import java.io.IOException; |
| 47 | import java.io.InputStream; |
| 48 | import java.nio.charset.StandardCharsets; |
| 49 | import java.security.KeyManagementException; |
| 50 | import java.security.KeyStoreException; |
| 51 | import java.security.NoSuchAlgorithmException; |
| 52 | import java.security.cert.CertificateException; |
| 53 | import java.security.cert.X509Certificate; |
| 54 | import java.util.Base64; |
| 55 | import java.util.Map; |
| 56 | import java.util.concurrent.ConcurrentHashMap; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * The implementation of HttpSBController. |
| 60 | */ |
| 61 | public class HttpSBControllerImpl implements HttpSBController { |
| 62 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 63 | private static final Logger log = LoggerFactory.getLogger(HttpSBControllerImpl.class); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 64 | private static final String XML = "xml"; |
| 65 | private static final String JSON = "json"; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 66 | protected static final String DOUBLESLASH = "//"; |
| 67 | protected static final String COLON = ":"; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 68 | private static final int STATUS_OK = Response.Status.OK.getStatusCode(); |
| 69 | private static final int STATUS_CREATED = Response.Status.CREATED.getStatusCode(); |
| 70 | private static final int STATUS_ACCEPTED = Response.Status.ACCEPTED.getStatusCode(); |
| 71 | private static final String HTTPS = "https"; |
| 72 | private static final String AUTHORIZATION_PROPERTY = "authorization"; |
| 73 | private static final String BASIC_AUTH_PREFIX = "Basic "; |
| 74 | |
| 75 | private final Map<DeviceId, RestSBDevice> deviceMap = new ConcurrentHashMap<>(); |
| 76 | private final Map<DeviceId, Client> clientMap = new ConcurrentHashMap<>(); |
| 77 | |
| 78 | public Map<DeviceId, RestSBDevice> getDeviceMap() { |
| 79 | return deviceMap; |
| 80 | } |
| 81 | |
| 82 | public Map<DeviceId, Client> getClientMap() { |
| 83 | return clientMap; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Map<DeviceId, RestSBDevice> getDevices() { |
| 88 | return ImmutableMap.copyOf(deviceMap); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public RestSBDevice getDevice(DeviceId deviceInfo) { |
| 93 | return deviceMap.get(deviceInfo); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public RestSBDevice getDevice(IpAddress ip, int port) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 98 | 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] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void addDevice(RestSBDevice device) { |
| 103 | if (!deviceMap.containsKey(device.deviceId())) { |
| 104 | Client client = ignoreSslClient(); |
| 105 | if (device.username() != null) { |
| 106 | String username = device.username(); |
| 107 | String password = device.password() == null ? "" : device.password(); |
| 108 | authenticate(client, username, password); |
| 109 | } |
| 110 | clientMap.put(device.deviceId(), client); |
| 111 | deviceMap.put(device.deviceId(), device); |
| 112 | } else { |
| 113 | log.warn("Trying to add a device that is already existing {}", device.deviceId()); |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void removeDevice(DeviceId deviceId) { |
| 120 | clientMap.remove(deviceId); |
| 121 | deviceMap.remove(deviceId); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public boolean post(DeviceId device, String request, InputStream payload, String mediaType) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 126 | return checkStatusCode(post(device, request, payload, typeOfMediaType(mediaType))); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 130 | public int post(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
| 131 | Response response = getResponse(device, request, payload, mediaType); |
| 132 | if (response == null) { |
| 133 | return Status.NO_CONTENT.getStatusCode(); |
| 134 | } |
| 135 | return response.getStatus(); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public <T> T post(DeviceId device, String request, InputStream payload, String mediaType, Class<T> responseClass) { |
| 140 | return post(device, request, payload, typeOfMediaType(mediaType), responseClass); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public <T> T post(DeviceId device, String request, InputStream payload, MediaType mediaType, |
| 145 | Class<T> responseClass) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 146 | Response response = getResponse(device, request, payload, mediaType); |
Palash Kala | da4798d | 2017-05-23 20:16:55 +0900 | [diff] [blame] | 147 | if (response != null && response.hasEntity()) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 148 | return response.readEntity(responseClass); |
| 149 | } |
| 150 | log.error("Response from device {} for request {} contains no entity", device, request); |
| 151 | return null; |
| 152 | } |
| 153 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 154 | private Response getResponse(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 155 | |
| 156 | WebTarget wt = getWebTarget(device, request); |
| 157 | |
| 158 | Response response = null; |
| 159 | if (payload != null) { |
| 160 | try { |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 161 | response = wt.request(mediaType) |
| 162 | .post(Entity.entity(IOUtils.toString(payload, StandardCharsets.UTF_8), mediaType)); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 163 | } catch (IOException e) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 164 | 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] | 165 | } |
| 166 | } else { |
Georgios Katsikas | 186b958 | 2017-05-31 17:25:54 +0200 | [diff] [blame] | 167 | response = wt.request(mediaType).post(Entity.entity(null, mediaType)); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 168 | } |
| 169 | return response; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public boolean put(DeviceId device, String request, InputStream payload, String mediaType) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 174 | return checkStatusCode(put(device, request, payload, typeOfMediaType(mediaType))); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public int put(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 179 | |
| 180 | WebTarget wt = getWebTarget(device, request); |
| 181 | |
| 182 | Response response = null; |
| 183 | if (payload != null) { |
| 184 | try { |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 185 | response = wt.request(mediaType).put(Entity.entity(IOUtils. |
| 186 | toString(payload, StandardCharsets.UTF_8), mediaType)); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 187 | } catch (IOException e) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 188 | 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] | 189 | } |
| 190 | } else { |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 191 | response = wt.request(mediaType).put(Entity.entity(null, mediaType)); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 192 | } |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 193 | |
| 194 | if (response == null) { |
| 195 | return Status.NO_CONTENT.getStatusCode(); |
| 196 | } |
| 197 | return response.getStatus(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public InputStream get(DeviceId device, String request, String mediaType) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 202 | return get(device, request, typeOfMediaType(mediaType)); |
| 203 | } |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 204 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 205 | @Override |
| 206 | public InputStream get(DeviceId device, String request, MediaType mediaType) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 207 | WebTarget wt = getWebTarget(device, request); |
| 208 | |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 209 | Response s = wt.request(mediaType).get(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 210 | |
| 211 | if (checkReply(s)) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 212 | return new ByteArrayInputStream(s.readEntity((String.class)).getBytes(StandardCharsets.UTF_8)); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 213 | } |
| 214 | return null; |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public boolean patch(DeviceId device, String request, InputStream payload, String mediaType) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 219 | return checkStatusCode(patch(device, request, payload, typeOfMediaType(mediaType))); |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public int patch(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 224 | |
| 225 | try { |
| 226 | log.debug("Url request {} ", getUrlString(device, request)); |
| 227 | HttpPatch httprequest = new HttpPatch(getUrlString(device, request)); |
| 228 | if (deviceMap.get(device).username() != null) { |
| 229 | String pwd = deviceMap.get(device).password() == null ? "" : COLON + deviceMap.get(device).password(); |
| 230 | String userPassword = deviceMap.get(device).username() + pwd; |
| 231 | String base64string = Base64.getEncoder().encodeToString(userPassword.getBytes(StandardCharsets.UTF_8)); |
| 232 | httprequest.addHeader(AUTHORIZATION_PROPERTY, BASIC_AUTH_PREFIX + base64string); |
| 233 | } |
| 234 | if (payload != null) { |
| 235 | StringEntity input = new StringEntity(IOUtils.toString(payload, StandardCharsets.UTF_8)); |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 236 | input.setContentType(mediaType.toString()); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 237 | httprequest.setEntity(input); |
| 238 | } |
| 239 | CloseableHttpClient httpClient; |
| 240 | if (deviceMap.containsKey(device) && deviceMap.get(device).protocol().equals(HTTPS)) { |
| 241 | httpClient = getApacheSslBypassClient(); |
| 242 | } else { |
| 243 | httpClient = HttpClients.createDefault(); |
| 244 | } |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 245 | return httpClient.execute(httprequest).getStatusLine().getStatusCode(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 246 | } catch (IOException | NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 247 | log.error("Cannot do PATCH {} request on device {}", request, device, e); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 248 | } |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 249 | return Status.BAD_REQUEST.getStatusCode(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public boolean delete(DeviceId device, String request, InputStream payload, String mediaType) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 254 | return checkStatusCode(delete(device, request, payload, typeOfMediaType(mediaType))); |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public int delete(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 259 | |
| 260 | WebTarget wt = getWebTarget(device, request); |
| 261 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 262 | // FIXME: do we need to delete an entry by enclosing data in DELETE |
| 263 | // request? |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 264 | // 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] | 265 | Response response = wt.request(mediaType).delete(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 266 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 267 | return response.getStatus(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 270 | private MediaType typeOfMediaType(String type) { |
| 271 | switch (type) { |
| 272 | case XML: |
| 273 | return MediaType.APPLICATION_XML_TYPE; |
| 274 | case JSON: |
| 275 | return MediaType.APPLICATION_JSON_TYPE; |
Michal Mach | f0ce45e | 2017-06-20 11:54:08 +0200 | [diff] [blame] | 276 | case MediaType.WILDCARD: |
| 277 | return MediaType.WILDCARD_TYPE; |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 278 | default: |
| 279 | throw new IllegalArgumentException("Unsupported media type " + type); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 280 | |
| 281 | } |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | private void authenticate(Client client, String username, String password) { |
| 285 | client.register(HttpAuthenticationFeature.basic(username, password)); |
| 286 | } |
| 287 | |
| 288 | protected WebTarget getWebTarget(DeviceId device, String request) { |
| 289 | log.debug("Sending request to URL {} ", getUrlString(device, request)); |
| 290 | return clientMap.get(device).target(getUrlString(device, request)); |
| 291 | } |
| 292 | |
| 293 | //FIXME security issue: this trusts every SSL certificate, even if is self-signed. Also deprecated methods. |
| 294 | private CloseableHttpClient getApacheSslBypassClient() throws NoSuchAlgorithmException, |
| 295 | KeyManagementException, KeyStoreException { |
| 296 | return HttpClients.custom(). |
| 297 | setHostnameVerifier(new AllowAllHostnameVerifier()). |
| 298 | setSslcontext(new SSLContextBuilder() |
| 299 | .loadTrustMaterial(null, (arg0, arg1) -> true) |
| 300 | .build()).build(); |
| 301 | } |
| 302 | |
Michal Mach | bcd58c7 | 2017-06-19 17:12:34 +0200 | [diff] [blame] | 303 | protected String getUrlString(DeviceId deviceId, String request) { |
| 304 | RestSBDevice restSBDevice = deviceMap.get(deviceId); |
| 305 | if (restSBDevice == null) { |
| 306 | log.warn("restSbDevice cannot be NULL!"); |
| 307 | return ""; |
| 308 | } |
| 309 | if (restSBDevice.url() != null) { |
| 310 | return restSBDevice.protocol() + COLON + DOUBLESLASH + restSBDevice.url() + request; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 311 | } else { |
Michal Mach | bcd58c7 | 2017-06-19 17:12:34 +0200 | [diff] [blame] | 312 | return restSBDevice.protocol() + COLON + DOUBLESLASH + restSBDevice.ip().toString() |
| 313 | + COLON + restSBDevice.port() + request; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
| 317 | private boolean checkReply(Response response) { |
| 318 | if (response != null) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 319 | boolean statusCode = checkStatusCode(response.getStatus()); |
| 320 | if (!statusCode && response.hasEntity()) { |
| 321 | log.error("Failed request, HTTP error msg : " + response.readEntity(String.class)); |
| 322 | } |
| 323 | return statusCode; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 324 | } |
| 325 | log.error("Null reply from device"); |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | private boolean checkStatusCode(int statusCode) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 330 | if (statusCode == STATUS_OK || statusCode == STATUS_CREATED || statusCode == STATUS_ACCEPTED) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 331 | return true; |
| 332 | } else { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 333 | log.error("Failed request, HTTP error code : " + statusCode); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 334 | return false; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | private Client ignoreSslClient() { |
| 339 | SSLContext sslcontext = null; |
| 340 | |
| 341 | try { |
| 342 | sslcontext = SSLContext.getInstance("TLS"); |
| 343 | sslcontext.init(null, new TrustManager[]{new X509TrustManager() { |
| 344 | public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { |
| 345 | } |
| 346 | |
| 347 | public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { |
| 348 | } |
| 349 | |
| 350 | public X509Certificate[] getAcceptedIssuers() { |
| 351 | return new X509Certificate[0]; |
| 352 | } |
| 353 | } }, new java.security.SecureRandom()); |
| 354 | } catch (NoSuchAlgorithmException | KeyManagementException e) { |
| 355 | e.printStackTrace(); |
| 356 | } |
| 357 | |
| 358 | return ClientBuilder.newBuilder().sslContext(sslcontext).hostnameVerifier((s1, s2) -> true).build(); |
| 359 | } |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 360 | |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 361 | } |