-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
824 enforce official supported integration hostname (#833)
Co-authored-by: Gaëtan Muller <[email protected]>
- Loading branch information
Showing
15 changed files
with
314 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...usiness/src/main/java/ch/srgssr/pillarbox/core/business/integrationlayer/service/IlUrl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) SRG SSR. All rights reserved. | ||
* License information is available from the LICENSE file. | ||
*/ | ||
package ch.srgssr.pillarbox.core.business.integrationlayer.service | ||
|
||
import android.net.Uri | ||
import ch.srgssr.pillarbox.core.business.integrationlayer.data.isValidMediaUrn | ||
|
||
/** | ||
* @property host The [IlHost] to use. | ||
* @property urn The URN of the media to request. | ||
* @property vector The [Vector] to use. | ||
* @property forceSAM Force SAM usage. | ||
* @property ilLocation The [IlLocation] of the request. | ||
*/ | ||
data class IlUrl( | ||
val host: IlHost, | ||
val urn: String, | ||
val vector: Vector, | ||
val forceSAM: Boolean = false, | ||
val ilLocation: IlLocation? = null, | ||
) { | ||
|
||
init { | ||
require(urn.isValidMediaUrn()) | ||
} | ||
|
||
/** | ||
* [Uri] representation of this [IlUrl]. | ||
*/ | ||
val uri: Uri = Uri.parse(host.baseHostUrl).buildUpon().apply { | ||
if (forceSAM) { | ||
appendEncodedPath("sam") | ||
appendQueryParameter(PARAM_FORCE_SAM, true.toString()) | ||
} | ||
appendEncodedPath(PATH) | ||
appendEncodedPath(urn) | ||
ilLocation?.let { | ||
appendQueryParameter(PARAM_FORCE_LOCATION, it.toString()) | ||
} | ||
appendQueryParameter(PARAM_VECTOR, vector.toString()) | ||
appendQueryParameter(PARAM_ONLY_CHAPTERS, true.toString()) | ||
}.build() | ||
|
||
internal companion object { | ||
private const val PARAM_ONLY_CHAPTERS = "onlyChapters" | ||
private const val PARAM_FORCE_SAM = "forceSAM" | ||
private const val PARAM_FORCE_LOCATION = "forceLocation" | ||
private const val PARAM_VECTOR = "vector" | ||
private const val PATH = "integrationlayer/2.1/mediaComposition/byUrn/" | ||
|
||
/** | ||
* Converts an [Uri] into a valid [IlUrl]. | ||
* | ||
* @return An [IlUrl] or throws an [IllegalArgumentException] if the [Uri] can't be parsed. | ||
*/ | ||
internal fun Uri.toIlUrl(): IlUrl { | ||
val urn = lastPathSegment | ||
require(urn.isValidMediaUrn()) { "Invalid URN $urn found in $this" } | ||
val host = IlHost.parse(toString()) | ||
requireNotNull(host) { "Invalid URL $this" } | ||
val forceSAM = getQueryParameter(PARAM_FORCE_SAM)?.toBooleanStrictOrNull() == true || pathSegments.contains("sam") | ||
val ilLocation = getQueryParameter(PARAM_FORCE_LOCATION)?.let { IlLocation.fromName(it) } | ||
val vector = getQueryParameter(PARAM_VECTOR)?.let { Vector.fromLabel(it) } ?: Vector.MOBILE | ||
|
||
return IlUrl(host = host, urn = checkNotNull(urn), vector = vector, ilLocation = ilLocation, forceSAM = forceSAM) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.