package co.id.datacomsolusindo.ipphonebridge import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.databind.ObjectMapper import org.apache.commons.io.IOUtils import org.springframework.http.MediaType 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.util.HashMap import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse @RestController class BridgeRestController { @PostMapping("/_response/{id}") fun responseFromRest(@PathVariable("id") id: String, req: HttpServletRequest): String { val objectMapper = ObjectMapper() val typeRef: TypeReference>> = object : TypeReference>>() {} val body = IOUtils.toByteArray(req.getPart("body").inputStream) Singletons.responseQue.getOrPut(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 } ) } return "{\"success\":true}" } @GetMapping("/_request/{id}") fun getResponseObj(@PathVariable("id") id: String): RequestBuilder { if (Singletons.requestInstance.getOrElse(id) { null } == null) { throw Exception("not found") } val rb = Singletons.requestInstance.getOrElse(id) { null }?.requestBuilder if (rb == null) { throw Exception("not found") } else { return rb } } private val logSetting = "\n" + "\n" + " \n" + " %d %p [%c{1}] %m%n\n" + " log\n" + " \n" + "\n" + " \n" + " \n" + " \n" + " \n" + "\n" + " \n" + " \n" + " \${LOG_PATTERN}\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "\n" + " \n" + " \n" + " \${LOG_PATTERN}\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " [CLIENT-APPENDER]\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " [CLIENT-LOGGER] \n" + " \n" + "" @GetMapping("/log-setting.xml", produces = [MediaType.APPLICATION_XML_VALUE]) fun getLogSetting(servletResponse: HttpServletResponse): String { val appender = ClientHolder.get().entries.joinToString { "\n" + " \n" + " \${LOG_PATTERN}\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " " } val logger = ClientHolder.get().entries.joinToString { "\n" + " \n" + " \n" + " " } return logSetting.replace("[CLIENT-APPENDER]", appender).replace("[CLIENT-LOGGER]", logger) } @GetMapping("/clientStat") fun getClientData(): Map { return ClientHolder.get() } @GetMapping("/clientStat/{id}") fun getClientDataOne(@PathVariable("id") id: String): Client? { return ClientHolder.get()[id] } }