[STRATOS-1277] - Return 409 Conflict status if deployment policy and 
application policy already exists


Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/477c366b
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/477c366b
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/477c366b

Branch: refs/heads/master
Commit: 477c366bbdd3541b48a84ef72104fb392c74c190
Parents: b65ede0
Author: Chamila de Alwis <[email protected]>
Authored: Sat Apr 18 00:11:39 2015 +0530
Committer: Chamila de Alwis <[email protected]>
Committed: Sat Apr 18 00:11:39 2015 +0530

----------------------------------------------------------------------
 .../rest/endpoint/api/StratosApiV41.java        | 42 ++++++++++++++------
 .../rest/endpoint/api/StratosApiV41Utils.java   | 10 ++---
 2 files changed, 34 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/477c366b/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 d5fdc5a..371c709 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
@@ -172,13 +172,21 @@ public class StratosApiV41 extends AbstractApi {
     public Response addDeploymentPolicy(
             DeploymentPolicyBean deploymentPolicyDefinitionBean) throws 
RestAPIException {
 
-        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();
+        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;
+            }
+        }
     }
 
     /**
@@ -704,11 +712,19 @@ public class StratosApiV41 extends AbstractApi {
     @AuthorizationAction("/permission/admin/manage/addApplicationPolicy")
     public Response addApplicationPolicy(
             ApplicationPolicyBean applicationPolicy) throws RestAPIException {
-        StratosApiV41Utils.addApplicationPolicy(applicationPolicy);
-        URI url = 
uriInfo.getAbsolutePathBuilder().path(applicationPolicy.getId()).build();
-        return Response.created(url).entity(new 
SuccessResponseBean(Response.Status.CREATED.getStatusCode(),
-                String.format("Application policy added successfully: 
[application-policy] %s",
-                        applicationPolicy.getId()))).build();
+        try {
+            StratosApiV41Utils.addApplicationPolicy(applicationPolicy);
+            URI url = 
uriInfo.getAbsolutePathBuilder().path(applicationPolicy.getId()).build();
+            return Response.created(url).entity(new 
SuccessResponseBean(Response.Status.CREATED.getStatusCode(),
+                    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;
+            }
+        }
     }
 
     /**
@@ -855,7 +871,7 @@ public class StratosApiV41 extends AbstractApi {
     @AuthorizationAction("/permission/protected/manage/getApplicationSignUp")
     public Response getApplicationSignUp(
             @PathParam("applicationId") String applicationId) throws 
RestAPIException {
-        ApplicationSignUpBean applicationSignUpBean = null;
+        ApplicationSignUpBean applicationSignUpBean;
         try {
             applicationSignUpBean = 
StratosApiV41Utils.getApplicationSignUp(applicationId);
             if (applicationSignUpBean == null) {

http://git-wip-us.apache.org/repos/asf/stratos/blob/477c366b/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 d6a76c1..63f7300 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
@@ -573,7 +573,7 @@ public class StratosApiV41Utils {
             }
             serviceClient.addApplicationPolicy(applicationPolicy);
         } catch (RemoteException e) {
-            String msg = "Could not add application policy" + 
e.getLocalizedMessage();
+            String msg = "Could not add application policy. " + 
e.getLocalizedMessage();
             log.error(msg, e);
             throw new RestAPIException(msg);
         } catch (AutoscalerServiceInvalidPolicyExceptionException e) {
@@ -581,7 +581,7 @@ public class StratosApiV41Utils {
             log.error(msg, e);
             throw new RestAPIException(msg);
         } catch (AutoscalerServiceRemoteExceptionException e) {
-            String msg = "Could not add application policy" + 
e.getLocalizedMessage();
+            String msg = "Could not add application policy. " + 
e.getLocalizedMessage();
             log.error(msg, e);
             throw new RestAPIException(msg);
         } catch (AutoscalerServiceInvalidApplicationPolicyExceptionException 
e) {
@@ -2121,13 +2121,13 @@ public class StratosApiV41Utils {
             }
         } catch (AutoscalerServiceDeploymentPolicyNotExistsExceptionException 
e) {
             String msg =
-                    "Deployment policy already exist [Deployment-policy-id]" + 
deployementPolicyDefinitionBean.getId();
+                    "Deployment policy already exists [Deployment-policy-id]" 
+ deployementPolicyDefinitionBean.getId();
             log.error(msg, e);
             throw new RestAPIException(msg);
         } catch (Exception e) {
-            String msg = "Could not add deployment policy";
+            String msg = "Could not add deployment policy.";
             log.error(msg, e);
-            throw new RestAPIException(msg);
+            throw new RestAPIException(msg, e);
         }
     }
 

Reply via email to