[somfytahoma] Avoid potentiel NPE (#10453)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2021-04-04 17:34:25 +02:00 committed by GitHub
parent c738b212ec
commit c1258d78ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 16 deletions

View File

@ -229,7 +229,10 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler {
SomfyTahomaLoginResponse data = gson.fromJson(response.getContentAsString(), SomfyTahomaLoginResponse data = gson.fromJson(response.getContentAsString(),
SomfyTahomaLoginResponse.class); SomfyTahomaLoginResponse.class);
if (data.isSuccess()) { if (data == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Received invalid data (login)");
} else if (data.isSuccess()) {
logger.debug("SomfyTahoma version: {}", data.getVersion()); logger.debug("SomfyTahoma version: {}", data.getVersion());
String id = registerEvents(); String id = registerEvents();
if (id != null && !id.equals(UNAUTHORIZED)) { if (id != null && !id.equals(UNAUTHORIZED)) {
@ -247,11 +250,11 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler {
} }
} }
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {
logger.debug("Received invalid data", e); logger.debug("Received invalid data (login)", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Received invalid data"); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Received invalid data (login)");
} catch (InterruptedException | ExecutionException | TimeoutException e) { } catch (InterruptedException | ExecutionException | TimeoutException e) {
if (e instanceof ExecutionException) { if (e instanceof ExecutionException) {
if (e.getMessage().contains(AUTHENTICATION_CHALLENGE)) { if (isAuthenticationChallenge(e)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Authentication challenge"); "Authentication challenge");
setTooManyRequests(); setTooManyRequests();
@ -377,9 +380,11 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler {
public synchronized @Nullable SomfyTahomaDevice getCachedDevice(String url) { public synchronized @Nullable SomfyTahomaDevice getCachedDevice(String url) {
List<SomfyTahomaDevice> devices = cachedDevices.getValue(); List<SomfyTahomaDevice> devices = cachedDevices.getValue();
for (SomfyTahomaDevice device : devices) { if (devices != null) {
if (url.equals(device.getDeviceURL())) { for (SomfyTahomaDevice device : devices) {
return device; if (url.equals(device.getDeviceURL())) {
return device;
}
} }
} }
return null; return null;
@ -451,15 +456,32 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler {
} }
private void processExecutionRegisteredEvent(SomfyTahomaEvent event) { private void processExecutionRegisteredEvent(SomfyTahomaEvent event) {
JsonElement el = event.getAction(); boolean invalidData = false;
if (el.isJsonArray()) { try {
SomfyTahomaAction[] actions = gson.fromJson(el, SomfyTahomaAction[].class); JsonElement el = event.getAction();
for (SomfyTahomaAction action : actions) { if (el.isJsonArray()) {
registerExecution(action.getDeviceURL(), event.getExecId()); SomfyTahomaAction[] actions = gson.fromJson(el, SomfyTahomaAction[].class);
if (actions == null) {
invalidData = true;
} else {
for (SomfyTahomaAction action : actions) {
registerExecution(action.getDeviceURL(), event.getExecId());
}
}
} else {
SomfyTahomaAction action = gson.fromJson(el, SomfyTahomaAction.class);
if (action == null) {
invalidData = true;
} else {
registerExecution(action.getDeviceURL(), event.getExecId());
}
} }
} else { } catch (JsonSyntaxException e) {
SomfyTahomaAction action = gson.fromJson(el, SomfyTahomaAction.class); invalidData = true;
registerExecution(action.getDeviceURL(), event.getExecId()); }
if (invalidData) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Received invalid data (execution registered)");
} }
} }
@ -771,7 +793,8 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler {
} }
private boolean isAuthenticationChallenge(Exception ex) { private boolean isAuthenticationChallenge(Exception ex) {
return ex.getMessage().contains(AUTHENTICATION_CHALLENGE); String msg = ex.getMessage();
return msg != null && msg.contains(AUTHENTICATION_CHALLENGE);
} }
@Override @Override