BridgeRestController.kt 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package co.id.datacomsolusindo.ipphonebridge
  2. import com.fasterxml.jackson.core.type.TypeReference
  3. import com.fasterxml.jackson.databind.ObjectMapper
  4. import org.apache.commons.io.IOUtils
  5. import org.springframework.http.MediaType
  6. import org.springframework.web.bind.annotation.GetMapping
  7. import org.springframework.web.bind.annotation.PathVariable
  8. import org.springframework.web.bind.annotation.PostMapping
  9. import org.springframework.web.bind.annotation.RestController
  10. import java.util.HashMap
  11. import javax.servlet.http.HttpServletRequest
  12. import javax.servlet.http.HttpServletResponse
  13. @RestController
  14. class BridgeRestController {
  15. @PostMapping("/_response/{id}")
  16. fun responseFromRest(@PathVariable("id") id: String, req: HttpServletRequest): String {
  17. val objectMapper = ObjectMapper()
  18. val typeRef: TypeReference<HashMap<String, Array<String>>> =
  19. object : TypeReference<HashMap<String, Array<String>>>() {}
  20. val body = IOUtils.toByteArray(req.getPart("body").inputStream)
  21. Singletons.responseQue.getOrPut(id) {
  22. Resp(
  23. body,
  24. String(IOUtils.toByteArray(req.getPart("status").inputStream)).toInt(),
  25. if (req.getPart("header") != null) {
  26. objectMapper.readValue(String(IOUtils.toByteArray(req.getPart("header").inputStream)), typeRef)
  27. } else {
  28. null
  29. }
  30. )
  31. }
  32. return "{\"success\":true}"
  33. }
  34. @GetMapping("/_request/{id}")
  35. fun getResponseObj(@PathVariable("id") id: String): RequestBuilder {
  36. if (Singletons.requestInstance.getOrElse(id) { null } == null) {
  37. throw Exception("not found")
  38. }
  39. val rb = Singletons.requestInstance.getOrElse(id) { null }?.requestBuilder
  40. if (rb == null) {
  41. throw Exception("not found")
  42. } else {
  43. return rb
  44. }
  45. }
  46. private val logSetting = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
  47. "<Configuration status=\"INFO\" name=\"MyApp\" packages=\"\">\n" +
  48. " <Properties>\n" +
  49. " <Property name=\"LOG_PATTERN\">%d %p [%c{1}] %m%n</Property>\n" +
  50. " <Property name=\"APP_LOG_ROOT\">log</Property>\n" +
  51. " </Properties>\n" +
  52. "\n" +
  53. " <Appenders>\n" +
  54. " <Console name=\"Console\" target=\"SYSTEM_OUT\" follow=\"true\">\n" +
  55. " <PatternLayout pattern=\"\${LOG_PATTERN}\"/>\n" +
  56. " </Console>\n" +
  57. "\n" +
  58. " <RollingFile name=\"RollingFile\" fileName=\"\${APP_LOG_ROOT}/app/app.log\"\n" +
  59. " filePattern=\"\${APP_LOG_ROOT}/app/\$\${date:yyyy - MM}/app-%d{yyyy-MM-dd}-%i.log.zip\">\n" +
  60. " <PatternLayout>\n" +
  61. " <Pattern>\${LOG_PATTERN}</Pattern>\n" +
  62. " </PatternLayout>\n" +
  63. " <LevelRangeFilter minLevel=\"INFO\" maxLevel=\"INFO\" onMatch=\"ACCEPT\" onMismatch=\"DENY\"/>\n" +
  64. " <Policies>\n" +
  65. " <TimeBasedTriggeringPolicy/>\n" +
  66. " <SizeBasedTriggeringPolicy size=\"10 MB\"/>\n" +
  67. " </Policies>\n" +
  68. " </RollingFile>\n" +
  69. "\n" +
  70. " <RollingFile name=\"RollingFileError\" fileName=\"\${APP_LOG_ROOT}/error/app-error.log\"\n" +
  71. " filePattern=\"\${APP_LOG_ROOT}/error/\$\${date:yyyy - MM}/app-error-%d{yyyy-MM-dd}-%i.log.zip\">\n" +
  72. " <PatternLayout>\n" +
  73. " <Pattern>\${LOG_PATTERN}</Pattern>\n" +
  74. " </PatternLayout>\n" +
  75. " <LevelRangeFilter minLevel=\"FATAL\" maxLevel=\"WARN\" onMatch=\"ACCEPT\" onMismatch=\"DENY\"/>\n" +
  76. " <Policies>\n" +
  77. " <TimeBasedTriggeringPolicy/>\n" +
  78. " <SizeBasedTriggeringPolicy size=\"10 MB\"/>\n" +
  79. " </Policies>\n" +
  80. " </RollingFile> \n" +
  81. " [CLIENT-APPENDER]\n" +
  82. " </Appenders>\n" +
  83. " <Loggers>\n" +
  84. " <Root level=\"INFO\">\n" +
  85. " <AppenderRef ref=\"Console\"/>\n" +
  86. " <AppenderRef ref=\"RollingFile\"/>\n" +
  87. " <AppenderRef ref=\"RollingFileError\"/>\n" +
  88. " </Root>\n" +
  89. " <Logger name=\"co.id.datacomsolusindo.ipphonebridge\" additivity=\"false\" level=\"INFO\">\n" +
  90. " <AppenderRef ref=\"RollingFile\"/>\n" +
  91. " <AppenderRef ref=\"Console\"/>\n" +
  92. " </Logger>\n" +
  93. " [CLIENT-LOGGER] \n" +
  94. " </Loggers>\n" +
  95. "</Configuration>"
  96. @GetMapping("/log-setting.xml", produces = [MediaType.APPLICATION_XML_VALUE])
  97. fun getLogSetting(servletResponse: HttpServletResponse): String {
  98. val appender = ClientHolder.get().entries.joinToString {
  99. "<RollingFile name=\"ClientAct${it.value.number}\" fileName=\"\${APP_LOG_ROOT}/client/${it.value.number}/act.log\"\n" +
  100. " filePattern=\"\${APP_LOG_ROOT}/client/${it.value.number}/\$\${date:yyyy - MM}/act-%d{yyyy-MM-dd}-%i.log.zip\">\n" +
  101. " <PatternLayout>\n" +
  102. " <Pattern>\${LOG_PATTERN}</Pattern>\n" +
  103. " </PatternLayout>\n" +
  104. " <LevelRangeFilter minLevel=\"INFO\" maxLevel=\"INFO\" onMatch=\"ACCEPT\" onMismatch=\"DENY\"/>\n" +
  105. " <Policies>\n" +
  106. " <TimeBasedTriggeringPolicy/>\n" +
  107. " <SizeBasedTriggeringPolicy size=\"10 MB\"/>\n" +
  108. " </Policies>\n" +
  109. " </RollingFile>"
  110. }
  111. val logger = ClientHolder.get().entries.joinToString {
  112. "<Logger name=\"client-${it.value.number}\" additivity=\"false\" level=\"INFO\">\n" +
  113. " <AppenderRef ref=\"ClientAct${it.value.number}\"/>\n" +
  114. " <AppenderRef ref=\"Console\"/>\n" +
  115. " </Logger> "
  116. }
  117. return logSetting.replace("[CLIENT-APPENDER]", appender).replace("[CLIENT-LOGGER]", logger)
  118. }
  119. @GetMapping("/clientStat")
  120. fun getClientData(): Map<String, Client> {
  121. return ClientHolder.get()
  122. }
  123. @GetMapping("/clientStat/{id}")
  124. fun getClientDataOne(@PathVariable("id") id: String): Client? {
  125. return ClientHolder.get()[id]
  126. }
  127. }