use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class OwnerResource method importManifest.
/**
* Imports a manifest zip file for the given organization.
*
* This will bring in any products, content, and subscriptions that were assigned to
* the distributor who generated the manifest.
*
* @deprecated use GET /owners/:owner_key/imports/async
*
* @return a ImportRecord object if the import is successful.
* @httpcode 400
* @httpcode 404
* @httpcode 500
* @httpcode 200
* @httpcode 409
*/
@POST
@Path("{owner_key}/imports")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ApiOperation(notes = "Imports a manifest zip file for the given organization. " + "This will bring in any products, content, and subscriptions that were " + "assigned to the distributor who generated the manifest.", value = "Import Manifest")
@ApiResponses({ @ApiResponse(code = 400, message = ""), @ApiResponse(code = 404, message = "Owner not found"), @ApiResponse(code = 500, message = ""), @ApiResponse(code = 409, message = "") })
@Deprecated
public ImportRecord importManifest(@PathParam("owner_key") @Verify(Owner.class) String ownerKey, @QueryParam("force") String[] overrideConflicts, MultipartInput input) {
ConflictOverrides overrides = processConflictOverrideParams(overrideConflicts);
UploadMetadata fileData = new UploadMetadata();
Owner owner = findOwnerByKey(ownerKey);
try {
fileData = getArchiveFromResponse(input);
return manifestManager.importManifest(owner, fileData.getData(), fileData.getUploadedFilename(), overrides);
} catch (IOException e) {
log.error("Reading error during importing", e);
manifestManager.recordImportFailure(owner, e, fileData.getUploadedFilename());
throw new IseException(i18n.tr("Error reading export archive"), e);
}// These come back with internationalized messages, so we can transfer:
catch (SyncDataFormatException e) {
log.error("Format error of the data in a manifest", e);
manifestManager.recordImportFailure(owner, e, fileData.getUploadedFilename());
throw new BadRequestException(e.getMessage(), e);
} catch (ImporterException e) {
log.error("Problem with archive", e);
manifestManager.recordImportFailure(owner, e, fileData.getUploadedFilename());
throw new IseException(e.getMessage(), e);
}// to pass on the http return code
catch (CandlepinException e) {
log.error("Recording import failure", e);
manifestManager.recordImportFailure(owner, e, fileData.getUploadedFilename());
throw e;
} finally {
log.info("Import attempt completed for owner {}", owner.getDisplayName());
}
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class OwnerResource method deleteOwner.
/**
* Removes an Owner
*
* @httpcode 404
* @httpcode 200
*/
@DELETE
@Path("/{owner_key}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(notes = "Removes an Owner", value = "Delete Owner")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found") })
public void deleteOwner(@PathParam("owner_key") String ownerKey, @QueryParam("revoke") @DefaultValue("true") boolean revoke, @QueryParam("force") @DefaultValue("false") boolean force) {
Owner owner = findOwnerByKey(ownerKey);
Event event = eventFactory.ownerDeleted(owner);
if (!force && consumerCurator.doesShareConsumerExist(owner)) {
throw new BadRequestException(i18n.tr("owner \"{0}\" cannot be deleted while there is a share consumer. " + "You may use \"force\" to bypass.", owner.getKey()));
}
try {
ownerManager.cleanupAndDelete(owner, revoke);
} catch (PersistenceException e) {
if (e.getCause() instanceof ConstraintViolationException) {
throw new ConflictException(e.getMessage(), e);
} else {
throw e;
}
}
sink.queueEvent(event);
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class OwnerResource method listPools.
/**
* Retrieves a list of Pools for an Owner
*
* @param ownerKey id of the owner whose entitlement pools are sought.
* @param matches Find pools matching the given pattern in a variety of fields.
* * and ? wildcards are supported.
* @return a list of Pool objects
* @httpcode 400
* @httpcode 404
* @httpcode 200
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{owner_key}/pools")
@SuppressWarnings("checkstyle:indentation")
@ApiOperation(notes = "Retrieves a list of Pools for an Owner", value = "List Pools")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found"), @ApiResponse(code = 400, message = "Invalid request") })
public List<PoolDTO> listPools(@PathParam("owner_key") @Verify(value = Owner.class, subResource = SubResource.POOLS) String ownerKey, @QueryParam("consumer") String consumerUuid, @QueryParam("activation_key") String activationKeyName, @QueryParam("product") String productId, @QueryParam("subscription") String subscriptionId, @ApiParam("Include pools that are not suited to the unit's facts.") @QueryParam("listall") @DefaultValue("false") boolean listAll, @ApiParam("Date to use as current time for lookup criteria. Defaults" + " to current date if not specified.") @QueryParam("activeon") @DefaultValue(DateFormat.NOW) @DateFormat Date activeOn, @ApiParam("Find pools matching the given pattern in a variety of fields;" + " * and ? wildcards are supported; may be specified multiple times") @QueryParam("matches") List<String> matches, @ApiParam("The attributes to return based on the specified types.") @QueryParam("attribute") @CandlepinParam(type = KeyValueParameter.class) List<KeyValueParameter> attrFilters, @ApiParam("When set to true, it will add future dated pools to the result, " + "based on the activeon date.") @QueryParam("add_future") @DefaultValue("false") boolean addFuture, @ApiParam("When set to true, it will return only future dated pools to the result, " + "based on the activeon date.") @QueryParam("only_future") @DefaultValue("false") boolean onlyFuture, @ApiParam("Will only return pools with a start date after the supplied date. " + "Overrides the activeOn date.") @QueryParam("after") @DateFormat Date after, @ApiParam("One or more pool IDs to use to filter the output; only pools with IDs matching " + "those provided will be returned; may be specified multiple times") @QueryParam("poolid") List<String> poolIds, @Context Principal principal, @Context PageRequest pageRequest) {
Owner owner = findOwnerByKey(ownerKey);
Consumer c = null;
if (consumerUuid != null) {
c = consumerCurator.findByUuid(consumerUuid);
if (c == null) {
throw new NotFoundException(i18n.tr("Unit: {0} not found", consumerUuid));
}
if (!c.getOwnerId().equals(owner.getId())) {
throw new BadRequestException("Consumer specified does not belong to owner on path");
}
if (!principal.canAccess(c, SubResource.NONE, Access.READ_ONLY)) {
throw new ForbiddenException(i18n.tr("User {0} cannot access consumer {1}", principal.getPrincipalName(), c.getUuid()));
}
}
ActivationKey key = null;
if (activationKeyName != null) {
key = activationKeyCurator.lookupForOwner(activationKeyName, owner);
if (key == null) {
throw new BadRequestException(i18n.tr("ActivationKey with id {0} could not be found.", activationKeyName));
}
}
if (addFuture && onlyFuture) {
throw new BadRequestException(i18n.tr("The flags add_future and only_future cannot be used at the same time."));
}
if (after != null && (addFuture || onlyFuture)) {
throw new BadRequestException(i18n.tr("The flags add_future and only_future cannot be used with the parameter after."));
}
if (after != null) {
activeOn = null;
}
// Process the filters passed for the attributes
PoolFilterBuilder poolFilters = new PoolFilterBuilder();
for (KeyValueParameter filterParam : attrFilters) {
poolFilters.addAttributeFilter(filterParam.key(), filterParam.value());
}
if (matches != null) {
matches.stream().filter(elem -> elem != null && !elem.isEmpty()).forEach(elem -> poolFilters.addMatchesFilter(elem));
}
if (poolIds != null && !poolIds.isEmpty()) {
poolFilters.addIdFilters(poolIds);
}
Page<List<Pool>> page = poolManager.listAvailableEntitlementPools(c, key, owner.getId(), productId, subscriptionId, activeOn, listAll, poolFilters, pageRequest, addFuture, onlyFuture, after);
List<Pool> poolList = page.getPageData();
calculatedAttributesUtil.setCalculatedAttributes(poolList, activeOn);
calculatedAttributesUtil.setQuantityAttributes(poolList, c, activeOn);
// Store the page for the LinkHeaderResponseFilter
ResteasyProviderFactory.pushContext(Page.class, page);
List<PoolDTO> poolDTOs = new ArrayList<>();
for (Pool pool : poolList) {
poolDTOs.add(translator.translate(pool, PoolDTO.class));
}
return poolDTOs;
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class PoolResource method deletePool.
@ApiOperation(notes = "Remove a Pool", value = "deletePool")
@ApiResponses({ @ApiResponse(code = 404, message = "if the pool with the specified id is not found") })
@DELETE
@Path("/{pool_id}")
@Produces(MediaType.APPLICATION_JSON)
public void deletePool(@PathParam("pool_id") String id) {
Pool pool = poolManager.find(id);
if (pool == null) {
throw new NotFoundException(i18n.tr("Entitlement Pool with ID \"{0}\" could not be found.", id));
}
if (pool.getType() != PoolType.NORMAL) {
throw new BadRequestException(i18n.tr("Cannot delete bonus pools, as they are auto generated"));
}
if (pool.isCreatedByShare()) {
throw new BadRequestException(i18n.tr("Cannot delete shared pools, This should be triggered by" + " deleting the share entitlement instead"));
}
poolManager.deletePools(Collections.singleton(pool));
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class ConsumerResource method setupOwner.
private Owner setupOwner(Principal principal, String ownerKey) {
// has admin rights for. If more than one, we have to error out.
if (ownerKey == null && (principal instanceof UserPrincipal)) {
// check for this cast?
List<String> ownerKeys = ((UserPrincipal) principal).getOwnerKeys();
if (ownerKeys.size() != 1) {
throw new BadRequestException(i18n.tr("You must specify an organization for new units."));
}
ownerKey = ownerKeys.get(0);
}
createOwnerIfNeeded(principal);
Owner owner = ownerCurator.lookupByKey(ownerKey);
if (owner == null) {
throw new BadRequestException(i18n.tr("Organization {0} does not exist.", ownerKey));
}
// Check permissions for current principal on the owner:
if ((principal instanceof UserPrincipal) && !principal.canAccess(owner, SubResource.CONSUMERS, Access.CREATE)) {
log.warn("User {} does not have access to create consumers in org {}", principal.getPrincipalName(), owner.getKey());
throw new NotFoundException(i18n.tr("owner with key: {0} was not found.", owner.getKey()));
}
return owner;
}
Aggregations