Skip to content

Commit

Permalink
improve catch exception during parsing of invalid RSS xml feed, switc…
Browse files Browse the repository at this point in the history
…h Runtime Image from Java 17 to 21
  • Loading branch information
tillkuhn committed Nov 9, 2023
1 parent 1179c90 commit a334230
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion kotlin/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#
# Default ARG values can be overwritten by passing --build-arg
# - https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact
ARG FROM_TAG=17-jre
# New Java 21 LTS has arrived in September 2023 (new default unless a different version is provide as build-arg)
ARG FROM_TAG=21-jre
FROM eclipse-temurin:$FROM_TAG

# dynamic value be passed by evaluating git describe --abbrev=0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.timafe.angkor.service.utils

import com.rometools.rome.feed.synd.SyndEntry
import com.rometools.rome.feed.synd.SyndFeed
import com.rometools.rome.io.ParsingFeedException
import com.rometools.rome.io.SyndFeedInput
import com.rometools.rome.io.XmlReader
import org.slf4j.Logger
Expand All @@ -18,14 +19,19 @@ class FeedUtils {
fun <T> parseFeed(feedUrl: String, mapperFunc: (syndEntry: SyndEntry) -> T): List<T> {
val input = SyndFeedInput()
log.info("[Feeder] Loading feedUrl $feedUrl")
val feed: SyndFeed = if (feedUrl.startsWith("https://")) input.build(XmlReader(URL(feedUrl)))
else input.build(XmlReader(File(feedUrl))) //.readLines()

val entities = mutableListOf<T>()
feed.entries.forEach { feedItem ->
entities.add(mapperFunc(feedItem))
// external feed could return invalid xml, in which case we log the error but return empty list
return try {
val feed: SyndFeed = if (feedUrl.startsWith("https://")) input.build(XmlReader(URL(feedUrl)))
else input.build(XmlReader(File(feedUrl))) //.readLines()
val entities = mutableListOf<T>()
feed.entries.forEach { feedItem ->
entities.add(mapperFunc(feedItem))
}
entities.toList()
} catch (pfe: ParsingFeedException) {
log.error("[Feeder] Parse Error, cannot build RSS feed from ${feedUrl}: ${pfe.message}}")
emptyList()
}
return entities.toList()
}
}
}

0 comments on commit a334230

Please sign in to comment.