-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3194 from PenghaiZhang/feature/drm-rest-endpoints
Feature/DRM rest endpoints
- Loading branch information
Showing
10 changed files
with
451 additions
and
2 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
102 changes: 102 additions & 0 deletions
102
Source/Plugins/Core/com.equella.core/scalasrc/com/tle/web/api/drm/DrmResource.scala
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,102 @@ | ||
/* | ||
* Licensed to The Apereo Foundation under one or more contributor license | ||
* agreements. See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* The Apereo Foundation licenses this file to you under the Apache License, | ||
* Version 2.0, (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.tle.web.api.drm | ||
|
||
import com.dytech.edge.exceptions.{DRMException, ItemNotFoundException} | ||
import com.tle.beans.item.{DrmSettings, Item, ItemId} | ||
import com.tle.exceptions.AccessDeniedException | ||
import com.tle.legacy.LegacyGuice | ||
import com.tle.web.api.ApiErrorResponse.{ | ||
badRequest, | ||
forbiddenRequest, | ||
resourceNotFound, | ||
unauthorizedRequest | ||
} | ||
import io.swagger.annotations.{Api, ApiOperation, ApiParam} | ||
import org.jboss.resteasy.annotations.cache.NoCache | ||
import javax.ws.rs.core.Response | ||
import javax.ws.rs.{BadRequestException, GET, NotFoundException, POST, Path, PathParam, Produces} | ||
import scala.util.control.Exception.allCatch | ||
import scala.util.{Failure, Success, Try} | ||
|
||
@NoCache | ||
@Path("item/{uuid}/{version}/drm") | ||
@Produces(Array("application/json")) | ||
@Api("Item DRM") | ||
class DrmResource { | ||
val drmService = LegacyGuice.drmService | ||
|
||
@GET | ||
@ApiOperation( | ||
value = "List DRM terms", | ||
notes = "This endpoint is used to list an Item's DRM terms.", | ||
response = classOf[ItemDrmDetails], | ||
) | ||
def getDrmTerms(@ApiParam("Item UUID") @PathParam("uuid") uuid: String, | ||
@ApiParam("Item Version") @PathParam("version") version: Int): Response = { | ||
val getTerms = (uuid: String, version: Int) => | ||
Try { | ||
getItem.andThen(_.getDrmSettings)(new ItemId(uuid, version)) | ||
} | ||
|
||
val mapToItemDrmDetails = (drm: DrmSettings) => | ||
Try { | ||
Option(drm) match { | ||
case Some(drmSettings) => ItemDrmDetails(drmSettings) | ||
case None => | ||
throw new NotFoundException(s"Failed to find DRM terms for item: $uuid/$version") | ||
} | ||
} | ||
|
||
val result = getTerms(uuid, version) flatMap mapToItemDrmDetails | ||
respond(result) | ||
} | ||
|
||
@POST | ||
@ApiOperation( | ||
value = "Accept DRM terms", | ||
notes = "This endpoint is used to accept an Item's DRM terms.", | ||
response = classOf[Long], | ||
) | ||
def acceptDrm(@ApiParam("Item UUID") @PathParam("uuid") uuid: String, | ||
@ApiParam("Item Version") @PathParam("version") version: Int): Response = { | ||
|
||
val acceptLicense: Item => Long = drmService.acceptLicenseOrThrow | ||
val result = allCatch withTry (getItem andThen acceptLicense)(new ItemId(uuid, version)) | ||
respond(result) | ||
} | ||
|
||
// Take a subtype of Throwable and return a function which takes a sequence of string and returns a Response. | ||
private def mapException[T <: Throwable](e: T): Seq[String] => Response = { | ||
e match { | ||
case _: BadRequestException => badRequest | ||
case _: AccessDeniedException => forbiddenRequest | ||
case _: DRMException => unauthorizedRequest | ||
case _ @(_: ItemNotFoundException | _: NotFoundException) => resourceNotFound | ||
} | ||
} | ||
|
||
private def respond[T](attempt: Try[T]): Response = | ||
attempt match { | ||
case Success(result) => Response.ok().entity(result).build() | ||
case Failure(e) => mapException(e)(Seq(e.getMessage)) | ||
} | ||
|
||
private val getItem: ItemId => Item = LegacyGuice.itemService.getUnsecure | ||
} |
99 changes: 99 additions & 0 deletions
99
Source/Plugins/Core/com.equella.core/scalasrc/com/tle/web/api/drm/ItemDrmDetails.scala
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,99 @@ | ||
/* | ||
* Licensed to The Apereo Foundation under one or more contributor license | ||
* agreements. See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* The Apereo Foundation licenses this file to you under the Apache License, | ||
* Version 2.0, (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.tle.web.api.drm | ||
|
||
import com.tle.beans.item.DrmSettings | ||
import com.tle.core.i18n.CoreStrings | ||
import com.tle.web.viewitem.I18nDRM | ||
import scala.collection.JavaConverters._ | ||
|
||
case class DrmParties( | ||
/** Server side language string for DRM party. */ | ||
title: String, | ||
/** A list of text consisting each party's name and email. */ | ||
partyList: List[String]) | ||
|
||
case class DrmCustomTerms( | ||
/** Server side language string for DRM terms. */ | ||
title: String, | ||
/** Terms of using the Item. */ | ||
terms: String) | ||
|
||
case class DrmAgreements( | ||
/** Text describing what regular permissions are granted to the user. */ | ||
regularPermission: Option[String], | ||
/** Text describing what additional permissions are granted to the user. */ | ||
additionalPermission: Option[String], | ||
/** Text describing that the use of Item is limited to education sector. */ | ||
educationSector: Option[String], | ||
/** Text describing parties related to the Item. */ | ||
parties: Option[DrmParties], | ||
/** Other terms and conditions applied to the Item. */ | ||
customTerms: Option[DrmCustomTerms] | ||
) | ||
|
||
case class ItemDrmDetails( | ||
/** Server side language string used as the DRM acceptance title */ | ||
title: String = CoreStrings.text("summary.content.termsofuse.title"), | ||
/** Server side language string used as the DRM acceptance subtitle */ | ||
subtitle: String = CoreStrings.text("summary.content.termsofuse.terms.title"), | ||
/** Server side language string used as the DRM acceptance description */ | ||
description: String = CoreStrings.text("summary.content.termsofuse.terms.description"), | ||
/** All terms and conditions that user must accept to use the Item */ | ||
agreements: DrmAgreements) | ||
|
||
object ItemDrmDetails { | ||
def buildPermissionText(permissions: String, title: String): Option[String] = { | ||
if (permissions.nonEmpty) Option(s"$title $permissions") else None | ||
} | ||
|
||
def apply(drmSettings: DrmSettings): ItemDrmDetails = { | ||
val drmI18n = new I18nDRM(drmSettings) | ||
|
||
val customTerms = | ||
Option(drmI18n.getTerms).map(terms => | ||
DrmCustomTerms(CoreStrings.text("drm.mustagree"), terms)) | ||
|
||
val regularPermission = | ||
buildPermissionText(drmI18n.getPermissions1List, drmI18n.getItemMayFreelyBeText) | ||
val additionalPermission = | ||
buildPermissionText(drmI18n.getPermissions2List, drmI18n.getAdditionallyUserMayText) | ||
|
||
val educationSector = | ||
if (drmI18n.isUseEducation) Option(drmI18n.getEducationSectorText) else None | ||
|
||
val parties = if (drmI18n.isAttribution && !drmI18n.getParties.isEmpty) { | ||
Option( | ||
DrmParties(drmI18n.getAttributeOwnersText, | ||
drmI18n.getParties.asScala.map(p => s"${p.getName} ${p.getEmail}").toList)) | ||
} else { | ||
None | ||
} | ||
|
||
val agreements = DrmAgreements( | ||
regularPermission, | ||
additionalPermission, | ||
educationSector, | ||
parties, | ||
customTerms | ||
) | ||
|
||
ItemDrmDetails(agreements = agreements) | ||
} | ||
} |
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.