ソースを参照

change _response to multipart request

herlans 4 年 前
コミット
cd010fc409

+ 2 - 0
conf/general.ini

@@ -0,0 +1,2 @@
+[server]
+port = 9090

+ 12 - 3
pom.xml

@@ -5,7 +5,7 @@
 	<parent>
 		<groupId>org.springframework.boot</groupId>
 		<artifactId>spring-boot-starter-parent</artifactId>
-		<version>2.2.0.RELEASE</version>
+		<version>2.3.0.RELEASE</version>
 		<relativePath/> <!-- lookup parent from repository -->
 	</parent>
 	<groupId>co.id.datacomsolusindo</groupId>
@@ -16,7 +16,7 @@
 
 	<properties>
 		<java.version>1.8</java.version>
-		<kotlin.version>1.3.60</kotlin.version>
+		<kotlin.version>1.3.72</kotlin.version>
 	</properties>
 
 	<dependencies>
@@ -40,7 +40,16 @@
 			<groupId>org.jetbrains.kotlin</groupId>
 			<artifactId>kotlin-stdlib-jdk8</artifactId>
 		</dependency>
-
+		<dependency>
+			<groupId>org.ini4j</groupId>
+			<artifactId>ini4j</artifactId>
+			<version>0.5.4</version>
+		</dependency>
+		<dependency>
+			<groupId>commons-io</groupId>
+			<artifactId>commons-io</artifactId>
+			<version>2.6</version>
+		</dependency>
 		<dependency>
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-test</artifactId>

+ 85 - 21
src/main/kotlin/co/id/datacomsolusindo/ipphonebridge/BridgeFIlter.kt

@@ -1,25 +1,56 @@
 package co.id.datacomsolusindo.ipphonebridge
 
-import org.springframework.boot.web.servlet.FilterRegistrationBean
-import org.springframework.context.annotation.Bean
+import com.fasterxml.jackson.core.type.TypeReference
+import com.fasterxml.jackson.databind.ObjectMapper
+import org.apache.commons.io.IOUtils
+import org.springframework.core.Ordered
 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
 import org.springframework.stereotype.Component
-import org.springframework.web.bind.annotation.*
+import org.springframework.web.bind.annotation.GetMapping
+import org.springframework.web.bind.annotation.PathVariable
+import org.springframework.web.bind.annotation.PostMapping
+import org.springframework.web.bind.annotation.RestController
+import java.io.IOException
+import java.io.InputStream
 import java.io.Serializable
+import java.net.InetAddress
 import java.util.*
 import java.util.concurrent.TimeUnit
-import javax.servlet.Filter
-import javax.servlet.FilterChain
-import javax.servlet.ServletRequest
-import javax.servlet.ServletResponse
+import javax.servlet.*
 import javax.servlet.http.HttpServletRequest
 import javax.servlet.http.HttpServletResponse
 import kotlin.streams.toList
 
