diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index 2568df0..802e9a1 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -11,7 +11,7 @@ android {
defaultConfig {
applicationId = "com.faraphel.tasks_valider"
- minSdk = 24
+ minSdk = 26
targetSdk = 34
versionCode = 1
versionName = "1.0"
@@ -52,8 +52,8 @@ android {
}
dependencies {
- implementation("androidx.core:core-ktx:1.13.0")
- implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
+ implementation("androidx.core:core-ktx:1.13.1")
+ implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.1")
implementation("androidx.activity:activity-compose:1.9.0")
implementation(platform("androidx.compose:compose-bom:2023.08.00"))
implementation("androidx.compose.ui:ui")
@@ -66,6 +66,8 @@ dependencies {
implementation("org.nanohttpd:nanohttpd:2.3.1")
implementation("com.google.code.gson:gson:2.10.1")
implementation("com.squareup.okhttp3:okhttp-android:5.0.0-alpha.14")
+ implementation("com.journeyapps:zxing-android-embedded:4.3.0")
+ implementation("com.google.zxing:core:3.5.3")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
@@ -74,4 +76,5 @@ dependencies {
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
ksp("androidx.room:room-compiler:2.6.1")
+ implementation(kotlin("reflect"))
}
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 051a946..32f3c79 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -5,12 +5,12 @@
-
+
@@ -26,6 +26,10 @@
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />
+
+
+
+
@@ -47,9 +51,12 @@
android:theme="@style/Theme.Tasksvalider">
-
+
+
+
+
diff --git a/app/src/main/java/com/faraphel/tasks_valider/MainActivity.kt b/app/src/main/java/com/faraphel/tasks_valider/MainActivity.kt
index b35c547..3b00408 100644
--- a/app/src/main/java/com/faraphel/tasks_valider/MainActivity.kt
+++ b/app/src/main/java/com/faraphel/tasks_valider/MainActivity.kt
@@ -6,24 +6,41 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
-import com.faraphel.tasks_valider.connectivity.bwf.BwfManager
+import androidx.room.Room
+import com.faraphel.tasks_valider.connectivity.bwd.BwdManager
import com.faraphel.tasks_valider.database.TaskDatabase
-import com.faraphel.tasks_valider.ui.screen.communication.CommunicationScreen
+import com.faraphel.tasks_valider.database.populateTaskDatabaseTest
+import com.faraphel.tasks_valider.ui.screen.communication.CommunicationModeSelectionScreen
class MainActivity : ComponentActivity() {
- private var bwfManager: BwfManager? = null ///< the WiFi-Direct helper
+ private var bwdManager: BwdManager? = null ///< the WiFi-Direct helper
+ private lateinit var database: TaskDatabase ///< the database manager
- companion object {
- private lateinit var database: TaskDatabase ///< the database manager
- }
-
- @RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
+ // Reset the database | TODO(Faraphel): only for testing purpose
+ this.deleteDatabase("local")
+
+ // Create the database
+ this.database = Room.databaseBuilder(
+ this,
+ TaskDatabase::class.java,
+ "local"
+ ).build()
+
+ // Populate the database with test data
+ // TODO(Faraphel): remove test data
+ Thread {
+ populateTaskDatabaseTest(database)
+ }.let { thread ->
+ thread.start()
+ thread.join()
+ }
+
this.setContent {
- CommunicationScreen(this)
+ CommunicationModeSelectionScreen(this, database)
}
}
@@ -32,13 +49,13 @@ class MainActivity : ComponentActivity() {
super.onResume()
// enable the WiFi-Direct events
- this.registerReceiver(this.bwfManager, BwfManager.ALL_INTENT_FILTER)
+ this.registerReceiver(this.bwdManager, BwdManager.ALL_INTENT_FILTER)
}
override fun onPause() {
super.onPause()
// disable the WiFi-Direct events
- this.unregisterReceiver(this.bwfManager)
+ this.unregisterReceiver(this.bwdManager)
}
}
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/BwfManager.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/BwdManager.kt
similarity index 91%
rename from app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/BwfManager.kt
rename to app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/BwdManager.kt
index 31f6681..e1d1994 100644
--- a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/BwfManager.kt
+++ b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/BwdManager.kt
@@ -1,4 +1,4 @@
-package com.faraphel.tasks_valider.connectivity.bwf
+package com.faraphel.tasks_valider.connectivity.bwd
import android.Manifest
import android.app.Activity
@@ -10,7 +10,7 @@ import android.content.pm.PackageManager
import android.net.wifi.p2p.*
import android.util.Log
import androidx.compose.runtime.mutableStateOf
-import com.faraphel.tasks_valider.connectivity.bwf.error.*
+import com.faraphel.tasks_valider.connectivity.bwd.error.*
/**
@@ -22,7 +22,7 @@ import com.faraphel.tasks_valider.connectivity.bwf.error.*
* @param manager The WiFi-Direct manager
* @param channel The WiFi-Direct channel
*/
-class BwfManager(
+class BwdManager(
private var manager: WifiP2pManager,
private var channel: WifiP2pManager.Channel,
) : BroadcastReceiver() {
@@ -41,11 +41,11 @@ class BwfManager(
* Create a new BwfManager from an activity.
* @param activity The activity to create the manager from
*/
- fun fromActivity(activity: Activity): BwfManager {
+ fun fromActivity(activity: Activity): BwdManager {
// check if the system support WiFi-Direct
if (this.isSupported(activity)) {
Log.e("wifi-p2p", "this device does not support the WiFi-Direct feature")
- throw BwfNotSupportedException()
+ throw BwdNotSupportedException()
}
// TODO(Faraphel): more check on permissions
@@ -62,11 +62,11 @@ class BwfManager(
// get the WiFi-Direct manager
val manager = activity.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager?
- ?: throw BwfPermissionException()
+ ?: throw BwdPermissionException()
// get the WiFi-Direct channel
val channel = manager.initialize(activity, activity.mainLooper, null)
- return BwfManager(manager, channel)
+ return BwdManager(manager, channel)
// NOTE(Faraphel): the broadcast receiver should be registered in the activity onResume
}
@@ -83,7 +83,7 @@ class BwfManager(
fun connect(config: WifiP2pConfig, callback: () -> Unit = {}) =
this.manager.connect(this.channel, config, object : WifiP2pManager.ActionListener {
override fun onSuccess() { callback() }
- override fun onFailure(reason: Int) = throw BwfConnectException(reason)
+ override fun onFailure(reason: Int) = throw BwdConnectException(reason)
})
/**
@@ -105,7 +105,7 @@ class BwfManager(
fun discoverPeers(callback: () -> Unit = {}) =
this.manager.discoverPeers(this.channel, object : WifiP2pManager.ActionListener {
override fun onSuccess() { callback() }
- override fun onFailure(reason: Int) = throw BwfDiscoverException(reason)
+ override fun onFailure(reason: Int) = throw BwdDiscoverException(reason)
})
/**
@@ -133,7 +133,7 @@ class BwfManager(
fun createGroup(callback: () -> Unit = {}) =
this.manager.createGroup(this.channel, object : WifiP2pManager.ActionListener {
override fun onSuccess() { callback() }
- override fun onFailure(reason: Int) = throw BwfCreateGroupException(reason)
+ override fun onFailure(reason: Int) = throw BwdCreateGroupException(reason)
})
/**
@@ -143,7 +143,7 @@ class BwfManager(
fun removeGroup(callback: () -> Unit = {}) =
this.manager.removeGroup(this.channel, object : WifiP2pManager.ActionListener {
override fun onSuccess() { callback() }
- override fun onFailure(reason: Int) = throw BwfRemoveGroupException(reason)
+ override fun onFailure(reason: Int) = throw BwdRemoveGroupException(reason)
})
/**
@@ -161,7 +161,7 @@ class BwfManager(
this.requestGroupInfo { group ->
// if a group exist, quit it
if (group != null)
- this.removeGroup { this@BwfManager.createGroup(callback) }
+ this.removeGroup { this@BwdManager.createGroup(callback) }
else
// create the group
this.createGroup(callback)
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/README.md b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/README.md
similarity index 100%
rename from app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/README.md
rename to app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/README.md
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdConnectException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdConnectException.kt
new file mode 100644
index 0000000..4808a4b
--- /dev/null
+++ b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdConnectException.kt
@@ -0,0 +1,5 @@
+package com.faraphel.tasks_valider.connectivity.bwd.error
+
+class BwdConnectException(
+ reason: Int
+) : BwdException("Cannot connect to the peer. Reason: $reason")
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdCreateGroupException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdCreateGroupException.kt
new file mode 100644
index 0000000..e336cc0
--- /dev/null
+++ b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdCreateGroupException.kt
@@ -0,0 +1,5 @@
+package com.faraphel.tasks_valider.connectivity.bwd.error
+
+class BwdCreateGroupException (
+ reason: Int
+) : BwdException("Could not create the group : $reason")
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdDiscoverException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdDiscoverException.kt
new file mode 100644
index 0000000..ab6fece
--- /dev/null
+++ b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdDiscoverException.kt
@@ -0,0 +1,5 @@
+package com.faraphel.tasks_valider.connectivity.bwd.error
+
+class BwdDiscoverException(
+ reason: Int
+) : BwdException("Could not discover peers : $reason")
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdException.kt
similarity index 61%
rename from app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfException.kt
rename to app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdException.kt
index 7fd8810..3d5e99b 100644
--- a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfException.kt
+++ b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdException.kt
@@ -1,9 +1,9 @@
-package com.faraphel.tasks_valider.connectivity.bwf.error
+package com.faraphel.tasks_valider.connectivity.bwd.error
/**
* Base Exception for everything concerning the WifiP2pHelper class
*/
-open class BwfException(
+open class BwdException(
override val message: String?
) : Exception(message)
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdInvalidActionException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdInvalidActionException.kt
new file mode 100644
index 0000000..ce50bd8
--- /dev/null
+++ b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdInvalidActionException.kt
@@ -0,0 +1,5 @@
+package com.faraphel.tasks_valider.connectivity.bwd.error
+
+class BwdInvalidActionException(
+ action: String
+) : BwdException("This WiFi-Direct action is not supported : $action")
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdNotSupportedException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdNotSupportedException.kt
new file mode 100644
index 0000000..9a2b46b
--- /dev/null
+++ b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdNotSupportedException.kt
@@ -0,0 +1,4 @@
+package com.faraphel.tasks_valider.connectivity.bwd.error
+
+class BwdNotSupportedException :
+ BwdException("WiFi-Direct is not supported on this device.")
\ No newline at end of file
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdPermissionException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdPermissionException.kt
new file mode 100644
index 0000000..f30bd58
--- /dev/null
+++ b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdPermissionException.kt
@@ -0,0 +1,4 @@
+package com.faraphel.tasks_valider.connectivity.bwd.error
+
+class BwdPermissionException :
+ BwdException("WiFi-Direct requires permissions to work properly. Please grant the permissions.")
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdRemoveGroupException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdRemoveGroupException.kt
new file mode 100644
index 0000000..e5456e7
--- /dev/null
+++ b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwd/error/BwdRemoveGroupException.kt
@@ -0,0 +1,5 @@
+package com.faraphel.tasks_valider.connectivity.bwd.error
+
+class BwdRemoveGroupException (
+ reason: Int
+) : BwdException("Could not remove the group : $reason")
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfConnectException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfConnectException.kt
deleted file mode 100644
index a581c09..0000000
--- a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfConnectException.kt
+++ /dev/null
@@ -1,5 +0,0 @@
-package com.faraphel.tasks_valider.connectivity.bwf.error
-
-class BwfConnectException(
- reason: Int
-) : BwfException("Cannot connect to the peer. Reason: $reason")
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfCreateGroupException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfCreateGroupException.kt
deleted file mode 100644
index 0ce1169..0000000
--- a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfCreateGroupException.kt
+++ /dev/null
@@ -1,5 +0,0 @@
-package com.faraphel.tasks_valider.connectivity.bwf.error
-
-class BwfCreateGroupException (
- reason: Int
-) : BwfException("Could not create the group : $reason")
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfDiscoverException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfDiscoverException.kt
deleted file mode 100644
index a4e818a..0000000
--- a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfDiscoverException.kt
+++ /dev/null
@@ -1,5 +0,0 @@
-package com.faraphel.tasks_valider.connectivity.bwf.error
-
-class BwfDiscoverException(
- reason: Int
-) : BwfException("Could not discover peers : $reason")
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfInvalidActionException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfInvalidActionException.kt
deleted file mode 100644
index 0b690a3..0000000
--- a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfInvalidActionException.kt
+++ /dev/null
@@ -1,5 +0,0 @@
-package com.faraphel.tasks_valider.connectivity.bwf.error
-
-class BwfInvalidActionException(
- action: String
-) : BwfException("This WiFi-Direct action is not supported : $action")
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfNotSupportedException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfNotSupportedException.kt
deleted file mode 100644
index 3585496..0000000
--- a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfNotSupportedException.kt
+++ /dev/null
@@ -1,4 +0,0 @@
-package com.faraphel.tasks_valider.connectivity.bwf.error
-
-class BwfNotSupportedException :
- BwfException("WiFi-Direct is not supported on this device.")
\ No newline at end of file
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfPermissionException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfPermissionException.kt
deleted file mode 100644
index 81c85ee..0000000
--- a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfPermissionException.kt
+++ /dev/null
@@ -1,4 +0,0 @@
-package com.faraphel.tasks_valider.connectivity.bwf.error
-
-class BwfPermissionException :
- BwfException("WiFi-Direct requires permissions to work properly. Please grant the permissions.")
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfRemoveGroupException.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfRemoveGroupException.kt
deleted file mode 100644
index 92d0789..0000000
--- a/app/src/main/java/com/faraphel/tasks_valider/connectivity/bwf/error/BwfRemoveGroupException.kt
+++ /dev/null
@@ -1,5 +0,0 @@
-package com.faraphel.tasks_valider.connectivity.bwf.error
-
-class BwfRemoveGroupException (
- reason: Int
-) : BwfException("Could not remove the group : $reason")
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/task/TaskClient.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/task/TaskClient.kt
index b1917e1..e4b7bd5 100644
--- a/app/src/main/java/com/faraphel/tasks_valider/connectivity/task/TaskClient.kt
+++ b/app/src/main/java/com/faraphel/tasks_valider/connectivity/task/TaskClient.kt
@@ -1,13 +1,11 @@
package com.faraphel.tasks_valider.connectivity.task
-import okhttp3.HttpUrl
-import okhttp3.MediaType.Companion.toMediaType
-import okhttp3.OkHttpClient
-import okhttp3.RequestBody.Companion.toRequestBody
+import com.faraphel.tasks_valider.database.api.client.TaskEntityHttpClient
+import com.faraphel.tasks_valider.database.api.client.entities.*
/**
- * A client to handle the room connection.
+ * A client to handle the room connection and access the API
* @param address the address of the server
* @param port the port of the server
* @param baseCookies list of cookies to use (optional)
@@ -17,84 +15,15 @@ class TaskClient(
private val port: Int,
private val baseCookies: List = listOf()
) {
- private val baseUrl = "http://$address:$port"
- private val client = OkHttpClient().newBuilder().cookieJar(
- // TODO(Faraphel): should be moved into another object
- object : okhttp3.CookieJar {
- private val cookies = baseCookies.toMutableList() ///< list of cookies
+ private val httpClient = TaskEntityHttpClient(address, port, baseCookies)
- override fun loadForRequest(url: HttpUrl): List {
- return this.cookies
- }
- override fun saveFromResponse(url: HttpUrl, cookies: List) {
- this.cookies.addAll(cookies)
- }
- }
- ).build()
+ val clientApi = ClassClientApi(httpClient)
+ val personApi = PersonClientApi(httpClient)
+ val sessionApi = SessionClientApi(httpClient)
+ val subjectApi = SubjectClientApi(httpClient)
+ val taskApi = TaskClientApi(httpClient)
+ val validationApi = ValidationClientApi(httpClient)
- // TODO(Faraphel): automatically convert content to the correct type ?
-
- /**
- * Return a basic request to the server
- * @param endpoint the endpoint of the server
- */
- private fun baseRequestBuilder(endpoint: String): okhttp3.Request.Builder =
- okhttp3.Request.Builder().url("$baseUrl/$endpoint")
-
- /**
- * Run a HEAD request
- * @param endpoint the endpoint of the server
- */
- fun head(endpoint: String): okhttp3.Request =
- this.baseRequestBuilder(endpoint).head().build()
-
- /**
- * Run a GET request
- * @param endpoint the endpoint of the server
- */
- fun get(endpoint: String): okhttp3.Response =
- this.client.newCall(
- this.baseRequestBuilder(endpoint)
- .get()
- .build()
- ).execute()
-
- /**
- * Run a POST request
- * @param endpoint the endpoint of the server
- * @param content the content of the request
- * @param type the type of the content
- */
- fun post(endpoint: String, content: String, type: String = "text/plain"): okhttp3.Response =
- this.client.newCall(
- this.baseRequestBuilder(endpoint)
- .post(content.toRequestBody(type.toMediaType()))
- .build()
- ).execute()
-
- /**
- * Run a PATCH request
- * @param endpoint the endpoint of the server
- * @param content the content of the request
- * @param type the type of the content
- */
- fun patch(endpoint: String, content: String, type: String = "text/plain"): okhttp3.Response =
- this.client.newCall(
- this.baseRequestBuilder(endpoint)
- .patch(content.toRequestBody(type.toMediaType()))
- .build()
- ).execute()
-
- /**
- * Run a DELETE request
- * @param endpoint the endpoint of the server
- * @param content the content of the request
- * @param type the type of the content
- */
- fun delete(endpoint: String, content: String, type: String = "text/plain"): okhttp3.Response =
- this.client.newCall(
- this.baseRequestBuilder(endpoint)
- .delete(content.toRequestBody(type.toMediaType()))
- .build()
- ).execute()
+ val relationClassPersonApi = RelationClassPersonClientApi(httpClient)
+ val relationPersonSessionSubjectApi = RelationPersonSessionSubjectClientApi(httpClient)
}
\ No newline at end of file
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/task/TaskServer.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/task/TaskServer.kt
index ba2151a..a4c1893 100644
--- a/app/src/main/java/com/faraphel/tasks_valider/connectivity/task/TaskServer.kt
+++ b/app/src/main/java/com/faraphel/tasks_valider/connectivity/task/TaskServer.kt
@@ -1,45 +1,51 @@
package com.faraphel.tasks_valider.connectivity.task
import com.faraphel.tasks_valider.connectivity.task.api.TaskSessionManagerApi
-import com.faraphel.tasks_valider.connectivity.task.session.TaskRole
-import com.faraphel.tasks_valider.connectivity.task.session.TaskSession
import com.faraphel.tasks_valider.connectivity.task.session.TaskSessionManager
import com.faraphel.tasks_valider.database.TaskDatabase
-import com.faraphel.tasks_valider.database.api.TaskDatabaseApi
+import com.faraphel.tasks_valider.database.api.server.DatabaseApi
+import com.faraphel.tasks_valider.database.entities.PersonEntity
+import com.faraphel.tasks_valider.database.entities.SessionEntity
import fi.iki.elonen.NanoHTTPD
/**
* A server to handle the task API to allow clients to interact with the database.
- * @param port the port of the server
- * @param database the database to interact with
+ * @param port the port of the server.
+ * @param database the database to interact with.
+ * @param session the current session.
+ * @param adminPersonEntity the person that represent the host of the session.
*/
class TaskServer(
private val port: Int,
- private val database: TaskDatabase
+ private val database: TaskDatabase,
+ private val session: SessionEntity,
+ private val adminPersonEntity: PersonEntity,
) : NanoHTTPD(port) {
- companion object {
- private val TASK_SESSION_ADMIN = TaskSession( ///< the admin default session
- role = TaskRole.ADMIN
- )
+ private val sessionManager = TaskSessionManager(adminPersonEntity) ///< the session manager
+ private val databaseApi = DatabaseApi(this.database, session) ///< the api of the database
+ private val sessionManagerApi = TaskSessionManagerApi(this.sessionManager, this.database) ///< the api of the session manager
+
+ /**
+ * Get the admin person entity
+ * @return the admin person entity
+ */
+ fun getAdminPersonEntity(): PersonEntity {
+ return this.sessionManager.getAdminPersonEntity()
}
- private val sessionManager = TaskSessionManager() ///< the session manager
- private val adminSessionId = this.sessionManager.newSessionData(TASK_SESSION_ADMIN) ///< default admin session id
-
- private val sessionManagerApi = TaskSessionManagerApi(this.sessionManager) ///< the api of the session manager
- private val databaseApi = TaskDatabaseApi(this.database) ///< the api of the database
-
/**
* Return a new client that can be used by the admin
+ * @return the client
*/
- fun getClientAdmin(): TaskClient {
+ fun getAdminClient(): TaskClient {
// create the session cookie for the admin
val cookieSession = okhttp3.Cookie.Builder()
.domain("localhost")
- .name("sessionId")
- .value(adminSessionId)
- .build()
+ .name("sessionToken")
+ .value(this.sessionManager.adminSessionToken)
+ .build()
+
// create a new client
return TaskClient(
"localhost",
@@ -54,7 +60,8 @@ class TaskServer(
*/
override fun serve(httpSession: IHTTPSession): Response {
// get the session data of the client
- val taskSession = this.sessionManager.getOrCreateSessionData(httpSession)
+ val taskSessionData = this.sessionManager.getSessionData(httpSession)
+ val taskSession = taskSessionData?.second
// parse the url
val uri: String = httpSession.uri.trim('/')
@@ -69,11 +76,11 @@ class TaskServer(
)
// get the response from the correct part of the application
- val response = when (requestType) {
+ return when (requestType) {
// session requests
"sessions" -> this.sessionManagerApi.handleRequest(taskSession, httpSession, path)
// entities requests
- "entities" -> return this.databaseApi.handleRequest(taskSession, httpSession, path)
+ "entities" -> this.databaseApi.handleRequest(taskSession, httpSession, path)
// invalid requests
else ->
newFixedLengthResponse(
@@ -82,9 +89,6 @@ class TaskServer(
"Unknown request type"
)
}
-
- // wrap additional information in the response
- return this.sessionManager.responseSetSessionData(response, httpSession.cookies)
}
/**
diff --git a/app/src/main/java/com/faraphel/tasks_valider/connectivity/task/api/TaskSessionManagerApi.kt b/app/src/main/java/com/faraphel/tasks_valider/connectivity/task/api/TaskSessionManagerApi.kt
index 305e3f3..6d6f055 100644
--- a/app/src/main/java/com/faraphel/tasks_valider/connectivity/task/api/TaskSessionManagerApi.kt
+++ b/app/src/main/java/com/faraphel/tasks_valider/connectivity/task/api/TaskSessionManagerApi.kt
@@ -1,10 +1,10 @@
package com.faraphel.tasks_valider.connectivity.task.api
import com.faraphel.tasks_valider.connectivity.task.session.TaskPermission
-import com.faraphel.tasks_valider.connectivity.task.session.TaskRole
import com.faraphel.tasks_valider.connectivity.task.session.TaskSession
import com.faraphel.tasks_valider.connectivity.task.session.TaskSessionManager
-import com.google.gson.Gson
+import com.faraphel.tasks_valider.database.TaskDatabase
+import com.faraphel.tasks_valider.utils.parser
import com.google.gson.reflect.TypeToken
import fi.iki.elonen.NanoHTTPD
@@ -12,9 +12,10 @@ import fi.iki.elonen.NanoHTTPD
/**
* the HTTP API for the session manager
*/
-class TaskSessionManagerApi(private val sessionManager: TaskSessionManager) {
- private val jsonParser = Gson() ///< the json parser
-
+class TaskSessionManagerApi(
+ private val sessionManager: TaskSessionManager,
+ private val database: TaskDatabase
+) {
/**
* Handle a HTTP Api request
* @param taskSession the data of the client session
@@ -22,109 +23,118 @@ class TaskSessionManagerApi(private val sessionManager: TaskSessionManager) {
* @param path the path of the request
*/
fun handleRequest(
- taskSession: TaskSession,
+ taskSession: TaskSession?,
httpSession: NanoHTTPD.IHTTPSession,
path: MutableList,
): NanoHTTPD.Response {
- // get the target session id
- val targetSessionId = path.removeFirstOrNull()
+ val action = path.removeFirstOrNull()
- return if (targetSessionId == null) {
- // no specific session targeted
- this.handleRequestGeneric(taskSession, httpSession)
- } else {
- // a specific session is targeted
- this.handleRequestSpecific(taskSession, httpSession, targetSessionId)
+ return when (action) {
+ "self" -> this.handleRequestSelf(taskSession, httpSession, path)
+ "all" -> this.handleRequestAll(taskSession, httpSession, path)
+ else ->
+ NanoHTTPD.newFixedLengthResponse(
+ NanoHTTPD.Response.Status.BAD_REQUEST,
+ "text/plain",
+ "Unknown action"
+ )
}
}
/**
- * Handle a request with no specific session targeted
+ * Handle an HTTP Api request about the user own session
*/
- private fun handleRequestGeneric(
- taskSession: TaskSession,
+ fun handleRequestSelf(
+ taskSession: TaskSession?,
httpSession: NanoHTTPD.IHTTPSession,
+ path: MutableList,
): NanoHTTPD.Response {
when (httpSession.method) {
- // get all the session data
+ // get the session data
NanoHTTPD.Method.GET -> {
- // check the permission of the session
- if (taskSession.role.permissions.contains(TaskPermission.READ))
+ // check if the session is valid
+ if (taskSession == null)
return NanoHTTPD.newFixedLengthResponse(
- NanoHTTPD.Response.Status.FORBIDDEN,
+ NanoHTTPD.Response.Status.UNAUTHORIZED,
"text/plain",
- "Forbidden"
+ "No session"
)
// return the session data
return NanoHTTPD.newFixedLengthResponse(
NanoHTTPD.Response.Status.OK,
"application/json",
- jsonParser.toJson(taskSession)
+ parser.toJson(taskSession)
)
}
- // other action are limited
- else -> {
- return NanoHTTPD.newFixedLengthResponse(
- NanoHTTPD.Response.Status.METHOD_NOT_ALLOWED,
- "text/plain",
- "Unknown method"
- )
- }
- }
- }
- private fun handleRequestSpecific(
- taskSession: TaskSession,
- httpSession: NanoHTTPD.IHTTPSession,
- targetSessionId: String,
- ): NanoHTTPD.Response {
- when (httpSession.method) {
- // change a specific client session data
+ // connect the user to the session
NanoHTTPD.Method.POST -> {
- // check the permission of the session
- if (taskSession.role.permissions.contains(TaskPermission.ADMIN))
+ // get the user identifiers
+ val identifiers: Map = parser.fromJson(
+ httpSession.inputStream.bufferedReader().readText(),
+ object : TypeToken