Update rest API with new exceptions, handling status codes better
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/b9d2e3e0 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/b9d2e3e0 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/b9d2e3e0 Branch: refs/heads/master Commit: b9d2e3e047064edb2a89f0eabdc8aaf0fbaea80f Parents: 2fb25df Author: Lahiru Sandaruwan <[email protected]> Authored: Wed Apr 29 15:16:00 2015 +0530 Committer: Lahiru Sandaruwan <[email protected]> Committed: Wed Apr 29 15:38:11 2015 +0530 ---------------------------------------------------------------------- .../rest/endpoint/api/StratosApiV40Utils.java | 9 +- .../rest/endpoint/api/StratosApiV41.java | 195 +++++++++++++------ .../rest/endpoint/api/StratosApiV41Utils.java | 192 +++++++++--------- 3 files changed, 226 insertions(+), 170 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/b9d2e3e0/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV40Utils.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV40Utils.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV40Utils.java index 2cdea8c..061a988 100644 --- a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV40Utils.java +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV40Utils.java @@ -22,6 +22,7 @@ import org.apache.axis2.AxisFault; import org.apache.axis2.context.ConfigurationContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.stratos.autoscaler.stub.AutoscalerServiceAutoScalingPolicyAlreadyExistExceptionException; import org.apache.stratos.autoscaler.stub.AutoscalerServiceInvalidPolicyExceptionException; import org.apache.stratos.cloud.controller.stub.CloudControllerServiceCartridgeAlreadyExistsExceptionException; import org.apache.stratos.cloud.controller.stub.CloudControllerServiceInvalidCartridgeDefinitionExceptionException; @@ -164,11 +165,9 @@ public class StratosApiV40Utils { } catch (RemoteException e) { log.error(e.getMessage(), e); throw new RestAPIException(e.getMessage(), e); - } catch (AutoscalerServiceInvalidPolicyExceptionException e) { - String message = e.getFaultMessage() - .getInvalidPolicyException().getMessage(); - log.error(message, e); - throw new RestAPIException(message, e); + } catch (AutoscalerServiceAutoScalingPolicyAlreadyExistExceptionException e) { + log.error(e.getMessage(), e); + throw new RestAPIException(e.getMessage(), e); } } http://git-wip-us.apache.org/repos/asf/stratos/blob/b9d2e3e0/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java index c176b9f..1356187 100644 --- a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java @@ -20,6 +20,9 @@ package org.apache.stratos.rest.endpoint.api; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.stratos.autoscaler.stub.*; +import org.apache.stratos.cloud.controller.stub.CloudControllerServiceNetworkPartitionAlreadyExistsExceptionException; +import org.apache.stratos.cloud.controller.stub.CloudControllerServiceNetworkPartitionNotExistsExceptionException; import org.apache.stratos.common.beans.*; import org.apache.stratos.common.beans.application.ApplicationBean; import org.apache.stratos.common.beans.application.ApplicationNetworkPartitionIdListBean; @@ -38,13 +41,12 @@ import org.apache.stratos.common.beans.policy.deployment.ApplicationPolicyBean; import org.apache.stratos.common.beans.policy.deployment.DeploymentPolicyBean; import org.apache.stratos.common.beans.topology.ApplicationInfoBean; import org.apache.stratos.common.beans.topology.ClusterBean; +import org.apache.stratos.common.exception.InvalidEmailException; +import org.apache.stratos.manager.service.stub.StratosManagerServiceDomainMappingExceptionException; import org.apache.stratos.rest.endpoint.Utils; import org.apache.stratos.rest.endpoint.annotation.AuthorizationAction; import org.apache.stratos.rest.endpoint.annotation.SuperTenantService; -import org.apache.stratos.rest.endpoint.exception.ApplicationAlreadyDeployedException; -import org.apache.stratos.rest.endpoint.exception.ApplicationAlreadyExistException; -import org.apache.stratos.rest.endpoint.exception.RestAPIException; -import org.apache.stratos.rest.endpoint.exception.TenantNotFoundException; +import org.apache.stratos.rest.endpoint.exception.*; import org.wso2.carbon.context.PrivilegedCarbonContext; import javax.servlet.http.HttpServletRequest; @@ -155,21 +157,21 @@ public class StratosApiV41 extends AbstractApi { public Response addDeploymentPolicy( DeploymentPolicyBean deploymentPolicyDefinitionBean) throws RestAPIException { + String deploymentPolicyID = deploymentPolicyDefinitionBean.getId(); try { - String deploymentPolicyID = deploymentPolicyDefinitionBean.getId(); // TODO :: Deployment policy validation StratosApiV41Utils.addDeploymentPolicy(deploymentPolicyDefinitionBean); - URI url = uriInfo.getAbsolutePathBuilder().path(deploymentPolicyID).build(); - return Response.created(url).entity(new SuccessResponseBean(Response.Status.CREATED.getStatusCode(), - String.format("Deployment policy added successfully: " + "[deployment-policy-id] %s", - deploymentPolicyID))).build(); } catch (RestAPIException e) { - if (e.getCause().getMessage().contains("already exists")) { - return Response.status(Response.Status.CONFLICT).build(); - } else { - throw e; - } + throw e; + } catch (AutoscalerServiceInvalidDeploymentPolicyExceptionException e) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } catch (AutoscalerServiceDeploymentPolicyAlreadyExistsExceptionException e) { + return Response.status(Response.Status.CONFLICT).build(); } + URI url = uriInfo.getAbsolutePathBuilder().path(deploymentPolicyID).build(); + return Response.created(url).entity(new SuccessResponseBean(Response.Status.CREATED.getStatusCode(), + String.format("Deployment policy added successfully: " + "[deployment-policy-id] %s", + deploymentPolicyID))).build(); } /** @@ -231,7 +233,15 @@ public class StratosApiV41 extends AbstractApi { String deploymentPolicyID = deploymentPolicyDefinitionBean.getId(); // TODO :: Deployment policy validation - StratosApiV41Utils.updateDeploymentPolicy(deploymentPolicyDefinitionBean); + try { + StratosApiV41Utils.updateDeploymentPolicy(deploymentPolicyDefinitionBean); + } catch (AutoscalerServiceInvalidPolicyExceptionException e) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } catch (AutoscalerServiceInvalidDeploymentPolicyExceptionException e) { + return Response.status(Response.Status.NOT_FOUND).build(); + } catch (AutoscalerServiceDeploymentPolicyNotExistsExceptionException e) { + return Response.status(Response.Status.NOT_FOUND).build(); + } URI url = uriInfo.getAbsolutePathBuilder().path(deploymentPolicyID).build(); return Response.ok(url).entity(new SuccessResponseBean(Response.Status.OK.getStatusCode(), String.format("Deployment policy updated successfully: " + "[deployment-policy-id] %s", @@ -253,7 +263,11 @@ public class StratosApiV41 extends AbstractApi { public Response removeDeploymentPolicy( @PathParam("depolymentPolicyID") String deploymentPolicyID) throws RestAPIException { - StratosApiV41Utils.removeDeploymentPolicy(deploymentPolicyID); + try { + StratosApiV41Utils.removeDeploymentPolicy(deploymentPolicyID); + } catch (AutoscalerServiceDeploymentPolicyNotExistsExceptionException e) { + return Response.status(Response.Status.NOT_FOUND).build(); + } URI url = uriInfo.getAbsolutePathBuilder().path(deploymentPolicyID).build(); return Response.ok(url).entity(new SuccessResponseBean(Response.Status.OK.getStatusCode(), String.format("Deployment policy removed successfully: " + "[deployment-policy-id] %s", @@ -517,15 +531,13 @@ public class StratosApiV41 extends AbstractApi { public Response addNetworkPartition( NetworkPartitionBean networkPartitionBean) throws RestAPIException { String networkPartitionId = networkPartitionBean.getId(); - NetworkPartitionBean networkPartition = StratosApiV41Utils.getNetworkPartition(networkPartitionId); - if (networkPartition != null) { - String msg = String - .format("Network partition already exists: [network-partition-id] %s", networkPartitionId); - log.warn(msg); + try { + StratosApiV41Utils.addNetworkPartition(networkPartitionBean); + } catch (CloudControllerServiceNetworkPartitionAlreadyExistsExceptionException e) { return Response.status(Response.Status.CONFLICT) - .entity(new ErrorResponseBean(Response.Status.CONFLICT.getStatusCode(), msg)).build(); + .entity(new ErrorResponseBean(Response.Status.CONFLICT.getStatusCode(), e.getLocalizedMessage())) + .build(); } - StratosApiV41Utils.addNetworkPartition(networkPartitionBean); URI url = uriInfo.getAbsolutePathBuilder().path(networkPartitionId).build(); return Response.created(url).entity(new SuccessResponseBean(Response.Status.CREATED.getStatusCode(), String.format("Network partition added successfully: [network-partition] %s", networkPartitionId))) @@ -586,11 +598,12 @@ public class StratosApiV41 extends AbstractApi { @AuthorizationAction("/permission/protected/manage/removeNetworkPartition") public Response removeNetworkPartition( @PathParam("networkPartitionId") String networkPartitionId) throws RestAPIException { - NetworkPartitionBean networkPartition = StratosApiV41Utils.getNetworkPartition(networkPartitionId); - if (networkPartition == null) { + + try { + StratosApiV41Utils.removeNetworkPartition(networkPartitionId); + } catch (CloudControllerServiceNetworkPartitionNotExistsExceptionException e) { return Response.status(Response.Status.NOT_FOUND).build(); } - StratosApiV41Utils.removeNetworkPartition(networkPartitionId); return Response.ok().entity(new SuccessResponseBean(Response.Status.OK.getStatusCode(), String.format("Network Partition deleted successfully: [network-partition] %s", networkPartitionId))).build(); @@ -746,11 +759,11 @@ public class StratosApiV41 extends AbstractApi { String.format("Application policy added successfully: [application-policy] %s", applicationPolicy.getId()))).build(); } catch (RestAPIException e) { - if (e.getMessage().contains("already exists")) { - return Response.status(Response.Status.CONFLICT).build(); - } else { - throw e; - } + throw e; + } catch (AutoscalerServiceInvalidPolicyExceptionException e) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } catch (AutoscalerServiceInvalidApplicationPolicyExceptionException e) { + return Response.status(Response.Status.BAD_REQUEST).build(); } } @@ -768,12 +781,15 @@ public class StratosApiV41 extends AbstractApi { @AuthorizationAction("/permission/protected/manage/getApplicationPolicy") public Response getApplicationPolicy( @PathParam("applicationPolicyId") String applicationPolicyId) throws RestAPIException { - ApplicationPolicyBean applicationPolicyBean = StratosApiV41Utils.getApplicationPolicy(applicationPolicyId); - if (applicationPolicyBean == null) { - return Response.status(Response.Status.NOT_FOUND).build(); + try { + ApplicationPolicyBean applicationPolicyBean = StratosApiV41Utils.getApplicationPolicy(applicationPolicyId); + if (applicationPolicyBean == null) { + return Response.status(Response.Status.NOT_FOUND).build(); + } + return Response.ok(applicationPolicyBean).build(); + } catch (ApplicationPolicyIdIsEmptyException e) { + return Response.status(Response.Status.BAD_REQUEST).build(); } - - return Response.ok(applicationPolicyBean).build(); } /** @@ -811,11 +827,16 @@ public class StratosApiV41 extends AbstractApi { @AuthorizationAction("/permission/admin/manage/removeApplicationPolicy") public Response removeApplicationPolicy( @PathParam("applicationPolicyId") String applicationPolicyId) throws RestAPIException { - - StratosApiV41Utils.removeApplicationPolicy(applicationPolicyId); - return Response.ok().entity(new SuccessResponseBean(Response.Status.OK.getStatusCode(), - String.format("Application policy deleted successfully: [application-policy] %s", - applicationPolicyId))).build(); + try { + StratosApiV41Utils.removeApplicationPolicy(applicationPolicyId); + return Response.ok().entity(new SuccessResponseBean(Response.Status.OK.getStatusCode(), + String.format("Application policy deleted successfully: [application-policy] %s", + applicationPolicyId))).build(); + } catch (ApplicationPolicyIdIsEmptyException e) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } catch (AutoscalerServiceInvalidPolicyExceptionException e) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } } /** @@ -833,7 +854,13 @@ public class StratosApiV41 extends AbstractApi { public Response updateApplicationPolicy( ApplicationPolicyBean applicationPolicy) throws RestAPIException { - StratosApiV41Utils.updateApplicationPolicy(applicationPolicy); + try { + StratosApiV41Utils.updateApplicationPolicy(applicationPolicy); + } catch (AutoscalerServiceInvalidApplicationPolicyExceptionException e) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } catch (AutoscalerServiceApplicatioinPolicyNotExistsExceptionException e) { + return Response.status(Response.Status.NOT_FOUND).build(); + } return Response.ok().entity(new SuccessResponseBean(Response.Status.OK.getStatusCode(), String.format("Application policy updated successfully: [application-policy] %s", applicationPolicy.getId()))).build(); @@ -946,7 +973,11 @@ public class StratosApiV41 extends AbstractApi { public Response addDomainMappings( @PathParam("applicationId") String applicationId, ApplicationDomainMappingsBean domainMappingsBean) throws RestAPIException { - StratosApiV41Utils.addApplicationDomainMappings(applicationId, domainMappingsBean); + try { + StratosApiV41Utils.addApplicationDomainMappings(applicationId, domainMappingsBean); + } catch (StratosManagerServiceDomainMappingExceptionException e) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } List<DomainMappingBean> mappings = domainMappingsBean.getDomainMappings(); List<String> domainMappingList = new ArrayList<String>(); for (DomainMappingBean domainMappingBean : mappings) { @@ -974,7 +1005,11 @@ public class StratosApiV41 extends AbstractApi { public Response removeDomainMappings( @PathParam("applicationId") String applicationId, ApplicationDomainMappingsBean domainMapppingsBean) throws RestAPIException { - StratosApiV41Utils.removeApplicationDomainMappings(applicationId, domainMapppingsBean); + try { + StratosApiV41Utils.removeApplicationDomainMappings(applicationId, domainMapppingsBean); + } catch (StratosManagerServiceDomainMappingExceptionException e) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } List<DomainMappingBean> mappings = domainMapppingsBean.getDomainMappings(); List<String> domainMappingList = new ArrayList<String>(); for (DomainMappingBean domainMappingBean : mappings) { @@ -999,9 +1034,14 @@ public class StratosApiV41 extends AbstractApi { @AuthorizationAction("/permission/protected/manage/getDomainMappings") public Response getDomainMappings( @PathParam("applicationId") String applicationId) throws RestAPIException { - List<DomainMappingBean> domainMappingsBeanList = StratosApiV41Utils.getApplicationDomainMappings(applicationId); - if (domainMappingsBeanList == null || domainMappingsBeanList.isEmpty()) { - return Response.status(Response.Status.NOT_FOUND).build(); + List<DomainMappingBean> domainMappingsBeanList = null; + try { + domainMappingsBeanList = StratosApiV41Utils.getApplicationDomainMappings(applicationId); + if (domainMappingsBeanList == null || domainMappingsBeanList.isEmpty()) { + return Response.status(Response.Status.NOT_FOUND).build(); + } + } catch (StratosManagerServiceDomainMappingExceptionException e) { + return Response.status(Response.Status.BAD_REQUEST).build(); } DomainMappingBean[] domainMappingsBeans = domainMappingsBeanList @@ -1159,12 +1199,12 @@ public class StratosApiV41 extends AbstractApi { return Response.created(url).entity(new SuccessResponseBean(Response.Status.CREATED.getStatusCode(), String.format("Autoscaling policy added successfully: [autoscale-policy] %s", autoscalePolicy.getId()))).build(); + } catch (AutoscalerServiceInvalidPolicyExceptionException e) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } catch (AutoscalerServiceAutoScalingPolicyAlreadyExistExceptionException e) { + return Response.status(Response.Status.CONFLICT).build(); } catch (RestAPIException e) { - if (e.getMessage().contains("already exists")) { - return Response.status(Response.Status.CONFLICT).build(); - } else { - throw e; - } + throw e; } } @@ -1183,7 +1223,11 @@ public class StratosApiV41 extends AbstractApi { public Response updateAutoscalingPolicy( AutoscalePolicyBean autoscalePolicy) throws RestAPIException { - StratosApiV41Utils.updateAutoscalingPolicy(autoscalePolicy); + try { + StratosApiV41Utils.updateAutoscalingPolicy(autoscalePolicy); + } catch (AutoscalerServiceInvalidPolicyExceptionException e) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } return Response.ok().entity(new SuccessResponseBean(Response.Status.OK.getStatusCode(), String.format("Autoscaling policy updated successfully: [autoscale-policy] %s", autoscalePolicy.getId()))).build(); @@ -1204,7 +1248,11 @@ public class StratosApiV41 extends AbstractApi { public Response updateNetworkPartition( NetworkPartitionBean networkPartition) throws RestAPIException { - StratosApiV41Utils.updateNetworkPartition(networkPartition); + try { + StratosApiV41Utils.updateNetworkPartition(networkPartition); + } catch (CloudControllerServiceNetworkPartitionNotExistsExceptionException e) { + return Response.status(Response.Status.NOT_FOUND).build(); + } return Response.ok().entity(new SuccessResponseBean(Response.Status.OK.getStatusCode(), String.format("Network Partition updated successfully: [network-partition] %s", networkPartition.getId()))).build(); @@ -1225,7 +1273,14 @@ public class StratosApiV41 extends AbstractApi { public Response removeAutoscalingPolicy( @PathParam("autoscalingPolicyId") String autoscalingPolicyId) throws RestAPIException { - StratosApiV41Utils.removeAutoscalingPolicy(autoscalingPolicyId); + try { + StratosApiV41Utils.removeAutoscalingPolicy(autoscalingPolicyId); + } catch (AutoscalerServiceUnremovablePolicyExceptionException e) { + return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorResponseBean( + Response.Status.NOT_ACCEPTABLE.getStatusCode(), e.getMessage())).build(); + } catch (AutoscalerServicePolicyDoesNotExistExceptionException e) { + return Response.status(Response.Status.NOT_FOUND).build(); + } return Response.ok().entity(new SuccessResponseBean(Response.Status.OK.getStatusCode(), String.format("Autoscaling policy deleted successfully: [autoscale-policy] %s", autoscalingPolicyId))).build(); @@ -1246,12 +1301,15 @@ public class StratosApiV41 extends AbstractApi { @AuthorizationAction("/permission/admin/manage/cluster") public Response getCluster( @PathParam("clusterId") String clusterId) throws RestAPIException { - - ClusterBean clusterBean = StratosApiV41Utils.getClusterInfo(clusterId); - if (clusterBean == null) { - return Response.status(Response.Status.NOT_FOUND).build(); - } else { - return Response.ok().entity(clusterBean).build(); + try { + ClusterBean clusterBean = StratosApiV41Utils.getClusterInfo(clusterId); + if (clusterBean == null) { + return Response.status(Response.Status.NOT_FOUND).build(); + } else { + return Response.ok().entity(clusterBean).build(); + } + } catch (ClusterIdIsEmptyException e) { + return Response.status(Response.Status.BAD_REQUEST).build(); } } @@ -1274,11 +1332,18 @@ public class StratosApiV41 extends AbstractApi { public Response addTenant( org.apache.stratos.common.beans.TenantInfoBean tenantInfoBean) throws RestAPIException { - StratosApiV41Utils.addTenant(tenantInfoBean); + try { + StratosApiV41Utils.addTenant(tenantInfoBean); + + } catch (InvalidEmailException e) { + Response.status(Response.Status.BAD_REQUEST).build(); + } catch (InvalidDomainException e) { + Response.status(Response.Status.BAD_REQUEST).build(); + } URI url = uriInfo.getAbsolutePathBuilder().path(tenantInfoBean.getTenantDomain()).build(); return Response.created(url).entity( - new SuccessResponseBean(Response.Status.CREATED.getStatusCode(), - String.format("Tenant added successfully: [tenant] %s", tenantInfoBean.getTenantDomain()))).build(); + new SuccessResponseBean(Response.Status.CREATED.getStatusCode(), String.format( + "Tenant added successfully: [tenant] %s", tenantInfoBean.getTenantDomain()))).build(); } /** @@ -1300,6 +1365,8 @@ public class StratosApiV41 extends AbstractApi { StratosApiV41Utils.updateExistingTenant(tenantInfoBean); } catch (TenantNotFoundException ex) { Response.status(Response.Status.NOT_FOUND).build(); + } catch (InvalidEmailException e) { + Response.status(Response.Status.BAD_REQUEST).build(); } catch (Exception e) { String msg = "Error in updating tenant " + tenantInfoBean.getTenantDomain(); log.error(msg, e); http://git-wip-us.apache.org/repos/asf/stratos/blob/b9d2e3e0/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java index 87b628f..aafaf66 100644 --- a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java @@ -57,8 +57,10 @@ import org.apache.stratos.common.beans.topology.GroupInstanceBean; import org.apache.stratos.common.client.AutoscalerServiceClient; import org.apache.stratos.common.client.CloudControllerServiceClient; import org.apache.stratos.common.client.StratosManagerServiceClient; +import org.apache.stratos.common.exception.InvalidEmailException; import org.apache.stratos.common.util.ClaimsMgtUtil; import org.apache.stratos.common.util.CommonUtil; +import org.apache.stratos.manager.service.stub.StratosManagerServiceDomainMappingExceptionException; import org.apache.stratos.manager.service.stub.domain.application.signup.ApplicationSignUp; import org.apache.stratos.manager.service.stub.domain.application.signup.ArtifactRepository; import org.apache.stratos.manager.service.stub.domain.application.signup.DomainMapping; @@ -73,10 +75,7 @@ import org.apache.stratos.messaging.domain.topology.Cluster; import org.apache.stratos.messaging.message.receiver.application.ApplicationManager; import org.apache.stratos.messaging.message.receiver.topology.TopologyManager; import org.apache.stratos.rest.endpoint.ServiceHolder; -import org.apache.stratos.rest.endpoint.exception.ApplicationAlreadyDeployedException; -import org.apache.stratos.rest.endpoint.exception.ApplicationAlreadyExistException; -import org.apache.stratos.rest.endpoint.exception.RestAPIException; -import org.apache.stratos.rest.endpoint.exception.TenantNotFoundException; +import org.apache.stratos.rest.endpoint.exception.*; import org.apache.stratos.rest.endpoint.util.converter.ObjectConverter; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -322,7 +321,8 @@ public class StratosApiV41Utils { List<CartridgeBean> cartridges = new ArrayList<CartridgeBean>(); if (log.isDebugEnabled()) { - log.debug("Getting available cartridges. [Search String]: " + cartridgeSearchString + ", [Multi-Tenant]: " + multiTenant); + log.debug("Getting available cartridges. [Search String]: " + cartridgeSearchString + ", [Multi-Tenant]: " + + multiTenant); } @@ -551,7 +551,9 @@ public class StratosApiV41Utils { // Util methods for Autoscaling policies - public static void addAutoscalingPolicy(AutoscalePolicyBean autoscalePolicyBean) throws RestAPIException { + public static void addAutoscalingPolicy(AutoscalePolicyBean autoscalePolicyBean) throws RestAPIException, + AutoscalerServiceInvalidPolicyExceptionException, + AutoscalerServiceAutoScalingPolicyAlreadyExistExceptionException { log.info(String.format("Adding autoscaling policy: [id] %s", autoscalePolicyBean.getId())); @@ -562,26 +564,22 @@ public class StratosApiV41Utils { convertToCCAutoscalerPojo(autoscalePolicyBean); try { - autoscalerServiceClient - .addAutoscalingPolicy(autoscalePolicy); + autoscalerServiceClient.addAutoscalingPolicy(autoscalePolicy); } catch (RemoteException e) { log.error(e.getMessage(), e); throw new RestAPIException(e.getMessage(), e); - } catch (AutoscalerServiceInvalidPolicyExceptionException e) { - String message = e.getFaultMessage() - .getInvalidPolicyException().getMessage(); - log.error(message, e); - throw new RestAPIException(message, e); } } } - public static void addApplicationPolicy(ApplicationPolicyBean applicationPolicyBean) throws RestAPIException { + public static void addApplicationPolicy(ApplicationPolicyBean applicationPolicyBean) throws RestAPIException, + AutoscalerServiceInvalidPolicyExceptionException, + AutoscalerServiceInvalidApplicationPolicyExceptionException { if (applicationPolicyBean == null) { String msg = "Application policy bean is null"; log.error(msg); - throw new RestAPIException(msg); + throw new ApplicationPolicyIsEmptyException(msg); } AutoscalerServiceClient serviceClient = getAutoscalerServiceClient(); @@ -591,29 +589,23 @@ public class StratosApiV41Utils { if (applicationPolicy == null) { String msg = "Application policy is null"; log.error(msg); - throw new RestAPIException(msg); + throw new ApplicationPolicyIsEmptyException(msg); } serviceClient.addApplicationPolicy(applicationPolicy); } catch (RemoteException e) { String msg = "Could not add application policy. " + e.getLocalizedMessage(); log.error(msg, e); throw new RestAPIException(msg); - } catch (AutoscalerServiceInvalidPolicyExceptionException e) { - String msg = "Could not add application policy. Invalid policy. " + e.getLocalizedMessage(); - log.error(msg, e); - throw new RestAPIException(msg); } catch (AutoscalerServiceRemoteExceptionException e) { String msg = "Could not add application policy. " + e.getLocalizedMessage(); log.error(msg, e); throw new RestAPIException(msg); - } catch (AutoscalerServiceInvalidApplicationPolicyExceptionException e) { - String msg = "Could not add application policy. Invalid application policy. " + e.getLocalizedMessage(); - log.error(msg, e); - throw new RestAPIException(msg); } } - public static void updateApplicationPolicy(ApplicationPolicyBean applicationPolicyBean) throws RestAPIException { + public static void updateApplicationPolicy(ApplicationPolicyBean applicationPolicyBean) throws RestAPIException, + AutoscalerServiceInvalidApplicationPolicyExceptionException, + AutoscalerServiceApplicatioinPolicyNotExistsExceptionException { log.info(String.format("Updating application policy: [id] %s", applicationPolicyBean.getId())); @@ -629,19 +621,10 @@ public class StratosApiV41Utils { String msg = "Could not update application policy" + e.getLocalizedMessage(); log.error(msg, e); throw new RestAPIException(msg); - } catch (AutoscalerServiceApplicatioinPolicyNotExistsExceptionException e) { - String msg = "Could not update application policy. Application policy not exists" + - e.getLocalizedMessage(); - log.error(msg, e); - throw new RestAPIException(msg); } catch (AutoscalerServiceRemoteExceptionException e) { String msg = "Could not update application policy" + e.getLocalizedMessage(); log.error(msg, e); throw new RestAPIException(msg); - } catch (AutoscalerServiceInvalidApplicationPolicyExceptionException e) { - String msg = "Could not update application policy. Invalid application policy" + e.getLocalizedMessage(); - log.error(msg, e); - throw new RestAPIException(msg); } } } @@ -667,39 +650,40 @@ public class StratosApiV41Utils { if (applicationPolicyId == null) { String msg = "Application policy bean id null"; log.error(msg); - throw new RestAPIException(msg); + throw new ApplicationPolicyIdIsEmptyException(msg); } if (StringUtils.isBlank(applicationPolicyId)) { String msg = "Application policy id is empty"; log.error(msg); - throw new RestAPIException(msg); + throw new ApplicationPolicyIdIsEmptyException(msg); } try { AutoscalerServiceClient serviceClient = AutoscalerServiceClient.getInstance(); ApplicationPolicy applicationPolicy = serviceClient.getApplicationPolicy(applicationPolicyId); return ObjectConverter.convertASStubApplicationPolicyToApplicationPolicy(applicationPolicy); - } catch (Exception e) { + } catch (RemoteException e) { String message = String.format("Could not get application policy [application-policy-id] %s", applicationPolicyId); log.error(message); - throw new RuntimeException(message, e); + throw new RestAPIException(message, e); } } - public static void removeApplicationPolicy(String applicationPolicyId) throws RestAPIException { + public static void removeApplicationPolicy(String applicationPolicyId) throws RestAPIException, + AutoscalerServiceInvalidPolicyExceptionException { if (applicationPolicyId == null) { String msg = "Application policy bean id null"; log.error(msg); - throw new RestAPIException(msg); + throw new ApplicationPolicyIdIsEmptyException(msg); } if (StringUtils.isBlank(applicationPolicyId)) { String msg = "Application policy id is empty"; log.error(msg); - throw new RestAPIException(msg); + throw new ApplicationPolicyIdIsEmptyException(msg); } AutoscalerServiceClient serviceClient = getAutoscalerServiceClient(); @@ -709,14 +693,11 @@ public class StratosApiV41Utils { String msg = "Could not remove application policy. " + e.getLocalizedMessage(); log.error(msg, e); throw new RestAPIException(msg); - } catch (AutoscalerServiceInvalidPolicyExceptionException e) { - String msg = "Could not remove application policy. " + e.getLocalizedMessage(); - log.error(msg, e); - throw new RestAPIException(msg); } } - public static void updateAutoscalingPolicy(AutoscalePolicyBean autoscalePolicyBean) throws RestAPIException { + public static void updateAutoscalingPolicy(AutoscalePolicyBean autoscalePolicyBean) throws RestAPIException, + AutoscalerServiceInvalidPolicyExceptionException { log.info(String.format("Updating autoscaling policy: [id] %s", autoscalePolicyBean.getId())); @@ -730,15 +711,13 @@ public class StratosApiV41Utils { } catch (RemoteException e) { log.error(e.getMessage(), e); throw new RestAPIException(e.getMessage(), e); - } catch (AutoscalerServiceInvalidPolicyExceptionException e) { - String message = e.getFaultMessage().getInvalidPolicyException().getMessage(); - log.error(message, e); - throw new RestAPIException(message, e); } } } - public static void removeAutoscalingPolicy(String autoscalePolicyId) throws RestAPIException { + public static void removeAutoscalingPolicy(String autoscalePolicyId) throws RestAPIException, + AutoscalerServicePolicyDoesNotExistExceptionException, + AutoscalerServiceUnremovablePolicyExceptionException { log.info(String.format("Removing autoscaling policy: [id] %s", autoscalePolicyId)); @@ -750,10 +729,6 @@ public class StratosApiV41Utils { } catch (RemoteException e) { log.error(e.getMessage(), e); throw new RestAPIException(e.getMessage(), e); - } catch (AutoscalerServiceInvalidPolicyExceptionException e) { - String message = e.getFaultMessage().getInvalidPolicyException().getMessage(); - log.error(message, e); - throw new RestAPIException(message, e); } } } @@ -962,7 +937,8 @@ public class StratosApiV41Utils { try { AutoscalerServiceClient asServiceClient = AutoscalerServiceClient.getInstance(); ServiceGroup[] serviceGroups = asServiceClient.getServiceGroups(); - if (serviceGroups == null || serviceGroups.length == 0 || (serviceGroups.length == 1 && serviceGroups[0] == null)) { + if (serviceGroups == null || serviceGroups.length == 0 || (serviceGroups.length == 1 && serviceGroups[0] + == null)) { return null; } @@ -2003,7 +1979,8 @@ public class StratosApiV41Utils { } public static void addApplicationDomainMappings( - String applicationId, ApplicationDomainMappingsBean domainMapppingsBean) throws RestAPIException { + String applicationId, ApplicationDomainMappingsBean domainMapppingsBean) throws RestAPIException, + StratosManagerServiceDomainMappingExceptionException { try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -2030,7 +2007,7 @@ public class StratosApiV41Utils { } } } - } catch (Exception e) { + } catch (RemoteException e) { String message = "Could not add domain mappings: [application-id] " + applicationId; log.error(message, e); throw new RestAPIException(message, e); @@ -2053,7 +2030,7 @@ public class StratosApiV41Utils { public static void removeApplicationDomainMappings(String applicationId, ApplicationDomainMappingsBean domainMapppingsBean) - throws RestAPIException { + throws RestAPIException, StratosManagerServiceDomainMappingExceptionException { try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -2070,14 +2047,15 @@ public class StratosApiV41Utils { } } } - } catch (Exception e) { + } catch (RemoteException e) { String message = "Could not remove domain mappings: [application-id] " + applicationId; log.error(message, e); throw new RestAPIException(message, e); } } - public static List<DomainMappingBean> getApplicationDomainMappings(String applicationId) throws RestAPIException { + public static List<DomainMappingBean> getApplicationDomainMappings(String applicationId) throws RestAPIException, + StratosManagerServiceDomainMappingExceptionException { try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); List<DomainMappingBean> domainMappingsBeans = new ArrayList<DomainMappingBean>(); @@ -2093,76 +2071,79 @@ public class StratosApiV41Utils { } } return domainMappingsBeans; - } catch (Exception e) { + } catch (RemoteException e) { String message = "Could not get domain mappings: [application-id] " + applicationId; log.error(message, e); throw new RestAPIException(message, e); } } - public static void addNetworkPartition(NetworkPartitionBean networkPartitionBean) { + public static void addNetworkPartition(NetworkPartitionBean networkPartitionBean) throws RestAPIException, + CloudControllerServiceNetworkPartitionAlreadyExistsExceptionException { try { CloudControllerServiceClient serviceClient = CloudControllerServiceClient.getInstance(); serviceClient.addNetworkPartition( ObjectConverter.convertNetworkPartitionToCCStubNetworkPartition(networkPartitionBean)); - } catch (Exception e) { + } catch (RemoteException e) { String message = "Could not add network partition"; log.error(message); - throw new RuntimeException(message, e); + throw new RestAPIException(message, e); } } - public static NetworkPartitionBean[] getNetworkPartitions() { + public static NetworkPartitionBean[] getNetworkPartitions() throws RestAPIException { try { CloudControllerServiceClient serviceClient = CloudControllerServiceClient.getInstance(); org.apache.stratos.cloud.controller.stub.domain.NetworkPartition[] networkPartitions = serviceClient.getNetworkPartitions(); return ObjectConverter.convertCCStubNetworkPartitionsToNetworkPartitions(networkPartitions); - } catch (Exception e) { + } catch (RemoteException e) { String message = "Could not get network partitions"; log.error(message); - throw new RuntimeException(message, e); + throw new RestAPIException(message, e); } } - public static void removeNetworkPartition(String networkPartitionId) { + public static void removeNetworkPartition(String networkPartitionId) throws RestAPIException, + CloudControllerServiceNetworkPartitionNotExistsExceptionException { try { CloudControllerServiceClient serviceClient = CloudControllerServiceClient.getInstance(); serviceClient.removeNetworkPartition(networkPartitionId); - } catch (Exception e) { + } catch (RemoteException e) { String message = String.format( "Could not remove network partition: [network-partition-id] %s", networkPartitionId); log.error(message); - throw new RuntimeException(message, e); + throw new RestAPIException(message, e); } } - public static NetworkPartitionBean getNetworkPartition(String networkPartitionId) { + public static NetworkPartitionBean getNetworkPartition(String networkPartitionId) throws RestAPIException { try { CloudControllerServiceClient serviceClient = CloudControllerServiceClient.getInstance(); org.apache.stratos.cloud.controller.stub.domain.NetworkPartition networkPartition = serviceClient.getNetworkPartition(networkPartitionId); return ObjectConverter.convertCCStubNetworkPartitionToNetworkPartition(networkPartition); - } catch (Exception e) { + } catch (RemoteException e) { String message = String.format( "Could not get network partition: [network-partition-id] %s", networkPartitionId); log.error(message); - throw new RuntimeException(message, e); + throw new RestAPIException(message, e); } } - public static void updateNetworkPartition(NetworkPartitionBean networkPartition) { + public static void updateNetworkPartition(NetworkPartitionBean networkPartition) throws RestAPIException, + CloudControllerServiceNetworkPartitionNotExistsExceptionException { try { CloudControllerServiceClient serviceClient = CloudControllerServiceClient.getInstance(); serviceClient.updateNetworkPartition(ObjectConverter. convertNetworkPartitionToCCStubNetworkPartition(networkPartition)); - } catch (Exception e) { + } catch (RemoteException e) { String message = String.format("Could not update network partition: [network-partition-id] %s,", networkPartition.getId()); log.error(message); - throw new RuntimeException(message, e); + throw new RestAPIException(message, e); } } @@ -2172,7 +2153,9 @@ public class StratosApiV41Utils { * @param deployementPolicyDefinitionBean DeploymentPolicyBean */ public static void addDeploymentPolicy(DeploymentPolicyBean deployementPolicyDefinitionBean) - throws RestAPIException { + throws RestAPIException, + AutoscalerServiceDeploymentPolicyAlreadyExistsExceptionException, + AutoscalerServiceInvalidDeploymentPolicyExceptionException{ try { if (log.isDebugEnabled()) { log.debug(String.format("Adding deployment policy: [deployment-policy-id] %s ", @@ -2188,12 +2171,11 @@ public class StratosApiV41Utils { log.debug(String.format("Successfully added deploymentPolicy: [deployment-policy-id] %s ", deployementPolicyDefinitionBean.getId())); } - } catch (AutoscalerServiceDeploymentPolicyNotExistsExceptionException e) { - String msg = - "Deployment policy already exists [Deployment-policy-id]" + deployementPolicyDefinitionBean.getId(); + } catch (RemoteException e) { + String msg = "Could not add deployment policy."; log.error(msg, e); - throw new RestAPIException(msg); - } catch (Exception e) { + throw new RestAPIException(msg, e); + } catch (AutoscalerServiceRemoteExceptionException e) { String msg = "Could not add deployment policy."; log.error(msg, e); throw new RestAPIException(msg, e); @@ -2217,7 +2199,7 @@ public class StratosApiV41Utils { return null; } deploymentPolicyBean = ObjectConverter.convetCCStubDeploymentPolicytoDeploymentPolicy(deploymentPolicy); - } catch (Exception e) { + } catch (RemoteException e) { String msg = "Could not find deployment policy: [deployment-policy-id] " + deploymentPolicyID; log.error(msg, e); throw new RestAPIException(msg); @@ -2231,15 +2213,15 @@ public class StratosApiV41Utils { * * @return array of {@link DeploymentPolicyBean} */ - public static DeploymentPolicyBean[] getDeployementPolicies() { + public static DeploymentPolicyBean[] getDeployementPolicies() throws RestAPIException { try { org.apache.stratos.autoscaler.stub.deployment.policy.DeploymentPolicy[] deploymentPolicies = AutoscalerServiceClient.getInstance().getDeploymentPolicies(); return ObjectConverter.convertASStubDeploymentPoliciesToDeploymentPolicies(deploymentPolicies); - } catch (Exception e) { + } catch (RemoteException e) { String message = "Could not get deployment policies"; log.error(message); - throw new RuntimeException(message, e); + throw new RestAPIException(message, e); } } @@ -2250,7 +2232,9 @@ public class StratosApiV41Utils { * @throws RestAPIException */ public static void updateDeploymentPolicy(DeploymentPolicyBean deploymentPolicyDefinitionBean) - throws RestAPIException { + throws RestAPIException, AutoscalerServiceInvalidPolicyExceptionException, + AutoscalerServiceInvalidDeploymentPolicyExceptionException, + AutoscalerServiceDeploymentPolicyNotExistsExceptionException { try { if (log.isDebugEnabled()) { log.debug(String.format("Updating deployment policy: [deployment-policy-id] %s ", @@ -2266,12 +2250,18 @@ public class StratosApiV41Utils { log.debug(String.format("DeploymentPolicy updated successfully : [deployment-policy-id] %s ", deploymentPolicyDefinitionBean.getId())); } - } catch (AutoscalerServiceDeploymentPolicyNotExistsExceptionException e) { - String msg = - "Deployment policy does not exist [Deployment-policy-id]" + deploymentPolicyDefinitionBean.getId(); + } catch (RemoteException e) { + + String msg = "Could not update deployment policy " + e.getLocalizedMessage(); log.error(msg, e); throw new RestAPIException(msg); - } catch (Exception e) { + } catch (AutoscalerServiceCloudControllerConnectionExceptionException e) { + + String msg = "Could not update deployment policy " + e.getLocalizedMessage(); + log.error(msg, e); + throw new RestAPIException(msg); + } catch (AutoscalerServiceRemoteExceptionException e) { + String msg = "Could not update deployment policy " + e.getLocalizedMessage(); log.error(msg, e); throw new RestAPIException(msg); @@ -2285,10 +2275,10 @@ public class StratosApiV41Utils { * @throws RestAPIException */ public static void removeDeploymentPolicy(String deploymentPolicyID) - throws RestAPIException { + throws RestAPIException, AutoscalerServiceDeploymentPolicyNotExistsExceptionException { try { AutoscalerServiceClient.getInstance().removeDeploymentPolicy(deploymentPolicyID); - } catch (Exception e) { + } catch (RemoteException e) { String msg = "Could not remove deployment policy " + e.getLocalizedMessage(); log.error(msg, e); throw new RestAPIException(msg); @@ -2298,7 +2288,7 @@ public class StratosApiV41Utils { public static ClusterBean getClusterInfo(String clusterId) throws RestAPIException { if (StringUtils.isEmpty(clusterId)) { - throw new RestAPIException("Cluster Id can not be empty"); + throw new ClusterIdIsEmptyException("Cluster Id can not be empty"); } Cluster cluster = TopologyManager.getTopology().getCluster(clusterId); @@ -2317,14 +2307,13 @@ public class StratosApiV41Utils { * @param tenantInfoBean * @throws RestAPIException */ - public static void addTenant(org.apache.stratos.common.beans.TenantInfoBean tenantInfoBean) throws RestAPIException { + public static void addTenant(org.apache.stratos.common.beans.TenantInfoBean tenantInfoBean) throws RestAPIException, + InvalidEmailException { try { CommonUtil.validateEmail(tenantInfoBean.getEmail()); } catch (Exception e) { - String msg = "Invalid email is provided"; - log.error(msg, e); - throw new RestAPIException(msg); + throw new InvalidEmailException(e.getMessage()); } String tenantDomain = tenantInfoBean.getTenantDomain(); @@ -2333,7 +2322,7 @@ public class StratosApiV41Utils { } catch (Exception e) { String msg = "Tenant domain validation error for tenant " + tenantDomain; log.error(msg, e); - throw new RestAPIException(msg); + throw new InvalidDomainException(msg); } UserRegistry userRegistry = (UserRegistry) PrivilegedCarbonContext.getThreadLocalCarbonContext(). @@ -2407,7 +2396,8 @@ public class StratosApiV41Utils { * @param tenantInfoBean * @throws Exception */ - public static void updateExistingTenant(org.apache.stratos.common.beans.TenantInfoBean tenantInfoBean) throws Exception { + public static void updateExistingTenant(org.apache.stratos.common.beans.TenantInfoBean tenantInfoBean) throws + Exception, InvalidEmailException { TenantManager tenantManager = ServiceHolder.getTenantManager(); UserStoreManager userStoreManager;
