Skip to content

Commit

Permalink
[RFR-1057] Add attachments metadata, remove serialization dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
hb0 committed Jul 15, 2024
2 parents 7c29d35 + 819ef2c commit 7e3453b
Show file tree
Hide file tree
Showing 23 changed files with 1,043 additions and 214 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,16 @@ val startLocation = GeoLocation(1637744753012L, 51.1, 13.1)
val endLocation = GeoLocation(1637744993000, 51.2, 13.2)
val modality = "BICYCLE"
val formatVersion = RequestMetaData.CURRENT_TRANSFER_FILE_FORMAT_VERSION
val logCount = 0
val imageCount = 0
val videoCount = 0
val filesSize = 0L
val metaData = RequestMetaData(
deviceId, measurementId,
osVersion, deviceType, appVersion, length,
locationCount, startLocation, endLocation,
modality, formatVersion
modality, formatVersion,
logCount, imageCount, videoCount, filesSize
)

// Replace with a Cyface Binary or another file when using a custom Collector
Expand Down
3 changes: 0 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ version = "0.0.0" // Automatically overwritten by CI

// Versions of dependencies
extra["slf4jVersion"] = "2.0.7"
// TODO: Check if we can remove this after we port the new auth from android-backend to this library
extra["cyfaceSerializationVersion"] = "3.2.0"
extra["googleApiClientVersion"] = "2.2.0" // transmission protocol
extra["gradleWrapperVersion"] = "7.6.1"

