Browse Source

change url pattern and add response header

herlans 5 years ago
parent
commit
e4fe3e84fc

+ 87 - 25
src/main/kotlin/co/id/datacomsolusindo/ipphonebridge/BridgeFIlter.kt

@@ -2,6 +2,8 @@ package co.id.datacomsolusindo.ipphonebridge
 
 import org.springframework.boot.web.servlet.FilterRegistrationBean
 import org.springframework.context.annotation.Bean
+import org.springframework.core.annotation.Order
+import org.springframework.http.HttpHeaders
 import org.springframework.http.HttpMethod
 import org.springframework.http.HttpStatus
 import org.springframework.messaging.simp.SimpMessagingTemplate
@@ -18,15 +20,21 @@ import javax.servlet.http.HttpServletRequest
 import javax.servlet.http.HttpServletResponse
 import kotlin.streams.toList
 
-class ForwarderFilter(private val template: SimpMessagingTemplate) : Filter {
-    override fun doFilter(
-            request: ServletRequest,
-            response: ServletResponse,
-            chain: FilterChain) {
+@Component
+@Order(1)
+class BridgeFilter(private val template: SimpMessagingTemplate) : Filter {
+    override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
         val req = request as HttpServletRequest
         val splitPath = req.requestURI.split("/")
-        val client = splitPath[2]
-        val toPath = "/" + splitPath.takeLast(splitPath.size - 3).joinToString("/")
+        val client = splitPath[1]
+        val toPath = "/" + splitPath.takeLast(splitPath.size - 1).joinToString("/")
+        if (client.startsWith("_")) {
+            chain.doFilter(request, response)
+            return
+        }
+
+        println("client $client")
+        println("path $toPath")
 
         req.parameterMap.entries.forEach {
             println(it.key + " : " + it.value.joinToString(", "))
@@ -59,20 +67,73 @@ class ForwarderFilter(private val template: SimpMessagingTemplate) : Filter {
         }
 
         val resFromSocket = Singletons.responseQue[reqId]
-
+//, val headers: HttpHeaders? = null
         res.status = resFromSocket!!.statusCode.value()
         resFromSocket.body?.let { res.writer.write(it) }
-
+        resFromSocket.headers?.let {
+            it.entries.forEach {en-> res.setHeader(en.key,en.value.joinToString(", ")) }
+        }
         res.flushBuffer()
         Singletons.requestInstance.remove(reqId)
         return
     }
-
 }
 
+//class ForwarderFilter(private val template: SimpMessagingTemplate) : Filter {
+//    override fun doFilter(
+//            request: ServletRequest,
+//            response: ServletResponse,
+//            chain: FilterChain) {
+//        val req = request as HttpServletRequest
+//        val splitPath = req.requestURI.split("/")
+//        val client = splitPath[2]
+//        val toPath = "/" + splitPath.takeLast(splitPath.size - 3).joinToString("/")
+//
+//        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 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)
+//
+//        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) {
+//
+//                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.value()
+//        resFromSocket.body?.let { res.writer.write(it) }
+//
+//        res.flushBuffer()
+//        Singletons.requestInstance.remove(reqId)
+//        return
+//    }
+//
+//}
+
 class RequestQue(val id: String, val requestBuilder: RequestBuilder, var responseObj: ResponseObj?)
 
-class ResponseObj(val id: String, val statusCode: HttpStatus, val body: String?)
+class ResponseObj(val id: String, val statusCode: HttpStatus, val body: String?, val headers: HttpHeaders? = null)
 
 class RequestTrigger(val id: String)
 
@@ -82,28 +143,29 @@ class RequestBuilder(val id: String, val path: String, val method: HttpMethod, v
     var queryString: MutableMap<String, Array<String>>? = null
 }
 
-
-@Component
-class FilterConfiguration(val template: SimpMessagingTemplate) {
-    @Bean
-    fun loggingFilter(): FilterRegistrationBean<ForwarderFilter> {
-        val registrationBean = FilterRegistrationBean<ForwarderFilter>()
-        registrationBean.filter = ForwarderFilter(template)
-        registrationBean.addUrlPatterns("/bridge/*")
-        return registrationBean
-    }
-
-}
+//
+//@Component
+//class FilterConfiguration(val template: SimpMessagingTemplate) {
+//    @Bean
+//    fun loggingFilter(): FilterRegistrationBean<ForwarderFilter> {
+//        val registrationBean = FilterRegistrationBean<ForwarderFilter>()
+//        registrationBean.filter = ForwarderFilter(template)
+//        registrationBean.addUrlPatterns("/bridge/*")
+//        return registrationBean
+//    }
+//
+//}
+//
 
 @RestController
 class BridgeRestController {
-    @PostMapping("/response")
+    @PostMapping("/_response")
     fun responseFromRest(@RequestBody message: ResponseObj): String {
         Singletons.responseQue[message.id] = message
         return "{\"success\":true}"
     }
 
-    @GetMapping("/request/{id}")
+    @GetMapping("/_request/{id}")
     fun getResponseObj(@PathVariable("id") id: String): RequestBuilder {
         if (Singletons.requestInstance[id] == null) {
             throw Exception("not found")

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

@@ -17,7 +17,7 @@ class WebSocketConfig : WebSocketMessageBrokerConfigurer {
     }
 
     override fun registerStompEndpoints(registry: StompEndpointRegistry) {
-        registry.addEndpoint("/websocket").setAllowedOrigins("*")
+        registry.addEndpoint("/_websocket").setAllowedOrigins("*")
     }
 
 }