[roku] Improve exception handling for failed http connections (#15932)

Signed-off-by: Michael Lobstein <michael.lobstein@gmail.com>
This commit is contained in:
mlobstein 2023-11-29 01:12:27 -06:00 committed by GitHub
parent 6df602599e
commit e33cccc29c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 21 deletions

View File

@ -23,6 +23,10 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
public class RokuHttpException extends Exception {
private static final long serialVersionUID = 1L;
public RokuHttpException(String errorMessage, Throwable t) {
super(errorMessage, t);
}
public RokuHttpException(String errorMessage) {
super(errorMessage);
}

View File

@ -35,8 +35,6 @@ import org.openhab.binding.roku.internal.dto.Player;
import org.openhab.binding.roku.internal.dto.TvChannel;
import org.openhab.binding.roku.internal.dto.TvChannels;
import org.openhab.binding.roku.internal.dto.TvChannels.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Methods for accessing the HTTP interface of the Roku
@ -45,7 +43,6 @@ import org.slf4j.LoggerFactory;
*/
@NonNullByDefault
public class RokuCommunicator {
private final Logger logger = LoggerFactory.getLogger(RokuCommunicator.class);
private final HttpClient httpClient;
private final String urlKeyPress;
@ -113,10 +110,10 @@ public class RokuCommunicator {
try {
JAXBContext ctx = JAXBUtils.JAXBCONTEXT_DEVICE_INFO;
if (ctx != null) {
final String response = getCommand(urlQryDevice);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
if (unmarshaller != null) {
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
.createXMLStreamReader(new StringReader(getCommand(urlQryDevice)));
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
DeviceInfo device = (DeviceInfo) unmarshaller.unmarshal(xsr);
if (device != null) {
return device;
@ -139,10 +136,10 @@ public class RokuCommunicator {
try {
JAXBContext ctx = JAXBUtils.JAXBCONTEXT_ACTIVE_APP;
if (ctx != null) {
final String response = getCommand(urlQryActiveApp);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
if (unmarshaller != null) {
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
.createXMLStreamReader(new StringReader(getCommand(urlQryActiveApp)));
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
ActiveApp activeApp = (ActiveApp) unmarshaller.unmarshal(xsr);
if (activeApp != null) {
return activeApp;
@ -165,10 +162,10 @@ public class RokuCommunicator {
try {
JAXBContext ctx = JAXBUtils.JAXBCONTEXT_APPS;
if (ctx != null) {
final String response = getCommand(urlQryApps);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
if (unmarshaller != null) {
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
.createXMLStreamReader(new StringReader(getCommand(urlQryApps)));
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
Apps appList = (Apps) unmarshaller.unmarshal(xsr);
if (appList != null) {
return appList.getApp();
@ -191,10 +188,10 @@ public class RokuCommunicator {
try {
JAXBContext ctx = JAXBUtils.JAXBCONTEXT_PLAYER;
if (ctx != null) {
final String response = getCommand(urlQryPlayer);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
if (unmarshaller != null) {
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
.createXMLStreamReader(new StringReader(getCommand(urlQryPlayer)));
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
Player playerInfo = (Player) unmarshaller.unmarshal(xsr);
if (playerInfo != null) {
return playerInfo;
@ -217,10 +214,10 @@ public class RokuCommunicator {
try {
JAXBContext ctx = JAXBUtils.JAXBCONTEXT_TVCHANNEL;
if (ctx != null) {
final String response = getCommand(urlQryActiveTvChannel);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
if (unmarshaller != null) {
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
.createXMLStreamReader(new StringReader(getCommand(urlQryActiveTvChannel)));
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
TvChannel tvChannelInfo = (TvChannel) unmarshaller.unmarshal(xsr);
if (tvChannelInfo != null) {
return tvChannelInfo;
@ -243,10 +240,10 @@ public class RokuCommunicator {
try {
JAXBContext ctx = JAXBUtils.JAXBCONTEXT_TVCHANNELS;
if (ctx != null) {
final String response = getCommand(urlQryTvChannels);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
if (unmarshaller != null) {
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
.createXMLStreamReader(new StringReader(getCommand(urlQryTvChannels)));
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
TvChannels tvChannels = (TvChannels) unmarshaller.unmarshal(xsr);
if (tvChannels != null) {
return tvChannels.getChannel();
@ -264,13 +261,16 @@ public class RokuCommunicator {
*
* @param url The url to send with the command embedded in the URI
* @return The response content of the http request
* @throws RokuHttpException
*/
private String getCommand(String url) {
private String getCommand(String url) throws RokuHttpException {
try {
return httpClient.GET(url).getContentAsString();
} catch (InterruptedException | TimeoutException | ExecutionException e) {
logger.debug("Error executing player GET command, URL: {}, {} ", url, e.getMessage());
return "";
} catch (TimeoutException | ExecutionException e) {
throw new RokuHttpException("Error executing GET command for URL: " + url, e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RokuHttpException("InterruptedException executing GET command for URL: " + url, e);
}
}
@ -283,8 +283,11 @@ public class RokuCommunicator {
private void postCommand(String url) throws RokuHttpException {
try {
httpClient.POST(url).method(HttpMethod.POST).send();
} catch (InterruptedException | TimeoutException | ExecutionException e) {
throw new RokuHttpException("Error executing player POST command, URL: " + url + e.getMessage());
} catch (TimeoutException | ExecutionException e) {
throw new RokuHttpException("Error executing POST command, URL: " + url, e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RokuHttpException("InterruptedException executing POST command for URL: " + url, e);
}
}
}