Expand Down Expand Up @@ -100,7 +98,6 @@ tasks.test {

dependencies {
// Internal Cyface Dependencies
api("de.cyface:serializer:${project.extra["cyfaceSerializationVersion"]}")
implementation("com.google.api-client:google-api-client:${project.extra["googleApiClientVersion"]}")

// Kotlin Support
Expand Down
219 changes: 94 additions & 125 deletions src/main/kotlin/de/cyface/uploader/DefaultUploader.kt

Large diffs are not rendered by default.

42 changes: 9 additions & 33 deletions src/main/kotlin/de/cyface/uploader/RequestInitializeHandler.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Cyface GmbH
* Copyright 2021-2024 Cyface GmbH
*
* This file is part of the Cyface Uploader.
*
Expand All @@ -21,60 +21,36 @@ package de.cyface.uploader
import com.google.api.client.http.HttpHeaders
import com.google.api.client.http.HttpRequest
import com.google.api.client.http.HttpRequestInitializer
import de.cyface.model.RequestMetaData
import de.cyface.uploader.model.Uploadable
import java.io.IOException

/**
* Assembles a request as requested to upload data.
*
* @author Armin Schnabel
* @version 1.0.0
* @since 7.0.0
* @property metaData the `MetaData` used to request the upload permission from the server
* @property uploadable the `MetaData` used to request the upload permission from the server
* @property jwtBearer the JWT token to authenticate the upload requests
*/
class RequestInitializeHandler(
private val metaData: RequestMetaData,
private val uploadable: Uploadable,
private val jwtBearer: String
) : HttpRequestInitializer {
@Throws(IOException::class)

override fun initialize(request: HttpRequest) {
val headers = HttpHeaders()
headers.authorization = jwtBearer
addMetaData(metaData, headers)
applyMetaToHttpHeaders(headers)
// sets the metadata in both requests but until we don't use the session-URI
// feature we can't store the metadata from the pre-request to be used in the upload
// and the library does not support just to set the upload request header
request.headers = headers
}

private fun addMetaData(metaData: RequestMetaData, headers: HttpHeaders) {
// Location meta data
metaData.startLocation?.let { startLocation ->
headers["startLocLat"] = startLocation.latitude.toString()
headers["startLocLon"] = startLocation.longitude.toString()
headers["startLocTS"] = startLocation.timestamp.toString()
private fun applyMetaToHttpHeaders(headers: HttpHeaders) {
val map = uploadable.toMap()
map.forEach { (key, value) ->
headers[key] = value
}
metaData.endLocation?.let { endLocation ->
headers["endLocLat"] = endLocation.latitude.toString()
headers["endLocLon"] = endLocation.longitude.toString()
headers["endLocTS"] = endLocation.timestamp.toString()
}
headers["locationCount"] = metaData.locationCount.toString()

// Remaining meta data
headers["deviceId"] = metaData.deviceIdentifier
headers["measurementId"] = java.lang.Long.valueOf(metaData.measurementIdentifier).toString()
headers["deviceType"] = metaData.deviceType
headers["osVersion"] = metaData.operatingSystemVersion
headers["appVersion"] = metaData.applicationVersion
headers["length"] = metaData.length.toString()
headers["modality"] = metaData.modality
headers["formatVersion"] = metaData.formatVersion.toString()
headers["logCount"] = metaData.logCount
headers["imageCount"] = metaData.imageCount
headers["videoCount"] = metaData.videoCount
headers["filesSize"] = metaData.filesSize
}
}
23 changes: 11 additions & 12 deletions src/main/kotlin/de/cyface/uploader/Uploader.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Cyface GmbH
* Copyright 2023-2024 Cyface GmbH
*
* This file is part of the Cyface Uploader.
*
Expand All @@ -18,8 +18,10 @@
*/
package de.cyface.uploader

import de.cyface.model.RequestMetaData
import de.cyface.uploader.exception.UploadFailed
import de.cyface.uploader.model.Attachment
import de.cyface.uploader.model.Measurement
import de.cyface.uploader.model.Uploadable
import java.io.File
import java.net.MalformedURLException
import java.net.URL
Expand All @@ -28,16 +30,14 @@ import java.net.URL
* Interface for uploading files to a Cyface Data Collector.
*
* @author Armin Schnabel
* @version 2.0.0
* @since 1.0.0
*/
interface Uploader {

/**
* Uploads the provided measurement file to the server.
*
* @param jwtToken A String in the format "eyXyz123***".
* @param metaData The [RequestMetaData] required for the upload request.
* @param uploadable The [Uploadable] describing the data to upload.
* @param file The data file to upload via this post request.
* @param progressListener The [UploadProgressListener] to be informed about the upload progress.
* @throws UploadFailed when an error occurred.
Expand All @@ -47,7 +47,7 @@ interface Uploader {
@Throws(UploadFailed::class)
fun uploadMeasurement(
jwtToken: String,
metaData: RequestMetaData,
uploadable: Measurement,
file: File,
progressListener: UploadProgressListener
): Result
Expand All @@ -56,8 +56,7 @@ interface Uploader {
* Uploads the provided attachment file to the server, associated with a specific measurement.
*
* @param jwtToken A String in the format "eyXyz123***".
* @param metaData The [RequestMetaData] required for the upload request.
* @param measurementId The id of the measurement the file is attached to.
* @param uploadable The [Uploadable] describing the data to upload.
* @param file The attachment file to upload via this post request.
* @param fileName How the transfer file should be named when uploading.
* @param progressListener The [UploadProgressListener] to be informed about the upload progress.
Expand All @@ -68,11 +67,10 @@ interface Uploader {
@Throws(UploadFailed::class)
fun uploadAttachment(
jwtToken: String,
metaData: RequestMetaData,
measurementId: Long,
uploadable: Attachment,
file: File,
fileName: String,
progressListener: UploadProgressListener
progressListener: UploadProgressListener,
): Result

/**
Expand All @@ -85,10 +83,11 @@ interface Uploader {
/**
* Determines the URL endpoint for uploading attachment files associated with a specific measurement.
*
* @param deviceId The ID of the device the measurement is attached to.
* @param measurementId The ID of the measurement the files are attached to.
* @return The URL endpoint used for uploading attachment files.
* @throws MalformedURLException if the endpoint address is malformed.
*/
@Throws(MalformedURLException::class)
fun attachmentsEndpoint(measurementId: Long): URL
fun attachmentsEndpoint(deviceId: String, measurementId: Long): URL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 Cyface GmbH
*
* This file is part of the Cyface Uploader.
*
* The Cyface Uploader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Cyface Uploader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Cyface Uploader. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cyface.uploader.exception

/**
* Exception thrown when the client tries to upload a file in a formatVersion older than the current version.
*
* @author Armin Schnabel
* @param message Details about the reason for this exception.
*/
class DeprecatedFormatVersion(message: String) : Exception(message)
30 changes: 30 additions & 0 deletions src/main/kotlin/de/cyface/uploader/exception/InvalidMetaData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2021-2024 Cyface GmbH
*
* This file is part of the Cyface Uploader.
*
* The Cyface Uploader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Cyface Uploader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Cyface Uploader. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cyface.uploader.exception

/**
* Exception thrown when the upload or pre-request does not contain the expected metadata.
*
* @author Armin Schnabel
*/
class InvalidMetaData : Exception {
constructor(message: String, cause: Throwable) : super(message, cause)
constructor(message: String) : super(message)
constructor(cause: Throwable) : super(cause)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Cyface GmbH
* Copyright 2021-2024 Cyface GmbH
*
* This file is part of the Cyface Uploader.
*
Expand All @@ -24,10 +24,13 @@ package de.cyface.uploader.exception
* This is usually indicated by OkHttp via `SSLException`.
*
* @author Armin Schnabel
* @version 1.0.2
* @since 1.0.0
*/
class NetworkUnavailableException : Exception {
/**
* @param exception The `Exception` that caused this one.
*/
constructor(exception: Exception) : super(exception)

/**
* @param detailedMessage A more detailed message explaining the context for this `Exception`.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Cyface GmbH
* Copyright 2021-2024 Cyface GmbH
*
* This file is part of the Cyface Uploader.
*
Expand All @@ -23,8 +23,6 @@ package de.cyface.uploader.exception
* via a message or another wrapped `Exception`.
*
* @author Klemens Muthmann
* @version 1.1.4
* @since 1.0.0
*/
class SynchronisationException : Exception {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 Cyface GmbH
* Copyright 2019-2024 Cyface GmbH
*
* This file is part of the Cyface Uploader.
*
Expand All @@ -22,10 +22,13 @@ package de.cyface.uploader.exception
* An `Exception` thrown when the synchronization is interrupted.
*
* @author Armin Schnabel
* @version 1.0.1
* @since 1.0.0
*/
class SynchronizationInterruptedException : Exception {
/**
* @param exception The `Exception` that caused this one.
*/
constructor(exception: Exception) : super(exception)

/**
* @param detailedMessage A more detailed message explaining the context for this `Exception`.
*/
Expand Down
27 changes: 27 additions & 0 deletions src/main/kotlin/de/cyface/uploader/exception/TooFewLocations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 Cyface GmbH
*
* This file is part of the Cyface Uploader.
*
* The Cyface Uploader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Cyface Uploader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Cyface Uploader. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cyface.uploader.exception

/**
* Exception thrown when the client tries to upload a file with the locationCount smaller than required.
*
* @author Armin Schnabel
* @param message Details about the reason for this exception.
*/
class TooFewLocations(message: String) : Exception(message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 Cyface GmbH
*
* This file is part of the Cyface Uploader.
*
* The Cyface Uploader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Cyface Uploader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Cyface Uploader. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cyface.uploader.exception

/**
* Exception thrown when the client tries to upload a file in an unknown formatVersion.
*
* @author Armin Schnabel
* @param message Details about the reason for this exception.
*/
class UnknownFormatVersion(message: String) : Exception(message)
4 changes: 1 addition & 3 deletions src/main/kotlin/de/cyface/uploader/exception/UploadFailed.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Cyface GmbH
* Copyright 2023-2024 Cyface GmbH
*
* This file is part of the Cyface Uploader.
*
Expand All @@ -22,8 +22,6 @@ package de.cyface.uploader.exception
* An `Exception` thrown when there is an expected error with the upload request so the UI can handle this.
*
* @author Armin Schnabel
* @version 1.0.0
* @since 7.7.0
*/
class UploadFailed : Exception {
/**
Expand Down
Loading

0 comments on commit 7e3453b

Please sign in to comment.