Skip to content

Commit ac6392c

Browse files
authored
Add a retrieveLicenseDocument method to LcpService (#519)
1 parent a91e76f commit ac6392c

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

readium/lcp/src/main/java/org/readium/r2/lcp/LcpService.kt

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.readium.r2.shared.publication.protection.ContentProtection
2828
import org.readium.r2.shared.util.Try
2929
import org.readium.r2.shared.util.asset.Asset
3030
import org.readium.r2.shared.util.asset.AssetRetriever
31+
import org.readium.r2.shared.util.asset.ContainerAsset
3132
import org.readium.r2.shared.util.format.Format
3233

3334
/**
@@ -118,6 +119,13 @@ public interface LcpService {
118119
allowUserInteraction: Boolean
119120
): Try<LcpLicense, LcpError>
120121

122+
/**
123+
* Retrieves the license document from a LCP-protected publication asset.
124+
*/
125+
public suspend fun retrieveLicenseDocument(
126+
asset: ContainerAsset
127+
): Try<LicenseDocument, LcpError>
128+
121129
/**
122130
* Injects a [licenseDocument] into the given [publicationFile] package.
123131
*

readium/lcp/src/main/java/org/readium/r2/lcp/service/LicensesService.kt

+16
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import org.readium.r2.shared.util.FileExtension
4242
import org.readium.r2.shared.util.Try
4343
import org.readium.r2.shared.util.asset.Asset
4444
import org.readium.r2.shared.util.asset.AssetRetriever
45+
import org.readium.r2.shared.util.asset.ContainerAsset
4546
import org.readium.r2.shared.util.format.Format
4647
import org.readium.r2.shared.util.format.FormatHints
4748
import org.readium.r2.shared.util.format.FormatSpecification
@@ -243,6 +244,21 @@ internal class LicensesService(
243244
Try.failure(LcpError.wrap(e))
244245
}
245246

247+
override suspend fun retrieveLicenseDocument(
248+
asset: ContainerAsset
249+
): Try<LicenseDocument, LcpError> =
250+
withContext(Dispatchers.IO) {
251+
try {
252+
val licenseContainer = createLicenseContainer(context, asset)
253+
val licenseData = licenseContainer.read()
254+
Try.success(LicenseDocument(licenseData))
255+
} catch (e: CancellationException) {
256+
throw e
257+
} catch (e: Exception) {
258+
Try.failure(LcpError.wrap(e))
259+
}
260+
}
261+
246262
private suspend fun retrieveLicense(
247263
container: LicenseContainer,
248264
authentication: LcpAuthenticating,

0 commit comments

Comments
 (0)