You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by sm...@apache.org on 2019/09/09 19:17:50 UTC
[airavata-custos] 46/48: added validations and refactored exception
handling
This is an automated email from the ASF dual-hosted git repository.
smarru pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata-custos.git
commit 7fee0b2b119b427a58e6a995fc5c0a8ba96dd2c8
Author: Aarushi <aa...@gmail.com>
AuthorDate: Thu Sep 5 11:29:47 2019 -0400
added validations and refactored exception handling
---
.../service/api/controllers/DomainsController.java | 28 +-
.../service/api/controllers/EntityController.java | 27 +-
.../api/controllers/EntityTypeController.java | 41 +--
.../service/api/controllers/GroupController.java | 135 ++-----
.../api/controllers/PermissionTypeController.java | 40 +--
.../service/api/controllers/UsersController.java | 36 +-
.../src/main/resources/application.properties | 3 +-
.../core/exceptions/InvalidRequestException.java | 16 +
.../custos/sharing/service/core/models/Domain.java | 2 +-
.../core/service/SharingRegistryService.java | 396 ++++++++++++++++-----
pom.xml | 5 +
11 files changed, 399 insertions(+), 330 deletions(-)
diff --git a/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/DomainsController.java b/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/DomainsController.java
index 9ab93e9..8de7bec 100644
--- a/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/DomainsController.java
+++ b/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/DomainsController.java
@@ -1,7 +1,6 @@
package org.apache.custos.sharing.service.api.controllers;
import org.apache.custos.sharing.service.core.models.Domain;
-import org.apache.custos.sharing.service.core.exceptions.SharingRegistryException;
import org.apache.custos.sharing.service.core.service.SharingRegistryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@@ -21,10 +20,9 @@ public class DomainsController {
<p>API method to create a new domain</p>
*/
@RequestMapping(method= RequestMethod.POST)
- public @ResponseBody Domain createDomain(@Valid @RequestBody Domain domain){
- Domain createdDomain = sharingRegistryService.createDomain(domain);
- if(createdDomain != null) return createdDomain;
- else throw new SharingRegistryException("Domain creation failed");
+ public ResponseEntity<Domain> createDomain(@Valid @RequestBody Domain domain){
+ Domain createdDomain = sharingRegistryService.createDomain(domain);
+ return new ResponseEntity<>(createdDomain, HttpStatus.CREATED);
}
/**
@@ -32,9 +30,8 @@ public class DomainsController {
*/
@RequestMapping(method = RequestMethod.PUT)
public ResponseEntity<String> updateDomain(@Valid @RequestBody Domain domain){
- boolean response = sharingRegistryService.updateDomain(domain);
- if(response) return new ResponseEntity<>(HttpStatus.OK);
- else throw new SharingRegistryException("Domain update failed");
+ sharingRegistryService.updateDomain(domain);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
@@ -56,8 +53,7 @@ public class DomainsController {
@RequestMapping(value = "/{domainId}",method = RequestMethod.GET)
public ResponseEntity<Domain> getDomain(@PathVariable("domainId") String domainId){
Domain domain = sharingRegistryService.getDomain(domainId);
- if(domain != null) return new ResponseEntity<>(domain, HttpStatus.OK);
- else return new ResponseEntity<>(HttpStatus.NOT_FOUND);
+ return new ResponseEntity<>(domain, HttpStatus.OK);
}
/**
@@ -65,19 +61,15 @@ public class DomainsController {
*/
@RequestMapping(value = "/{domainId}", method = RequestMethod.DELETE)
public ResponseEntity<String> deleteDomain(@PathVariable("domainId") String domainId){
- boolean response = sharingRegistryService.deleteDomain(domainId);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }else{
- throw new SharingRegistryException("Could not delete the domain");
- }
+ sharingRegistryService.deleteDomain(domainId);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
- <p>API method to get all domain.</p>
+ <p>API method to get all domains.</p>
*/
@RequestMapping(method = RequestMethod.GET)
- public @ResponseBody List<Domain> getDomains(@RequestParam(value = "offset") int offset, @RequestParam(value = "limit") int limit){
+ public @ResponseBody List<Domain> getDomains(@RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "-1") int limit){
return sharingRegistryService.getDomains(offset, limit);
}
diff --git a/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/EntityController.java b/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/EntityController.java
index b155fd7..ab39521 100644
--- a/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/EntityController.java
+++ b/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/EntityController.java
@@ -26,12 +26,9 @@ public class EntityController {
<p>API method to register new entity</p>
*/
@RequestMapping(method = RequestMethod.POST)
- public @ResponseBody Entity createEntity(@Valid @RequestBody Entity entity){
+ public ResponseEntity<Entity> createEntity(@Valid @RequestBody Entity entity){
Entity createdEntity = sharingRegistryService.createEntity(entity);
- if(createdEntity != null){
- return createdEntity;
- }
- else throw new SharingRegistryException("Failed to create the entity");
+ return new ResponseEntity<>(createdEntity, HttpStatus.CREATED);
}
/**
@@ -39,9 +36,8 @@ public class EntityController {
*/
@RequestMapping(method = RequestMethod.PUT)
public ResponseEntity<String> updateEntity(@Valid @RequestBody Entity entity){
- boolean response = sharingRegistryService.updateEntity(entity);
- if(response) return new ResponseEntity<>(HttpStatus.OK);
- else throw new SharingRegistryException("Failed to update the entity");
+ sharingRegistryService.updateEntity(entity);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
@@ -62,28 +58,25 @@ public class EntityController {
*/
@RequestMapping(value = "/{entityId}", method = RequestMethod.DELETE)
public ResponseEntity<String> deleteEntity(@PathVariable("entityId") String entityId, @PathVariable("domainId") String domainId){
- boolean response = sharingRegistryService.deleteEntity(domainId, entityId);
- if(response) return new ResponseEntity<>(HttpStatus.OK);
- else throw new SharingRegistryException("Could not delete the entity");
+ sharingRegistryService.deleteEntity(domainId, entityId);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
<p>API method to get entity</p>
*/
@RequestMapping(value = "/{entityId}", method = RequestMethod.GET)
- public @ResponseBody Entity getEntity(@PathVariable("entityId") String entityId, @PathVariable("domainId") String domainId){
+ public ResponseEntity<Entity> getEntity(@PathVariable("entityId") String entityId, @PathVariable("domainId") String domainId){
Entity entity = sharingRegistryService.getEntity(domainId, entityId);
- if(entity != null) return entity;
- else throw new ResourceNotFoundException("Could not find entity with entityId:" + entityId + " in domain:" + domainId);
+ return new ResponseEntity<>(entity, HttpStatus.OK);
}
/**
<p>API method to search entities</p>
*/
@RequestMapping(value = "/users/{userId}", method = RequestMethod.GET)
- public @ResponseBody List<Entity> searchEntities(@RequestBody List<SearchCriteria> filters, @PathVariable("domainId") String domainId, @PathVariable("userId") String userId, @RequestParam("offset") int offset, @RequestParam("limit") int limit){
- List<Entity> entities = sharingRegistryService.searchEntities(domainId, userId, filters, offset, limit);
- return entities;
+ public @ResponseBody List<Entity> searchEntities(@RequestBody List<SearchCriteria> filters, @PathVariable("domainId") String domainId, @PathVariable("userId") String userId, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "-1") int limit){
+ return sharingRegistryService.searchEntities(domainId, userId, filters, offset, limit);
}
/**
diff --git a/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/EntityTypeController.java b/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/EntityTypeController.java
index 1b8624b..234012a 100644
--- a/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/EntityTypeController.java
+++ b/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/EntityTypeController.java
@@ -24,14 +24,9 @@ public class EntityTypeController {
<p>API method to create a new entity type</p>
*/
@RequestMapping(method = RequestMethod.POST)
- public EntityType createEntityType(@Valid @RequestBody EntityType entityType){
+ public ResponseEntity<EntityType> createEntityType(@Valid @RequestBody EntityType entityType){
EntityType createdEntityType = sharingRegistryService.createEntityType(entityType);
- if(createdEntityType != null){
- return createdEntityType;
- }
- else{
- throw new SharingRegistryException("Failed to create the entity type");
- }
+ return new ResponseEntity<>(createdEntityType, HttpStatus.CREATED);
}
/**
@@ -39,13 +34,8 @@ public class EntityTypeController {
*/
@RequestMapping(method = RequestMethod.PUT)
public ResponseEntity<String> updateEntityType(@Valid @RequestBody EntityType entityType){
- boolean response = sharingRegistryService.updateEntityType(entityType);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }
- else{
- throw new SharingRegistryException("Failed to updated the entity type");
- }
+ sharingRegistryService.updateEntityType(entityType);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
@@ -66,35 +56,24 @@ public class EntityTypeController {
*/
@RequestMapping(value = "/{entityTypeId}", method = RequestMethod.DELETE)
public ResponseEntity<String> deleteEntityType(@PathVariable("entityTypeId") String entityTypeId, @PathVariable("domainId") String domainId){
- boolean response = sharingRegistryService.deleteEntityType(domainId, entityTypeId);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }
- else{
- throw new SharingRegistryException("Failed to delete the entity type");
- }
+ sharingRegistryService.deleteEntityType(domainId, entityTypeId);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
<p>API method to get an entity type</p>
*/
@RequestMapping(value = "/{entityTypeId}", method = RequestMethod.GET)
- public @ResponseBody EntityType getEntityType(@PathVariable("entityTypeId") String entityTypeId, @PathVariable("domainId") String domainId){
+ public ResponseEntity<EntityType> getEntityType(@PathVariable("entityTypeId") String entityTypeId, @PathVariable("domainId") String domainId){
EntityType entityType = sharingRegistryService.getEntityType(domainId, entityTypeId);
- if(entityType != null){
- return entityType;
- }
- else{
- throw new ResourceNotFoundException("Could not find the entity type with entityTypeId: " + entityTypeId + " in domainId: "+ domainId);
- }
+ return new ResponseEntity<>(entityType, HttpStatus.OK);
}
/**
<p>API method to get entity types in a domainId.</p>
*/
@RequestMapping(method = RequestMethod.GET)
- public @ResponseBody List<EntityType> getEntityTypes(@PathVariable("domainId") String domainId, @RequestParam("offset") int offset, @RequestParam("limit") int limit){
- List<EntityType> entityTypes = sharingRegistryService.getEntityTypes(domainId, offset, limit);
- return entityTypes;
+ public @ResponseBody List<EntityType> getEntityTypes(@PathVariable("domainId") String domainId, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "-1") int limit){
+ return sharingRegistryService.getEntityTypes(domainId, offset, limit);
}
}
diff --git a/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/GroupController.java b/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/GroupController.java
index 6dbc993..5860644 100644
--- a/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/GroupController.java
+++ b/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/GroupController.java
@@ -1,10 +1,7 @@
package org.apache.custos.sharing.service.api.controllers;
-import org.apache.custos.sharing.service.core.models.GroupAdmin;
import org.apache.custos.sharing.service.core.models.User;
import org.apache.custos.sharing.service.core.models.UserGroup;
-import org.apache.custos.sharing.service.core.exceptions.ResourceNotFoundException;
-import org.apache.custos.sharing.service.core.exceptions.SharingRegistryException;
import org.apache.custos.sharing.service.core.service.SharingRegistryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@@ -25,13 +22,9 @@ public class GroupController {
<p>API method to create a new group</p>
*/
@RequestMapping(method = RequestMethod.POST)
- public @ResponseBody UserGroup createGroup(@Valid @RequestBody UserGroup group){
+ public ResponseEntity<UserGroup> createGroup(@Valid @RequestBody UserGroup group){
UserGroup createdUserGroup = sharingRegistryService.createGroup(group);
- if(createdUserGroup != null){
- return createdUserGroup;
- }else{
- throw new SharingRegistryException("Failed to create group");
- }
+ return new ResponseEntity<>(createdUserGroup, HttpStatus.CREATED);
}
/**
@@ -39,13 +32,8 @@ public class GroupController {
*/
@RequestMapping(method = RequestMethod.PUT)
public ResponseEntity<String> updateGroup(@Valid @RequestBody UserGroup group){
- boolean response = sharingRegistryService.updateGroup(group);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }
- else{
- throw new SharingRegistryException("Failed to update the group");
- }
+ sharingRegistryService.updateGroup(group);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
@@ -66,26 +54,17 @@ public class GroupController {
*/
@RequestMapping(value = "/{groupId}", method = RequestMethod.DELETE)
public ResponseEntity<String> deleteGroup(@PathVariable("domainId") String domainId, @PathVariable("groupId") String groupId){
- boolean response = sharingRegistryService.deleteGroup(domainId, groupId);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }
- else{
- throw new SharingRegistryException("Failed to delete the group");
- }
+ sharingRegistryService.deleteGroup(domainId, groupId);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
<p>API method to get a group</p>
*/
@RequestMapping(value = "/{groupId}", method = RequestMethod.GET)
- public @ResponseBody UserGroup getGroup(@PathVariable("groupId") String groupId, @PathVariable("domainId") String domainId){
+ public ResponseEntity<UserGroup> getGroup(@PathVariable("groupId") String groupId, @PathVariable("domainId") String domainId){
UserGroup userGroup = sharingRegistryService.getGroup(domainId,groupId);
- if(userGroup != null){
- return userGroup;
- }else{
- throw new ResourceNotFoundException("Could not find the user group with groupId: "+ groupId + " in domainId: "+ domainId);
- }
+ return new ResponseEntity<>(userGroup,HttpStatus.OK);
}
/**
@@ -100,14 +79,9 @@ public class GroupController {
<p>API method to add list of users to a group</p>
*/
@RequestMapping(value = "/{groupId}/users", method = RequestMethod.POST)
- public ResponseEntity<String> addUsersToGroup(@PathVariable("groupId") String groupId, @PathVariable("domainId") String domainId, @RequestBody List<String> userIds){
- boolean response = sharingRegistryService.addUsersToGroup(domainId, userIds, groupId);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }
- else{
- throw new SharingRegistryException("Failed to add users to the group");
- }
+ public ResponseEntity<String> addUsersToGroup(@PathVariable("groupId") String groupId, @PathVariable("domainId") String domainId, @RequestParam("ids") List<String> userIds){
+ sharingRegistryService.addUsersToGroup(domainId, userIds, groupId);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
@@ -115,13 +89,8 @@ public class GroupController {
*/
@RequestMapping(value = "/{groupId}/users", method = RequestMethod.DELETE)
public ResponseEntity<String> removeUsersFromGroup(@PathVariable("groupId") String groupId, @PathVariable("domainId") String domainId, @RequestParam("ids") List<String> userIds){
- boolean response = sharingRegistryService.removeUsersFromGroup(domainId, userIds, groupId);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }
- else{
- throw new SharingRegistryException("Failed to remove users from the group");
- }
+ sharingRegistryService.removeUsersFromGroup(domainId, userIds, groupId);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
@@ -129,41 +98,26 @@ public class GroupController {
*/
@RequestMapping(value = "/{groupId}/owners/{ownerId}", method = RequestMethod.PUT)
public ResponseEntity<String> transferGroupOwnership(@PathVariable("domainId") String domainId,@PathVariable("groupId") String groupId, @PathVariable("ownerId") String newOwnerId){
- boolean response = sharingRegistryService.transferGroupOwnership(domainId,groupId, newOwnerId);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }
- else{
- throw new SharingRegistryException("Failed to change group owner");
- }
+ sharingRegistryService.transferGroupOwnership(domainId,groupId, newOwnerId);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
<p>API method to add Admin for a group</p>
*/
@RequestMapping(value = "/{groupId}/admins", method = RequestMethod.POST)
- public ResponseEntity<String> addGroupAdmins(@PathVariable("domainId") String domainId,@PathVariable("groupId") String groupId, @RequestBody List<GroupAdmin> admins){
- boolean response = sharingRegistryService.addGroupAdmins(domainId,groupId, admins);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }
- else{
- throw new SharingRegistryException("Failed to add group admins");
- }
+ public ResponseEntity<String> addGroupAdmins(@PathVariable("domainId") String domainId,@PathVariable("groupId") String groupId, @RequestParam("adminIds") List<String> admins){
+ sharingRegistryService.addGroupAdmins(domainId,groupId, admins);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
<p>API method to remove Admin for a group</p>
*/
@RequestMapping(value = "/{groupId}/admins", method = RequestMethod.DELETE)
- public ResponseEntity<String> removeGroupAdmins(@PathVariable("domainId") String domainId,@PathVariable("groupId") String groupId, @RequestParam("ids") List<String> adminIds){
- boolean response = sharingRegistryService.removeGroupAdmins(domainId,groupId, adminIds);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }
- else{
- throw new SharingRegistryException("Failed to remove group admins");
- }
+ public ResponseEntity<String> removeGroupAdmins(@PathVariable("domainId") String domainId,@PathVariable("groupId") String groupId, @RequestParam("adminIds") List<String> adminIds){
+ sharingRegistryService.removeGroupAdmins(domainId,groupId, adminIds);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
@@ -196,7 +150,7 @@ public class GroupController {
<p>API method to get list of child users in a group. Only the direct members will be returned.</p>
*/
@RequestMapping(value = "/{groupId}/users", method = RequestMethod.GET)
- public @ResponseBody List<User> getGroupMembersOfTypeUser(@PathVariable("domainId") String domainId, @PathVariable("groupId") String groupId, @RequestParam("offset") int offset, @RequestParam("limit") int limit){
+ public @ResponseBody List<User> getGroupMembersOfTypeUser(@PathVariable("domainId") String domainId, @PathVariable("groupId") String groupId, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(value = "limit", defaultValue = "-1") int limit){
return sharingRegistryService.getGroupMembersOfTypeUser(domainId, groupId, offset, limit);
}
@@ -204,7 +158,7 @@ public class GroupController {
<p>API method to get list of child groups in a group. Only the direct members will be returned.</p>
*/
@RequestMapping(value = "/{groupId}/subgroups", method = RequestMethod.GET)
- public @ResponseBody List<UserGroup> getGroupMembersOfTypeGroup(@PathVariable("domainId") String domainId, @PathVariable("groupId") String groupId, @RequestParam("offset") int offset, @RequestParam("limit") int limit){
+ public @ResponseBody List<UserGroup> getGroupMembersOfTypeGroup(@PathVariable("domainId") String domainId, @PathVariable("groupId") String groupId, @RequestParam(name= "offset", defaultValue = "0") int offset, @RequestParam(value = "limit", defaultValue = "-1") int limit){
return sharingRegistryService.getGroupMembersOfTypeGroup(domainId, groupId, offset, limit);
}
@@ -212,13 +166,9 @@ public class GroupController {
<p>API method to add a child group to a parent group.</p>
*/
@RequestMapping(value = "/{groupId}/subgroups", method = RequestMethod.POST)
- public ResponseEntity<String> addChildGroupsToParentGroup(@PathVariable("domainId") String domainId, @PathVariable("groupId") String groupId, @RequestBody List<String> childIds){
- boolean response = sharingRegistryService.addChildGroupsToParentGroup(domainId, childIds, groupId);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }else{
- throw new SharingRegistryException("Failed to add child groups to parent group");
- }
+ public ResponseEntity<String> addChildGroupsToParentGroup(@PathVariable("domainId") String domainId, @PathVariable("groupId") String groupId, @RequestParam("subgroupIds") List<String> childIds){
+ sharingRegistryService.addChildGroupsToParentGroup(domainId, childIds, groupId);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
@@ -226,12 +176,8 @@ public class GroupController {
*/
@RequestMapping(value = "/{groupId}/subgroups/{childGroupId}", method = RequestMethod.DELETE)
public ResponseEntity<String> removeChildGroupFromParentGroup(@PathVariable("domainId") String domainId, @PathVariable("groupId") String groupId, @PathVariable("childGroupId") String childGroupId){
- boolean response = sharingRegistryService.removeChildGroupFromParentGroup(domainId, childGroupId, groupId);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }else{
- throw new SharingRegistryException("Failed to remove child groups from parent group");
- }
+ sharingRegistryService.removeChildGroupFromParentGroup(domainId, childGroupId, groupId);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
@@ -241,29 +187,4 @@ public class GroupController {
public @ResponseBody List<UserGroup> getAllMemberGroupsForUser(@PathVariable("domainId") String domainId, @PathVariable("userId") String userId){
return sharingRegistryService.getAllMemberGroupsForUser(domainId, userId);
}
-
-// public static void main(String[] args) throws Exception{
-// String url = "http://localhost:7070";
-// String r = "/domains/default/groups";
-// UserGroup test = new UserGroup();
-// test.setGroupId("teqq1");
-// test.setDomainId("default");
-// test.setOwnerId("default-admin@default");
-// test.setName("");
-// test.setGroupType(GroupType.USER_LEVEL_GROUP);
-// ServiceRequest serviceRequest = new ServiceRequest();
-// HttpResponse response = serviceRequest.httpPost(url, r, test);
-// System.out.println(response);
-// if(response.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK) {
-// BufferedReader rd = new BufferedReader(
-// new InputStreamReader(response.getEntity().getContent()));
-// StringBuffer result = new StringBuffer();
-// String line = "";
-// while ((line = rd.readLine()) != null) {
-// result.append(line);
-// }
-// JSONObject jsonObject = new JSONObject(result.toString());
-// System.out.println(jsonObject.get("groupId"));
-// }
-// }
}
diff --git a/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/PermissionTypeController.java b/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/PermissionTypeController.java
index 81c9941..e7d66fb 100644
--- a/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/PermissionTypeController.java
+++ b/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/PermissionTypeController.java
@@ -1,18 +1,13 @@
package org.apache.custos.sharing.service.api.controllers;
import org.apache.custos.sharing.service.core.models.PermissionType;
-import org.apache.custos.sharing.service.core.exceptions.ResourceNotFoundException;
-import org.apache.custos.sharing.service.core.exceptions.SharingRegistryException;
import org.apache.custos.sharing.service.core.service.SharingRegistryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
-
import javax.validation.Valid;
-import java.util.Collections;
import java.util.List;
-import java.util.Map;
@RestController
@RequestMapping("domain/{domainId}/permissionTypes")
@@ -24,13 +19,9 @@ public class PermissionTypeController {
<p>API method to create permission type</p>
*/
@RequestMapping(method = RequestMethod.POST)
- public @ResponseBody PermissionType createPermissionType(@Valid @RequestBody PermissionType permissionType){
+ public ResponseEntity<PermissionType> createPermissionType(@Valid @RequestBody PermissionType permissionType){
PermissionType createdPermissionType = sharingRegistryService.createPermissionType(permissionType);
- if(createdPermissionType != null){
- return createdPermissionType;
- }else{
- throw new SharingRegistryException("Could not create the permission type");
- }
+ return new ResponseEntity<>(createdPermissionType, HttpStatus.CREATED);
}
/**
@@ -38,13 +29,8 @@ public class PermissionTypeController {
*/
@RequestMapping(method = RequestMethod.PUT)
public ResponseEntity<String> updatePermissionType(@Valid @RequestBody PermissionType permissionType){
-
- boolean response = sharingRegistryService.updatePermissionType(permissionType);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }else{
- throw new SharingRegistryException("Could not update the permission type");
- }
+ sharingRegistryService.updatePermissionType(permissionType);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
@@ -65,25 +51,17 @@ public class PermissionTypeController {
*/
@RequestMapping(value= "/{permissionTypeId}", method = RequestMethod.DELETE)
public ResponseEntity<String> deletePermissionType(@PathVariable("permissionTypeId") String permissionTypeId, @PathVariable("domainId") String domainId){
- boolean response = sharingRegistryService.deletePermissionType(domainId,permissionTypeId);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }else{
- throw new SharingRegistryException("Could not delete the permission type");
- }
+ sharingRegistryService.deletePermissionType(domainId,permissionTypeId);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
<p>API method to get permission type</p>
*/
@RequestMapping(value= "/{permissionTypeId}", method = RequestMethod.GET)
- public @ResponseBody PermissionType getPermissionType(@PathVariable("permissionTypeId") String permissionTypeId, @PathVariable("domainId") String domainId){
+ public ResponseEntity<PermissionType> getPermissionType(@PathVariable("permissionTypeId") String permissionTypeId, @PathVariable("domainId") String domainId){
PermissionType permissionType = sharingRegistryService.getPermissionType(domainId, permissionTypeId);
- if(permissionType != null){
- return permissionType;
- }else{
- throw new ResourceNotFoundException("Could not find the permission type with permission Id: "+ permissionTypeId + " in domainId: " + domainId);
- }
+ return new ResponseEntity<>(permissionType, HttpStatus.OK);
}
/**
@@ -91,7 +69,7 @@ public class PermissionTypeController {
*/
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody
- List<PermissionType> getPermissionTypes(@PathVariable("domainId") String domainId, @RequestParam("offset") int offset, @RequestParam("limit") int limit){
+ List<PermissionType> getPermissionTypes(@PathVariable("domainId") String domainId, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "-1") int limit){
return sharingRegistryService.getPermissionTypes(domainId, offset, limit);
}
diff --git a/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/UsersController.java b/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/UsersController.java
index b880170..f9b3e06 100644
--- a/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/UsersController.java
+++ b/custos-sharing-registry-service/sharing-service-api/src/main/java/org/apache/custos/sharing/service/api/controllers/UsersController.java
@@ -1,8 +1,6 @@
package org.apache.custos.sharing.service.api.controllers;
import org.apache.custos.sharing.service.core.models.User;
-import org.apache.custos.sharing.service.core.exceptions.ResourceNotFoundException;
-import org.apache.custos.sharing.service.core.exceptions.SharingRegistryException;
import org.apache.custos.sharing.service.core.service.SharingRegistryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@@ -23,13 +21,9 @@ public class UsersController {
<p>API method to register a user in the system</p>
*/
@RequestMapping(method = RequestMethod.POST)
- public @ResponseBody User createUser(@Valid @RequestBody User user){
+ public ResponseEntity<User> createUser(@Valid @RequestBody User user){
User createdUser = sharingRegistryService.createUser(user);
- if(createdUser != null){
- return createdUser;
- }else{
- throw new SharingRegistryException("Failed to create the user");
- }
+ return new ResponseEntity<>(createdUser, HttpStatus.CREATED);
}
/**
@@ -37,12 +31,8 @@ public class UsersController {
*/
@RequestMapping(method = RequestMethod.PUT)
public ResponseEntity<String> updateUser(@Valid @RequestBody User user){
- boolean response = sharingRegistryService.updatedUser(user);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }else{
- throw new SharingRegistryException("Failed to update the user");
- }
+ sharingRegistryService.updatedUser(user);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
@@ -63,25 +53,17 @@ public class UsersController {
*/
@RequestMapping(value="/{userId}",method = RequestMethod.DELETE)
public ResponseEntity<String> deleteUser(@PathVariable("userId") String userId, @PathVariable("domainId") String domainId){
- boolean response = sharingRegistryService.deleteUser(domainId, userId);
- if(response){
- return new ResponseEntity<>(HttpStatus.OK);
- }else{
- throw new SharingRegistryException("Failed to delete the user");
- }
+ sharingRegistryService.deleteUser(domainId, userId);
+ return new ResponseEntity<>(HttpStatus.OK);
}
/**
<p>API method to get a user</p>
*/
@RequestMapping(value = "/{userId}", method = RequestMethod.GET)
- public User getUser(@PathVariable("domainId") String domainId,@PathVariable("userId") String userId){
+ public ResponseEntity<User> getUser(@PathVariable("domainId") String domainId,@PathVariable("userId") String userId){
User user = sharingRegistryService.getUser(domainId, userId);
- if(user != null){
- return user;
- }else {
- throw new ResourceNotFoundException("Could not find the user with userId: "+ userId + " for domainId: "+ domainId);
- }
+ return new ResponseEntity<>(user, HttpStatus.OK);
}
/**
@@ -91,7 +73,7 @@ public class UsersController {
<li>limit : Number of max results to be sent</li>
*/
@RequestMapping(method = RequestMethod.GET)
- public List<User> getUsers(@PathVariable("domainId") String domainId, @RequestParam("offset") int offset, @RequestParam("limit") int limit){
+ public List<User> getUsers(@PathVariable("domainId") String domainId, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "-1") int limit){
return sharingRegistryService.getUsers(domainId,offset, limit);
}
}
diff --git a/custos-sharing-registry-service/sharing-service-api/src/main/resources/application.properties b/custos-sharing-registry-service/sharing-service-api/src/main/resources/application.properties
index 2d64be9..56037ac 100644
--- a/custos-sharing-registry-service/sharing-service-api/src/main/resources/application.properties
+++ b/custos-sharing-registry-service/sharing-service-api/src/main/resources/application.properties
@@ -1,2 +1,3 @@
server.address=0.0.0.0
-server.port= 7070
\ No newline at end of file
+server.port= 7070
+management.endpoints.web.exposure.include=info,health,mappings
\ No newline at end of file
diff --git a/custos-sharing-registry-service/sharing-service-core/src/main/java/org/apache/custos/sharing/service/core/exceptions/InvalidRequestException.java b/custos-sharing-registry-service/sharing-service-core/src/main/java/org/apache/custos/sharing/service/core/exceptions/InvalidRequestException.java
new file mode 100644
index 0000000..e15e0a2
--- /dev/null
+++ b/custos-sharing-registry-service/sharing-service-core/src/main/java/org/apache/custos/sharing/service/core/exceptions/InvalidRequestException.java
@@ -0,0 +1,16 @@
+package org.apache.custos.sharing.service.core.exceptions;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+@ResponseStatus(value= HttpStatus.BAD_REQUEST, reason = "Request cannot be completed because the server refused to fulfill the request.")
+public class InvalidRequestException extends RuntimeException{
+
+ public InvalidRequestException(String errorMessage) {
+ super(errorMessage);
+ }
+
+ public InvalidRequestException(String errorMessage, Throwable err) {
+ super(errorMessage, err);
+ }
+}
diff --git a/custos-sharing-registry-service/sharing-service-core/src/main/java/org/apache/custos/sharing/service/core/models/Domain.java b/custos-sharing-registry-service/sharing-service-core/src/main/java/org/apache/custos/sharing/service/core/models/Domain.java
index fe71464..15734d8 100644
--- a/custos-sharing-registry-service/sharing-service-core/src/main/java/org/apache/custos/sharing/service/core/models/Domain.java
+++ b/custos-sharing-registry-service/sharing-service-core/src/main/java/org/apache/custos/sharing/service/core/models/Domain.java
@@ -48,7 +48,7 @@ public class Domain {
this.createdTime = createdTime;
}
- public double getUpdatedTime() {
+ public Long getUpdatedTime() {
return updatedTime;
}
diff --git a/custos-sharing-registry-service/sharing-service-core/src/main/java/org/apache/custos/sharing/service/core/service/SharingRegistryService.java b/custos-sharing-registry-service/sharing-service-core/src/main/java/org/apache/custos/sharing/service/core/service/SharingRegistryService.java
index 921adc5..c9d0ecc 100644
--- a/custos-sharing-registry-service/sharing-service-core/src/main/java/org/apache/custos/sharing/service/core/service/SharingRegistryService.java
+++ b/custos-sharing-registry-service/sharing-service-core/src/main/java/org/apache/custos/sharing/service/core/service/SharingRegistryService.java
@@ -21,6 +21,8 @@ package org.apache.custos.sharing.service.core.service;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.custos.commons.utils.DBInitializer;
+import org.apache.custos.sharing.service.core.exceptions.InvalidRequestException;
+import org.apache.custos.sharing.service.core.exceptions.ResourceNotFoundException;
import org.apache.custos.sharing.service.core.models.*;
import org.apache.custos.sharing.service.core.db.entities.*;
import org.apache.custos.sharing.service.core.db.repositories.*;
@@ -28,14 +30,13 @@ import org.apache.custos.sharing.service.core.db.utils.DBConstants;
import org.apache.custos.sharing.service.core.db.utils.SharingRegistryDBInitConfig;
import org.apache.custos.sharing.service.core.exceptions.DuplicateEntryException;
import org.apache.custos.sharing.service.core.exceptions.SharingRegistryException;
-import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
-
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
+import java.util.stream.Collectors;
@Service
public class SharingRegistryService {
@@ -59,7 +60,6 @@ public class SharingRegistryService {
public Domain createDomain(Domain domain) throws SharingRegistryException, DuplicateEntryException {
try{
- domain.setDomainId(domain.getDomainId());
if((new DomainRepository()).get(domain.getDomainId()) != null)
throw new DuplicateEntryException("There exist domain with given domain id");
@@ -78,21 +78,31 @@ public class SharingRegistryService {
(new PermissionTypeRepository()).create(permissionType);
return createdDomain;
+ }catch(DuplicateEntryException ex){
+ throw ex;
}catch (Throwable ex){
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public boolean updateDomain(Domain domain) throws SharingRegistryException {
+ public boolean updateDomain(Domain domain) throws SharingRegistryException, ResourceNotFoundException {
try{
Domain oldDomain = (new DomainRepository()).get(domain.getDomainId());
+ if(oldDomain == null){
+ throw new ResourceNotFoundException("Could not find the domain with domainId: "+ domain.getDomainId());
+ }
domain.setCreatedTime(oldDomain.getCreatedTime());
domain.setUpdatedTime(System.currentTimeMillis());
domain = getUpdatedObject(oldDomain, domain);
- (new DomainRepository()).update(domain);
- return true;
- }catch (Throwable ex) {
+ Domain updatedDomain = (new DomainRepository()).update(domain);
+ if(updatedDomain != null && updatedDomain.getDomainId().equals(oldDomain.getDomainId())){
+ return true;
+ }
+ throw new SharingRegistryException("Could not update the domain with domainId: "+ oldDomain.getDomainId());
+ }catch(ResourceNotFoundException ex) {
+ throw ex;
+ }catch(Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
@@ -112,19 +122,32 @@ public class SharingRegistryService {
}
}
- public boolean deleteDomain(String domainId) throws SharingRegistryException {
+ public boolean deleteDomain(String domainId) throws SharingRegistryException, ResourceNotFoundException {
try{
- return (new DomainRepository()).delete(domainId);
+ if(!isDomainExists(domainId)){
+ throw new ResourceNotFoundException("Could not find domain with domainId: "+ domainId);
+ }
+ boolean deleted = (new DomainRepository()).delete(domainId);
+ if(deleted) return true;
+ else throw new SharingRegistryException("Could not delete the domain with domainId: "+ domainId);
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public Domain getDomain(String domainId) throws SharingRegistryException {
+ public Domain getDomain(String domainId) throws SharingRegistryException, ResourceNotFoundException {
try{
- return (new DomainRepository()).get(domainId);
- }catch (Throwable ex) {
+ Domain domain = (new DomainRepository()).get(domainId);
+ if(domain == null){
+ throw new ResourceNotFoundException("Could not find domain with domainId:" + domainId);
+ }
+ return domain;
+ }catch (ResourceNotFoundException ex){
+ throw ex;
+ } catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
@@ -163,9 +186,13 @@ public class SharingRegistryService {
userGroup.setOwnerId(user.getUserId());
userGroup.setGroupType(GroupType.USER_LEVEL_GROUP);
userGroup.setGroupCardinality(GroupCardinality.SINGLE_USER);
+ userGroup.setCreatedTime(System.currentTimeMillis());
+ userGroup.setUpdatedTime(System.currentTimeMillis());
(new UserGroupRepository()).create(userGroup);
return createdUser;
+ }catch(DuplicateEntryException ex){
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -174,6 +201,9 @@ public class SharingRegistryService {
public boolean updatedUser(User user) throws SharingRegistryException {
try{
+ if(!isUserExists(user.getDomainId(), user.getUserId())){
+ throw new ResourceNotFoundException("Could not find user with userId: " + user.getUserId());
+ }
UserPK userPK = new UserPK();
userPK.setUserId(user.getUserId());
userPK.setDomainId(user.getDomainId());
@@ -181,7 +211,7 @@ public class SharingRegistryService {
user.setCreatedTime(oldUser.getCreatedTime());
user.setUpdatedTime(System.currentTimeMillis());
user = getUpdatedObject(oldUser, user);
- (new UserRepository()).update(user);
+ User updatedUser = (new UserRepository()).update(user);
UserGroupPK userGroupPK = new UserGroupPK();
userGroupPK.setGroupId(user.getUserId());
@@ -190,7 +220,10 @@ public class SharingRegistryService {
userGroup.setName(user.getUserName());
userGroup.setDescription("user " + user.getUserName() + " group");
updateGroup(userGroup);
- return true;
+ if(updatedUser != null && updatedUser.getUserId().equals(user.getUserId())){
+ return true;
+ }
+ throw new SharingRegistryException("Could not update user with userId: " + user.getUserId());
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -214,41 +247,52 @@ public class SharingRegistryService {
}
}
- public boolean deleteUser(String domainId, String userId) throws SharingRegistryException {
+ public boolean deleteUser(String domainId, String userId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isUserExists(domainId, userId)) throw new ResourceNotFoundException("Could not find userId: "+userId +" in domainId:" + domainId);
UserPK userPK = new UserPK();
userPK.setUserId(userId);
userPK.setDomainId(domainId);
- (new UserRepository()).delete(userPK);
+ boolean deleteuser = (new UserRepository()).delete(userPK);
UserGroupPK userGroupPK = new UserGroupPK();
userGroupPK.setGroupId(userId);
userGroupPK.setDomainId(domainId);
- (new UserGroupRepository()).delete(userGroupPK);
- return true;
- }catch (Throwable ex) {
+ boolean deleteusergroup = (new UserGroupRepository()).delete(userGroupPK);
+ if(deleteuser && deleteusergroup) return true;
+ throw new SharingRegistryException("Could not delete user: "+ userId);
+ }catch (ResourceNotFoundException ex){
+ throw ex;
+ }
+ catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public User getUser(String domainId, String userId) throws SharingRegistryException {
+ public User getUser(String domainId, String userId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isUserExists(domainId, userId)) throw new ResourceNotFoundException("Could not find userId: "+userId +" in domainId:" + domainId);
UserPK userPK = new UserPK();
userPK.setUserId(userId);
userPK.setDomainId(domainId);
return (new UserRepository()).get(userPK);
+ }catch (ResourceNotFoundException ex){
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public List<User> getUsers(String domain, int offset, int limit) throws SharingRegistryException {
+ public List<User> getUsers(String domain, int offset, int limit) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isDomainExists(domain)) throw new ResourceNotFoundException("Could not find domain with domainId: "+ domain);
HashMap<String, String> filters = new HashMap<>();
filters.put(DBConstants.UserTable.DOMAIN_ID, domain);
return (new UserRepository()).select(filters, offset, limit);
+ }catch (ResourceNotFoundException ex){
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -275,10 +319,9 @@ public class SharingRegistryService {
UserGroup createdUserGroup = (new UserGroupRepository()).create(group);
addUsersToGroup(group.getDomainId(), Arrays.asList(group.getOwnerId()), group.getGroupId());
- addGroupAdmins(group.getDomainId(), group.getGroupId(), group.getGroupAdmins());
+ addGroupAdmins(group.getDomainId(), group.getGroupId(), group.getGroupAdmins().stream().map(y->y.getAdminId()).collect(Collectors.toList()));
return createdUserGroup;
} catch (DuplicateEntryException ex) {
- logger.error(ex.getMessage(), ex);
throw ex;
} catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
@@ -286,22 +329,27 @@ public class SharingRegistryService {
}
}
- public boolean updateGroup(UserGroup group) throws SharingRegistryException {
- try{
+ public boolean updateGroup(UserGroup group) throws SharingRegistryException, ResourceNotFoundException, InvalidRequestException {
+ try {
+ if (isGroupExists(group.getDomainId(), group.getGroupId())) {
+ throw new ResourceNotFoundException("Could not find the group with groupId: " + group.getGroupId());
+ }
group.setUpdatedTime(System.currentTimeMillis());
UserGroupPK userGroupPK = new UserGroupPK();
userGroupPK.setGroupId(group.getGroupId());
userGroupPK.setDomainId(group.getDomainId());
UserGroup oldGroup = (new UserGroupRepository()).get(userGroupPK);
+ if(!group.getOwnerId().equals(oldGroup.getOwnerId()))
+ throw new InvalidRequestException("Group owner cannot be changed");
group.setGroupCardinality(oldGroup.getGroupCardinality());
group.setCreatedTime(oldGroup.getCreatedTime());
group = getUpdatedObject(oldGroup, group);
-
- if(!group.getOwnerId().equals(oldGroup.getOwnerId()))
- throw new SharingRegistryException("Group owner cannot be changed");
-
- (new UserGroupRepository()).update(group);
+ UserGroup updatedGroup = (new UserGroupRepository()).update(group);
+ if (!updatedGroup.getGroupId().equals(oldGroup.getGroupId()))
+ throw new SharingRegistryException("Group could not be updated");
return true;
+ }catch (InvalidRequestException | ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -318,56 +366,76 @@ public class SharingRegistryService {
public boolean isGroupExists(String domainId, String groupId) throws SharingRegistryException {
try{
+ if (isGroupExists(domainId, groupId)) {
+ throw new ResourceNotFoundException("Could not find the group with groupId: " + groupId);
+ }
UserGroupPK userGroupPK = new UserGroupPK();
userGroupPK.setDomainId(domainId);
userGroupPK.setGroupId(groupId);
return (new UserGroupRepository()).isExists(userGroupPK);
- }catch (Throwable ex) {
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
+ }
+ catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public boolean deleteGroup(String domainId, String groupId) throws SharingRegistryException {
+ public boolean deleteGroup(String domainId, String groupId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if (isGroupExists(domainId, groupId)) {
+ throw new ResourceNotFoundException("Could not find the group with groupId: " + groupId);
+ }
UserGroupPK userGroupPK = new UserGroupPK();
userGroupPK.setGroupId(groupId);
userGroupPK.setDomainId(domainId);
- (new UserGroupRepository()).delete(userGroupPK);
+ boolean deleted = (new UserGroupRepository()).delete(userGroupPK);
+ if(!deleted) throw new SharingRegistryException("Could not deleted the group with groupId: " + groupId);
return true;
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public UserGroup getGroup(String domainId, String groupId) throws SharingRegistryException {
+ public UserGroup getGroup(String domainId, String groupId) throws SharingRegistryException, ResourceNotFoundException {
try{
UserGroupPK userGroupPK = new UserGroupPK();
userGroupPK.setGroupId(groupId);
userGroupPK.setDomainId(domainId);
- return (new UserGroupRepository()).get(userGroupPK);
+ UserGroup userGroup = (new UserGroupRepository()).get(userGroupPK);
+ if(userGroup != null) return userGroup;
+ throw new ResourceNotFoundException("Could not find the group with groupId: " + groupId);
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public List<UserGroup> getGroups(String domain, int offset, int limit) throws SharingRegistryException {
+ public List<UserGroup> getGroups(String domain, int offset, int limit) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isDomainExists(domain)) throw new ResourceNotFoundException("Could not find the domain with domainId: "+domain);
HashMap<String, String> filters = new HashMap<>();
filters.put(DBConstants.UserGroupTable.DOMAIN_ID, domain);
// Only return groups with MULTI_USER cardinality which is the only type of cardinality allowed for client created groups
filters.put(DBConstants.UserGroupTable.GROUP_CARDINALITY, GroupCardinality.MULTI_USER.name());
return (new UserGroupRepository()).select(filters, offset, limit);
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public boolean addUsersToGroup(String domainId, List<String> userIds, String groupId) throws SharingRegistryException {
+ public boolean addUsersToGroup(String domainId, List<String> userIds, String groupId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isDomainExists(domainId)) throw new ResourceNotFoundException("Could not find the domain with domainId: "+domainId);
for(int i=0; i < userIds.size(); i++){
GroupMembership groupMembership = new GroupMembership();
groupMembership.setParentId(groupId);
@@ -379,17 +447,20 @@ public class SharingRegistryService {
(new GroupMembershipRepository()).create(groupMembership);
}
return true;
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public boolean removeUsersFromGroup(String domainId, List<String> userIds, String groupId) throws SharingRegistryException {
+ public boolean removeUsersFromGroup(String domainId, List<String> userIds, String groupId) throws SharingRegistryException, ResourceNotFoundException, InvalidRequestException{
try{
+ if(!isDomainExists(domainId)) throw new ResourceNotFoundException("Could not find the domain with domainId: "+domainId);
for (String userId: userIds) {
if (hasOwnerAccess(domainId, groupId, userId)) {
- throw new SharingRegistryException("List of User Ids contains Owner Id. Cannot remove owner from the group");
+ throw new InvalidRequestException("List of User Ids contains Owner Id. Cannot remove owner from the group");
}
}
@@ -401,6 +472,8 @@ public class SharingRegistryService {
(new GroupMembershipRepository()).delete(groupMembershipPK);
}
return true;
+ }catch (ResourceNotFoundException | InvalidRequestException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -409,13 +482,14 @@ public class SharingRegistryService {
public boolean transferGroupOwnership(String domainId, String groupId, String newOwnerId) throws SharingRegistryException {
try {
+ if(!isDomainExists(domainId) || !isGroupExists(domainId, groupId) || !isUserExists(domainId, newOwnerId)) throw new ResourceNotFoundException("Could not find resource");
List<User> groupUser = getGroupMembersOfTypeUser(domainId, groupId, 0, -1);
if (!isUserBelongsToGroup(groupUser, newOwnerId)) {
- throw new SharingRegistryException("New group owner is not part of the group");
+ throw new InvalidRequestException("New group owner is not part of the group");
}
if (hasOwnerAccess(domainId, groupId, newOwnerId)) {
- throw new DuplicateEntryException("User already the current owner of the group");
+ throw new InvalidRequestException("User already the current owner of the group");
}
// remove the new owner as Admin if present
if (hasAdminAccess(domainId, groupId, newOwnerId)) {
@@ -436,8 +510,9 @@ public class SharingRegistryService {
(new UserGroupRepository()).update(newUserGroup);
return true;
- }
- catch (Throwable ex) {
+ }catch (ResourceNotFoundException | InvalidRequestException ex) {
+ throw ex;
+ }catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
@@ -452,28 +527,32 @@ public class SharingRegistryService {
return false;
}
- public boolean addGroupAdmins(String domainId, String groupId, List<GroupAdmin> admins) throws SharingRegistryException {
+ public boolean addGroupAdmins(String domainId, String groupId, List<String> admins) throws SharingRegistryException, ResourceNotFoundException,DuplicateEntryException, InvalidRequestException {
try{
+ if(!isDomainExists(domainId) || !isGroupExists(domainId, groupId)) throw new ResourceNotFoundException("Could not find resource");
List<User> groupUser = getGroupMembersOfTypeUser(domainId, groupId, 0, -1);
- for (GroupAdmin admin: admins) {
- String adminId = admin.getAdminId();
+ for (String adminId: admins) {
if (! isUserBelongsToGroup(groupUser, adminId)) {
- throw new SharingRegistryException("Admin not the user of the group. GroupId : "+ groupId + ", AdminId : "+ adminId);
+ throw new InvalidRequestException("Admin not the user of the group. GroupId : "+ groupId + ", AdminId : "+ adminId);
}
GroupAdminPK groupAdminPK = new GroupAdminPK();
groupAdminPK.setGroupId(groupId);
groupAdminPK.setAdminId(adminId);
groupAdminPK.setDomainId(domainId);
-
+ GroupAdmin groupAdmin = new GroupAdmin();
+ groupAdmin.setAdminId(adminId);
+ groupAdmin.setDomainId(domainId);
+ groupAdmin.setGroupId(groupId);
if((new GroupAdminRepository()).get(groupAdminPK) != null)
throw new DuplicateEntryException("User already an admin for the group");
- (new GroupAdminRepository()).create(admin);
+ (new GroupAdminRepository()).create(groupAdmin);
}
return true;
- }
- catch (Throwable ex) {
+ }catch (ResourceNotFoundException | DuplicateEntryException | InvalidRequestException ex) {
+ throw ex;
+ }catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
@@ -496,8 +575,9 @@ public class SharingRegistryService {
}
}
- public boolean hasAdminAccess(String domainId, String groupId, String adminId) throws SharingRegistryException {
+ public boolean hasAdminAccess(String domainId, String groupId, String adminId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isDomainExists(domainId) || !isGroupExists(domainId, groupId) || !isUserExists(domainId, adminId)) throw new ResourceNotFoundException("Could not find resource");
GroupAdminPK groupAdminPK = new GroupAdminPK();
groupAdminPK.setGroupId(groupId);
groupAdminPK.setAdminId(adminId);
@@ -506,8 +586,9 @@ public class SharingRegistryService {
if((new GroupAdminRepository()).get(groupAdminPK) != null)
return true;
return false;
- }
- catch (Throwable ex) {
+ }catch (ResourceNotFoundException ex){
+ throw ex;
+ }catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
@@ -515,6 +596,7 @@ public class SharingRegistryService {
public boolean hasOwnerAccess(String domainId, String groupId, String ownerId) throws SharingRegistryException {
try {
+ if(!isDomainExists(domainId) || !isGroupExists(domainId, groupId) || !isUserExists(domainId, ownerId)) throw new ResourceNotFoundException("Could not find resource");
UserGroupPK userGroupPK = new UserGroupPK();
userGroupPK.setGroupId(groupId);
userGroupPK.setDomainId(domainId);
@@ -523,8 +605,9 @@ public class SharingRegistryService {
if(getGroup.getOwnerId().equals(ownerId))
return true;
return false;
- }
- catch (Throwable ex) {
+ }catch (ResourceNotFoundException ex){
+ throw ex;
+ }catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
@@ -532,30 +615,38 @@ public class SharingRegistryService {
public List<User> getGroupMembersOfTypeUser(String domainId, String groupId, int offset, int limit) throws SharingRegistryException {
try{
+ if(!isDomainExists(domainId) || !isGroupExists(domainId, groupId)) throw new ResourceNotFoundException("Could not find resource");
//TODO limit offset
List<User> groupMemberUsers = (new GroupMembershipRepository()).getAllChildUsers(domainId, groupId);
return groupMemberUsers;
+ }catch (ResourceNotFoundException ex){
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public List<UserGroup> getGroupMembersOfTypeGroup(String domainId, String groupId, int offset, int limit) throws SharingRegistryException {
+ public List<UserGroup> getGroupMembersOfTypeGroup(String domainId, String groupId, int offset, int limit) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isDomainExists(domainId) || !isGroupExists(domainId, groupId)) throw new ResourceNotFoundException("Could not find resource");
//TODO limit offset
List<UserGroup> groupMemberGroups = (new GroupMembershipRepository()).getAllChildGroups(domainId, groupId);
return groupMemberGroups;
+ }catch (ResourceNotFoundException ex){
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public boolean addChildGroupsToParentGroup(String domainId, List<String> childIds, String groupId) throws SharingRegistryException {
+ public boolean addChildGroupsToParentGroup(String domainId, List<String> childIds, String groupId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isDomainExists(domainId) || !isGroupExists(domainId, groupId)) throw new ResourceNotFoundException("Could not find resource");
for(String childId : childIds) {
//Todo check for cyclic dependencies
+ if(!isGroupExists(domainId, childId)) throw new ResourceNotFoundException("Could not find resource");
GroupMembership groupMembership = new GroupMembership();
groupMembership.setParentId(groupId);
groupMembership.setChildId(childId);
@@ -566,6 +657,8 @@ public class SharingRegistryService {
(new GroupMembershipRepository()).create(groupMembership);
}
return true;
+ }catch (ResourceNotFoundException ex){
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -574,22 +667,28 @@ public class SharingRegistryService {
public boolean removeChildGroupFromParentGroup(String domainId, String childId, String groupId) throws SharingRegistryException {
try{
+ if(!isDomainExists(domainId) || !isGroupExists(domainId, groupId) || !isGroupExists(domainId, childId)) throw new ResourceNotFoundException("Could not find resource");
GroupMembershipPK groupMembershipPK = new GroupMembershipPK();
groupMembershipPK.setParentId(groupId);
groupMembershipPK.setChildId(childId);
groupMembershipPK.setDomainId(domainId);
(new GroupMembershipRepository()).delete(groupMembershipPK);
return true;
+ }catch (ResourceNotFoundException ex){
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public List<UserGroup> getAllMemberGroupsForUser(String domainId, String userId) throws SharingRegistryException {
+ public List<UserGroup> getAllMemberGroupsForUser(String domainId, String userId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isDomainExists(domainId) || !isUserExists(domainId, userId)) throw new ResourceNotFoundException("Could not find resource");
GroupMembershipRepository groupMembershipRepository = new GroupMembershipRepository();
return groupMembershipRepository.getAllMemberGroupsForUser(domainId, userId);
+ }catch (ResourceNotFoundException ex){
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -611,14 +710,17 @@ public class SharingRegistryService {
entityType.setCreatedTime(System.currentTimeMillis());
entityType.setUpdatedTime(System.currentTimeMillis());
return (new EntityTypeRepository()).create(entityType);
- }catch (Throwable ex) {
+ }catch (DuplicateEntryException ex) {
+ throw ex;
+ }catch(Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public boolean updateEntityType(EntityType entityType) throws SharingRegistryException {
+ public boolean updateEntityType(EntityType entityType) throws SharingRegistryException, ResourceNotFoundException{
try{
+ if(!isEntityTypeExists(entityType.getDomainId(), entityType.getEntityTypeId())) throw new ResourceNotFoundException("Could not find entity type with id: " + entityType.getEntityTypeId());
entityType.setUpdatedTime(System.currentTimeMillis());
EntityTypePK entityTypePK = new EntityTypePK();
entityTypePK.setDomainId(entityType.getDomainId());
@@ -626,8 +728,11 @@ public class SharingRegistryService {
EntityType oldEntityType = (new EntityTypeRepository()).get(entityTypePK);
entityType.setCreatedTime(oldEntityType.getCreatedTime());
entityType = getUpdatedObject(oldEntityType, entityType);
- (new EntityTypeRepository()).update(entityType);
- return true;
+ EntityType updatedEntityType = (new EntityTypeRepository()).update(entityType);
+ if(updatedEntityType != null && updatedEntityType.getEntityTypeId().equals(entityType.getEntityTypeId())) return true;
+ throw new SharingRegistryException("Could not update the entity type");
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -639,48 +744,60 @@ public class SharingRegistryService {
*
* @param entityTypeId
*/
- public boolean isEntityTypeExists(String domainId, String entityTypeId) throws SharingRegistryException {
+ public boolean isEntityTypeExists(String domainId, String entityTypeId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isEntityTypeExists(domainId, entityTypeId)) throw new ResourceNotFoundException("Could not find entity type with id: " + entityTypeId);
EntityTypePK entityTypePK = new EntityTypePK();
entityTypePK.setDomainId(domainId);
entityTypePK.setEntityTypeId(entityTypeId);
return (new EntityTypeRepository()).isExists(entityTypePK);
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public boolean deleteEntityType(String domainId, String entityTypeId) throws SharingRegistryException {
+ public boolean deleteEntityType(String domainId, String entityTypeId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isEntityTypeExists(domainId, entityTypeId)) throw new ResourceNotFoundException("Could not find entity type with id: " + entityTypeId);
EntityTypePK entityTypePK = new EntityTypePK();
entityTypePK.setDomainId(domainId);
entityTypePK.setEntityTypeId(entityTypeId);
(new EntityTypeRepository()).delete(entityTypePK);
return true;
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public EntityType getEntityType(String domainId, String entityTypeId) throws SharingRegistryException {
+ public EntityType getEntityType(String domainId, String entityTypeId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isEntityTypeExists(domainId, entityTypeId)) throw new ResourceNotFoundException("Could not find entity type with id: " + entityTypeId);
EntityTypePK entityTypePK = new EntityTypePK();
entityTypePK.setDomainId(domainId);
entityTypePK.setEntityTypeId(entityTypeId);
return (new EntityTypeRepository()).get(entityTypePK);
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public List<EntityType> getEntityTypes(String domain, int offset, int limit) throws SharingRegistryException {
+ public List<EntityType> getEntityTypes(String domain, int offset, int limit) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isDomainExists(domain)) throw new ResourceNotFoundException("Could not find domain with id: " + domain);
HashMap<String, String> filters = new HashMap<>();
filters.put(DBConstants.EntityTypeTable.DOMAIN_ID, domain);
return (new EntityTypeRepository()).select(filters, offset, limit);
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -702,22 +819,28 @@ public class SharingRegistryService {
permissionType.setUpdatedTime(System.currentTimeMillis());
(new PermissionTypeRepository()).create(permissionType);
return permissionType;
+ }catch (DuplicateEntryException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public boolean updatePermissionType(PermissionType permissionType) throws SharingRegistryException {
+ public boolean updatePermissionType(PermissionType permissionType) throws SharingRegistryException, ResourceNotFoundException{
try{
+ if(!isPermissionExists(permissionType.getDomainId(), permissionType.getPermissionTypeId())) throw new ResourceNotFoundException("Permission Type not found");
permissionType.setUpdatedTime(System.currentTimeMillis());
PermissionTypePK permissionTypePK = new PermissionTypePK();
permissionTypePK.setDomainId(permissionType.getDomainId());
permissionTypePK.setPermissionTypeId(permissionType.getPermissionTypeId());
PermissionType oldPermissionType = (new PermissionTypeRepository()).get(permissionTypePK);
permissionType = getUpdatedObject(oldPermissionType, permissionType);
- (new PermissionTypeRepository()).update(permissionType);
- return true;
+ PermissionType updatedPermissionType = (new PermissionTypeRepository()).update(permissionType);
+ if(updatedPermissionType != null && updatedPermissionType.getPermissionTypeId().equals(permissionType.getPermissionTypeId())) return true;
+ throw new SharingRegistryException("Could not update the permission type");
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -729,48 +852,60 @@ public class SharingRegistryService {
*
* @param permissionId
*/
- public boolean isPermissionExists(String domainId, String permissionId) throws SharingRegistryException {
+ public boolean isPermissionExists(String domainId, String permissionId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isPermissionExists(domainId, permissionId)) throw new ResourceNotFoundException("Permission Type not found");
PermissionTypePK permissionTypePK = new PermissionTypePK();
permissionTypePK.setDomainId(domainId);
permissionTypePK.setPermissionTypeId(permissionId);
return (new PermissionTypeRepository()).isExists(permissionTypePK);
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public boolean deletePermissionType(String domainId, String permissionTypeId) throws SharingRegistryException {
+ public boolean deletePermissionType(String domainId, String permissionTypeId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isPermissionExists(domainId, permissionTypeId)) throw new ResourceNotFoundException("Permission Type not found");
PermissionTypePK permissionTypePK = new PermissionTypePK();
permissionTypePK.setDomainId(domainId);
permissionTypePK.setPermissionTypeId(permissionTypeId);
(new PermissionTypeRepository()).delete(permissionTypePK);
return true;
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public PermissionType getPermissionType(String domainId, String permissionTypeId) throws SharingRegistryException {
+ public PermissionType getPermissionType(String domainId, String permissionTypeId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isPermissionExists(domainId, permissionTypeId)) throw new ResourceNotFoundException("Permission Type not found");
PermissionTypePK permissionTypePK = new PermissionTypePK();
permissionTypePK.setDomainId(domainId);
permissionTypePK.setPermissionTypeId(permissionTypeId);
return (new PermissionTypeRepository()).get(permissionTypePK);
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public List<PermissionType> getPermissionTypes(String domain, int offset, int limit) throws SharingRegistryException {
+ public List<PermissionType> getPermissionTypes(String domain, int offset, int limit) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isDomainExists(domain)) throw new ResourceNotFoundException("Could not find the domain");
HashMap<String, String> filters = new HashMap<>();
filters.put(DBConstants.PermissionTypeTable.DOMAIN_ID, domain);
return (new PermissionTypeRepository()).select(filters, offset, limit);
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -828,7 +963,9 @@ public class SharingRegistryService {
}
return createdEntity;
- }catch (Throwable ex) {
+ }catch(DuplicateEntryException ex) {
+ throw ex;
+ }catch(Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
@@ -853,8 +990,9 @@ public class SharingRegistryService {
}
}
- public boolean updateEntity(Entity entity) throws SharingRegistryException {
+ public boolean updateEntity(Entity entity) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isEntityExists(entity.getDomainId(), entity.getEntityId())) throw new ResourceNotFoundException("Could not find entity with entityId: " + entity.getEntityId());
//TODO Check for permission changes
entity.setUpdatedTime(System.currentTimeMillis());
EntityPK entityPK = new EntityPK();
@@ -877,8 +1015,11 @@ public class SharingRegistryService {
}
entity = getUpdatedObject(oldEntity, entity);
entity.setSharedCount((new SharingRepository()).getSharedCount(entity.getDomainId(), entity.getEntityId()));
- (new EntityRepository()).update(entity);
- return true;
+ Entity updatedEntity = (new EntityRepository()).update(entity);
+ if(updatedEntity != null && updatedEntity.getEntityId().equals(entity.getEntityId())) return true;
+ throw new SharingRegistryException("Could not update the entity");
+ }catch(ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -902,26 +1043,32 @@ public class SharingRegistryService {
}
}
- public boolean deleteEntity(String domainId, String entityId) throws SharingRegistryException {
+ public boolean deleteEntity(String domainId, String entityId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isEntityExists(domainId, entityId)) throw new ResourceNotFoundException("Could not find entity with entityId:" + entityId + " in domain:" + domainId);
//TODO Check for permission changes
EntityPK entityPK = new EntityPK();
entityPK.setDomainId(domainId);
entityPK.setEntityId(entityId);
(new EntityRepository()).delete(entityPK);
return true;
+ }catch(ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public Entity getEntity(String domainId, String entityId) throws SharingRegistryException {
+ public Entity getEntity(String domainId, String entityId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isEntityExists(domainId, entityId)) throw new ResourceNotFoundException("Could not find entity with entityId:" + entityId + " in domain:" + domainId);
EntityPK entityPK = new EntityPK();
entityPK.setDomainId(domainId);
entityPK.setEntityId(entityId);
return (new EntityRepository()).get(entityPK);
+ }catch(ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -929,21 +1076,27 @@ public class SharingRegistryService {
}
public List<Entity> searchEntities(String domainId, String userId, List<SearchCriteria> filters,
- int offset, int limit) throws SharingRegistryException {
+ int offset, int limit) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isUserExists(domainId, userId)) throw new ResourceNotFoundException("Could not find user:" + userId + " in domain:" + domainId);
List<String> groupIds = new ArrayList<>();
groupIds.add(userId);
(new GroupMembershipRepository()).getAllParentMembershipsForChild(domainId, userId).stream().forEach(gm -> groupIds.add(gm.getParentId()));
return (new EntityRepository()).searchEntities(domainId, groupIds, filters, offset, limit);
+ }catch(ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public List<User> getListOfSharedUsers(String domainId, String entityId, String permissionTypeId) throws SharingRegistryException {
+ public List<User> getListOfSharedUsers(String domainId, String entityId, String permissionTypeId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isEntityExists(domainId, entityId) || !isPermissionExists(domainId, permissionTypeId)) throw new ResourceNotFoundException("Could not find the resource");
return (new UserRepository()).getAccessibleUsers(domainId, entityId, permissionTypeId);
+ }catch(ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -951,18 +1104,24 @@ public class SharingRegistryService {
}
public List<User> getListOfDirectlySharedUsers(String domainId, String entityId, String permissionTypeId)
- throws SharingRegistryException {
+ throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isEntityExists(domainId, entityId) || !isPermissionExists(domainId, permissionTypeId)) throw new ResourceNotFoundException("Could not find the resource");
return (new UserRepository()).getDirectlyAccessibleUsers(domainId, entityId, permissionTypeId);
+ }catch(ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public List<UserGroup> getListOfSharedGroups(String domainId, String entityId, String permissionTypeId) throws SharingRegistryException {
+ public List<UserGroup> getListOfSharedGroups(String domainId, String entityId, String permissionTypeId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isEntityExists(domainId, entityId) || !isPermissionExists(domainId, permissionTypeId)) throw new ResourceNotFoundException("Could not find the resource");
return (new UserGroupRepository()).getAccessibleGroups(domainId, entityId, permissionTypeId);
+ }catch(ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -970,9 +1129,12 @@ public class SharingRegistryService {
}
public List<UserGroup> getListOfDirectlySharedGroups(String domainId, String entityId, String permissionTypeId)
- throws SharingRegistryException {
+ throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isEntityExists(domainId, entityId) || !isPermissionExists(domainId, permissionTypeId)) throw new ResourceNotFoundException("Could not find the resource");
return (new UserGroupRepository()).getDirectlyAccessibleGroups(domainId, entityId, permissionTypeId);
+ }catch(ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -988,28 +1150,45 @@ public class SharingRegistryService {
* @param cascadePermission
* @return
* @throws SharingRegistryException
- * @throws TException
+ * @throws ResourceNotFoundException
*/
- public boolean shareEntityWithUsers(String domainId, String entityId, List<String> userList, String permissionTypeId, boolean cascadePermission) throws SharingRegistryException {
+
+ public boolean shareEntityWithUsers(String domainId, String entityId, List<String> userList, String permissionTypeId, boolean cascadePermission) throws SharingRegistryException, ResourceNotFoundException {
try{
+ for(String user: userList){
+ if(!isUserExists(domainId, user)) throw new ResourceNotFoundException("Could not find the user");
+ }
+ if(!isDomainExists(domainId) || !isEntityExists(domainId, entityId) || !isPermissionExists(domainId, permissionTypeId)) throw new ResourceNotFoundException("Could not find the user");
return shareEntity(domainId, entityId, userList, permissionTypeId, cascadePermission);
+ }catch(ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public boolean shareEntityWithGroups(String domainId, String entityId, List<String> groupList, String permissionTypeId, boolean cascadePermission) throws SharingRegistryException {
+ public boolean shareEntityWithGroups(String domainId, String entityId, List<String> groupList, String permissionTypeId, boolean cascadePermission) throws SharingRegistryException, ResourceNotFoundException {
try{
+ for(String group: groupList){
+ if(!isGroupExists(domainId, group)) throw new ResourceNotFoundException("Could not find the group");
+ }
+ if(!isDomainExists(domainId) || !isEntityExists(domainId, entityId) || !isPermissionExists(domainId, permissionTypeId)) throw new ResourceNotFoundException("Could not find the user");
return shareEntity(domainId, entityId, groupList, permissionTypeId, cascadePermission);
+ }catch(ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- private boolean shareEntity(String domainId, String entityId, List<String> groupOrUserList, String permissionTypeId, boolean cascadePermission) throws SharingRegistryException {
+ private boolean shareEntity(String domainId, String entityId, List<String> groupOrUserList, String permissionTypeId, boolean cascadePermission) throws SharingRegistryException, ResourceNotFoundException {
try{
+ for(String groupOrUser: groupOrUserList){
+ if(!isUserExists(domainId, groupOrUser) && !isGroupExists(domainId, groupOrUser)) throw new ResourceNotFoundException("Could not find the user");
+ }
+ if(!isDomainExists(domainId) || !isEntityExists(domainId, entityId) || !isPermissionExists(domainId, permissionTypeId)) throw new ResourceNotFoundException("Could not find the user");
if(permissionTypeId.equals((new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId))){
throw new SharingRegistryException(OWNER_PERMISSION_NAME + " permission cannot be assigned or removed");
}
@@ -1067,31 +1246,45 @@ public class SharingRegistryService {
entity.setSharedCount((new SharingRepository()).getSharedCount(domainId, entityId));
(new EntityRepository()).update(entity);
return true;
+ }catch(ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public boolean revokeEntitySharingFromUsers(String domainId, String entityId, List<String> userList, String permissionTypeId) throws SharingRegistryException {
+ public boolean revokeEntitySharingFromUsers(String domainId, String entityId, List<String> userList, String permissionTypeId) throws SharingRegistryException, ResourceNotFoundException, InvalidRequestException {
try{
+ for(String user: userList){
+ if(!isUserExists(domainId, user)) throw new ResourceNotFoundException("Could not find the user");
+ }
+ if(!isDomainExists(domainId) || !isEntityExists(domainId, entityId) || !isPermissionExists(domainId, permissionTypeId)) throw new ResourceNotFoundException("Could not find the user");
if(permissionTypeId.equals((new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId))){
- throw new SharingRegistryException(OWNER_PERMISSION_NAME + " permission cannot be assigned or removed");
+ throw new InvalidRequestException(OWNER_PERMISSION_NAME + " permission cannot be assigned or removed");
}
return revokeEntitySharing(domainId, entityId, userList, permissionTypeId);
- }catch (Throwable ex) {
+ }catch (InvalidRequestException | ResourceNotFoundException ex) {
+ throw ex;
+ }catch(Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public boolean revokeEntitySharingFromGroups(String domainId, String entityId, List<String> groupList, String permissionTypeId) throws SharingRegistryException {
+ public boolean revokeEntitySharingFromGroups(String domainId, String entityId, List<String> groupList, String permissionTypeId) throws SharingRegistryException, ResourceNotFoundException, InvalidRequestException {
try{
+ for(String group: groupList){
+ if(!isGroupExists(domainId, group)) throw new ResourceNotFoundException("Could not find the group");
+ }
+ if(!isDomainExists(domainId) || !isEntityExists(domainId, entityId) || !isPermissionExists(domainId, permissionTypeId)) throw new ResourceNotFoundException("Could not find the user");
if(permissionTypeId.equals((new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId))){
- throw new SharingRegistryException(OWNER_PERMISSION_NAME + " permission cannot be assigned or removed");
+ throw new InvalidRequestException(OWNER_PERMISSION_NAME + " permission cannot be assigned or removed");
}
return revokeEntitySharing(domainId, entityId, groupList, permissionTypeId);
+ }catch (InvalidRequestException | ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
@@ -1099,8 +1292,9 @@ public class SharingRegistryService {
}
- public boolean userHasAccess(String domainId, String userId, String entityId, String permissionTypeId) throws SharingRegistryException {
+ public boolean userHasAccess(String domainId, String userId, String entityId, String permissionTypeId) throws SharingRegistryException, ResourceNotFoundException {
try{
+ if(!isDomainExists(domainId) || !isEntityExists(domainId, entityId) || !isPermissionExists(domainId, permissionTypeId) || !isUserExists(domainId, userId)) throw new ResourceNotFoundException("Could not find the resource");
//check whether the user has permission directly or indirectly
List<GroupMembership> parentMemberships = (new GroupMembershipRepository()).getAllParentMembershipsForChild(domainId, userId);
List<String> groupIds = new ArrayList<>();
@@ -1108,16 +1302,22 @@ public class SharingRegistryService {
groupIds.add(userId);
return (new SharingRepository()).hasAccess(domainId, entityId, groupIds, Arrays.asList(permissionTypeId,
(new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId)));
+ }catch (ResourceNotFoundException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
}
}
- public boolean revokeEntitySharing(String domainId, String entityId, List<String> groupOrUserList, String permissionTypeId) throws SharingRegistryException {
+ public boolean revokeEntitySharing(String domainId, String entityId, List<String> groupOrUserList, String permissionTypeId) throws SharingRegistryException, InvalidRequestException, ResourceNotFoundException {
try{
+ for(String groupOrUser: groupOrUserList){
+ if(!isUserExists(domainId, groupOrUser) && !isGroupExists(domainId, groupOrUser)) throw new ResourceNotFoundException("Could not find the user");
+ }
+ if(!isDomainExists(domainId) || !isEntityExists(domainId, entityId) || !isPermissionExists(domainId, permissionTypeId)) throw new ResourceNotFoundException("Could not find the user");
if(permissionTypeId.equals((new PermissionTypeRepository()).getOwnerPermissionTypeIdForDomain(domainId))){
- throw new SharingRegistryException(OWNER_PERMISSION_NAME + " permission cannot be removed");
+ throw new InvalidRequestException(OWNER_PERMISSION_NAME + " permission cannot be removed");
}
//revoking permission for the entity
@@ -1156,6 +1356,8 @@ public class SharingRegistryService {
entity.setSharedCount((new SharingRepository()).getSharedCount(domainId, entityId));
(new EntityRepository()).update(entity);
return true;
+ }catch (ResourceNotFoundException | InvalidRequestException ex) {
+ throw ex;
}catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
throw new SharingRegistryException(ex.getMessage() + " Stack trace:" + ExceptionUtils.getStackTrace(ex));
diff --git a/pom.xml b/pom.xml
index 1ba86f7..0169baf 100644
--- a/pom.xml
+++ b/pom.xml
@@ -52,6 +52,11 @@
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-actuator</artifactId>
+ <version>2.1.6.RELEASE</version>
+ </dependency>
</dependencies>
<build>
<plugins>