Skip to content

Commit

Permalink
return an appropriate error when trying to create an entity version t…
Browse files Browse the repository at this point in the history
…hat arleady exists
  • Loading branch information
EricWittmann committed Aug 5, 2015
1 parent e12346a commit db4f108
Show file tree
Hide file tree
Showing 15 changed files with 309 additions and 16 deletions.
Expand Up @@ -109,6 +109,7 @@
import io.apiman.manager.api.rest.contract.exceptions.AbstractRestException;
import io.apiman.manager.api.rest.contract.exceptions.ApplicationAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ApplicationNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.ApplicationVersionAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ApplicationVersionNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.ContractAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ContractNotFoundException;
Expand All @@ -122,13 +123,15 @@
import io.apiman.manager.api.rest.contract.exceptions.OrganizationNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.PlanAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.PlanNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.PlanVersionAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.PlanVersionNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.PolicyDefinitionNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.PolicyNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.RoleNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.ServiceAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ServiceDefinitionNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.ServiceNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.ServiceVersionAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ServiceVersionNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.SystemErrorException;
import io.apiman.manager.api.rest.contract.exceptions.UserNotFoundException;
Expand Down Expand Up @@ -513,8 +516,9 @@ public void updateApp(String organizationId, String applicationId, UpdateApplica
* @see io.apiman.manager.api.rest.contract.IOrganizationResource#createAppVersion(java.lang.String, java.lang.String, io.apiman.manager.api.beans.apps.NewApplicationVersionBean)
*/
@Override
public ApplicationVersionBean createAppVersion(String organizationId, String applicationId, NewApplicationVersionBean bean)
throws ApplicationNotFoundException, NotAuthorizedException, InvalidVersionException {
public ApplicationVersionBean createAppVersion(String organizationId, String applicationId,
NewApplicationVersionBean bean) throws ApplicationNotFoundException, NotAuthorizedException,
InvalidVersionException, ApplicationVersionAlreadyExistsException {
if (!securityContext.hasPermission(PermissionType.appEdit, organizationId))
throw ExceptionFactory.notAuthorizedException();
FieldValidator.validateVersion(bean.getVersion());
Expand All @@ -527,6 +531,10 @@ public ApplicationVersionBean createAppVersion(String organizationId, String app
throw ExceptionFactory.applicationNotFoundException(applicationId);
}

if (storage.getApplicationVersion(organizationId, applicationId, bean.getVersion()) != null) {
throw ExceptionFactory.applicationVersionAlreadyExistsException(applicationId, bean.getVersion());
}

newVersion = createAppVersionInternal(bean, application);
storage.commitTx();
} catch (AbstractRestException e) {
Expand Down Expand Up @@ -601,8 +609,9 @@ public ApplicationVersionBean getAppVersion(String organizationId, String applic
try {
storage.beginTx();
ApplicationVersionBean applicationVersion = storage.getApplicationVersion(organizationId, applicationId, version);
if (applicationVersion == null)
if (applicationVersion == null) {
throw ExceptionFactory.applicationVersionNotFoundException(applicationId, version);
}
storage.commitTx();
log.debug(String.format("Got new application version %s: %s", applicationVersion.getApplication().getName(), applicationVersion)); //$NON-NLS-1$
return applicationVersion;
Expand Down Expand Up @@ -1310,8 +1319,9 @@ public void updateService(String organizationId, String serviceId, UpdateService
* @see io.apiman.manager.api.rest.contract.IOrganizationResource#createServiceVersion(java.lang.String, java.lang.String, io.apiman.manager.api.beans.services.NewServiceVersionBean)
*/
@Override
public ServiceVersionBean createServiceVersion(String organizationId, String serviceId, NewServiceVersionBean bean)
throws ServiceNotFoundException, NotAuthorizedException, InvalidVersionException {
public ServiceVersionBean createServiceVersion(String organizationId, String serviceId,
NewServiceVersionBean bean) throws ServiceNotFoundException, NotAuthorizedException,
InvalidVersionException, ServiceVersionAlreadyExistsException {
if (!securityContext.hasPermission(PermissionType.svcEdit, organizationId))
throw ExceptionFactory.notAuthorizedException();
FieldValidator.validateVersion(bean.getVersion());
Expand All @@ -1326,6 +1336,10 @@ public ServiceVersionBean createServiceVersion(String organizationId, String ser
throw ExceptionFactory.serviceNotFoundException(serviceId);
}

if (storage.getServiceVersion(organizationId, serviceId, bean.getVersion()) != null) {
throw ExceptionFactory.serviceVersionAlreadyExistsException(serviceId, bean.getVersion());
}

newVersion = createServiceVersionInternal(bean, service, gateway);
storage.commitTx();
} catch (AbstractRestException e) {
Expand Down Expand Up @@ -2307,7 +2321,8 @@ public void updatePlan(String organizationId, String planId, UpdatePlanBean bean
*/
@Override
public PlanVersionBean createPlanVersion(String organizationId, String planId, NewPlanVersionBean bean)
throws PlanNotFoundException, NotAuthorizedException, InvalidVersionException {
throws PlanNotFoundException, NotAuthorizedException, InvalidVersionException,
PlanVersionAlreadyExistsException {
if (!securityContext.hasPermission(PermissionType.planEdit, organizationId))
throw ExceptionFactory.notAuthorizedException();
FieldValidator.validateVersion(bean.getVersion());
Expand All @@ -2320,6 +2335,10 @@ public PlanVersionBean createPlanVersion(String organizationId, String planId, N
throw ExceptionFactory.planNotFoundException(planId);
}

if (storage.getPlanVersion(organizationId, planId, bean.getVersion()) != null) {
throw ExceptionFactory.planVersionAlreadyExistsException(planId, bean.getVersion());
}

newVersion = createPlanVersionInternal(bean, plan);
storage.commitTx();
} catch (AbstractRestException e) {
Expand Down
Expand Up @@ -19,6 +19,7 @@
import io.apiman.manager.api.rest.contract.exceptions.ActionException;
import io.apiman.manager.api.rest.contract.exceptions.ApplicationAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ApplicationNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.ApplicationVersionAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ApplicationVersionNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.ContractAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ContractNotFoundException;
Expand All @@ -35,6 +36,7 @@
import io.apiman.manager.api.rest.contract.exceptions.OrganizationNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.PlanAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.PlanNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.PlanVersionAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.PlanVersionNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.PluginAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.PluginNotFoundException;
Expand All @@ -48,6 +50,7 @@
import io.apiman.manager.api.rest.contract.exceptions.ServiceAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ServiceDefinitionNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.ServiceNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.ServiceVersionAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ServiceVersionNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.UserNotFoundException;
import io.apiman.manager.api.rest.impl.i18n.Messages;
Expand Down Expand Up @@ -121,6 +124,16 @@ public static final ApplicationAlreadyExistsException applicationAlreadyExistsEx
return new ApplicationAlreadyExistsException(Messages.i18n.format("ApplicationAlreadyExists", applicationName)); //$NON-NLS-1$
}

/**
* Creates an exception from an application name.
* @param applicationName the application name
* @param version the version
* @return the exception
*/
public static final ApplicationVersionAlreadyExistsException applicationVersionAlreadyExistsException(String applicationName, String version) {
return new ApplicationVersionAlreadyExistsException(Messages.i18n.format("ApplicationVersionAlreadyExists", applicationName, version)); //$NON-NLS-1$
}

/**
* Creates an exception.
* @return the exception
Expand Down Expand Up @@ -174,6 +187,16 @@ public static final ServiceAlreadyExistsException serviceAlreadyExistsException(
return new ServiceAlreadyExistsException(Messages.i18n.format("ServiceAlreadyExists", serviceName)); //$NON-NLS-1$
}

/**
* Creates an exception from an service name.
* @param serviceName the service name
* @param version the version
* @return the exception
*/
public static final ServiceVersionAlreadyExistsException serviceVersionAlreadyExistsException(String serviceName, String version) {
return new ServiceVersionAlreadyExistsException(Messages.i18n.format("ServiceVersionAlreadyExists", serviceName, version)); //$NON-NLS-1$
}

/**
* Creates an exception from an service id.
* @param serviceId the service id
Expand Down Expand Up @@ -237,6 +260,16 @@ public static final PlanAlreadyExistsException planAlreadyExistsException(String
return new PlanAlreadyExistsException(Messages.i18n.format("PlanAlreadyExists", planName)); //$NON-NLS-1$
}

/**
* Creates an exception from an plan name.
* @param planName the plan name
* @param version the version
* @return the exception
*/
public static final PlanVersionAlreadyExistsException planVersionAlreadyExistsException(String planName, String version) {
return new PlanVersionAlreadyExistsException(Messages.i18n.format("PlanVersionAlreadyExists", planName, version)); //$NON-NLS-1$
}

/**
* Creates an exception from an plan id.
* @param planId the plan id
Expand Down
Expand Up @@ -13,6 +13,9 @@ ApplicationAlreadyExists=Application already exists: {0}
ApplicationDoesNotExist=Application does not exist: {0}
ApplicationVersionDoesNotExist=Application Version does not exist: {0}/{1}
ServiceAlreadyExists=Service already exists: {0}
ApplicationVersionAlreadyExists=Application version already exists: {0} version {1}
PlanVersionAlreadyExists=Plan version already exists: {0} version {1}
ServiceVersionAlreadyExists=Service version already exists: {0} version {1}
ServiceDoesNotExist=Service does not exist: {0}
ServiceVersionDoesNotExist=Service Version does not exist: {0}/{1}
ServiceDefinitionDoesNotExist=Service definition does not exist: {0}/{1}
Expand Down
Expand Up @@ -67,6 +67,7 @@
import io.apiman.manager.api.beans.summary.ServiceVersionSummaryBean;
import io.apiman.manager.api.rest.contract.exceptions.ApplicationAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ApplicationNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.ApplicationVersionAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ApplicationVersionNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.ContractAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ContractNotFoundException;
Expand All @@ -80,11 +81,13 @@
import io.apiman.manager.api.rest.contract.exceptions.OrganizationNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.PlanAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.PlanNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.PlanVersionAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.PlanVersionNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.PolicyNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.RoleNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.ServiceAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ServiceNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.ServiceVersionAlreadyExistsException;
import io.apiman.manager.api.rest.contract.exceptions.ServiceVersionNotFoundException;
import io.apiman.manager.api.rest.contract.exceptions.UserNotFoundException;

Expand Down Expand Up @@ -294,6 +297,7 @@ public void updateApp(@PathParam("organizationId") String organizationId,
* @param bean Initial information about the new Application version.
* @statuscode 200 If the Application version is created successfully.
* @statuscode 404 If the Application does not exist.
* @statuscode 409 If the Application version already exists.
* @return Full details about the newly created Application version.
* @throws ApplicationNotFoundException when trying to get, update, or delete an application that does not exist
* @throws NotAuthorizedException when the user attempts to do or see something that they are not authorized (do not have permission) to
Expand All @@ -305,7 +309,8 @@ public void updateApp(@PathParam("organizationId") String organizationId,
@Produces(MediaType.APPLICATION_JSON)
public ApplicationVersionBean createAppVersion(@PathParam("organizationId") String organizationId,
@PathParam("applicationId") String applicationId, NewApplicationVersionBean bean)
throws ApplicationNotFoundException, NotAuthorizedException, InvalidVersionException;
throws ApplicationNotFoundException, NotAuthorizedException, InvalidVersionException,
ApplicationVersionAlreadyExistsException;

/**
* Use this endpoint to list all of the versions of an Application.
Expand Down Expand Up @@ -806,6 +811,7 @@ public SearchResultsBean<AuditEntryBean> getServiceActivity(
* @param bean Initial information about the new Service version.
* @statuscode 200 If the Service version is created successfully.
* @statuscode 404 If the Service does not exist.
* @statuscode 409 If the Service version already exists.
* @return Full details about the newly created Service version.
* @throws ServiceNotFoundException when trying to get, update, or delete an service that does not exist
* @throws NotAuthorizedException when the user attempts to do or see something that they are not authorized (do not have permission) to
Expand All @@ -817,7 +823,8 @@ public SearchResultsBean<AuditEntryBean> getServiceActivity(
@Produces(MediaType.APPLICATION_JSON)
public ServiceVersionBean createServiceVersion(@PathParam("organizationId") String organizationId,
@PathParam("serviceId") String serviceId, NewServiceVersionBean bean)
throws ServiceNotFoundException, NotAuthorizedException, InvalidVersionException;
throws ServiceNotFoundException, NotAuthorizedException, InvalidVersionException,
ServiceVersionAlreadyExistsException;

/**
* Use this endpoint to list all of the versions of a Service.
Expand Down Expand Up @@ -1331,6 +1338,7 @@ public void updatePlan(@PathParam("organizationId") String organizationId,
* @param bean Initial information about the new Plan version.
* @statuscode 200 If the Plan version is created successfully.
* @statuscode 404 If the Plan does not exist.
* @statuscode 409 If the Plan version already exists.
* @return Full details about the newly created Plan version.
* when trying to get, update, or delete an plan that does not exist
* @throws PlanNotFoundException when trying to get, update, or delete an plan that does not exist
Expand All @@ -1342,8 +1350,8 @@ public void updatePlan(@PathParam("organizationId") String organizationId,
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public PlanVersionBean createPlanVersion(@PathParam("organizationId") String organizationId,
@PathParam("planId") String planId, NewPlanVersionBean bean)
throws PlanNotFoundException, NotAuthorizedException, InvalidVersionException;
@PathParam("planId") String planId, NewPlanVersionBean bean) throws PlanNotFoundException,
NotAuthorizedException, InvalidVersionException, PlanVersionAlreadyExistsException;

/**
* Use this endpoint to list all of the versions of a Plan.
Expand Down
@@ -0,0 +1,59 @@
/*
* Copyright 2014 JBoss Inc
*
* Licensed 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 io.apiman.manager.api.rest.contract.exceptions;


/**
* Thrown when trying to create an Application that already exists.
*
* @author eric.wittmann@redhat.com
*/
public class ApplicationVersionAlreadyExistsException extends AbstractAlreadyExistsException {

private static final long serialVersionUID = -7790690590728305808L;

/**
* Constructor.
*/
public ApplicationVersionAlreadyExistsException() {
}

/**
* Constructor.
* @param message the message
*/
public ApplicationVersionAlreadyExistsException(String message) {
super(message);
}

/**
* @see io.apiman.manager.api.rest.contract.exceptions.AbstractRestException#getErrorCode()
*/
@Override
public int getErrorCode() {
return ErrorCodes.APP_VERSION_ALREADY_EXISTS;
}

/**
* @see io.apiman.manager.api.rest.contract.exceptions.AbstractRestException#getMoreInfoUrl()
*/
@Override
public String getMoreInfoUrl() {
return ErrorCodes.APP_VERSION_ALREADY_EXISTS_INFO;
}

}

0 comments on commit db4f108

Please sign in to comment.