You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by ga...@apache.org on 2015/09/10 13:35:58 UTC

stratos git commit: Adding rest API methods to add tenant signup from super tenant

Repository: stratos
Updated Branches:
  refs/heads/stratos-4.1.x 8c1c56808 -> bc54ffbf8


Adding rest API methods to add tenant signup from super tenant


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

Branch: refs/heads/stratos-4.1.x
Commit: bc54ffbf83a7e891b0291c1bf238e1c6206f3f81
Parents: 8c1c568
Author: gayangunarathne <ga...@wso2.com>
Authored: Thu Sep 10 17:05:35 2015 +0530
Committer: gayangunarathne <ga...@wso2.com>
Committed: Thu Sep 10 17:05:35 2015 +0530

----------------------------------------------------------------------
 .../rest/endpoint/api/StratosApiV41.java        | 62 +++++++++++++++++++-
 .../rest/endpoint/api/StratosApiV41Utils.java   | 30 ++++++----
 2 files changed, 77 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/bc54ffbf/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 af0ff89..3c2a5bc 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
@@ -50,6 +50,7 @@ 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.*;
+import org.wso2.carbon.context.CarbonContext;
 import org.wso2.carbon.context.PrivilegedCarbonContext;
 
 import javax.servlet.http.HttpServletRequest;
