mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
briar-headless: Inject a singleton ObjectMapper for JSON parsing
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.briarproject.briar.headless
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent
|
||||
@@ -113,4 +114,8 @@ internal class HeadlessModule(private val appDir: File) {
|
||||
}
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
internal fun provideObjectMapper() = ObjectMapper()
|
||||
|
||||
}
|
||||
|
||||
@@ -115,10 +115,9 @@ constructor(
|
||||
/**
|
||||
* Returns a String from the JSON field or throws [BadRequestResponse] if null or empty.
|
||||
*/
|
||||
fun Context.getFromJson(field: String) : String {
|
||||
fun Context.getFromJson(objectMapper: ObjectMapper, field: String) : String {
|
||||
try {
|
||||
// TODO use a static object mapper to avoid re-initializations
|
||||
val jsonNode = ObjectMapper().readTree(body())
|
||||
val jsonNode = objectMapper.readTree(body())
|
||||
if (!jsonNode.hasNonNull(field)) throw BadRequestResponse("'$field' missing in JSON")
|
||||
val result = jsonNode.get(field).asText()
|
||||
if (result == null || result.isEmpty()) throw BadRequestResponse("'$field' empty in JSON")
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.briarproject.briar.headless.blogs
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import io.javalin.BadRequestResponse
|
||||
import io.javalin.Context
|
||||
import org.briarproject.bramble.api.identity.IdentityManager
|
||||
@@ -21,6 +22,7 @@ constructor(
|
||||
private val blogManager: BlogManager,
|
||||
private val blogPostFactory: BlogPostFactory,
|
||||
private val identityManager: IdentityManager,
|
||||
private val objectMapper: ObjectMapper,
|
||||
private val clock: Clock
|
||||
) : BlogController {
|
||||
|
||||
@@ -35,7 +37,7 @@ constructor(
|
||||
}
|
||||
|
||||
override fun createPost(ctx: Context): Context {
|
||||
val text = ctx.getFromJson("text")
|
||||
val text = ctx.getFromJson(objectMapper, "text")
|
||||
if (utf8IsTooLong(text, MAX_BLOG_POST_TEXT_LENGTH))
|
||||
throw BadRequestResponse("Blog post text is too long")
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.briarproject.briar.headless.forums
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import io.javalin.BadRequestResponse
|
||||
import io.javalin.Context
|
||||
import org.briarproject.bramble.util.StringUtils.utf8IsTooLong
|
||||
@@ -14,14 +15,15 @@ import javax.inject.Singleton
|
||||
@Singleton
|
||||
internal class ForumControllerImpl
|
||||
@Inject
|
||||
constructor(private val forumManager: ForumManager) : ForumController {
|
||||
constructor(private val forumManager: ForumManager, private val objectMapper: ObjectMapper) :
|
||||
ForumController {
|
||||
|
||||
override fun list(ctx: Context): Context {
|
||||
return ctx.json(forumManager.forums.output())
|
||||
}
|
||||
|
||||
override fun create(ctx: Context): Context {
|
||||
val name = ctx.getFromJson("name")
|
||||
val name = ctx.getFromJson(objectMapper, "name")
|
||||
if (utf8IsTooLong(name, MAX_FORUM_NAME_LENGTH))
|
||||
throw BadRequestResponse("Forum name is too long")
|
||||
return ctx.json(forumManager.addForum(name).output())
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.briarproject.briar.headless.messaging
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import io.javalin.BadRequestResponse
|
||||
import io.javalin.Context
|
||||
import io.javalin.NotFoundResponse
|
||||
@@ -45,6 +46,7 @@ constructor(
|
||||
private val contactManager: ContactManager,
|
||||
private val webSocketController: WebSocketController,
|
||||
@DatabaseExecutor private val dbExecutor: Executor,
|
||||
private val objectMapper: ObjectMapper,
|
||||
private val clock: Clock
|
||||
) : MessagingController, EventListener {
|
||||
|
||||
@@ -60,7 +62,7 @@ constructor(
|
||||
override fun write(ctx: Context): Context {
|
||||
val contact = getContact(ctx)
|
||||
|
||||
val message = ctx.getFromJson("text")
|
||||
val message = ctx.getFromJson(objectMapper, "text")
|
||||
if (utf8IsTooLong(message, MAX_PRIVATE_MESSAGE_TEXT_LENGTH))
|
||||
throw BadRequestResponse("Message text is too long")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user