Forráskód Böngészése

Merge branch 'master' of http://139.162.7.140:3000/herlanS_/ipphone-bridge

# Conflicts:
#	pom.xml
herlanS_ 4 éve
szülő
commit
3ad4b626d2

+ 57 - 35
src/main/kotlin/co/id/datacomsolusindo/ipphonebridge/BridgeFIlter.kt

@@ -62,63 +62,85 @@ class SimpleCORSFilter : Filter {
 class BridgeFilter(private val template: SimpMessagingTemplate, val resourceLoader: ResourceLoader) : Filter {
     override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
         val req = request as HttpServletRequest
+        val res = response as HttpServletResponse
         val splitPath = req.requestURI.split("/")
         val client = splitPath[1]
         val toPath = "/" + splitPath.takeLast(splitPath.size - 1).joinToString("/")
-        if (client.startsWith("_")) {
+
+        if (client.startsWith("_") || client.startsWith("resources")) {
             chain.doFilter(request, response)
             return
-        }
-
-        println("client $client")
-        println("path $toPath")
+        } else {
+            val search = isResourceAvailable(toPath)
+            if (search != null && search != "assets/bootstrap.js") {
+                res.sendRedirect("/resources/$search")
+            } else {
+                println("client $client")
+                println("path $toPath")
 
 //        req.parameterMap.entries.forEach {
 //            println(it.key + " : " + it.value.joinToString(", "))
 //        }
 
-        val headerMap = req.headerNames.toList().associateBy({ it }, { req.getHeader(it) })
-        val body = req.reader.lines().toList().joinToString(System.lineSeparator())
+                val headerMap = req.headerNames.toList().associateBy({ it }, { req.getHeader(it) })
+                val body = req.reader.lines().toList().joinToString(System.lineSeparator())
 
-        val reqId = UUID.randomUUID().toString()
-        val rb = RequestBuilder(reqId, toPath, HttpMethod.valueOf(req.method), headerMap.toMutableMap())
-        rb.body = body
+                val reqId = UUID.randomUUID().toString()
+                val rb = RequestBuilder(reqId, toPath, HttpMethod.valueOf(req.method), headerMap.toMutableMap())
+                rb.body = body
 
-        rb.queryString = req.parameterMap
-        Singletons.requestInstance[reqId] = RequestQue(reqId, rb, null)
+                rb.queryString = req.parameterMap
+                Singletons.requestInstance[reqId] = RequestQue(reqId, rb, null)
 
-        template.convertAndSend("/topic/$client", RequestTrigger(reqId))
+                template.convertAndSend("/topic/$client", RequestTrigger(reqId))
 
-        val res = response as HttpServletResponse
-        var i = 0
 
-        while (Singletons.responseQue[reqId] == null) {
-            TimeUnit.MILLISECONDS.sleep(100)
-            i++
-            if (i >= 600) {
+                var i = 0
+
+                while (Singletons.responseQue[reqId] == null) {
+                    TimeUnit.MILLISECONDS.sleep(100)
+                    i++
+                    if (i >= 600) {
 
-                res.status = HttpStatus.REQUEST_TIMEOUT.value()
-                res.writer.write("{\"message\":\"Request timeout. Client not responding\"}")
+                        res.status = HttpStatus.REQUEST_TIMEOUT.value()
+                        res.writer.write("{\"message\":\"Request timeout. Client not responding\"}")
+                        res.flushBuffer()
+                        return
+                    }
+                }
+
+                val resFromSocket = Singletons.responseQue[reqId]
+                res.status = resFromSocket!!.statusCode
+                resFromSocket.headers?.let {
+                    it.entries.forEach { en ->
+//                println(en.key + " : " + en.value.joinToString(","))
+                        res.setHeader(en.key, en.value.joinToString(","))
+                    }
+                }
+                res.outputStream.write(IOUtils.toByteArray(resFromSocket.body))
+//        resFromSocket.body.let { res.writer.write(it) }
                 res.flushBuffer()
+                Singletons.requestInstance.remove(reqId)
                 return
             }
         }
 
-        val resFromSocket = Singletons.responseQue[reqId]
-        res.status = resFromSocket!!.statusCode
-        resFromSocket.headers?.let {
-            it.entries.forEach { en ->
-//                println(en.key + " : " + en.value.joinToString(","))
-                res.setHeader(en.key, en.value.joinToString(","))
-            }
-        }
-        res.outputStream.write(IOUtils.toByteArray(resFromSocket.body))
-//        resFromSocket.body.let { res.writer.write(it) }
-        res.flushBuffer()
-        Singletons.requestInstance.remove(reqId)
-        return
 
+    }
 
+    fun isResourceAvailable(path: String, itr: Int = 1): String? {
+        val splPath = path.split("/").takeLast(itr)
+        if (splPath.size >= itr) {
+            val search = splPath.joinToString("/")
+            println("search $search")
+            val resource = resourceLoader.getResource("file:dist/$search")
+            return if (resource.exists() && resource.file.isFile) {
+                return search
+            } else {
+                isResourceAvailable(path, itr + 1)
+            }
+        }
+        return null
     }
 }
 
@@ -141,7 +163,7 @@ class RequestBuilder(val id: String, val path: String, val method: HttpMethod, v
     var queryString: MutableMap<String, Array<String>>? = null
 }
 
-class Resp(val body: InputStream, val statusCode: Int, val headers: Map<String, Array<String>>? = null)
+class Resp(val body: InputStream?, val statusCode: Int, val headers: Map<String, Array<String>>? = null)
 
 object Singletons {
     val responseQue: MutableMap<String, Resp> by lazy { mutableMapOf<String, Resp>() }