Added alternative handling for UnmarshalException (#10715)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp
2021-05-20 20:26:51 +02:00
committed by GitHub
parent 7aab765b5a
commit 5a836b82cb
6 changed files with 127 additions and 75 deletions

View File

@@ -17,6 +17,7 @@ import static org.eclipse.jetty.http.HttpMethod.GET;
import java.io.StringReader;
import javax.xml.bind.JAXBException;
import javax.xml.bind.UnmarshalException;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
@@ -58,6 +59,7 @@ public class FritzAhaUpdateCallback extends FritzAhaReauthCallback {
this.handler = handler;
}
@SuppressWarnings({ "null", "unused" })
@Override
public void execute(int status, String response) {
super.execute(status, response);
@@ -66,13 +68,16 @@ public class FritzAhaUpdateCallback extends FritzAhaReauthCallback {
try {
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
Unmarshaller unmarshaller = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller();
DeviceListModel model = (DeviceListModel) unmarshaller.unmarshal(xsr);
DeviceListModel model = unmarshaller.unmarshal(xsr, DeviceListModel.class).getValue();
if (model != null) {
handler.onDeviceListAdded(model.getDevicelist());
} else {
logger.debug("no model in response");
}
handler.setStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
} catch (UnmarshalException e) {
logger.debug("Failed to unmarshal XML document: {}", e.getMessage());
handler.setStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
} catch (JAXBException | XMLStreamException e) {
logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e);
handler.setStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,

View File

@@ -17,6 +17,7 @@ import static org.eclipse.jetty.http.HttpMethod.GET;
import java.io.StringReader;
import javax.xml.bind.JAXBException;
import javax.xml.bind.UnmarshalException;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
@@ -54,6 +55,7 @@ public class FritzAhaUpdateTemplatesCallback extends FritzAhaReauthCallback {
this.handler = handler;
}
@SuppressWarnings({ "null", "unused" })
@Override
public void execute(int status, String response) {
super.execute(status, response);
@@ -62,12 +64,14 @@ public class FritzAhaUpdateTemplatesCallback extends FritzAhaReauthCallback {
try {
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
Unmarshaller unmarshaller = JAXBUtils.JAXBCONTEXT_TEMPLATES.createUnmarshaller();
TemplateListModel model = (TemplateListModel) unmarshaller.unmarshal(xsr);
TemplateListModel model = unmarshaller.unmarshal(xsr, TemplateListModel.class).getValue();
if (model != null) {
handler.addTemplateList(model.getTemplates());
} else {
logger.debug("no template in response");
}
} catch (UnmarshalException e) {
logger.debug("Failed to unmarshal XML document: {}", e.getMessage());
} catch (JAXBException | XMLStreamException e) {
logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e);
}

View File

@@ -21,13 +21,13 @@ import java.util.Optional;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.binding.avmfritz.internal.util.JAXBUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Tests for {@link DeviceListModel}.
@@ -38,12 +38,11 @@ import org.slf4j.LoggerFactory;
@NonNullByDefault
public class AVMFritzDeviceListModelTest {
private final Logger logger = LoggerFactory.getLogger(AVMFritzDeviceListModelTest.class);
private @NonNullByDefault({}) DeviceListModel devices;
@SuppressWarnings("null")
@BeforeEach
public void setUp() {
public void setUp() throws JAXBException, XMLStreamException {
//@formatter:off
final String xml =
"<devicelist version=\"1\">" +
@@ -62,13 +61,10 @@ public class AVMFritzDeviceListModelTest {
"<device identifier=\"13096 0007308\" id=\"30\" functionbitmask=\"1048864\" fwversion=\"05.10\" manufacturer=\"AVM\" productname=\"FRITZ!DECT 440\"><present>1</present><name>FRITZ!DECT 440 #15</name><temperature><celsius>230</celsius><offset>0</offset></temperature><humidity><rel_humidity>43</rel_humidity></humidity><battery>100</battery><batterylow>0</batterylow><button identifier=\"13096 0007308-1\" id=\"5000\"><name>FRITZ!DECT 440 #15: Oben rechts</name><lastpressedtimestamp>1549195586</lastpressedtimestamp></button><button identifier=\"13096 0007308-3\" id=\"5001\"><name>FRITZ!DECT 440 #15: Unten rechts</name><lastpressedtimestamp>1549195595</lastpressedtimestamp></button><button identifier=\"13096 0007308-5\" id=\"5002\"><name>FRITZ!DECT 440 #15: Unten links</name><lastpressedtimestamp>1549195586</lastpressedtimestamp></button><button identifier=\"13096 0007308-7\" id=\"5003\"><name>FRITZ!DECT 440 #15: Oben links</name><lastpressedtimestamp>1549195595</lastpressedtimestamp></button></device>" +
"<device identifier=\"14276 0503450-1\" id=\"2000\" functionbitmask=\"335888\" fwversion=\"0.0\" manufacturer=\"0x37c4\" productname=\"Rollotron 1213\"><present>1</present><txbusy>0</txbusy><name>Rollotron 1213 #1</name><blind><endpositionsset>1</endpositionsset><mode>manuell</mode></blind><levelcontrol><level>26</level><levelpercentage>10</levelpercentage></levelcontrol><etsiunitinfo><etsideviceid>406</etsideviceid><unittype>281</unittype><interfaces>256,513,516,517</interfaces></etsiunitinfo><alert><state>0</state><lastalertchgtimestamp></lastalertchgtimestamp></alert></device>" +
"</devicelist>";
//@formatter:off
try {
Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller();
devices = (DeviceListModel) u.unmarshal(new StringReader(xml));
} catch (JAXBException e) {
logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e);
}
//@formatter:on
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml));
Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller();
devices = u.unmarshal(xsr, DeviceListModel.class).getValue();
}
@Test

View File

@@ -19,6 +19,8 @@ import java.util.Optional;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
@@ -26,8 +28,6 @@ import org.junit.jupiter.api.Test;
import org.openhab.binding.avmfritz.internal.dto.templates.TemplateListModel;
import org.openhab.binding.avmfritz.internal.dto.templates.TemplateModel;
import org.openhab.binding.avmfritz.internal.util.JAXBUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Tests for {@link TemplateListModel}.
@@ -37,26 +37,21 @@ import org.slf4j.LoggerFactory;
@NonNullByDefault
public class AVMFritzTemplateListModelTest {
private final Logger logger = LoggerFactory.getLogger(AVMFritzTemplateListModelTest.class);
private @NonNullByDefault({}) TemplateListModel templates;
@SuppressWarnings("null")
@BeforeEach
public void setUp() {
public void setUp() throws JAXBException, XMLStreamException {
//@formatter:off
String xml =
"<templatelist version=\"1\">" +
"<template identifier=\"tmpXXXXX-39DC738C5\" id=\"30103\" functionbitmask=\"6784\" applymask=\"64\"><name>Test template #1</name><devices><device identifier=\"YY:5D:AA-900\" /><device identifier=\"XX:5D:AA-900\" /></devices><applymask><relay_automatic /></applymask></template>" +
"<template identifier=\"tmpXXXXX-39722FC0F\" id=\"30003\" functionbitmask=\"6784\" applymask=\"64\"><name>Test template #2</name><devices><device identifier=\"YY:5D:AA-900\" /></devices><applymask><relay_automatic /></applymask></template>" +
"</templatelist>";
//@formatter:off
try {
Unmarshaller u = JAXBUtils.JAXBCONTEXT_TEMPLATES.createUnmarshaller();
templates = (TemplateListModel) u.unmarshal(new StringReader(xml));
} catch (JAXBException e) {
logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e);
}
//@formatter:on
XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml));
Unmarshaller u = JAXBUtils.JAXBCONTEXT_TEMPLATES.createUnmarshaller();
templates = u.unmarshal(xsr, TemplateListModel.class).getValue();
}
@Test