mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 02:39:05 +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")
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.briarproject.briar.headless
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import io.javalin.Context
|
||||
import io.javalin.core.util.ContextUtil
|
||||
import io.mockk.mockk
|
||||
@@ -30,6 +31,8 @@ abstract class ControllerTest {
|
||||
private val response = mockk<HttpServletResponse>(relaxed = true)
|
||||
private val outputCtx = ContextUtil.init(request, response)
|
||||
|
||||
protected val objectMapper = ObjectMapper()
|
||||
|
||||
protected val group: Group = getGroup(getClientId(), 0)
|
||||
protected val author: Author = getAuthor()
|
||||
protected val localAuthor: LocalAuthor = getLocalAuthor()
|
||||
|
||||
@@ -23,7 +23,7 @@ internal class BlogControllerTest : ControllerTest() {
|
||||
private val blogPostFactory = mockk<BlogPostFactory>()
|
||||
|
||||
private val controller =
|
||||
BlogControllerImpl(blogManager, blogPostFactory, identityManager, clock)
|
||||
BlogControllerImpl(blogManager, blogPostFactory, identityManager, objectMapper, clock)
|
||||
|
||||
private val blog = Blog(group, author, false)
|
||||
private val parentId: MessageId? = null
|
||||
|
||||
@@ -16,7 +16,7 @@ internal class ForumControllerTest : ControllerTest() {
|
||||
|
||||
private val forumManager = mockk<ForumManager>()
|
||||
|
||||
private val controller = ForumControllerImpl(forumManager)
|
||||
private val controller = ForumControllerImpl(forumManager, objectMapper)
|
||||
|
||||
private val forum = Forum(group, getRandomString(5), getRandomBytes(5))
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ internal class MessagingControllerImplTest : ControllerTest() {
|
||||
contactManager,
|
||||
webSocketController,
|
||||
dbExecutor,
|
||||
objectMapper,
|
||||
clock
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user