feat(opds-v1): use static xml namespace prefix for opds-pse

this should increase compatibility with poorly implemented OPDS clients
This commit is contained in:
Gauthier Roebroeck 2023-12-12 15:14:54 +08:00
parent 721c5d16e9
commit 834b51d744
3 changed files with 67 additions and 0 deletions

View file

@ -0,0 +1,17 @@
package org.gotson.komga.infrastructure.xml
import org.gotson.komga.interfaces.api.opds.v1.dto.prefixToNamespace
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter
@Configuration
class MappingJackson2XmlHttpMessageConverterConfiguration {
@Bean
fun mappingJackson2XmlHttpMessageConverter(builder: Jackson2ObjectMapperBuilder) =
MappingJackson2XmlHttpMessageConverter(
builder.createXmlMapper(true).factory(NamespaceXmlFactory(prefixToNamespace = prefixToNamespace)).build(),
)
}

View file

@ -0,0 +1,46 @@
package org.gotson.komga.infrastructure.xml
import com.fasterxml.jackson.core.JsonEncoding
import com.fasterxml.jackson.core.io.IOContext
import com.fasterxml.jackson.dataformat.xml.XmlFactory
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator
import com.fasterxml.jackson.dataformat.xml.util.StaxUtil
import java.io.File
import java.io.OutputStream
import java.io.Writer
import javax.xml.stream.XMLStreamException
import javax.xml.stream.XMLStreamWriter
class NamespaceXmlFactory(
private val defaultNamespace: String? = null,
private val prefixToNamespace: Map<String, String> = emptyMap(),
) : XmlFactory() {
override fun _createXmlWriter(ctxt: IOContext?, w: Writer?): XMLStreamWriter =
super._createXmlWriter(ctxt, w).apply { configure() }
override fun createGenerator(out: OutputStream?, enc: JsonEncoding?): ToXmlGenerator =
super.createGenerator(out, enc).apply { staxWriter.configure() }
override fun createGenerator(out: OutputStream?): ToXmlGenerator =
super.createGenerator(out).apply { staxWriter.configure() }
override fun createGenerator(out: Writer?): ToXmlGenerator =
super.createGenerator(out).apply { staxWriter.configure() }
override fun createGenerator(f: File?, enc: JsonEncoding?): ToXmlGenerator =
super.createGenerator(f, enc).apply { staxWriter.configure() }
override fun createGenerator(sw: XMLStreamWriter?): ToXmlGenerator =
super.createGenerator(sw).apply { staxWriter.configure() }
private fun XMLStreamWriter.configure() =
try {
defaultNamespace?.let { this.setDefaultNamespace(it) }
for ((key, value) in prefixToNamespace) {
this.setPrefix(key, value)
}
} catch (e: XMLStreamException) {
StaxUtil.throwAsGenerationException(e, null)
}
}

View file

@ -3,3 +3,7 @@ package org.gotson.komga.interfaces.api.opds.v1.dto
const val ATOM = "http://www.w3.org/2005/Atom"
const val OPDS_PSE = "http://vaemendis.net/opds-pse/ns"
const val OPENSEARCH = "http://a9.com/-/spec/opensearch/1.1/"
val prefixToNamespace = mapOf(
"pse" to OPDS_PSE,
)