refactoring

This commit is contained in:
xis 2023-10-15 19:50:22 +02:00
parent 3a73fa37d6
commit 5c54067cdb
4 changed files with 31 additions and 17 deletions

View file

@ -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<Any> {
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)
}
}
}

View file

@ -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<Any> {
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<String, List<String>>()
request.headerNames?.let {
while (it.hasMoreElements()) {

View file

@ -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

View file

@ -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: