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; |
Georgios Katsikas | 74a8a44 | 2018-06-26 09:23:58 +0200 | [diff] [blame] | 40 | import javax.ws.rs.ProcessingException; |
Michal Mach | bcd58c7 | 2017-06-19 17:12:34 +0200 | [diff] [blame] | 41 | import javax.ws.rs.client.Client; |
| 42 | import javax.ws.rs.client.ClientBuilder; |
| 43 | import javax.ws.rs.client.Entity; |
| 44 | import javax.ws.rs.client.WebTarget; |
| 45 | import javax.ws.rs.core.MediaType; |
| 46 | import javax.ws.rs.core.Response; |
| 47 | import javax.ws.rs.core.Response.Status; |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 48 | import javax.ws.rs.sse.InboundSseEvent; |
| 49 | import javax.ws.rs.sse.SseEventSource; |
Michal Mach | bcd58c7 | 2017-06-19 17:12:34 +0200 | [diff] [blame] | 50 | import java.io.ByteArrayInputStream; |
| 51 | import java.io.IOException; |
| 52 | import java.io.InputStream; |
| 53 | import java.nio.charset.StandardCharsets; |
| 54 | import java.security.KeyManagementException; |
| 55 | import java.security.KeyStoreException; |
| 56 | import java.security.NoSuchAlgorithmException; |
| 57 | import java.security.cert.CertificateException; |
| 58 | import java.security.cert.X509Certificate; |
| 59 | import java.util.Base64; |
| 60 | import java.util.Map; |
| 61 | import java.util.concurrent.ConcurrentHashMap; |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 62 | import java.util.function.Consumer; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 63 | |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 64 | import static com.google.common.base.Preconditions.checkNotNull; |
| 65 | |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 66 | /** |
| 67 | * The implementation of HttpSBController. |
| 68 | */ |
| 69 | public class HttpSBControllerImpl implements HttpSBController { |
| 70 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 71 | private static final Logger log = LoggerFactory.getLogger(HttpSBControllerImpl.class); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 72 | private static final String XML = "xml"; |
| 73 | private static final String JSON = "json"; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 74 | protected static final String DOUBLESLASH = "//"; |
| 75 | protected static final String COLON = ":"; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 76 | private static final int STATUS_OK = Response.Status.OK.getStatusCode(); |
| 77 | private static final int STATUS_CREATED = Response.Status.CREATED.getStatusCode(); |
| 78 | private static final int STATUS_ACCEPTED = Response.Status.ACCEPTED.getStatusCode(); |
| 79 | private static final String HTTPS = "https"; |
| 80 | private static final String AUTHORIZATION_PROPERTY = "authorization"; |
| 81 | private static final String BASIC_AUTH_PREFIX = "Basic "; |
fahadnaeemkhan | 2675a27 | 2017-12-13 13:17:23 -0800 | [diff] [blame] | 82 | private static final String OAUTH2_BEARER_AUTH_PREFIX = "Bearer "; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 83 | |
| 84 | private final Map<DeviceId, RestSBDevice> deviceMap = new ConcurrentHashMap<>(); |
| 85 | private final Map<DeviceId, Client> clientMap = new ConcurrentHashMap<>(); |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 86 | private final Map<DeviceId, SseEventSource> sseEventSourceMap = new ConcurrentHashMap<>(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 87 | |
| 88 | public Map<DeviceId, RestSBDevice> getDeviceMap() { |
| 89 | return deviceMap; |
| 90 | } |
| 91 | |
| 92 | public Map<DeviceId, Client> getClientMap() { |
| 93 | return clientMap; |
| 94 | } |
| 95 | |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 96 | public Map<DeviceId, SseEventSource> getSseEventSourceMap() { |
| 97 | return sseEventSourceMap; |
| 98 | } |
| 99 | |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 100 | @Override |
| 101 | public Map<DeviceId, RestSBDevice> getDevices() { |
| 102 | return ImmutableMap.copyOf(deviceMap); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public RestSBDevice getDevice(DeviceId deviceInfo) { |
| 107 | return deviceMap.get(deviceInfo); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public RestSBDevice getDevice(IpAddress ip, int port) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 112 | 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] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void addDevice(RestSBDevice device) { |
| 117 | if (!deviceMap.containsKey(device.deviceId())) { |
| 118 | Client client = ignoreSslClient(); |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 119 | authenticate(client, device); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 120 | clientMap.put(device.deviceId(), client); |
| 121 | deviceMap.put(device.deviceId(), device); |
| 122 | } else { |
| 123 | log.warn("Trying to add a device that is already existing {}", device.deviceId()); |
| 124 | } |
| 125 | |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void removeDevice(DeviceId deviceId) { |
| 130 | clientMap.remove(deviceId); |
| 131 | deviceMap.remove(deviceId); |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 132 | sseEventSourceMap.remove(deviceId); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 136 | public int post(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
| 137 | Response response = getResponse(device, request, payload, mediaType); |
| 138 | if (response == null) { |
| 139 | return Status.NO_CONTENT.getStatusCode(); |
| 140 | } |
| 141 | return response.getStatus(); |
| 142 | } |
| 143 | |
| 144 | @Override |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 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 |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 176 | public int put(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 177 | |
| 178 | WebTarget wt = getWebTarget(device, request); |
| 179 | |
| 180 | Response response = null; |
| 181 | if (payload != null) { |
| 182 | try { |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 183 | response = wt.request(mediaType).put(Entity.entity(IOUtils. |
| 184 | toString(payload, StandardCharsets.UTF_8), mediaType)); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 185 | } catch (IOException e) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 186 | 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] | 187 | } |
| 188 | } else { |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 189 | response = wt.request(mediaType).put(Entity.entity(null, mediaType)); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 190 | } |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 191 | |
| 192 | if (response == null) { |
| 193 | return Status.NO_CONTENT.getStatusCode(); |
| 194 | } |
| 195 | return response.getStatus(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Override |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 199 | public InputStream get(DeviceId device, String request, MediaType mediaType) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 200 | WebTarget wt = getWebTarget(device, request); |
| 201 | |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 202 | Response s = wt.request(mediaType).get(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 203 | |
| 204 | if (checkReply(s)) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 205 | return new ByteArrayInputStream(s.readEntity((String.class)).getBytes(StandardCharsets.UTF_8)); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 206 | } |
| 207 | return null; |
| 208 | } |
| 209 | |
| 210 | @Override |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 211 | public int patch(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 212 | |
| 213 | try { |
| 214 | log.debug("Url request {} ", getUrlString(device, request)); |
| 215 | HttpPatch httprequest = new HttpPatch(getUrlString(device, request)); |
fahadnaeemkhan | 2675a27 | 2017-12-13 13:17:23 -0800 | [diff] [blame] | 216 | if (deviceMap.get(device).authentication() == AuthenticationScheme.BASIC) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 217 | String pwd = deviceMap.get(device).password() == null ? "" : COLON + deviceMap.get(device).password(); |
| 218 | String userPassword = deviceMap.get(device).username() + pwd; |
| 219 | String base64string = Base64.getEncoder().encodeToString(userPassword.getBytes(StandardCharsets.UTF_8)); |
| 220 | httprequest.addHeader(AUTHORIZATION_PROPERTY, BASIC_AUTH_PREFIX + base64string); |
fahadnaeemkhan | 2675a27 | 2017-12-13 13:17:23 -0800 | [diff] [blame] | 221 | } else if (deviceMap.get(device).authentication() == AuthenticationScheme.OAUTH2) { |
| 222 | String token = deviceMap.get(device).token(); |
| 223 | // TODO: support token types other then bearer of OAuth2 authentication |
| 224 | httprequest.addHeader(AUTHORIZATION_PROPERTY, OAUTH2_BEARER_AUTH_PREFIX + token); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 225 | } |
| 226 | if (payload != null) { |
| 227 | StringEntity input = new StringEntity(IOUtils.toString(payload, StandardCharsets.UTF_8)); |
Eunjin Choi | 51244d3 | 2017-05-15 14:09:56 +0900 | [diff] [blame] | 228 | input.setContentType(mediaType.toString()); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 229 | httprequest.setEntity(input); |
| 230 | } |
| 231 | CloseableHttpClient httpClient; |
| 232 | if (deviceMap.containsKey(device) && deviceMap.get(device).protocol().equals(HTTPS)) { |
| 233 | httpClient = getApacheSslBypassClient(); |
| 234 | } else { |
| 235 | httpClient = HttpClients.createDefault(); |
| 236 | } |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 237 | return httpClient.execute(httprequest).getStatusLine().getStatusCode(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 238 | } catch (IOException | NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 239 | log.error("Cannot do PATCH {} request on device {}", request, device, e); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 240 | } |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 241 | return Status.BAD_REQUEST.getStatusCode(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 245 | public int delete(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 246 | |
| 247 | WebTarget wt = getWebTarget(device, request); |
| 248 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 249 | // FIXME: do we need to delete an entry by enclosing data in DELETE |
| 250 | // request? |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 251 | // wouldn't it be nice to use PUT to implement the similar concept? |
Georgios Katsikas | 74a8a44 | 2018-06-26 09:23:58 +0200 | [diff] [blame] | 252 | Response response = null; |
| 253 | try { |
| 254 | response = wt.request(mediaType).delete(); |
| 255 | } catch (ProcessingException procEx) { |
| 256 | log.error("Cannot issue DELETE {} request on device {}", request, device); |
| 257 | return Status.SERVICE_UNAVAILABLE.getStatusCode(); |
| 258 | } |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 259 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 260 | return response.getStatus(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 261 | } |
| 262 | |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 263 | @Override |
| 264 | public int getServerSentEvents(DeviceId deviceId, String request, |
| 265 | Consumer<InboundSseEvent> onEvent, |
| 266 | Consumer<Throwable> onError) { |
| 267 | if (deviceId == null) { |
| 268 | log.warn("Device ID is null", request); |
| 269 | return Status.PRECONDITION_FAILED.getStatusCode(); |
| 270 | } |
| 271 | |
| 272 | if (request == null || request.isEmpty()) { |
| 273 | log.warn("Request cannot be empty", request); |
| 274 | return Status.PRECONDITION_FAILED.getStatusCode(); |
| 275 | } |
| 276 | |
| 277 | if (sseEventSourceMap.containsKey(deviceId)) { |
| 278 | log.warn("Device", deviceId, "is already listening to an SSE stream"); |
| 279 | return Status.CONFLICT.getStatusCode(); |
| 280 | } |
| 281 | |
| 282 | WebTarget wt = getWebTarget(deviceId, request); |
| 283 | SseEventSource sseEventSource = SseEventSource.target(wt).build(); |
| 284 | sseEventSource.register(onEvent, onError); |
| 285 | sseEventSource.open(); |
| 286 | if (sseEventSource.isOpen()) { |
| 287 | sseEventSourceMap.put(deviceId, sseEventSource); |
| 288 | log.info("Opened Server Sent Events request to ", request, "on", deviceId); |
| 289 | while (sseEventSource.isOpen()) { |
| 290 | try { |
| 291 | Thread.sleep(1010); |
| 292 | System.out.println("Listening for SSEs"); |
| 293 | } catch (InterruptedException e) { |
| 294 | log.error("Error", e); |
| 295 | } |
| 296 | } |
| 297 | return Status.NO_CONTENT.getStatusCode(); |
| 298 | } else { |
| 299 | log.error("Unable to open Server Sent Events request to ", request, "to", deviceId); |
| 300 | return Status.INTERNAL_SERVER_ERROR.getStatusCode(); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public int cancelServerSentEvents(DeviceId deviceId) { |
| 306 | if (sseEventSourceMap.containsKey(deviceId)) { |
| 307 | sseEventSourceMap.get(deviceId).close(); |
| 308 | sseEventSourceMap.remove(deviceId); |
| 309 | return Status.OK.getStatusCode(); |
| 310 | } else { |
| 311 | return Status.NOT_FOUND.getStatusCode(); |
| 312 | } |
| 313 | } |
| 314 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 315 | private MediaType typeOfMediaType(String type) { |
| 316 | switch (type) { |
| 317 | case XML: |
| 318 | return MediaType.APPLICATION_XML_TYPE; |
| 319 | case JSON: |
| 320 | return MediaType.APPLICATION_JSON_TYPE; |
Michal Mach | f0ce45e | 2017-06-20 11:54:08 +0200 | [diff] [blame] | 321 | case MediaType.WILDCARD: |
| 322 | return MediaType.WILDCARD_TYPE; |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 323 | default: |
| 324 | throw new IllegalArgumentException("Unsupported media type " + type); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 325 | |
| 326 | } |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 327 | } |
| 328 | |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 329 | private void authenticate(Client client, RestSBDevice device) { |
| 330 | AuthenticationScheme authScheme = device.authentication(); |
| 331 | if (authScheme == AuthenticationScheme.NO_AUTHENTICATION) { |
| 332 | log.debug("{} scheme is specified, ignoring authentication", authScheme); |
| 333 | return; |
| 334 | } else if (authScheme == AuthenticationScheme.OAUTH2) { |
| 335 | String token = checkNotNull(device.token()); |
| 336 | client.register(OAuth2ClientSupport.feature(token)); |
| 337 | } else if (authScheme == AuthenticationScheme.BASIC) { |
| 338 | String username = device.username(); |
| 339 | String password = device.password() == null ? "" : device.password(); |
| 340 | client.register(HttpAuthenticationFeature.basic(username, password)); |
| 341 | } else { |
| 342 | // TODO: Add support for other authentication schemes here. |
| 343 | throw new IllegalArgumentException(String.format("Unsupported authentication scheme: %s", |
| 344 | authScheme.name())); |
| 345 | } |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | protected WebTarget getWebTarget(DeviceId device, String request) { |
| 349 | log.debug("Sending request to URL {} ", getUrlString(device, request)); |
| 350 | return clientMap.get(device).target(getUrlString(device, request)); |
| 351 | } |
| 352 | |
| 353 | //FIXME security issue: this trusts every SSL certificate, even if is self-signed. Also deprecated methods. |
| 354 | private CloseableHttpClient getApacheSslBypassClient() throws NoSuchAlgorithmException, |
| 355 | KeyManagementException, KeyStoreException { |
| 356 | return HttpClients.custom(). |
| 357 | setHostnameVerifier(new AllowAllHostnameVerifier()). |
| 358 | setSslcontext(new SSLContextBuilder() |
| 359 | .loadTrustMaterial(null, (arg0, arg1) -> true) |
| 360 | .build()).build(); |
| 361 | } |
| 362 | |
Michal Mach | bcd58c7 | 2017-06-19 17:12:34 +0200 | [diff] [blame] | 363 | protected String getUrlString(DeviceId deviceId, String request) { |
| 364 | RestSBDevice restSBDevice = deviceMap.get(deviceId); |
| 365 | if (restSBDevice == null) { |
| 366 | log.warn("restSbDevice cannot be NULL!"); |
| 367 | return ""; |
| 368 | } |
| 369 | if (restSBDevice.url() != null) { |
| 370 | return restSBDevice.protocol() + COLON + DOUBLESLASH + restSBDevice.url() + request; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 371 | } else { |
Michal Mach | bcd58c7 | 2017-06-19 17:12:34 +0200 | [diff] [blame] | 372 | return restSBDevice.protocol() + COLON + DOUBLESLASH + restSBDevice.ip().toString() |
| 373 | + COLON + restSBDevice.port() + request; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | private boolean checkReply(Response response) { |
| 378 | if (response != null) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 379 | boolean statusCode = checkStatusCode(response.getStatus()); |
| 380 | if (!statusCode && response.hasEntity()) { |
| 381 | log.error("Failed request, HTTP error msg : " + response.readEntity(String.class)); |
| 382 | } |
| 383 | return statusCode; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 384 | } |
| 385 | log.error("Null reply from device"); |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | private boolean checkStatusCode(int statusCode) { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 390 | if (statusCode == STATUS_OK || statusCode == STATUS_CREATED || statusCode == STATUS_ACCEPTED) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 391 | return true; |
| 392 | } else { |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 393 | log.error("Failed request, HTTP error code : " + statusCode); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 394 | return false; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | private Client ignoreSslClient() { |
| 399 | SSLContext sslcontext = null; |
| 400 | |
| 401 | try { |
| 402 | sslcontext = SSLContext.getInstance("TLS"); |
| 403 | sslcontext.init(null, new TrustManager[]{new X509TrustManager() { |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 404 | @Override |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 405 | public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { |
| 406 | } |
| 407 | |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 408 | @Override |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 409 | public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { |
| 410 | } |
| 411 | |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 412 | @Override |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 413 | public X509Certificate[] getAcceptedIssuers() { |
| 414 | return new X509Certificate[0]; |
| 415 | } |
| 416 | } }, new java.security.SecureRandom()); |
| 417 | } catch (NoSuchAlgorithmException | KeyManagementException e) { |
Ray Milkey | ba547f9 | 2018-02-01 15:22:31 -0800 | [diff] [blame] | 418 | throw new IllegalStateException(e); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | return ClientBuilder.newBuilder().sslContext(sslcontext).hostnameVerifier((s1, s2) -> true).build(); |
| 422 | } |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 423 | |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 424 | } |