From 5c54067cdbb7ce7defe462e2f0cf6b37d0e3b34b Mon Sep 17 00:00:00 2001 From: xis Date: Sun, 15 Oct 2023 19:50:22 +0200 Subject: [PATCH] refactoring --- .../controller/UpnpController.kt | 16 +++++-------- ...equestMapper.kt => StreamMessageMapper.kt} | 24 ++++++++++++++++--- .../nextclouddlna/dlna/media/MediaServer.kt | 4 ++-- ....groovy => StreamMessageMapperTest.groovy} | 4 ++-- 4 files changed, 31 insertions(+), 17 deletions(-) rename src/main/kotlin/net/schowek/nextclouddlna/dlna/{StreamRequestMapper.kt => StreamMessageMapper.kt} (63%) rename src/test/groovy/net/schowek/nextclouddlna/dlna/{StreamRequestMapperTest.groovy => StreamMessageMapperTest.groovy} (94%) diff --git a/src/main/kotlin/net/schowek/nextclouddlna/controller/UpnpController.kt b/src/main/kotlin/net/schowek/nextclouddlna/controller/UpnpController.kt index 96a2dfc..0aabcc8 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/controller/UpnpController.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/controller/UpnpController.kt @@ -3,12 +3,10 @@ package net.schowek.nextclouddlna.controller import jakarta.servlet.http.HttpServletRequest import mu.KLogging import net.schowek.nextclouddlna.DlnaService -import net.schowek.nextclouddlna.dlna.StreamRequestMapper +import net.schowek.nextclouddlna.dlna.StreamMessageMapper import net.schowek.nextclouddlna.dlna.media.MediaServer import org.springframework.core.io.InputStreamResource import org.springframework.core.io.Resource -import org.springframework.http.HttpHeaders -import org.springframework.http.HttpStatusCode import org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.PathVariable @@ -20,7 +18,7 @@ import org.springframework.web.bind.annotation.RestController @RestController class UpnpController( - private val streamRequestMapper: StreamRequestMapper, + private val streamMessageMapper: StreamMessageMapper, private val dlnaService: DlnaService ) { @RequestMapping( @@ -50,12 +48,10 @@ class UpnpController( request: HttpServletRequest ): ResponseEntity { logger.info { "Upnp ${request.method} request from ${request.remoteAddr}: ${request.requestURI}" } - return with(dlnaService.processRequest(streamRequestMapper.map(request))) { - ResponseEntity( - body, - HttpHeaders().also { h -> headers.entries.forEach { e -> h.add(e.key, e.value.joinToString { it }) } }, - HttpStatusCode.valueOf(operation.statusCode) - ) + return streamMessageMapper.map(request).let { req -> + dlnaService.processRequest(req).let { res -> + streamMessageMapper.map(res) + } } } diff --git a/src/main/kotlin/net/schowek/nextclouddlna/dlna/StreamRequestMapper.kt b/src/main/kotlin/net/schowek/nextclouddlna/dlna/StreamMessageMapper.kt similarity index 63% rename from src/main/kotlin/net/schowek/nextclouddlna/dlna/StreamRequestMapper.kt rename to src/main/kotlin/net/schowek/nextclouddlna/dlna/StreamMessageMapper.kt index a83ccfb..3795b05 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/dlna/StreamRequestMapper.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/dlna/StreamMessageMapper.kt @@ -3,13 +3,17 @@ package net.schowek.nextclouddlna.dlna import jakarta.servlet.http.HttpServletRequest import mu.KLogging import org.jupnp.model.message.StreamRequestMessage +import org.jupnp.model.message.StreamResponseMessage import org.jupnp.model.message.UpnpHeaders import org.jupnp.model.message.UpnpRequest +import org.springframework.http.HttpHeaders +import org.springframework.http.HttpStatusCode +import org.springframework.http.ResponseEntity import org.springframework.stereotype.Component import java.net.URI @Component -class StreamRequestMapper { +class StreamMessageMapper { fun map(request: HttpServletRequest): StreamRequestMessage { val requestMessage = StreamRequestMessage( UpnpRequest.Method.getByHttpName(request.method), @@ -22,11 +26,25 @@ class StreamRequestMapper { throw RuntimeException("Method not supported: {}" + request.method) } - requestMessage.headers = createHeaders(request) + requestMessage.headers = upnpHeaders(request) return requestMessage } - private fun createHeaders(request: HttpServletRequest): UpnpHeaders { + fun map(response: StreamResponseMessage): ResponseEntity { + return with(response) { + ResponseEntity( + body, + HttpHeaders().also { h -> + headers.entries.forEach { e -> + h.add(e.key, e.value.joinToString { it }) + } + }, + HttpStatusCode.valueOf(operation.statusCode) + ) + } + } + + private fun upnpHeaders(request: HttpServletRequest): UpnpHeaders { val headers = mutableMapOf>() request.headerNames?.let { while (it.hasMoreElements()) { diff --git a/src/main/kotlin/net/schowek/nextclouddlna/dlna/media/MediaServer.kt b/src/main/kotlin/net/schowek/nextclouddlna/dlna/media/MediaServer.kt index 1ab20f4..5c0a1a6 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/dlna/media/MediaServer.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/dlna/media/MediaServer.kt @@ -21,7 +21,7 @@ class MediaServer( private val friendlyName: String, externalUrls: ExternalUrls ) { - val device = LocalDevice( + final val device = LocalDevice( DeviceIdentity(uniqueSystemIdentifier("Nextcloud-DLNA-MediaServer"), ADVERTISEMENT_AGE_IN_S), UDADeviceType(DEVICE_TYPE, VERSION), DeviceDetails(friendlyName, externalUrls.selfURI), @@ -36,7 +36,7 @@ class MediaServer( companion object : KLogging() { - const val ICON_FILENAME = "icon.png" + private const val ICON_FILENAME = "icon.png" private const val DEVICE_TYPE = "MediaServer" private const val VERSION = 1 private const val ADVERTISEMENT_AGE_IN_S = 60 diff --git a/src/test/groovy/net/schowek/nextclouddlna/dlna/StreamRequestMapperTest.groovy b/src/test/groovy/net/schowek/nextclouddlna/dlna/StreamMessageMapperTest.groovy similarity index 94% rename from src/test/groovy/net/schowek/nextclouddlna/dlna/StreamRequestMapperTest.groovy rename to src/test/groovy/net/schowek/nextclouddlna/dlna/StreamMessageMapperTest.groovy index a98d2e8..e2ed49d 100644 --- a/src/test/groovy/net/schowek/nextclouddlna/dlna/StreamRequestMapperTest.groovy +++ b/src/test/groovy/net/schowek/nextclouddlna/dlna/StreamMessageMapperTest.groovy @@ -5,8 +5,8 @@ import org.jupnp.model.message.UpnpRequest import org.springframework.mock.web.MockHttpServletRequest import spock.lang.Specification -class StreamRequestMapperTest extends Specification { - def sut = new StreamRequestMapper() +class StreamMessageMapperTest extends Specification { + def sut = new StreamMessageMapper() def "should map servlet request to streamRequestMessage"() { given: