tighten the api

This commit is contained in:
gowthaman.b
2023-11-11 16:13:59 +05:30
parent 31388bae59
commit f35851d339
15 changed files with 247 additions and 143 deletions

View File

@@ -11,6 +11,7 @@ import com.restapi.domain.DataNotFoundException
import com.restapi.domain.Session.objectMapper
import com.restapi.domain.Session.redis
import com.restapi.domain.Session.setAuthorizedUser
import io.ebean.DataIntegrityException
import io.ebean.DuplicateKeyException
import io.javalin.Javalin
import io.javalin.apibuilder.ApiBuilder.*
@@ -19,6 +20,7 @@ import io.javalin.http.util.NaiveRateLimit
import io.javalin.http.util.RateLimitUtil
import io.javalin.json.JavalinJackson
import io.javalin.security.RouteRole
import org.jose4j.jwt.consumer.InvalidJwtException
import org.slf4j.LoggerFactory
import java.net.URI
import java.net.URLEncoder
@@ -108,8 +110,6 @@ fun main(args: Array<String>) {
?.replace("Bearer: ", "")
?.trim() ?: throw UnauthorizedResponse()
logger.warn("authToken = $authToken")
setAuthorizedUser(parseAuthToken(authToken = authToken))
}
@@ -139,34 +139,54 @@ fun main(args: Array<String>) {
}
.exception(DuplicateKeyException::class.java) { _, ctx ->
.exception(DuplicateKeyException::class.java) { e, ctx ->
logger.warn("while processing ${ctx.path()}, exception ${e.message}", e)
ctx.json(
mapOf(
"error" to "Duplicate Data"
)
).status(HttpStatus.CONFLICT)
}
.exception(DataNotFoundException::class.java) { _, ctx ->
.exception(DataIntegrityException::class.java) { e, ctx ->
logger.warn("while processing ${ctx.path()}, exception ${e.message}", e)
ctx.json(
mapOf(
"error" to "References Missing"
)
).status(HttpStatus.EXPECTATION_FAILED)
}
.exception(DataNotFoundException::class.java) { e, ctx ->
logger.warn("while processing ${ctx.path()}, exception ${e.message}", e)
ctx.json(
mapOf(
"error" to "Data Not Found"
)
).status(HttpStatus.NOT_FOUND)
}
.exception(IllegalArgumentException::class.java) { _, ctx ->
.exception(IllegalArgumentException::class.java) { e, ctx ->
logger.warn("while processing ${ctx.path()}, exception ${e.message}", e)
ctx.json(
mapOf(
"error" to "Incorrect Data"
)
).status(HttpStatus.BAD_REQUEST)
}
.exception(JsonMappingException::class.java) { _, ctx ->
.exception(JsonMappingException::class.java) { e, ctx ->
logger.warn("while processing ${ctx.path()}, exception ${e.message}", e)
ctx.json(
mapOf(
"error" to "Incorrect Data"
)
).status(HttpStatus.BAD_REQUEST)
}
.exception(InvalidJwtException::class.java) { e, ctx ->
logger.warn("while processing ${ctx.path()}, exception ${e.message}", e)
ctx.json(
mapOf(
"error" to "Login required"
)
).status(HttpStatus.UNAUTHORIZED)
}
.start(appConfig.portNumber())
}