+
+@Component
+@Order(Ordered.HIGHEST_PRECEDENCE)
+class SimpleCORSFilter : Filter {
+    @Throws(ServletException::class)
+    override fun init(fc: FilterConfig?) {
+    }
+
+    @Throws(IOException::class, ServletException::class)
+    override fun doFilter(req: ServletRequest, resp: ServletResponse,
+                          chain: FilterChain) {
+        val response = resp as HttpServletResponse
+        val request = req as HttpServletRequest
+        response.setHeader("Access-Control-Allow-Origin", "*")
+        response.setHeader("Access-Control-Allow-Methods", "PATCH,POST,GET,OPTIONS,DELETE")
+        response.setHeader("Access-Control-Max-Age", "3600")
+        response.setHeader("Access-Control-Allow-Headers", "x-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN")
+        if ("OPTIONS".equals(request.method, ignoreCase = true)) {
+            response.status = HttpServletResponse.SC_OK
+        } else {
+            chain.doFilter(req, resp)
+        }
+    }
+
+    override fun destroy() {}
+}
+
 @Component
 @Order(1)
 class BridgeFilter(private val template: SimpMessagingTemplate) : Filter {
@@ -36,9 +67,10 @@ class BridgeFilter(private val template: SimpMessagingTemplate) : Filter {
         println("client $client")
         println("path $toPath")
 
-        req.parameterMap.entries.forEach {
-            println(it.key + " : " + it.value.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())
 
@@ -67,23 +99,32 @@ class BridgeFilter(private val template: SimpMessagingTemplate) : Filter {
         }
 
         val resFromSocket = Singletons.responseQue[reqId]
-        res.status = resFromSocket!!.statusCode.value()
+        res.status = resFromSocket!!.statusCode
         resFromSocket.headers?.let {
-            it.entries.forEach {en->
-                println(en.key + " : " + en.value.joinToString (","))
-                res.setHeader(en.key,en.value.joinToString(","))
+            it.entries.forEach { en ->
+//                println(en.key + " : " + en.value.joinToString(","))
+                res.setHeader(en.key, en.value.joinToString(","))
             }
         }
-        resFromSocket.body?.let { res.writer.write(it) }
+        res.outputStream.write(IOUtils.toByteArray(resFromSocket.body))
+//        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?)
+@Component
+@Order(Ordered.LOWEST_PRECEDENCE)
+class RequestFilter : Filter {
+    override fun doFilter(sreq: ServletRequest?, sres: ServletResponse?, fc: FilterChain?) {
+        val req = sreq as HttpServletRequest
+        println("Request With " + req.getHeader("User-Agent") + " Address " + InetAddress.getLocalHost().hostAddress + " To " + req.requestURL.toString() + "?" + req.queryString)
+        fc!!.doFilter(sreq, sres)
+    }
+}
 
-class ResponseObj(val id: String, val statusCode: HttpStatus, val body: String?, val headers: HttpHeaders? = null)
+class RequestQue(val id: String, val requestBuilder: RequestBuilder, var responseObj: Resp?)
 
 class RequestTrigger(val id: String)
 
@@ -92,11 +133,34 @@ 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)
+
+object Singletons {
+    val responseQue: MutableMap<String, Resp> by lazy { mutableMapOf<String, Resp>() }
+    val requestInstance: MutableMap<String, RequestQue> by lazy { mutableMapOf<String, RequestQue>() }
+}
+
 @RestController
 class BridgeRestController {
-    @PostMapping("/_response")
-    fun responseFromRest(@RequestBody message: ResponseObj): String {
-        Singletons.responseQue[message.id] = message
+    @PostMapping("/_response/{id}")
+    fun responseFromRest(@PathVariable("id") id: String, req: HttpServletRequest): String {
+        val objectMapper = ObjectMapper()
+        val typeRef: TypeReference<HashMap<String, Array<String>>> = object : TypeReference<HashMap<String, Array<String>>>() {}
+        Singletons.responseQue[id] = Resp(
+                req.getPart("body").inputStream,
+               String(IOUtils.toByteArray(req.getPart("status").inputStream)).toInt(),
+                if (req.getPart("header") != null) {
+                    objectMapper.readValue(String(IOUtils.toByteArray(req.getPart("header").inputStream)), typeRef)
+                } else {
+                    null
+                }
+        )
+
+//
+//        IOUtils.toByteArray(req.getPart("body").inputStream)
+//        println(req.getPart("header"))
+//        println(req.getPart("status"))
+
         return "{\"success\":true}"
     }
 

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

@@ -1,11 +1,15 @@
 package co.id.datacomsolusindo.ipphonebridge
 
+import org.ini4j.Wini
+import org.springframework.boot.SpringApplication
 import org.springframework.boot.autoconfigure.SpringBootApplication
 import org.springframework.boot.runApplication
 import org.springframework.messaging.simp.SimpMessagingTemplate
 import org.springframework.scheduling.annotation.EnableScheduling
 import org.springframework.scheduling.annotation.Scheduled
 import org.springframework.stereotype.Component
+import java.io.File
+import java.util.*
 
 
 @SpringBootApplication
@@ -13,7 +17,12 @@ import org.springframework.stereotype.Component
 class IpPhoneBridgeApplication
 
 fun main(args: Array<String>) {
-    runApplication<IpPhoneBridgeApplication>(*args)
+    val properties = Properties()
+    val ini = Wini(File("conf/general.ini"))
+    properties["server.port"] = ini.get("server", "port")
+    val sApp = SpringApplication(IpPhoneBridgeApplication::class.java)
+    sApp.setDefaultProperties(properties)
+    sApp.run(*args)
 }
 
 @Component
@@ -27,7 +36,4 @@ class ScheduledMessage(val template: SimpMessagingTemplate) {
 
 }
 
-object Singletons {
-    val responseQue: MutableMap<String, ResponseObj> by lazy { mutableMapOf<String, ResponseObj>() }
-    val requestInstance: MutableMap<String, RequestQue> by lazy { mutableMapOf<String, RequestQue>() }
-}
+

+ 1 - 2
src/main/resources/application.yml

@@ -1,2 +1 @@
-server:
-  port: 9090
+