From 943c0db298c3e2074a9b28c13dd7c5d90fdf3a59 Mon Sep 17 00:00:00 2001 From: mishako Date: Fri, 1 Apr 2016 21:07:51 +0200 Subject: [PATCH] Catch itunes duration parsing exceptions Parsing of a malformed or empty duration resulted in an uncaught exception that was breaking feed parsing completely. We already catch exceptions when parsing other fields, e.g. href attribute of the image tag. Also, it's more user-friendly to handle malformed data instead of failing everything. --- .../modules/itunes/io/ITunesParser.java | 10 +++++--- .../modules/itunes/ITunesParserTest.java | 25 +++++++++++++++++++ .../rometools/modules/itunes/duration-bad.xml | 8 ++++++ .../modules/itunes/duration-empty.xml | 8 ++++++ .../com/rometools/modules/itunes/duration.xml | 8 ++++++ 5 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 rome-modules/src/test/resources/com/rometools/modules/itunes/duration-bad.xml create mode 100644 rome-modules/src/test/resources/com/rometools/modules/itunes/duration-empty.xml create mode 100644 rome-modules/src/test/resources/com/rometools/modules/itunes/duration.xml diff --git a/rome-modules/src/main/java/com/rometools/modules/itunes/io/ITunesParser.java b/rome-modules/src/main/java/com/rometools/modules/itunes/io/ITunesParser.java index 1986df7..3a8381a 100644 --- a/rome-modules/src/main/java/com/rometools/modules/itunes/io/ITunesParser.java +++ b/rome-modules/src/main/java/com/rometools/modules/itunes/io/ITunesParser.java @@ -147,8 +147,12 @@ public class ITunesParser implements ModuleParser { final Element duration = element.getChild("duration", ns); if (duration != null && duration.getValue() != null) { - final Duration dur = new Duration(duration.getValue().trim()); - entryInfo.setDuration(dur); + try { + final Duration dur = new Duration(duration.getValue().trim()); + entryInfo.setDuration(dur); + } catch (Exception e) { + LOG.warn("Failed to parse duration: {}", duration.getValue()); + } } final Element closedCaptioned = element.getChild("isClosedCaptioned", ns); @@ -216,7 +220,7 @@ public class ITunesParser implements ModuleParser { final URL imageURL = new URL(image.getAttributeValue("href").trim()); module.setImage(imageURL); } catch (final MalformedURLException e) { - LOG.debug("Malformed URL Exception reading itunes:image tag: {}", image.getAttributeValue("href")); + LOG.warn("Malformed URL Exception reading itunes:image tag: {}", image.getAttributeValue("href")); } } } diff --git a/rome-modules/src/test/java/com/rometools/modules/itunes/ITunesParserTest.java b/rome-modules/src/test/java/com/rometools/modules/itunes/ITunesParserTest.java index 1ded481..8460563 100644 --- a/rome-modules/src/test/java/com/rometools/modules/itunes/ITunesParserTest.java +++ b/rome-modules/src/test/java/com/rometools/modules/itunes/ITunesParserTest.java @@ -34,6 +34,7 @@ import com.rometools.modules.itunes.AbstractITunesObject; import com.rometools.modules.itunes.EntryInformationImpl; import com.rometools.modules.itunes.FeedInformationImpl; import com.rometools.modules.itunes.io.ITunesGenerator; +import com.rometools.modules.itunes.types.Duration; import com.rometools.rome.feed.module.Module; import com.rometools.rome.feed.synd.SyndEntry; import com.rometools.rome.feed.synd.SyndFeed; @@ -133,4 +134,28 @@ public class ITunesParserTest extends AbstractTestCase { assertEquals(Integer.valueOf(2), entryInfo.getOrder()); assertEquals("http://example.org/image.png", entryInfo.getImage().toString()); } + + public void testDuration() throws Exception { + SyndFeed feed = new SyndFeedInput().build(new XmlReader(getClass().getResource("duration.xml"))); + SyndEntry entry = feed.getEntries().get(0); + EntryInformationImpl module = (EntryInformationImpl) entry.getModule(AbstractITunesObject.URI); + + assertEquals(1000, module.getDuration().getMilliseconds()); + } + + public void testDurationEmpty() throws Exception { + SyndFeed feed = new SyndFeedInput().build(new XmlReader(getClass().getResource("duration-empty.xml"))); + SyndEntry entry = feed.getEntries().get(0); + EntryInformationImpl module = (EntryInformationImpl) entry.getModule(AbstractITunesObject.URI); + + assertNull(module.getDuration()); + } + + public void testDurationBad() throws Exception { + SyndFeed feed = new SyndFeedInput().build(new XmlReader(getClass().getResource("duration-bad.xml"))); + SyndEntry entry = feed.getEntries().get(0); + EntryInformationImpl module = (EntryInformationImpl) entry.getModule(AbstractITunesObject.URI); + + assertNull(module.getDuration()); + } } diff --git a/rome-modules/src/test/resources/com/rometools/modules/itunes/duration-bad.xml b/rome-modules/src/test/resources/com/rometools/modules/itunes/duration-bad.xml new file mode 100644 index 0000000..f8b8a33 --- /dev/null +++ b/rome-modules/src/test/resources/com/rometools/modules/itunes/duration-bad.xml @@ -0,0 +1,8 @@ + + + + + 00:00:00:01 + + + diff --git a/rome-modules/src/test/resources/com/rometools/modules/itunes/duration-empty.xml b/rome-modules/src/test/resources/com/rometools/modules/itunes/duration-empty.xml new file mode 100644 index 0000000..85bde04 --- /dev/null +++ b/rome-modules/src/test/resources/com/rometools/modules/itunes/duration-empty.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/rome-modules/src/test/resources/com/rometools/modules/itunes/duration.xml b/rome-modules/src/test/resources/com/rometools/modules/itunes/duration.xml new file mode 100644 index 0000000..fcbaa88 --- /dev/null +++ b/rome-modules/src/test/resources/com/rometools/modules/itunes/duration.xml @@ -0,0 +1,8 @@ + + + + + 00:00:01 + + +