Search in sources :

Example 1 with ContentAccessListing

use of org.candlepin.resource.dto.ContentAccessListing in project candlepin by candlepin.

the class ConsumerResource method getContentAccessBody.

@ApiOperation(notes = "Retrieves the body of the Content Access Certificate for the Consumer", value = "getContentAccessBody", response = String.class)
@ApiResponses({ @ApiResponse(code = 404, message = ""), @ApiResponse(code = 304, message = "") })
@GET
@Path("{consumer_uuid}/accessible_content")
@Produces(MediaType.APPLICATION_JSON)
public Response getContentAccessBody(@PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @HeaderParam("If-Modified-Since") @DefaultValue("Thu, 01 Jan 1970 00:00:00 GMT") @DateFormat({ "EEE, dd MMM yyyy HH:mm:ss z" }) Date since) {
    log.debug("Getting content access certificate for consumer: {}", consumerUuid);
    Consumer consumer = consumerCurator.verifyAndLookupConsumer(consumerUuid);
    ConsumerType ctype = this.consumerTypeCurator.getConsumerType(consumer);
    if (ctype.isType(ConsumerTypeEnum.SHARE)) {
        throw new BadRequestException(i18n.tr("Content access body can not be requested for a share consumer"));
    }
    Owner owner = ownerCurator.findOwnerById(consumer.getOwnerId());
    String cam = owner.getContentAccessMode();
    if (!ContentAccessCertServiceAdapter.ORG_ENV_ACCESS_MODE.equals(cam)) {
        throw new BadRequestException(i18n.tr("Content access mode does not allow this request."));
    }
    if (!contentAccessCertService.hasCertChangedSince(consumer, since)) {
        return Response.status(Response.Status.NOT_MODIFIED).entity("Not modified since date supplied.").build();
    }
    ContentAccessListing result = new ContentAccessListing();
    try {
        ContentAccessCertificate cac = contentAccessCertService.getCertificate(consumer);
        if (cac == null) {
            throw new BadRequestException(i18n.tr("Cannot retrieve content access certificate"));
        }
        String cert = cac.getCert();
        String certificate = cert.substring(0, cert.indexOf("-----BEGIN ENTITLEMENT DATA-----\n"));
        String json = cert.substring(cert.indexOf("-----BEGIN ENTITLEMENT DATA-----\n"));
        List<String> pieces = new ArrayList<>();
        pieces.add(certificate);
        pieces.add(json);
        result.setContentListing(cac.getSerial().getId(), pieces);
        result.setLastUpdate(cac.getUpdated());
    } catch (IOException ioe) {
        throw new BadRequestException(i18n.tr("Cannot retrieve content access certificate"), ioe);
    } catch (GeneralSecurityException gse) {
        throw new BadRequestException(i18n.tr("Cannot retrieve content access certificate", gse));
    }
    return Response.ok(result, MediaType.APPLICATION_JSON).build();
}
Also used : Owner(org.candlepin.model.Owner) DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) GeneralSecurityException(java.security.GeneralSecurityException) ContentAccessListing(org.candlepin.resource.dto.ContentAccessListing) ArrayList(java.util.ArrayList) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ContentAccessCertificate(org.candlepin.model.ContentAccessCertificate) IOException(java.io.IOException) ConsumerType(org.candlepin.model.ConsumerType) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ArrayList (java.util.ArrayList)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 BadRequestException (org.candlepin.common.exceptions.BadRequestException)1 Consumer (org.candlepin.model.Consumer)1 ConsumerType (org.candlepin.model.ConsumerType)1 ContentAccessCertificate (org.candlepin.model.ContentAccessCertificate)1 DeletedConsumer (org.candlepin.model.DeletedConsumer)1 Owner (org.candlepin.model.Owner)1 ContentAccessListing (org.candlepin.resource.dto.ContentAccessListing)1