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);
}