mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
briar-headless: Next round of review comments
This commit is contained in:
@@ -16,7 +16,8 @@ import javax.inject.Singleton
|
||||
@Immutable
|
||||
@Singleton
|
||||
internal class BriarService
|
||||
@Inject constructor(
|
||||
@Inject
|
||||
constructor(
|
||||
private val accountManager: AccountManager,
|
||||
private val lifecycleManager: LifecycleManager,
|
||||
private val passwordStrengthEstimator: PasswordStrengthEstimator
|
||||
|
||||
@@ -5,7 +5,7 @@ import io.javalin.JavalinEvent.SERVER_START_FAILED
|
||||
import io.javalin.JavalinEvent.SERVER_STOPPED
|
||||
import io.javalin.apibuilder.ApiBuilder.*
|
||||
import io.javalin.core.util.ContextUtil
|
||||
import io.javalin.core.util.Header
|
||||
import io.javalin.core.util.Header.AUTHORIZATION
|
||||
import org.briarproject.briar.headless.blogs.BlogController
|
||||
import org.briarproject.briar.headless.contact.ContactController
|
||||
import org.briarproject.briar.headless.event.WebSocketController
|
||||
@@ -21,7 +21,8 @@ import kotlin.system.exitProcess
|
||||
|
||||
@Immutable
|
||||
@Singleton
|
||||
internal class Router @Inject
|
||||
internal class Router
|
||||
@Inject
|
||||
constructor(
|
||||
private val briarService: BriarService,
|
||||
private val webSocketController: WebSocketController,
|
||||
@@ -36,7 +37,7 @@ constructor(
|
||||
|
||||
fun start(authToken: String, port: Int, debug: Boolean) {
|
||||
briarService.start()
|
||||
getRuntime().addShutdownHook(Thread(Runnable { stop() }))
|
||||
getRuntime().addShutdownHook(Thread(this::stop))
|
||||
|
||||
val app = Javalin.create()
|
||||
.port(port)
|
||||
@@ -48,7 +49,7 @@ constructor(
|
||||
app.start()
|
||||
|
||||
app.accessManager { handler, ctx, _ ->
|
||||
if (ctx.header("Authorization") == "Bearer $authToken") {
|
||||
if (ctx.header(AUTHORIZATION) == "Bearer $authToken") {
|
||||
handler.handle(ctx)
|
||||
} else {
|
||||
ctx.status(401).result("Unauthorized")
|
||||
@@ -77,7 +78,7 @@ constructor(
|
||||
}
|
||||
app.ws("/v1/ws") { ws ->
|
||||
ws.onConnect { session ->
|
||||
val authHeader = session.header(Header.AUTHORIZATION)
|
||||
val authHeader = session.header(AUTHORIZATION)
|
||||
val token = ContextUtil.getBasicAuthCredentials(authHeader)?.username
|
||||
if (authToken == token) {
|
||||
logger.info("Adding websocket session with ${session.remoteAddress}")
|
||||
|
||||
@@ -15,7 +15,8 @@ import javax.inject.Singleton
|
||||
@Immutable
|
||||
@Singleton
|
||||
internal class BlogControllerImpl
|
||||
@Inject constructor(
|
||||
@Inject
|
||||
constructor(
|
||||
private val blogManager: BlogManager,
|
||||
private val blogPostFactory: BlogPostFactory,
|
||||
private val identityManager: IdentityManager,
|
||||
|
||||
@@ -9,7 +9,8 @@ import javax.inject.Singleton
|
||||
@Immutable
|
||||
@Singleton
|
||||
internal class ContactControllerImpl
|
||||
@Inject constructor(private val contactManager: ContactManager) : ContactController {
|
||||
@Inject
|
||||
constructor(private val contactManager: ContactManager) : ContactController {
|
||||
|
||||
override fun list(ctx: Context): Context {
|
||||
val contacts = contactManager.activeContacts.map { contact ->
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.briarproject.briar.headless.event
|
||||
|
||||
import io.javalin.websocket.WsSession
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor
|
||||
import org.briarproject.briar.headless.json.JsonDict
|
||||
import javax.annotation.concurrent.ThreadSafe
|
||||
|
||||
@ThreadSafe
|
||||
@@ -12,6 +13,6 @@ interface WebSocketController {
|
||||
/**
|
||||
* Sends an event to all open sessions using the [IoExecutor].
|
||||
*/
|
||||
fun sendEvent(name: String, obj: Any)
|
||||
fun sendEvent(name: String, obj: JsonDict)
|
||||
|
||||
}
|
||||
|
||||
@@ -4,11 +4,12 @@ import io.javalin.json.JavalinJson.toJson
|
||||
import io.javalin.websocket.WsSession
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor
|
||||
import org.briarproject.bramble.util.LogUtils.logException
|
||||
import org.briarproject.briar.headless.json.JsonDict
|
||||
import org.eclipse.jetty.websocket.api.WebSocketException
|
||||
import java.io.IOException
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.Executor
|
||||
import java.util.logging.Level
|
||||
import java.util.logging.Level.WARNING
|
||||
import java.util.logging.Logger.getLogger
|
||||
import javax.annotation.concurrent.Immutable
|
||||
import javax.inject.Inject
|
||||
@@ -17,21 +18,24 @@ import javax.inject.Singleton
|
||||
@Immutable
|
||||
@Singleton
|
||||
internal class WebSocketControllerImpl
|
||||
@Inject constructor(@IoExecutor private val ioExecutor: Executor) : WebSocketController {
|
||||
@Inject
|
||||
constructor(@IoExecutor private val ioExecutor: Executor) : WebSocketController {
|
||||
|
||||
private val logger = getLogger(WebSocketControllerImpl::javaClass.name)
|
||||
|
||||
override val sessions: MutableSet<WsSession> = ConcurrentHashMap.newKeySet<WsSession>()
|
||||
|
||||
override fun sendEvent(name: String, obj: Any) = ioExecutor.execute {
|
||||
override fun sendEvent(name: String, obj: JsonDict) {
|
||||
val event = toJson(OutputEvent(name, obj))
|
||||
sessions.forEach { session ->
|
||||
val event = OutputEvent(name, obj)
|
||||
try {
|
||||
session.send(toJson(event))
|
||||
} catch (e: WebSocketException) {
|
||||
logException(logger, Level.WARNING, e)
|
||||
} catch (e: IOException) {
|
||||
logException(logger, Level.WARNING, e)
|
||||
ioExecutor.execute {
|
||||
try {
|
||||
session.send(event)
|
||||
} catch (e: WebSocketException) {
|
||||
logException(logger, WARNING, e)
|
||||
} catch (e: IOException) {
|
||||
logException(logger, WARNING, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ import javax.inject.Singleton
|
||||
@Immutable
|
||||
@Singleton
|
||||
internal class ForumControllerImpl
|
||||
@Inject constructor(private val forumManager: ForumManager) : ForumController {
|
||||
@Inject
|
||||
constructor(private val forumManager: ForumManager) : ForumController {
|
||||
|
||||
override fun list(ctx: Context): Context {
|
||||
return ctx.json(forumManager.forums.output())
|
||||
|
||||
@@ -36,7 +36,8 @@ internal const val EVENT_PRIVATE_MESSAGE = "PrivateMessageReceivedEvent"
|
||||
@Immutable
|
||||
@Singleton
|
||||
internal class MessagingControllerImpl
|
||||
@Inject constructor(
|
||||
@Inject
|
||||
constructor(
|
||||
private val messagingManager: MessagingManager,
|
||||
private val conversationManager: ConversationManager,
|
||||
private val privateMessageFactory: PrivateMessageFactory,
|
||||
|
||||
@@ -44,7 +44,7 @@ internal fun BlogInvitationResponse.output(contactId: ContactId): JsonDict {
|
||||
|
||||
internal fun ForumInvitationResponse.output(contactId: ContactId): JsonDict {
|
||||
val dict = (this as InvitationResponse).output(contactId)
|
||||
dict["type"] = "BlogInvitationResponse"
|
||||
dict["type"] = "ForumInvitationResponse"
|
||||
return dict
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user