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