|
@@ -49,14 +49,19 @@ class SimpleCORSFilter : Filter {
|
|
|
}
|
|
|
|
|
|
@Throws(IOException::class, ServletException::class)
|
|
|
- override fun doFilter(req: ServletRequest, resp: ServletResponse,
|
|
|
- chain: FilterChain) {
|
|
|
+ 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,PUT,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")
|
|
|
+ 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 {
|
|
@@ -78,10 +83,10 @@ class BridgeFilter(private val template: SimpMessagingTemplate, val resourceLoad
|
|
|
val client = splitPath[1]
|
|
|
val toPath = "/" + splitPath.takeLast(splitPath.size - 1).joinToString("/")
|
|
|
if (
|
|
|
- client.startsWith("_")
|
|
|
- || client.startsWith("resources")
|
|
|
- || client.startsWith("clientStat")
|
|
|
- || client.startsWith("actuator")
|
|
|
+ client.startsWith("_")
|
|
|
+ || client.startsWith("resources")
|
|
|
+ || client.startsWith("clientStat")
|
|
|
+ || client.startsWith("actuator")
|
|
|
) {
|
|
|
chain.doFilter(request, response)
|
|
|
return
|
|
@@ -99,10 +104,10 @@ class BridgeFilter(private val template: SimpMessagingTemplate, val resourceLoad
|
|
|
rb.queryString = req.queryString
|
|
|
if (req.contentType != null && req.contentType.toLowerCase().indexOf("multipart/form-data") > -1) {
|
|
|
rb.parts =
|
|
|
- req.parts.map {
|
|
|
- val fb64 = Base64.getEncoder().encodeToString(it.inputStream.readBytes())
|
|
|
- FilePart(it.name, it.submittedFileName, fb64)
|
|
|
- }.toMutableList()
|
|
|
+ req.parts.map {
|
|
|
+ val fb64 = Base64.getEncoder().encodeToString(it.inputStream.readBytes())
|
|
|
+ FilePart(it.name, it.submittedFileName, fb64)
|
|
|
+ }.toMutableList()
|
|
|
} else {
|
|
|
val body = req.reader.lines().toList().joinToString(System.lineSeparator())
|
|
|
rb.body = body
|
|
@@ -131,7 +136,10 @@ class BridgeFilter(private val template: SimpMessagingTemplate, val resourceLoad
|
|
|
val partialID = UUID.randomUUID().toString()
|
|
|
val st2 = System.nanoTime()
|
|
|
partStr.forEachIndexed { idx, it ->
|
|
|
- template.convertAndSend("/topic/partial/$client", PartialData(partialID, partStr.size, idx + 1, it, st2))
|
|
|
+ template.convertAndSend(
|
|
|
+ "/topic/partial/$client",
|
|
|
+ PartialData(partialID, partStr.size, idx + 1, it, st2)
|
|
|
+ )
|
|
|
}
|
|
|
|
|
|
|
|
@@ -145,8 +153,10 @@ class BridgeFilter(private val template: SimpMessagingTemplate, val resourceLoad
|
|
|
res.flushBuffer()
|
|
|
Singletons.requestInstance.remove(reqId)
|
|
|
val endTime = System.nanoTime()
|
|
|
- val duration = (endTime - startTime).toDouble() / 1000000 //divide by 1000000 to get milliseconds.
|
|
|
- LogManager.getLogger("client.$client").info("Request failed, No Response. Time: $duration UA: " + req.getHeader("User-Agent") + " From: " + InetAddress.getLocalHost().hostAddress + " To: " + req.requestURL.toString() + "?" + req.queryString)
|
|
|
+ val duration =
|
|
|
+ (endTime - startTime).toDouble() / 1000000 //divide by 1000000 to get milliseconds.
|
|
|
+ LogManager.getLogger("client.$client")
|
|
|
+ .info("Request failed, No Response. Time: $duration UA: " + req.getHeader("User-Agent") + " From: " + InetAddress.getLocalHost().hostAddress + " To: " + req.requestURL.toString() + "?" + req.queryString)
|
|
|
|
|
|
ClientHolder.addFailedRequest(client)
|
|
|
return
|
|
@@ -158,10 +168,10 @@ class BridgeFilter(private val template: SimpMessagingTemplate, val resourceLoad
|
|
|
res.status = resFromSocket!!.statusCode
|
|
|
resFromSocket.headers?.let {
|
|
|
it.entries
|
|
|
- .filter { f -> resFromSocket.statusCode < 400 || (resFromSocket.statusCode >= 400 && f.key == "Content-Type") }
|
|
|
- .forEach { en ->
|
|
|
- res.setHeader(en.key, en.value.joinToString(","))
|
|
|
- }
|
|
|
+ .filter { f -> resFromSocket.statusCode < 400 || (resFromSocket.statusCode >= 400 && f.key == "Content-Type") }
|
|
|
+ .forEach { en ->
|
|
|
+ res.setHeader(en.key, en.value.joinToString(","))
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
resFromSocket.body?.let {
|
|
@@ -174,7 +184,8 @@ class BridgeFilter(private val template: SimpMessagingTemplate, val resourceLoad
|
|
|
val endTime = System.nanoTime()
|
|
|
|
|
|
val duration = (endTime - startTime).toDouble() / 1000000 //divide by 1000000 to get milliseconds.
|
|
|
- LogManager.getLogger("client.$client").info("Request Success. Time: $duration UA: " + req.getHeader("User-Agent") + " From: " + req.remoteAddr + " To: " + req.requestURL.toString() + "?" + req.queryString)
|
|
|
+ LogManager.getLogger("client.$client")
|
|
|
+ .info("Request Success. Time: $duration UA: " + req.getHeader("User-Agent") + " From: " + req.remoteAddr + " To: " + req.requestURL.toString() + "?" + req.queryString)
|
|
|
|
|
|
ClientHolder.addSuccessRequest(client, duration)
|
|
|
return
|
|
@@ -188,7 +199,8 @@ class BridgeFilter(private val template: SimpMessagingTemplate, val resourceLoad
|
|
|
Singletons.requestInstance.remove(reqId)
|
|
|
val endTime = System.nanoTime()
|
|
|
val duration = (endTime - startTime).toDouble() / 1000000 //divide by 1000000 to get milliseconds.
|
|
|
- LogManager.getLogger("client.$client").info("Request failed, Unknown error. Time: $duration UA: " + req.getHeader("User-Agent") + " From: " + InetAddress.getLocalHost().hostAddress + " To: " + req.requestURL.toString() + "?" + req.queryString)
|
|
|
+ LogManager.getLogger("client.$client")
|
|
|
+ .info("Request failed, Unknown error. Time: $duration UA: " + req.getHeader("User-Agent") + " From: " + InetAddress.getLocalHost().hostAddress + " To: " + req.requestURL.toString() + "?" + req.queryString)
|
|
|
ClientHolder.addFailedRequest(client)
|
|
|
return
|
|
|
}
|
|
@@ -210,7 +222,7 @@ class BridgeFilter(private val template: SimpMessagingTemplate, val resourceLoad
|
|
|
return f.listFiles()?.filter { it.isDirectory }?.map { it.name }?.map {
|
|
|
Pair(it, resourceLoader.getResource("file:web/$it/$search"))
|
|
|
}?.filter { it.second.exists() && it.second.file.isFile }?.map { "${it.first}/$search" }?.firstOrNull()
|
|
|
- ?: isResourceAvailable(path, itr + 1)
|
|
|
+ ?: isResourceAvailable(path, itr + 1)
|
|
|
}
|
|
|
return null
|
|
|
}
|
|
@@ -221,7 +233,12 @@ class PartialData(val id: String, val total: Int, val idx: Int, val data: String
|
|
|
|
|
|
//class RequestTrigger(val id: String)
|
|
|
|
|
|
-class RequestBuilder(val id: String, val path: String, val method: HttpMethod, var headers: MutableMap<String, String>) : Serializable {
|
|
|
+class RequestBuilder(
|
|
|
+ val id: String,
|
|
|
+ val path: String,
|
|
|
+ val method: HttpMethod,
|
|
|
+ var headers: MutableMap<String, String>
|
|
|
+) : Serializable {
|
|
|
var body: String? = null
|
|
|
var queryString: String? = null
|
|
|
var parts: MutableList<FilePart>? = null
|
|
@@ -265,7 +282,13 @@ object Singletons {
|
|
|
val buildSocketChunkData: MutableMap<String, ChunkCollector> by lazy { mutableMapOf<String, ChunkCollector>() }
|
|
|
}
|
|
|
|
|
|
-class SocketChunkData(val body: ByteArray?, val header: HttpHeaders?, val status: Int, val part: Int, val totalPart: Int)
|
|
|
+class SocketChunkData(
|
|
|
+ val body: ByteArray?,
|
|
|
+ val header: HttpHeaders?,
|
|
|
+ val status: Int,
|
|
|
+ val part: Int,
|
|
|
+ val totalPart: Int
|
|
|
+)
|
|
|
|
|
|
class ChunkCollector(private val id: String) {
|
|
|
private val listSocketChunk = mutableListOf<SocketChunkData>()
|
|
@@ -285,9 +308,9 @@ class ChunkCollector(private val id: String) {
|
|
|
var bd = ByteArray(0)
|
|
|
bodyList.forEach { bd += it }
|
|
|
Singletons.responseQue[id] = Resp(
|
|
|
- bd,
|
|
|
- dt.status,
|
|
|
- dt.header?.entries?.map { Pair(it.key, it.value.toTypedArray()) }?.toMap()
|
|
|
+ bd,
|
|
|
+ dt.status,
|
|
|
+ dt.header?.entries?.map { Pair(it.key, it.value.toTypedArray()) }?.toMap()
|
|
|
)
|
|
|
this.cancel()
|
|
|
}
|
|
@@ -302,6 +325,13 @@ class ChunkCollector(private val id: String) {
|
|
|
|
|
|
@Controller
|
|
|
class SocketChecker {
|
|
|
+// @MessageMapping("/notification/{id}")
|
|
|
+// @SendTo("/topic/notification/{id}")
|
|
|
+// @Throws(Exception::class)
|
|
|
+// fun notification(@DestinationVariable("id") id: String, message: MutableMap<*, *>): MutableMap<*, *> {
|
|
|
+// return mutableMapOf(Pair("Hi", "I'am Ok"))
|
|
|
+// }
|
|
|
+
|
|
|
@MessageMapping("/hi")
|
|
|
@SendTo("/topic/healthCheck")
|
|
|
@Throws(Exception::class)
|
|
@@ -318,9 +348,9 @@ class SocketChecker {
|
|
|
|
|
|
if (message.totalPart == 1) {
|
|
|
Singletons.responseQue[id] = Resp(
|
|
|
- message.body,
|
|
|
- message.status,
|
|
|
- message.header?.entries?.map { Pair(it.key, it.value.toTypedArray()) }?.toMap()
|
|
|
+ message.body,
|
|
|
+ message.status,
|
|
|
+ message.header?.entries?.map { Pair(it.key, it.value.toTypedArray()) }?.toMap()
|
|
|
)
|
|
|
} else {
|
|
|
var sc = Singletons.buildSocketChunkData[id]
|
|
@@ -344,16 +374,17 @@ class BridgeRestController {
|
|
|
@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>>>() {}
|
|
|
+ val typeRef: TypeReference<HashMap<String, Array<String>>> =
|
|
|
+ object : TypeReference<HashMap<String, Array<String>>>() {}
|
|
|
val body = IOUtils.toByteArray(req.getPart("body").inputStream)
|
|
|
Singletons.responseQue[id] = Resp(
|
|
|
- body,
|
|
|
- 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
|
|
|
- }
|
|
|
+ body,
|
|
|
+ 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
|
|
|
+ }
|
|
|
)
|
|
|
|
|
|
return "{\"success\":true}"
|
|
@@ -383,11 +414,11 @@ class BridgeRestController {
|
|
|
class MvcConfig : WebMvcConfigurer {
|
|
|
override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
|
|
|
registry
|
|
|
- .addResourceHandler("resources/**")
|
|
|
- .addResourceLocations("file:web/")
|
|
|
- .setCachePeriod(3600 * 24 * 30)
|
|
|
- .resourceChain(true)
|
|
|
- .addResolver(PathResourceResolver())
|
|
|
+ .addResourceHandler("resources/**")
|
|
|
+ .addResourceLocations("file:web/")
|
|
|
+ .setCachePeriod(3600 * 24 * 30)
|
|
|
+ .resourceChain(true)
|
|
|
+ .addResolver(PathResourceResolver())
|
|
|
}
|
|
|
}
|
|
|
|