Prechádzať zdrojové kódy

add resource not found for unregistered client

herlanS_ 3 rokov pred
rodič
commit
df5df932c7

+ 7 - 1
src/main/kotlin/co/id/datacomsolusindo/ipphonebridge/BridgeFIlter.kt

@@ -12,6 +12,7 @@ import org.springframework.http.HttpMethod
 import org.springframework.http.HttpStatus
 import org.springframework.messaging.simp.SimpMessagingTemplate
 import org.springframework.stereotype.Component
+import org.springframework.web.server.ResponseStatusException
 import org.springframework.web.servlet.config.annotation.EnableWebMvc
 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
@@ -23,7 +24,6 @@ import java.net.InetAddress
 import java.time.Duration
 import java.time.LocalDateTime
 import java.util.*
-import java.util.Collections.synchronizedList
 import java.util.concurrent.ConcurrentHashMap
 import java.util.concurrent.ConcurrentMap
 import java.util.concurrent.TimeUnit
@@ -81,7 +81,13 @@ class BridgeFilter(private val template: SimpMessagingTemplate, val resourceLoad
             if (search != null && !search.endsWith("assets/bootstrap.js")) {
                 res.sendRedirect("/resources/$search")
             } else {
+                if(!ClientHolder.get().containsKey(clientNumber.toString())){
+                    res.status = HttpStatus.NOT_FOUND.value()
+                    res.writer.write("{\"message\":\"resource not found\"}")
+                    res.flushBuffer()
+                    return
 
+                }
                 LogManager.getLogger("client-$client")
                     .info("Request Start, UA: " + req.getHeader("User-Agent") + " From: " + InetAddress.getLocalHost().hostAddress + " To: " + req.requestURL.toString() + "?" + req.queryString)
 

+ 16 - 5
src/main/kotlin/co/id/datacomsolusindo/ipphonebridge/IpPhoneBridgeApplication.kt

@@ -30,6 +30,8 @@ import java.net.URI
 import java.time.LocalDateTime
 import java.time.format.DateTimeFormatter
 import java.util.*
+import java.util.concurrent.ConcurrentHashMap
+import java.util.concurrent.ConcurrentMap
 import kotlin.system.exitProcess
 import kotlin.system.measureTimeMillis
 
@@ -74,7 +76,8 @@ fun main(args: Array<String>) {
 class Client(
     val number: String,
     var connectAt: String,
-    var lastRequest: String
+    var lastRequest: String,
+    val sessionId: String?
 ) : Serializable {
     var reqSuccess = 0
     var reqFailed = 0
@@ -83,7 +86,7 @@ class Client(
 
 object ClientHolder {
     private val mutexSucReq = Mutex()
-    private val clientMap: MutableMap<String, Client> = mutableMapOf()
+    private val clientMap: ConcurrentMap<String, Client> by lazy { ConcurrentHashMap() }
     private suspend fun massiveRun(action: suspend () -> Unit) {
         measureTimeMillis {
             coroutineScope { // sc
@@ -94,11 +97,17 @@ object ClientHolder {
         }
     }
 
+    fun removeBySessionId(sessionId: String?) {
+        clientMap.entries.filter { it.value.sessionId == sessionId }.forEach {
+            clientMap.remove(it.key)
+        }
+    }
+
     fun put(key: String, client: Client) {
-        clientMap[key] = client
+        clientMap.getOrPut(key) { client }
     }
 
-    fun get() = clientMap.toMap()
+    fun get() = clientMap
 
     fun addFailedRequest(clNum: String) {
         runBlocking {
@@ -183,6 +192,7 @@ class WebSocketEventListener {
     @EventListener
     fun onDisconnect(event: SessionDisconnectEvent) {
         AppLog.write(this.javaClass).info("disconnect with session id ${event.sessionId}")
+        ClientHolder.removeBySessionId(event.sessionId)
     }
 
     @EventListener
@@ -201,7 +211,8 @@ class WebSocketEventListener {
                 clNum, Client(
                     clNum,
                     LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")),
-                    LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
+                    LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")),
+                    sessionId
                 )
             )