@@ -1072,7 +1073,7 @@ public class StratosApiV41 extends AbstractApi {
     public Response addApplicationSignUp(
             @PathParam("applicationId") String applicationId, ApplicationSignUpBean applicationSignUpBean)
             throws RestAPIException {
-        StratosApiV41Utils.addApplicationSignUp(applicationId, applicationSignUpBean);
+        StratosApiV41Utils.addApplicationSignUp(applicationId, applicationSignUpBean, CarbonContext.getThreadLocalCarbonContext().getTenantId());
         URI url = uriInfo.getAbsolutePathBuilder().path(applicationId).build();
         return Response.created(url).entity(new ResponseMessageBean(ResponseMessageBean.SUCCESS,
                 String.format("Successfully signed up for: [application] %s", applicationId))).build();
@@ -1122,12 +1123,69 @@ public class StratosApiV41 extends AbstractApi {
     @AuthorizationAction("/permission/admin/stratos/applicationSignUps/manage")
     public Response removeApplicationSignUp(
             @PathParam("applicationId") String applicationId) throws RestAPIException {
-        StratosApiV41Utils.removeApplicationSignUp(applicationId);
+        StratosApiV41Utils.removeApplicationSignUp(applicationId, CarbonContext.getThreadLocalCarbonContext().getTenantId());
         return Response.ok().entity(new ResponseMessageBean(ResponseMessageBean.SUCCESS,
                 String.format("Application sign up removed successfully: [application] %s", applicationId))).build();
     }
 
     /**
+     * Signs up for an application.
+     *
+     * @param applicationId         the application id
+     * @param applicationSignUpBean the application sign up bean
+     * @return 200 if application sign up was successfull
+     * @throws RestAPIException the rest api exception
+     */
+    @POST
+    @Path("/applications/{applicationId}/signup/{tenantDomain}")
+    @Produces("application/json")
+    @Consumes("application/json")
+    @AuthorizationAction("/permission/admin/stratos/applicationSignUps/manage")
+    public Response addApplicationSignUpForTenant(
+            @PathParam("applicationId") String applicationId, ApplicationSignUpBean applicationSignUpBean, @PathParam("tenantDomain") String tenantDomain)
+            throws RestAPIException {
+        int contextTenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
+        if (contextTenantId == -1234) {
+            TenantInfoBean tenantInfo = StratosApiV41Utils.getTenantByDomain(tenantDomain);
+            StratosApiV41Utils.addApplicationSignUp(applicationId, applicationSignUpBean, tenantInfo.getTenantId());
+            URI url = uriInfo.getAbsolutePathBuilder().path(applicationId).build();
+            return Response.created(url).entity(new ResponseMessageBean(ResponseMessageBean.SUCCESS,
+                    String.format("Successfully signed up for: [application] %s", applicationId))).build();
+        } else {
+            return Response.status(Response.Status.METHOD_NOT_ALLOWED).entity(new ResponseMessageBean(
+                    ResponseMessageBean.ERROR, "This method not allowed")).build();
+        }
+
+    }
+
+
+    /**
+     * Removes the application sign up.
+     *
+     * @param applicationId the application id
+     * @return 200 if specified application sign up is removed
+     * @throws RestAPIException the rest api exception
+     */
+    @DELETE
+    @Path("/applications/{applicationId}/signup/{tenantDomain}")
+    @Produces("application/json")
+    @Consumes("application/json")
+    @AuthorizationAction("/permission/admin/stratos/applicationSignUps/manage")
+    public Response removeApplicationSignUpForTenant(
+            @PathParam("applicationId") String applicationId, @PathParam("tenantDomain") String tenantDomain) throws RestAPIException {
+        int contextTenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
+        if (contextTenantId == -1234) {
+            TenantInfoBean tenantInfo = StratosApiV41Utils.getTenantByDomain(tenantDomain);
+            StratosApiV41Utils.removeApplicationSignUp(applicationId, tenantInfo.getTenantId());
+            return Response.ok().entity(new ResponseMessageBean(ResponseMessageBean.SUCCESS,
+                    String.format("Application sign up removed successfully: [application] %s", applicationId))).build();
+        } else {
+            return Response.status(Response.Status.METHOD_NOT_ALLOWED).entity(new ResponseMessageBean(
+                    ResponseMessageBean.ERROR, "This method not allowed")).build();
+        }
+    }
+
+    /**
      * Adds the domain mappings for an application.
      *
      * @param applicationId      the application id

http://git-wip-us.apache.org/repos/asf/stratos/blob/bc54ffbf/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 baff88d..098e6c7 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
@@ -2420,9 +2420,10 @@ public class StratosApiV41Utils {
      *
      * @param applicationId         applicationId
      * @param applicationSignUpBean ApplicationSignUpBean
+     * @param tenantId
      * @throws RestAPIException
      */
-    public static void addApplicationSignUp(String applicationId, ApplicationSignUpBean applicationSignUpBean)
+    public static void addApplicationSignUp(String applicationId, ApplicationSignUpBean applicationSignUpBean, int tenantId)
             throws RestAPIException {
 
         if (StringUtils.isBlank(applicationId)) {
@@ -2453,8 +2454,6 @@ public class StratosApiV41Utils {
                 log.info(String.format("Adding applicationBean signup: [application-id] %s", applicationId));
             }
 
-            int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
-
             ApplicationSignUp applicationSignUp = ObjectConverter.convertApplicationSignUpBeanToStubApplicationSignUp(
                     applicationSignUpBean);
             applicationSignUp.setApplicationId(applicationId);
@@ -2570,9 +2569,10 @@ public class StratosApiV41Utils {
      * Remove Application SignUp
      *
      * @param applicationId applicationId
+     * @param tenantId
      * @throws RestAPIException
      */
-    public static void removeApplicationSignUp(String applicationId) throws RestAPIException {
+    public static void removeApplicationSignUp(String applicationId, int tenantId) throws RestAPIException {
         if (StringUtils.isBlank(applicationId)) {
             throw new RestAPIException("Application id is null");
         }
@@ -2586,8 +2586,6 @@ public class StratosApiV41Utils {
             throw new RestAPIException("Application singups not available for single-tenant applications");
         }
 
-        int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
-
         try {
             StratosManagerServiceClient serviceClient = StratosManagerServiceClient.getInstance();
             serviceClient.removeApplicationSignUp(applicationId, tenantId);
@@ -3272,7 +3270,7 @@ public class StratosApiV41Utils {
      * @return TenantInfoBean
      * @throws Exception
      */
-    public static org.apache.stratos.common.beans.TenantInfoBean getTenantByDomain(String tenantDomain) throws Exception {
+    public static org.apache.stratos.common.beans.TenantInfoBean getTenantByDomain(String tenantDomain) throws RestAPIException {
 
         TenantManager tenantManager = ServiceHolder.getTenantManager();
 
@@ -3283,7 +3281,7 @@ public class StratosApiV41Utils {
             String msg = "Error in retrieving the tenant id for the tenant domain: " +
                     tenantDomain + ".";
             log.error(msg, e);
-            throw new Exception(msg, e);
+            throw new RestAPIException(msg, e);
         }
         Tenant tenant;
         try {
@@ -3291,7 +3289,7 @@ public class StratosApiV41Utils {
         } catch (UserStoreException e) {
             String msg = "Error in retrieving the tenant from the tenant manager.";
             log.error(msg, e);
-            throw new Exception(msg, e);
+            throw new RestAPIException(msg, e);
         }
 
         TenantInfoBean bean;
@@ -3303,10 +3301,16 @@ public class StratosApiV41Utils {
             return null;
         }
 
-        // retrieve first and last names from the UserStoreManager
-        bean.setFirstName(ClaimsMgtUtil.getFirstNamefromUserStoreManager(ServiceHolder.getRealmService(), tenantId));
-        bean.setLastName(ClaimsMgtUtil.getLastNamefromUserStoreManager(ServiceHolder.getRealmService(), tenantId));
-
+        try {
+            // retrieve first and last names from the UserStoreManager
+            bean.setFirstName(ClaimsMgtUtil.getFirstNamefromUserStoreManager(ServiceHolder.getRealmService(), tenantId));
+            bean.setLastName(ClaimsMgtUtil.getLastNamefromUserStoreManager(ServiceHolder.getRealmService(), tenantId));
+        }
+        catch(UserStoreException e){
+                String msg = "Error in retrieving the tenant from the tenant manager.";
+                log.error(msg, e);
+                throw new RestAPIException(msg, e);
+        }
         return bean;
     }