优化性能

This commit is contained in:
tursom 2019-11-30 15:27:47 +08:00
parent a775ba1b5e
commit f3ac61c346
3 changed files with 53 additions and 33 deletions

View File

@ -2,5 +2,5 @@ dependencies {
compileOnly 'com.alibaba:fastjson:1.2.62'
compileOnly group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
compileOnly group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.10.1'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.10.1'
compileOnly group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.10.1'
}

View File

@ -20,4 +20,8 @@ class JsonWorkerImpl : JsonWorker {
override fun <T> fromJson(json: String, clazz: Class<T>): T? =
gson?.fromJson(json, clazz) ?: jackson?.readValue(json, clazz)
override fun toString(): String {
return "JsonWorkerImpl(gson=$gson, jackson=$jackson)"
}
}

View File

@ -128,18 +128,21 @@ open class RoutedHttpHandler<T : HttpContent, in E : ExceptionContent>(
}
routes.forEach { route ->
log?.info("method route {} mapped to {}", route, method)
router[safeRoute(route)] = handler@{ content ->
if (method.parameterTypes.isEmpty()) {
val result = method(obj) ?: return@handler
when {
method.getAnnotation(Html::class.java) != null -> finishHtml(result, content)
method.getAnnotation(Text::class.java) != null -> finishText(result, content)
method.getAnnotation(Json::class.java) != null -> finishJson(result, content)
else -> autoReturn(result, content)
}
} else {
method(obj, content)
router[safeRoute(route)] = if (method.parameterTypes.isEmpty()) when {
method.getAnnotation(Html::class.java) != null -> { content ->
method(obj)?.let { result -> finishHtml(result, content) }
}
method.getAnnotation(Text::class.java) != null -> { content ->
method(obj)?.let { result -> finishText(result, content) }
}
method.getAnnotation(Json::class.java) != null -> { content ->
method(obj)?.let { result -> finishJson(result, content) }
}
else -> { content ->
method(obj)?.let { result -> autoReturn(result, content) }
}
} else { content ->
method(obj, content)
}
}
}
@ -173,42 +176,55 @@ open class RoutedHttpHandler<T : HttpContent, in E : ExceptionContent>(
private fun safeRoute(route: String) = if (route.first() == '/') route else "/$route"
private fun autoReturn(result: Any, content: HttpContent) = when (result) {
is String -> content.finishText(result.toByteArray())
is ByteArray -> content.finishText(result)
is File -> {
content.autoContextType(result.name)
content.finishFile(result)
private fun autoReturn(result: Any, content: HttpContent) {
log?.debug("{}: autoReturn: {}", content.clientIp, result)
when (result) {
is String -> content.finishText(result.toByteArray())
is ByteArray -> content.finishText(result)
is File -> {
content.autoContextType(result.name)
content.finishFile(result)
}
is RandomAccessFile -> {
content.finishFile(result)
}
is Chunked -> content.finishChunked(result)
else -> finishJson(result, content)
}
is RandomAccessFile -> content.finishFile(result)
is Chunked -> content.finishChunked(result)
else -> finishJson(result, content)
}
private fun finishHtml(result: Any, content: HttpContent) = when (result) {
is ByteBuffer -> content.finishHtml(result)
is ByteArray -> content.finishHtml(result)
is String -> content.finishHtml(result.toByteArray())
else -> content.finishHtml(result.toString().toByteArray())
private fun finishHtml(result: Any, content: HttpContent) {
log?.debug("{}: finishHtml: {}", content.clientIp, result)
when (result) {
is ByteBuffer -> content.finishHtml(result)
is ByteArray -> content.finishHtml(result)
is String -> content.finishHtml(result.toByteArray())
else -> content.finishHtml(result.toString().toByteArray())
}
}
private fun finishText(result: Any, content: HttpContent) = when (result) {
is ByteBuffer -> content.finishText(result)
is ByteArray -> content.finishText(result)
is String -> content.finishText(result.toByteArray())
else -> content.finishText(result.toString().toByteArray())
private fun finishText(result: Any, content: HttpContent) {
log?.debug("{}: finishText: {}", content.clientIp, result)
when (result) {
is ByteBuffer -> content.finishText(result)
is ByteArray -> content.finishText(result)
is String -> content.finishText(result.toByteArray())
else -> content.finishText(result.toString().toByteArray())
}
}
private fun finishJson(result: Any, content: HttpContent) {
log?.debug("{}: finishJson: {}", content.clientIp, result)
when (result) {
is ByteBuffer -> content.finishJson(result)
is ByteArray -> content.finishJson(result)
is String -> content.finishJson("\"$result\"".toByteArray())
is Byte, Short, Int, Long, Float, Double, Boolean -> content.finishJson(result.toString().toByteArray())
else -> {
val json = json?.toJson(result)?.toByteArray()
val json = json?.toJson(result)
log?.debug("{}: finishJson: generate json: {}", content.clientIp, json)
if (json != null) {
content.finishJson(json)
content.finishJson(json.toByteArray())
} else {
content.finishText(result.toString().toByteArray())
}