mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
briar-headless: POST text as JSON in body instead of form parameter
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
package org.briarproject.briar.headless
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import io.javalin.BadRequestResponse
|
||||
import io.javalin.Context
|
||||
import io.javalin.Javalin
|
||||
import io.javalin.JavalinEvent.SERVER_START_FAILED
|
||||
import io.javalin.JavalinEvent.SERVER_STOPPED
|
||||
@@ -103,3 +107,19 @@ constructor(
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a String from the JSON field or throws [BadRequestResponse] if null or empty.
|
||||
*/
|
||||
fun Context.getFromJson(field: String) : String {
|
||||
try {
|
||||
// TODO use a static object mapper to avoid re-initializations
|
||||
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")
|
||||
return result
|
||||
} catch (e: JsonParseException) {
|
||||
throw BadRequestResponse("Invalid JSON")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,10 @@ import io.javalin.Context
|
||||
import org.briarproject.bramble.api.identity.IdentityManager
|
||||
import org.briarproject.bramble.api.system.Clock
|
||||
import org.briarproject.bramble.util.StringUtils
|
||||
import org.briarproject.briar.api.blog.BlogConstants
|
||||
import org.briarproject.briar.api.blog.BlogConstants.MAX_BLOG_POST_BODY_LENGTH
|
||||
import org.briarproject.briar.api.blog.BlogManager
|
||||
import org.briarproject.briar.api.blog.BlogPostFactory
|
||||
import org.briarproject.briar.headless.getFromJson
|
||||
import javax.annotation.concurrent.Immutable
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
@@ -26,16 +27,16 @@ constructor(
|
||||
override fun listPosts(ctx: Context): Context {
|
||||
val posts = blogManager.blogs
|
||||
.flatMap { blog -> blogManager.getPostHeaders(blog.id) }
|
||||
.asSequence()
|
||||
.sortedBy { it.timeReceived }
|
||||
.map { header -> header.output(blogManager.getPostBody(header.id)) }
|
||||
.toList()
|
||||
return ctx.json(posts)
|
||||
}
|
||||
|
||||
override fun createPost(ctx: Context): Context {
|
||||
val text = ctx.formParam("text")
|
||||
if (text == null || text.isEmpty())
|
||||
throw BadRequestResponse("Expecting blog post text")
|
||||
if (StringUtils.utf8IsTooLong(text, BlogConstants.MAX_BLOG_POST_BODY_LENGTH))
|
||||
val text = ctx.getFromJson("text")
|
||||
if (StringUtils.utf8IsTooLong(text, MAX_BLOG_POST_BODY_LENGTH))
|
||||
throw BadRequestResponse("Too long blog post text")
|
||||
|
||||
val author = identityManager.localAuthor
|
||||
|
||||
@@ -3,8 +3,9 @@ package org.briarproject.briar.headless.forums
|
||||
import io.javalin.BadRequestResponse
|
||||
import io.javalin.Context
|
||||
import org.briarproject.bramble.util.StringUtils
|
||||
import org.briarproject.briar.api.forum.ForumConstants
|
||||
import org.briarproject.briar.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH
|
||||
import org.briarproject.briar.api.forum.ForumManager
|
||||
import org.briarproject.briar.headless.getFromJson
|
||||
import javax.annotation.concurrent.Immutable
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
@@ -20,10 +21,8 @@ constructor(private val forumManager: ForumManager) : ForumController {
|
||||
}
|
||||
|
||||
override fun create(ctx: Context): Context {
|
||||
val name = ctx.formParam("text")
|
||||
if (name == null || name.isNullOrEmpty())
|
||||
throw BadRequestResponse("Expecting Forum Name")
|
||||
if (StringUtils.utf8IsTooLong(name, ForumConstants.MAX_FORUM_NAME_LENGTH))
|
||||
val name = ctx.getFromJson("text")
|
||||
if (StringUtils.utf8IsTooLong(name, MAX_FORUM_NAME_LENGTH))
|
||||
throw BadRequestResponse("Forum name is too long")
|
||||
return ctx.json(forumManager.addForum(name).output())
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest
|
||||
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse
|
||||
import org.briarproject.briar.headless.event.WebSocketController
|
||||
import org.briarproject.briar.headless.event.output
|
||||
import org.briarproject.briar.headless.getFromJson
|
||||
import org.briarproject.briar.headless.json.JsonDict
|
||||
import java.util.concurrent.Executor
|
||||
import javax.annotation.concurrent.Immutable
|
||||
@@ -59,9 +60,7 @@ constructor(
|
||||
override fun write(ctx: Context): Context {
|
||||
val contact = getContact(ctx)
|
||||
|
||||
val message = ctx.formParam("text")
|
||||
if (message == null || message.isEmpty())
|
||||
throw BadRequestResponse("Expecting Message text")
|
||||
val message = ctx.getFromJson("text")
|
||||
if (utf8IsTooLong(message, MAX_PRIVATE_MESSAGE_BODY_LENGTH))
|
||||
throw BadRequestResponse("Message text too large")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user