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();
}
Aggregations