From c6086c7b8137550b81e24878c800dc232b66a133 Mon Sep 17 00:00:00 2001 From: VenuChoudhary001 Date: Fri, 1 Sep 2023 11:32:17 +0530 Subject: [PATCH] [+/-]refactored code --- .../controller/RegistryEntityController.java | 2069 ++++++++++------- .../registry/helper/RegistryHelper.java | 26 +- .../src/main/resources/application.yml | 4 +- 3 files changed, 1254 insertions(+), 845 deletions(-) diff --git a/java/registry/src/main/java/dev/sunbirdrc/registry/controller/RegistryEntityController.java b/java/registry/src/main/java/dev/sunbirdrc/registry/controller/RegistryEntityController.java index 7726948fb..0ebf9d70a 100644 --- a/java/registry/src/main/java/dev/sunbirdrc/registry/controller/RegistryEntityController.java +++ b/java/registry/src/main/java/dev/sunbirdrc/registry/controller/RegistryEntityController.java @@ -1,7 +1,11 @@ package dev.sunbirdrc.registry.controller; +import static dev.sunbirdrc.registry.Constants.*; +import static dev.sunbirdrc.registry.middleware.util.Constants.ENTITY_TYPE; + import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -20,12 +24,15 @@ import dev.sunbirdrc.registry.middleware.util.JSONUtil; import dev.sunbirdrc.registry.middleware.util.OSSystemFields; import dev.sunbirdrc.registry.service.FileStorageService; -import dev.sunbirdrc.registry.service.ICertificateService; +import dev.sunbirdrc.registry.service.ICertificateService; import dev.sunbirdrc.registry.transform.Configuration; import dev.sunbirdrc.registry.transform.Data; import dev.sunbirdrc.registry.transform.ITransformer; import dev.sunbirdrc.registry.util.ViewTemplateManager; import dev.sunbirdrc.validators.ValidationException; +import java.lang.reflect.InvocationTargetException; +import java.util.*; +import javax.servlet.http.HttpServletRequest; import org.agrona.Strings; import org.apache.commons.lang3.StringUtils; import org.apache.tinkerpop.gremlin.structure.Vertex; @@ -42,880 +49,1284 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import javax.servlet.http.HttpServletRequest; -import java.lang.reflect.InvocationTargetException; -import java.util.*; -import com.fasterxml.jackson.databind.ObjectMapper; - -import static dev.sunbirdrc.registry.Constants.*; -import static dev.sunbirdrc.registry.middleware.util.Constants.ENTITY_TYPE; @RestController public class RegistryEntityController extends AbstractController { - private static final String TRANSACTION_ID = "transactionId"; - private static Logger logger = LoggerFactory.getLogger(RegistryEntityController.class); - - @Autowired - private ICertificateService certificateService; - - @Autowired - private FileStorageService fileStorageService; - - @Autowired - private AsyncRequest asyncRequest; - - @Autowired - private ViewTemplateManager viewTemplateManager; - - @Value("${authentication.enabled:true}") - boolean securityEnabled; - @Value("${certificate.enableExternalTemplates:false}") - boolean externalTemplatesEnabled; - - @RequestMapping(value = "/api/v1/{entityName}/invite", method = RequestMethod.POST) - public ResponseEntity invite( - @PathVariable String entityName, - @RequestHeader HttpHeaders header, - @RequestBody JsonNode rootNode, - HttpServletRequest request + private static final String TRANSACTION_ID = "transactionId"; + private static Logger logger = LoggerFactory.getLogger( + RegistryEntityController.class + ); + + @Autowired + private ICertificateService certificateService; + + @Autowired + private FileStorageService fileStorageService; + + @Autowired + private AsyncRequest asyncRequest; + + @Autowired + private ViewTemplateManager viewTemplateManager; + + @Value("${authentication.enabled:true}") + boolean securityEnabled; + + @Value("${certificate.enableExternalTemplates:false}") + boolean externalTemplatesEnabled; + + @RequestMapping( + value = "/api/v1/{entityName}/invite", + method = RequestMethod.POST + ) + public ResponseEntity invite( + @PathVariable String entityName, + @RequestHeader HttpHeaders header, + @RequestBody JsonNode rootNode, + HttpServletRequest request + ) { + final String TAG = "RegistryController:invite"; + logger.info("Inviting entity {}", rootNode); + ResponseParams responseParams = new ResponseParams(); + Response response = new Response( + Response.API_ID.INVITE, + "OK", + responseParams + ); + Map result = new HashMap<>(); + ObjectNode newRootNode = objectMapper.createObjectNode(); + newRootNode.set(entityName, rootNode); + try { + checkEntityNameInDefinitionManager(entityName); + registryHelper.authorizeInviteEntity(request, entityName); + watch.start(TAG); + String entityId = registryHelper.inviteEntity(newRootNode, ""); + registryHelper.autoRaiseClaim( + entityName, + entityId, + "", + null, + newRootNode, + dev.sunbirdrc.registry.Constants.USER_ANONYMOUS + ); + Map resultMap = new HashMap(); + resultMap.put(dbConnectionInfoMgr.getUuidPropertyName(), entityId); + result.put(entityName, resultMap); + response.setResult(result); + responseParams.setStatus(Response.Status.SUCCESSFUL); + watch.start(TAG); + return new ResponseEntity<>(response, HttpStatus.OK); + } catch (RecordNotFoundException e) { + createSchemaNotFoundResponse(e.getMessage(), responseParams); + response = new Response(Response.API_ID.INVITE, "ERROR", responseParams); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } catch ( + MiddlewareHaltException | ValidationException | OwnerCreationException e ) { - final String TAG = "RegistryController:invite"; - logger.info("Inviting entity {}", rootNode); - ResponseParams responseParams = new ResponseParams(); - Response response = new Response(Response.API_ID.INVITE, "OK", responseParams); - Map result = new HashMap<>(); - ObjectNode newRootNode = objectMapper.createObjectNode(); - newRootNode.set(entityName, rootNode); - try { - checkEntityNameInDefinitionManager(entityName); - registryHelper.authorizeInviteEntity(request, entityName); - watch.start(TAG); - String entityId = registryHelper.inviteEntity(newRootNode, ""); - registryHelper.autoRaiseClaim(entityName, entityId, "", null, newRootNode, dev.sunbirdrc.registry.Constants.USER_ANONYMOUS); - Map resultMap = new HashMap(); - resultMap.put(dbConnectionInfoMgr.getUuidPropertyName(), entityId); - result.put(entityName, resultMap); - response.setResult(result); - responseParams.setStatus(Response.Status.SUCCESSFUL); - watch.start(TAG); - return new ResponseEntity<>(response, HttpStatus.OK); - } catch (RecordNotFoundException e) { - createSchemaNotFoundResponse(e.getMessage(), responseParams); - response = new Response(Response.API_ID.INVITE, "ERROR", responseParams); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } catch (MiddlewareHaltException | ValidationException | OwnerCreationException e) { - return badRequestException(responseParams, response, e.getMessage()); - } catch (UnAuthorizedException unAuthorizedException) { - return createUnauthorizedExceptionResponse(unAuthorizedException); - } catch (Exception e) { - if (e.getCause() != null && e.getCause().getCause() != null && - e.getCause().getCause() instanceof InvocationTargetException) { - Throwable targetException = ((InvocationTargetException) (e.getCause().getCause())).getTargetException(); - if (targetException instanceof OwnerCreationException) { - return badRequestException(responseParams, response, targetException.getMessage()); - } - } - return internalErrorResponse(responseParams, response, e); + return badRequestException(responseParams, response, e.getMessage()); + } catch (UnAuthorizedException unAuthorizedException) { + return createUnauthorizedExceptionResponse(unAuthorizedException); + } catch (Exception e) { + if ( + e.getCause() != null && + e.getCause().getCause() != null && + e.getCause().getCause() instanceof InvocationTargetException + ) { + Throwable targetException = + ( + (InvocationTargetException) (e.getCause().getCause()) + ).getTargetException(); + if (targetException instanceof OwnerCreationException) { + return badRequestException( + responseParams, + response, + targetException.getMessage() + ); } + } + return internalErrorResponse(responseParams, response, e); } - - @NotNull - private void createSchemaNotFoundResponse(String errorMessage, ResponseParams responseParams) { + } + + @NotNull + private void createSchemaNotFoundResponse( + String errorMessage, + ResponseParams responseParams + ) { + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + responseParams.setErrmsg(errorMessage); + } + + private void checkEntityNameInDefinitionManager(String entityName) + throws RecordNotFoundException { + if (definitionsManager.getDefinition(entityName) == null) { + String errorMessage = String.format( + ErrorMessages.NOT_PART_OF_THE_SYSTEM_EXCEPTION, + entityName + ); + throw new RecordNotFoundException(errorMessage); + } + } + + @RequestMapping( + value = "/api/v1/{entityName}/{entityId}", + method = RequestMethod.DELETE + ) + public ResponseEntity deleteEntity( + @PathVariable String entityName, + @PathVariable String entityId, + @RequestHeader HttpHeaders header, + HttpServletRequest request + ) { + String userId = USER_ANONYMOUS; + logger.info("Deleting entityType {} with Id {}", entityName, entityId); + if (registryHelper.doesEntityOperationRequireAuthorization(entityName)) { + try { + userId = registryHelper.authorize(entityName, entityId, request); + } catch (Exception e) { + return createUnauthorizedExceptionResponse(e); + } + } + ResponseParams responseParams = new ResponseParams(); + Response response = new Response( + Response.API_ID.DELETE, + "OK", + responseParams + ); + try { + checkEntityNameInDefinitionManager(entityName); + String tag = "RegistryController.delete " + entityName; + watch.start(tag); + Vertex deletedEntity = registryHelper.deleteEntity( + entityName, + entityId, + userId + ); + if ( + deletedEntity != null && + deletedEntity.keys().contains(OSSystemFields._osSignedData.name()) + ) { + registryHelper.revokeExistingCredentials( + entityName, + entityId, + userId, + deletedEntity.value(OSSystemFields._osSignedData.name()) + ); + } + responseParams.setErrmsg(""); + responseParams.setStatus(Response.Status.SUCCESSFUL); + watch.stop(tag); + return new ResponseEntity<>(response, HttpStatus.OK); + } catch (RecordNotFoundException e) { + createSchemaNotFoundResponse(e.getMessage(), responseParams); + response = new Response(Response.API_ID.DELETE, "ERROR", responseParams); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } catch (Exception e) { + logger.error("RegistryController: Exception while Deleting entity", e); + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + responseParams.setErrmsg(e.getMessage()); + return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @RequestMapping( + value = "/api/v1/{entityName}/search", + method = RequestMethod.POST + ) + public ResponseEntity searchEntity( + @PathVariable String entityName, + @RequestHeader HttpHeaders header, + @RequestBody ObjectNode searchNode + ) { + ResponseParams responseParams = new ResponseParams(); + Response response = new Response( + Response.API_ID.SEARCH, + "OK", + responseParams + ); + + try { + watch.start("RegistryController.searchEntity"); + ArrayNode entity = JsonNodeFactory.instance.arrayNode(); + entity.add(entityName); + searchNode.set(ENTITY_TYPE, entity); + checkEntityNameInDefinitionManager(entityName); + if ( + definitionsManager + .getDefinition(entityName) + .getOsSchemaConfiguration() + .getEnableSearch() + ) { + JsonNode result = registryHelper.searchEntity(searchNode); + watch.stop("RegistryController.searchEntity"); + return new ResponseEntity<>(result.get(entityName), HttpStatus.OK); + } else { + watch.stop("RegistryController.searchEntity"); + logger.error("Searching on entity {} not allowed", entityName); + response.setResult(""); responseParams.setStatus(Response.Status.UNSUCCESSFUL); - responseParams.setErrmsg(errorMessage); + responseParams.setErrmsg( + String.format("Searching on entity %s not allowed", entityName) + ); + } + } catch (RecordNotFoundException e) { + createSchemaNotFoundResponse(e.getMessage(), responseParams); + response = new Response(Response.API_ID.SEARCH, "ERROR", responseParams); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } catch (Exception e) { + logger.error("Exception in controller while searching entities !", e); + response.setResult(""); + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + responseParams.setErrmsg(e.getMessage()); } - - private void checkEntityNameInDefinitionManager(String entityName) throws RecordNotFoundException { - if (definitionsManager.getDefinition(entityName) == null) { - String errorMessage = String.format(ErrorMessages.NOT_PART_OF_THE_SYSTEM_EXCEPTION, entityName); - throw new RecordNotFoundException(errorMessage); - } + return new ResponseEntity<>(response, HttpStatus.OK); + } + + @RequestMapping( + value = "/api/v1/{entityName}/{entityId}", + method = RequestMethod.PUT + ) + public ResponseEntity putEntity( + @PathVariable String entityName, + @PathVariable String entityId, + @RequestBody JsonNode rootNode, + HttpServletRequest request + ) { + logger.info("Updating entityType {} request body {}", entityName, rootNode); + String userId = USER_ANONYMOUS; + if (registryHelper.doesEntityOperationRequireAuthorization(entityName)) { + try { + userId = registryHelper.authorize(entityName, entityId, request); + } catch (Exception e) { + return createUnauthorizedExceptionResponse(e); + } } - - @RequestMapping(value = "/api/v1/{entityName}/{entityId}", method = RequestMethod.DELETE) - public ResponseEntity deleteEntity( - @PathVariable String entityName, - @PathVariable String entityId, - @RequestHeader HttpHeaders header, - HttpServletRequest request - ) { - - String userId = USER_ANONYMOUS; - logger.info("Deleting entityType {} with Id {}", entityName, entityId); - if (registryHelper.doesEntityOperationRequireAuthorization(entityName)) { - try { - - userId = registryHelper.authorize(entityName, entityId, request); - } catch (Exception e) { - return createUnauthorizedExceptionResponse(e); - } - } - ResponseParams responseParams = new ResponseParams(); - Response response = new Response(Response.API_ID.DELETE, "OK", responseParams); - try { - checkEntityNameInDefinitionManager(entityName); - String tag = "RegistryController.delete " + entityName; - watch.start(tag); - Vertex deletedEntity = registryHelper.deleteEntity(entityName, entityId, userId); - if (deletedEntity != null && deletedEntity.keys().contains(OSSystemFields._osSignedData.name())) { - registryHelper.revokeExistingCredentials(entityName, entityId, userId, deletedEntity.value(OSSystemFields._osSignedData.name())); - } - responseParams.setErrmsg(""); - responseParams.setStatus(Response.Status.SUCCESSFUL); - watch.stop(tag); - return new ResponseEntity<>(response, HttpStatus.OK); - - } catch (RecordNotFoundException e) { - createSchemaNotFoundResponse(e.getMessage(), responseParams); - response = new Response(Response.API_ID.DELETE, "ERROR", responseParams); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } catch (Exception e) { - logger.error("RegistryController: Exception while Deleting entity", e); - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - responseParams.setErrmsg(e.getMessage()); - return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); - - } + ResponseParams responseParams = new ResponseParams(); + Response response = new Response( + Response.API_ID.UPDATE, + "OK", + responseParams + ); + ((ObjectNode) rootNode).put(uuidPropertyName, entityId); + ObjectNode newRootNode = objectMapper.createObjectNode(); + newRootNode.set(entityName, rootNode); + + try { + checkEntityNameInDefinitionManager(entityName); + String tag = "RegistryController.update " + entityName; + watch.start(tag); + JsonNode existingNode = registryHelper.readEntity(newRootNode, userId); + String emailId = registryHelper.fetchEmailIdFromToken( + request, + entityName + ); + registryHelper.updateEntityAndState(existingNode, newRootNode, userId); + if ( + existingNode.get(entityName).has(OSSystemFields._osSignedData.name()) + ) { + registryHelper.revokeExistingCredentials( + entityName, + entityId, + userId, + existingNode + .get(entityName) + .get(OSSystemFields._osSignedData.name()) + .asText("") + ); + } + registryHelper.invalidateAttestation(entityName, entityId, userId, null); + registryHelper.autoRaiseClaim( + entityName, + entityId, + userId, + existingNode, + newRootNode, + emailId + ); + responseParams.setErrmsg(""); + responseParams.setStatus(Response.Status.SUCCESSFUL); + watch.stop(tag); + + return new ResponseEntity<>(response, HttpStatus.OK); + } catch (RecordNotFoundException e) { + createSchemaNotFoundResponse(e.getMessage(), responseParams); + response = new Response(Response.API_ID.PUT, "ERROR", responseParams); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } catch (Exception e) { + logger.error( + "RegistryController: Exception while updating entity (without id)!", + e + ); + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + responseParams.setErrmsg(e.getMessage()); + return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); } + } + + @RequestMapping(value = "/api/v1/{entityName}", method = RequestMethod.POST) + public ResponseEntity postEntity( + @PathVariable String entityName, + @RequestHeader HttpHeaders header, + @RequestBody JsonNode rootNode, + @RequestParam(defaultValue = "sync") String mode, + @RequestParam(defaultValue = "${webhook.url}") String callbackUrl, + HttpServletRequest request + ) throws Exception { + logger.info("MODE: {}", asyncRequest.isEnabled()); + logger.info("MODE: {}", asyncRequest.getWebhookUrl()); + logger.info("Adding entity {}", rootNode); + ResponseParams responseParams = new ResponseParams(); + Response response = new Response( + Response.API_ID.POST, + "OK", + responseParams + ); + ObjectMapper objectMapper = new ObjectMapper(); + Map result = new HashMap<>(); + ObjectNode newRootNode = objectMapper.createObjectNode(); + // + + if ("Schema".equals(entityName)) { + logger.info("FOUND SCHEMA || ADDING TO CHAIN"); + /** Anchoring schema to chain */ + JsonNode np = rootNode.get("schema"); + JsonNode str = objectMapper.readTree(np.asText()); + + JsonNode schemaNode = str.get("definitions"); + JsonNode props = schemaNode.get(rootNode.get("name").asText()); + JsonNode newProps = props.get("properties"); + + JsonNode outputSchema = objectMapper.createObjectNode() + .put("title", str.get("title").asText()) + .put("description", str.get("description").asText()) + .set("properties", props.get("properties")); + JsonNode finalSchema = objectMapper + .createObjectNode() + .set("schema", outputSchema); + JsonNode schemaId = registryHelper.anchorSchemaAPI(finalSchema); - @RequestMapping(value = "/api/v1/{entityName}/search", method = RequestMethod.POST) - public ResponseEntity searchEntity(@PathVariable String entityName, @RequestHeader HttpHeaders header, @RequestBody ObjectNode searchNode) { - - ResponseParams responseParams = new ResponseParams(); - Response response = new Response(Response.API_ID.SEARCH, "OK", responseParams); + + /**Binding schema id schema */ + ((ObjectNode)rootNode).set("cord_schema_id", schemaId.get("schemaId")); - try { - watch.start("RegistryController.searchEntity"); - ArrayNode entity = JsonNodeFactory.instance.arrayNode(); - entity.add(entityName); - searchNode.set(ENTITY_TYPE, entity); - checkEntityNameInDefinitionManager(entityName); - if (definitionsManager.getDefinition(entityName).getOsSchemaConfiguration().getEnableSearch()) { - JsonNode result = registryHelper.searchEntity(searchNode); - watch.stop("RegistryController.searchEntity"); - return new ResponseEntity<>(result.get(entityName), HttpStatus.OK); - } else { - watch.stop("RegistryController.searchEntity"); - logger.error("Searching on entity {} not allowed", entityName); - response.setResult(""); - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - responseParams.setErrmsg(String.format("Searching on entity %s not allowed", entityName)); - } - } catch (RecordNotFoundException e) { - createSchemaNotFoundResponse(e.getMessage(), responseParams); - response = new Response(Response.API_ID.SEARCH, "ERROR", responseParams); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } catch (Exception e) { - logger.error("Exception in controller while searching entities !", e); - response.setResult(""); - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - responseParams.setErrmsg(e.getMessage()); - } - return new ResponseEntity<>(response, HttpStatus.OK); } - - @RequestMapping(value = "/api/v1/{entityName}/{entityId}", method = RequestMethod.PUT) - public ResponseEntity putEntity( - @PathVariable String entityName, - @PathVariable String entityId, - @RequestBody JsonNode rootNode, - HttpServletRequest request) { - - logger.info("Updating entityType {} request body {}", entityName, rootNode); - String userId = USER_ANONYMOUS; - if (registryHelper.doesEntityOperationRequireAuthorization(entityName)) { - try { - - userId = registryHelper.authorize(entityName, entityId, request); - } catch (Exception e) { - return createUnauthorizedExceptionResponse(e); - } - } - ResponseParams responseParams = new ResponseParams(); - Response response = new Response(Response.API_ID.UPDATE, "OK", responseParams); - ((ObjectNode) rootNode).put(uuidPropertyName, entityId); - ObjectNode newRootNode = objectMapper.createObjectNode(); - newRootNode.set(entityName, rootNode); - - try { - checkEntityNameInDefinitionManager(entityName); - String tag = "RegistryController.update " + entityName; - watch.start(tag); - JsonNode existingNode = registryHelper.readEntity(newRootNode, userId); - String emailId = registryHelper.fetchEmailIdFromToken(request, entityName); - registryHelper.updateEntityAndState(existingNode, newRootNode, userId); - if (existingNode.get(entityName).has(OSSystemFields._osSignedData.name())) { - registryHelper.revokeExistingCredentials(entityName, entityId, userId, - existingNode.get(entityName).get(OSSystemFields._osSignedData.name()).asText("")); - } - registryHelper.invalidateAttestation(entityName, entityId, userId, null); - registryHelper.autoRaiseClaim(entityName, entityId, userId, existingNode, newRootNode, emailId); - responseParams.setErrmsg(""); - responseParams.setStatus(Response.Status.SUCCESSFUL); - watch.stop(tag); - - return new ResponseEntity<>(response, HttpStatus.OK); - - } catch (RecordNotFoundException e) { - createSchemaNotFoundResponse(e.getMessage(), responseParams); - response = new Response(Response.API_ID.PUT, "ERROR", responseParams); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } catch (Exception e) { - logger.error("RegistryController: Exception while updating entity (without id)!", e); - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - responseParams.setErrmsg(e.getMessage()); - return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); - } + newRootNode.set(entityName, rootNode); + logger.info("NEW NODE : {}",newRootNode); + + try { + checkEntityNameInDefinitionManager(entityName); + String userId = registryHelper.authorizeManageEntity(request, entityName); + String label = registryHelper.addEntity(newRootNode, userId); + String emailId = registryHelper.fetchEmailIdFromToken( + request, + entityName + ); + Map resultMap = new HashMap<>(); + if (asyncRequest.isEnabled()) { + resultMap.put(TRANSACTION_ID, label); + } else { + registryHelper.autoRaiseClaim( + entityName, + label, + userId, + null, + newRootNode, + emailId + ); + resultMap.put(dbConnectionInfoMgr.getUuidPropertyName(), label); + } + + result.put(entityName, resultMap); + response.setResult(result); + responseParams.setStatus(Response.Status.SUCCESSFUL); + watch.stop("RegistryController.addToExistingEntity"); + + return new ResponseEntity<>(response, HttpStatus.OK); + } catch (RecordNotFoundException e) { + createSchemaNotFoundResponse(e.getMessage(), responseParams); + response = new Response(Response.API_ID.POST, "ERROR", responseParams); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } catch (MiddlewareHaltException e) { + logger.info("Error in validating the request"); + return badRequestException(responseParams, response, e.getMessage()); + } catch (Exception e) { + logger.error("Exception in controller while adding entity !", e); + response.setResult(result); + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + responseParams.setErrmsg(e.getMessage()); + return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); } - - - @RequestMapping(value = "/api/v1/{entityName}", method = RequestMethod.POST) - public ResponseEntity postEntity( - @PathVariable String entityName, - @RequestHeader HttpHeaders header, - @RequestBody JsonNode rootNode, - @RequestParam(defaultValue = "sync") String mode, - @RequestParam(defaultValue = "${webhook.url}") String callbackUrl, - HttpServletRequest request - ) throws Exception { - logger.info("MODE: {}", asyncRequest.isEnabled()); - logger.info("MODE: {}", asyncRequest.getWebhookUrl()); - logger.info("Adding entity {}", rootNode); - ResponseParams responseParams = new ResponseParams(); - Response response = new Response(Response.API_ID.POST, "OK", responseParams); - - Map result = new HashMap<>(); - ObjectNode newRootNode = objectMapper.createObjectNode(); - newRootNode.set(entityName, rootNode); - try { - checkEntityNameInDefinitionManager(entityName); - String userId = registryHelper.authorizeManageEntity(request, entityName); - String label = registryHelper.addEntity(newRootNode, userId); - String emailId = registryHelper.fetchEmailIdFromToken(request, entityName); - - Map resultMap = new HashMap<>(); - if (asyncRequest.isEnabled()) { - resultMap.put(TRANSACTION_ID, label); - } else { - registryHelper.autoRaiseClaim(entityName, label, userId, null, newRootNode, emailId); - resultMap.put(dbConnectionInfoMgr.getUuidPropertyName(), label); - } - /** Anchoring schema to chain */ - JsonNode np=rootNode.get("schema"); - JsonNode str=new ObjectMapper().readTree(np.asText()); - JsonNode schemaNode=str.get("definitions"); - - JsonNode props=schemaNode.get("Place"); - JsonNode newProps=props.get("properties"); - - JsonNode outputSchema=new ObjectMapper().createObjectNode() - .put("title",str.get("title").asText()) - .put("description",str.get("description").asText()) - .set("properties",props.get("properties")); - - JsonNode finalSchema=new ObjectMapper().createObjectNode() - .set("schema",outputSchema); - registryHelper.anchorSchemaAPI(finalSchema); - - - result.put(entityName, resultMap); - response.setResult(result); - responseParams.setStatus(Response.Status.SUCCESSFUL); - watch.stop("RegistryController.addToExistingEntity"); - - return new ResponseEntity<>(response, HttpStatus.OK); - } catch (RecordNotFoundException e) { - createSchemaNotFoundResponse(e.getMessage(), responseParams); - response = new Response(Response.API_ID.POST, "ERROR", responseParams); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } catch (MiddlewareHaltException e) { - logger.info("Error in validating the request"); - return badRequestException(responseParams, response, e.getMessage()); - } catch (Exception e) { - logger.error("Exception in controller while adding entity !", e); - response.setResult(result); - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - responseParams.setErrmsg(e.getMessage()); - return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); - } + } + + @RequestMapping( + value = "/api/v1/{entityName}/{entityId}/**", + method = RequestMethod.PUT + ) + public ResponseEntity updatePropertyOfTheEntity( + HttpServletRequest request, + @PathVariable String entityName, + @PathVariable String entityId, + @RequestBody JsonNode requestBody + ) { + String userId = USER_ANONYMOUS; + if (registryHelper.doesEntityOperationRequireAuthorization(entityName)) { + try { + userId = registryHelper.authorize(entityName, entityId, request); + } catch (Exception e) { + return createUnauthorizedExceptionResponse(e); + } } - - - @RequestMapping(value = "/api/v1/{entityName}/{entityId}/**", method = RequestMethod.PUT) - public ResponseEntity updatePropertyOfTheEntity( - HttpServletRequest request, - @PathVariable String entityName, - @PathVariable String entityId, - @RequestBody JsonNode requestBody - - ) { - String userId = USER_ANONYMOUS; - if (registryHelper.doesEntityOperationRequireAuthorization(entityName)) { - try { - userId = registryHelper.authorize(entityName, entityId, request); - } catch (Exception e) { - return createUnauthorizedExceptionResponse(e); - } - } - ResponseParams responseParams = new ResponseParams(); - Response response = new Response(Response.API_ID.UPDATE, "OK", responseParams); - - try { - checkEntityNameInDefinitionManager(entityName); - String tag = "RegistryController.update " + entityName; - watch.start(tag); - requestBody = registryHelper.removeFormatAttr(requestBody); - JsonNode existingNode = registryHelper.readEntity(userId, entityName, entityId, false, null, false); - registryHelper.updateEntityProperty(entityName, entityId, requestBody, request, existingNode); - if (existingNode.get(entityName).has(OSSystemFields._osSignedData.name())) { - registryHelper.revokeExistingCredentials(entityName, entityId, userId, - existingNode.get(entityName).get(OSSystemFields._osSignedData.name()).asText("")); - } - responseParams.setErrmsg(""); - responseParams.setStatus(Response.Status.SUCCESSFUL); - registryHelper.invalidateAttestation(entityName, entityId, userId, registryHelper.getPropertyToUpdate(request, entityId)); - watch.stop(tag); - return new ResponseEntity<>(response, HttpStatus.OK); - } catch (RecordNotFoundException e) { - createSchemaNotFoundResponse(e.getMessage(), responseParams); - response = new Response(Response.API_ID.PUT, "ERROR", responseParams); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } catch (Exception e) { - responseParams.setErrmsg(e.getMessage()); - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } + ResponseParams responseParams = new ResponseParams(); + Response response = new Response( + Response.API_ID.UPDATE, + "OK", + responseParams + ); + + try { + checkEntityNameInDefinitionManager(entityName); + String tag = "RegistryController.update " + entityName; + watch.start(tag); + requestBody = registryHelper.removeFormatAttr(requestBody); + JsonNode existingNode = registryHelper.readEntity( + userId, + entityName, + entityId, + false, + null, + false + ); + registryHelper.updateEntityProperty( + entityName, + entityId, + requestBody, + request, + existingNode + ); + if ( + existingNode.get(entityName).has(OSSystemFields._osSignedData.name()) + ) { + registryHelper.revokeExistingCredentials( + entityName, + entityId, + userId, + existingNode + .get(entityName) + .get(OSSystemFields._osSignedData.name()) + .asText("") + ); + } + responseParams.setErrmsg(""); + responseParams.setStatus(Response.Status.SUCCESSFUL); + registryHelper.invalidateAttestation( + entityName, + entityId, + userId, + registryHelper.getPropertyToUpdate(request, entityId) + ); + watch.stop(tag); + return new ResponseEntity<>(response, HttpStatus.OK); + } catch (RecordNotFoundException e) { + createSchemaNotFoundResponse(e.getMessage(), responseParams); + response = new Response(Response.API_ID.PUT, "ERROR", responseParams); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } catch (Exception e) { + responseParams.setErrmsg(e.getMessage()); + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); } - - @RequestMapping(value = "/api/v1/{entityName}/{entityId}/**", method = RequestMethod.POST) - public ResponseEntity addNewPropertyToTheEntity( - HttpServletRequest request, - @PathVariable String entityName, - @PathVariable String entityId, - @RequestHeader HttpHeaders header, - @RequestBody JsonNode requestBody - ) { - ResponseParams responseParams = new ResponseParams(); - Response response = new Response(Response.API_ID.UPDATE, "OK", responseParams); - try { - checkEntityNameInDefinitionManager(entityName); - registryHelper.authorize(entityName, entityId, request); - } catch (RecordNotFoundException e) { - createSchemaNotFoundResponse(e.getMessage(), responseParams); - response = new Response(Response.API_ID.POST, "ERROR", responseParams); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } catch (Exception e) { - return createUnauthorizedExceptionResponse(e); - } - - try { - - String tag = "RegistryController.addNewPropertyToTheEntity " + entityName; - watch.start(tag); - String notes = getNotes(requestBody); - requestBody = registryHelper.removeFormatAttr(requestBody); - registryHelper.addEntityProperty(entityName, entityId, requestBody, request); - responseParams.setErrmsg(""); - responseParams.setStatus(Response.Status.SUCCESSFUL); - watch.stop(tag); - return new ResponseEntity<>(response, HttpStatus.OK); - } catch (Exception e) { - responseParams.setErrmsg(e.getMessage()); - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } + } + + @RequestMapping( + value = "/api/v1/{entityName}/{entityId}/**", + method = RequestMethod.POST + ) + public ResponseEntity addNewPropertyToTheEntity( + HttpServletRequest request, + @PathVariable String entityName, + @PathVariable String entityId, + @RequestHeader HttpHeaders header, + @RequestBody JsonNode requestBody + ) { + ResponseParams responseParams = new ResponseParams(); + Response response = new Response( + Response.API_ID.UPDATE, + "OK", + responseParams + ); + try { + checkEntityNameInDefinitionManager(entityName); + registryHelper.authorize(entityName, entityId, request); + } catch (RecordNotFoundException e) { + createSchemaNotFoundResponse(e.getMessage(), responseParams); + response = new Response(Response.API_ID.POST, "ERROR", responseParams); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } catch (Exception e) { + return createUnauthorizedExceptionResponse(e); } - private String getNotes(JsonNode requestBody) { - String notes = ""; - if (requestBody.has("notes")) { - notes = requestBody.get("notes").asText(); - JSONUtil.removeNodes(requestBody, Collections.singletonList("notes")); - } - return notes; - } - - private JsonNode getAttestationSignedData(String attestationId, JsonNode node) throws AttestationNotFoundException, JsonProcessingException { - JsonNode attestationNode = getAttestationNode(attestationId, node); - if (attestationNode.get(OSSystemFields._osAttestedData.name()) == null) - throw new AttestationNotFoundException(); - attestationNode = objectMapper.readTree(attestationNode.get(OSSystemFields._osAttestedData.name()).asText()); - return attestationNode; - } - - @Nullable - private JsonNode getAttestationNode(String attestationId, JsonNode node) { - Iterator iterator = node.iterator(); - JsonNode attestationNode = null; - while (iterator.hasNext()) { - attestationNode = iterator.next(); - if (attestationNode.get(uuidPropertyName).toString().equals(attestationId)) { - break; - } - } - return attestationNode; + try { + String tag = "RegistryController.addNewPropertyToTheEntity " + entityName; + watch.start(tag); + String notes = getNotes(requestBody); + requestBody = registryHelper.removeFormatAttr(requestBody); + registryHelper.addEntityProperty( + entityName, + entityId, + requestBody, + request + ); + responseParams.setErrmsg(""); + responseParams.setStatus(Response.Status.SUCCESSFUL); + watch.stop(tag); + return new ResponseEntity<>(response, HttpStatus.OK); + } catch (Exception e) { + responseParams.setErrmsg(e.getMessage()); + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); } + } - @RequestMapping(value = "/partner/api/v1/{entityName}", method = RequestMethod.GET) - public ResponseEntity getEntityWithConsent( - @PathVariable String entityName, - HttpServletRequest request) { - ResponseParams responseParams = new ResponseParams(); - try { - checkEntityNameInDefinitionManager(entityName); - ArrayList fields = getConsentFields(request); - JsonNode userInfoFromRegistry = registryHelper.getRequestedUserDetails(request, entityName); - JsonNode jsonNode = userInfoFromRegistry.get(entityName); - if (jsonNode instanceof ArrayNode) { - ArrayNode values = (ArrayNode) jsonNode; - if (values.size() > 0) { - JsonNode node = values.get(0); - if (node instanceof ObjectNode) { - ObjectNode entityNode = copyWhiteListedFields(fields, node); - return new ResponseEntity<>(entityNode, HttpStatus.OK); - } - } - } - return new ResponseEntity<>(HttpStatus.NOT_FOUND); - } catch (RecordNotFoundException e) { - createSchemaNotFoundResponse(e.getMessage(), responseParams); - Response response = new Response(Response.API_ID.GET, "ERROR", responseParams); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } catch (Exception e) { - logger.error("Error in partner api access", e); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); - } + private String getNotes(JsonNode requestBody) { + String notes = ""; + if (requestBody.has("notes")) { + notes = requestBody.get("notes").asText(); + JSONUtil.removeNodes(requestBody, Collections.singletonList("notes")); } - - private ObjectNode copyWhiteListedFields(ArrayList fields, JsonNode dataNode) { - ObjectNode node = JsonNodeFactory.instance.objectNode(); - for (String key : fields) { - node.set(key, dataNode.get(key)); - } - return node; + return notes; + } + + private JsonNode getAttestationSignedData( + String attestationId, + JsonNode node + ) throws AttestationNotFoundException, JsonProcessingException { + JsonNode attestationNode = getAttestationNode(attestationId, node); + if ( + attestationNode.get(OSSystemFields._osAttestedData.name()) == null + ) throw new AttestationNotFoundException(); + attestationNode = + objectMapper.readTree( + attestationNode.get(OSSystemFields._osAttestedData.name()).asText() + ); + return attestationNode; + } + + @Nullable + private JsonNode getAttestationNode(String attestationId, JsonNode node) { + Iterator iterator = node.iterator(); + JsonNode attestationNode = null; + while (iterator.hasNext()) { + attestationNode = iterator.next(); + if ( + attestationNode.get(uuidPropertyName).toString().equals(attestationId) + ) { + break; + } } - - private ArrayList getConsentFields(HttpServletRequest request) { - ArrayList fields = new ArrayList<>(); - KeycloakAuthenticationToken principal = (KeycloakAuthenticationToken) request.getUserPrincipal(); - try { - Map otherClaims = ((KeycloakPrincipal) principal.getPrincipal()).getKeycloakSecurityContext().getToken().getOtherClaims(); - if (otherClaims.keySet().contains(dev.sunbirdrc.registry.Constants.KEY_CONSENT) && otherClaims.get(dev.sunbirdrc.registry.Constants.KEY_CONSENT) instanceof Map) { - Map consentFields = (Map) otherClaims.get(dev.sunbirdrc.registry.Constants.KEY_CONSENT); - for (Object key : consentFields.keySet()) { - fields.add(key.toString()); - } - } - } catch (Exception ex) { - logger.error("Error while extracting other claims", ex); - } - return fields; - } - - @RequestMapping(value = "/api/v1/{entityName}/{entityId}", method = RequestMethod.GET, produces = - {MediaType.APPLICATION_PDF_VALUE, MediaType.TEXT_HTML_VALUE, Constants.SVG_MEDIA_TYPE}) - public ResponseEntity getEntityType(@PathVariable String entityName, - @PathVariable String entityId, - HttpServletRequest request, - @RequestHeader(required = false) String viewTemplateId) { - ResponseParams responseParams = new ResponseParams(); - Response response ; - if (registryHelper.doesEntityOperationRequireAuthorization(entityName) && securityEnabled) { - try { - - registryHelper.authorize(entityName, entityId, request); - } catch (Exception e) { - try { - checkEntityNameInDefinitionManager(entityName); - registryHelper.authorizeAttestor(entityName, request); - } catch (RecordNotFoundException re) { - createSchemaNotFoundResponse(re.getMessage(), responseParams); - response = new Response(Response.API_ID.GET, "ERROR", responseParams); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } catch (Exception exceptionFromAuthorizeAttestor) { - return new ResponseEntity<>(HttpStatus.FORBIDDEN); - } - } - } - try { - String readerUserId = getUserId(entityName, request); - JsonNode node = registryHelper.readEntity(readerUserId, entityName, entityId, false, - viewTemplateManager.getViewTemplateById(viewTemplateId), false) - .get(entityName); - JsonNode signedNode = objectMapper.readTree(node.get(OSSystemFields._osSignedData.name()).asText()); - return new ResponseEntity<>(certificateService.getCertificate(signedNode, - entityName, - entityId, - request.getHeader(HttpHeaders.ACCEPT), - getTemplateUrlFromRequest(request, entityName), - JSONUtil.removeNodesByPath(node, definitionsManager.getExcludingFieldsForEntity(entityName)) - ), HttpStatus.OK); - } catch (Exception exception) { - exception.printStackTrace(); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return attestationNode; + } + + @RequestMapping( + value = "/partner/api/v1/{entityName}", + method = RequestMethod.GET + ) + public ResponseEntity getEntityWithConsent( + @PathVariable String entityName, + HttpServletRequest request + ) { + ResponseParams responseParams = new ResponseParams(); + try { + checkEntityNameInDefinitionManager(entityName); + ArrayList fields = getConsentFields(request); + JsonNode userInfoFromRegistry = registryHelper.getRequestedUserDetails( + request, + entityName + ); + JsonNode jsonNode = userInfoFromRegistry.get(entityName); + if (jsonNode instanceof ArrayNode) { + ArrayNode values = (ArrayNode) jsonNode; + if (values.size() > 0) { + JsonNode node = values.get(0); + if (node instanceof ObjectNode) { + ObjectNode entityNode = copyWhiteListedFields(fields, node); + return new ResponseEntity<>(entityNode, HttpStatus.OK); + } } + } + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } catch (RecordNotFoundException e) { + createSchemaNotFoundResponse(e.getMessage(), responseParams); + Response response = new Response( + Response.API_ID.GET, + "ERROR", + responseParams + ); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } catch (Exception e) { + logger.error("Error in partner api access", e); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } - - - private String getTemplateUrlFromRequest(HttpServletRequest request, String entityName) { - if (externalTemplatesEnabled && !StringUtils.isEmpty(request.getHeader(Template))) { - return request.getHeader(Template); - } - if (definitionsManager.getCertificateTemplates(entityName) != null && definitionsManager.getCertificateTemplates(entityName).size() > 0 && !StringUtils.isEmpty(request.getHeader(TemplateKey))) { - String templateUri = definitionsManager.getCertificateTemplates(entityName).getOrDefault(request.getHeader(TemplateKey), null); - if (!StringUtils.isEmpty(templateUri)) { - try { - if (templateUri.startsWith(MINIO_URI_PREFIX)) { - return fileStorageService.getSignedUrl(templateUri.substring(MINIO_URI_PREFIX.length())); - } else if (templateUri.startsWith(HTTP_URI_PREFIX) || templateUri.startsWith(HTTPS_URI_PREFIX)) { - return templateUri; - } - } catch (Exception e) { - logger.error("Exception while parsing certificate templates DID urls", e); - return null; - } - } - - } - return null; - } - - @RequestMapping(value = "/api/v1/{entityName}/{entityId}", method = RequestMethod.GET) - public ResponseEntity getEntity( - @PathVariable String entityName, - @PathVariable String entityId, - @RequestHeader HttpHeaders header, HttpServletRequest request, - @RequestHeader(required = false) String viewTemplateId) { - boolean requireLDResponse = false; - boolean requireVCResponse = false; - for (MediaType t : header.getAccept()) { - if (t.toString().equals(Constants.LD_JSON_MEDIA_TYPE)) { - requireLDResponse = true; - break; - } else if (t.toString().equals(Constants.VC_JSON_MEDIA_TYPE)) { - requireVCResponse = true; - } - } - if (registryHelper.doesEntityOperationRequireAuthorization(entityName) && securityEnabled) { - try { - registryHelper.authorize(entityName, entityId, request); - } catch (Exception e) { - try { - registryHelper.authorizeAttestor(entityName, request); - } catch (Exception exceptionFromAuthorizeAttestor) { - return new ResponseEntity<>(HttpStatus.FORBIDDEN); - } - } + } + + private ObjectNode copyWhiteListedFields( + ArrayList fields, + JsonNode dataNode + ) { + ObjectNode node = JsonNodeFactory.instance.objectNode(); + for (String key : fields) { + node.set(key, dataNode.get(key)); + } + return node; + } + + private ArrayList getConsentFields(HttpServletRequest request) { + ArrayList fields = new ArrayList<>(); + KeycloakAuthenticationToken principal = (KeycloakAuthenticationToken) request.getUserPrincipal(); + try { + Map otherClaims = + ( + (KeycloakPrincipal) principal.getPrincipal() + ).getKeycloakSecurityContext() + .getToken() + .getOtherClaims(); + if ( + otherClaims + .keySet() + .contains(dev.sunbirdrc.registry.Constants.KEY_CONSENT) && + otherClaims.get( + dev.sunbirdrc.registry.Constants.KEY_CONSENT + ) instanceof Map + ) { + Map consentFields = (Map) otherClaims.get( + dev.sunbirdrc.registry.Constants.KEY_CONSENT + ); + for (Object key : consentFields.keySet()) { + fields.add(key.toString()); } - ResponseParams responseParams = new ResponseParams(); - Response response = new Response(Response.API_ID.READ, "OK", responseParams); + } + } catch (Exception ex) { + logger.error("Error while extracting other claims", ex); + } + return fields; + } + + @RequestMapping( + value = "/api/v1/{entityName}/{entityId}", + method = RequestMethod.GET, + produces = { + MediaType.APPLICATION_PDF_VALUE, + MediaType.TEXT_HTML_VALUE, + Constants.SVG_MEDIA_TYPE, + } + ) + public ResponseEntity getEntityType( + @PathVariable String entityName, + @PathVariable String entityId, + HttpServletRequest request, + @RequestHeader(required = false) String viewTemplateId + ) { + ResponseParams responseParams = new ResponseParams(); + Response response; + if ( + registryHelper.doesEntityOperationRequireAuthorization(entityName) && + securityEnabled + ) { + try { + registryHelper.authorize(entityName, entityId, request); + } catch (Exception e) { try { - checkEntityNameInDefinitionManager(entityName); - String readerUserId = getUserId(entityName, request); - JsonNode node = getEntityJsonNode(entityName, entityId, requireLDResponse, readerUserId, viewTemplateId); - if (requireLDResponse) { - addJsonLDSpec(node); - } else if (requireVCResponse) { - String vcString = node.get(OSSystemFields._osSignedData.name()).textValue(); - return new ResponseEntity<>(vcString, HttpStatus.OK); - } - return new ResponseEntity<>(node, HttpStatus.OK); - + checkEntityNameInDefinitionManager(entityName); + registryHelper.authorizeAttestor(entityName, request); } catch (RecordNotFoundException re) { - createSchemaNotFoundResponse(re.getMessage(), responseParams); - response = new Response(Response.API_ID.GET, "ERROR", responseParams); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } catch (Exception e) { - logger.error("Read Api Exception occurred ", e); - responseParams.setErrmsg(e.getMessage()); - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); + createSchemaNotFoundResponse(re.getMessage(), responseParams); + response = new Response(Response.API_ID.GET, "ERROR", responseParams); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } catch (Exception exceptionFromAuthorizeAttestor) { + return new ResponseEntity<>(HttpStatus.FORBIDDEN); } + } } - - private String getUserId(String entityName, HttpServletRequest request) throws Exception { - return registryHelper.getUserId(request, entityName); - } - - private void addJsonLDSpec(JsonNode node) { - + try { + String readerUserId = getUserId(entityName, request); + JsonNode node = registryHelper + .readEntity( + readerUserId, + entityName, + entityId, + false, + viewTemplateManager.getViewTemplateById(viewTemplateId), + false + ) + .get(entityName); + JsonNode signedNode = objectMapper.readTree( + node.get(OSSystemFields._osSignedData.name()).asText() + ); + return new ResponseEntity<>( + certificateService.getCertificate( + signedNode, + entityName, + entityId, + request.getHeader(HttpHeaders.ACCEPT), + getTemplateUrlFromRequest(request, entityName), + JSONUtil.removeNodesByPath( + node, + definitionsManager.getExcludingFieldsForEntity(entityName) + ) + ), + HttpStatus.OK + ); + } catch (Exception exception) { + exception.printStackTrace(); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } - - private JsonNode getEntityJsonNode(@PathVariable String entityName, @PathVariable String entityId, - boolean requireLDResponse, String userId, String viewTemplateId) throws Exception { - JsonNode resultNode = registryHelper.readEntity(userId, entityName, entityId, false, - viewTemplateManager.getViewTemplateById(viewTemplateId), false); - Data data = new Data<>(resultNode); - Configuration config = configurationHelper.getResponseConfiguration(requireLDResponse); - ITransformer responseTransformer = transformer.getInstance(config); - Data resultContent = responseTransformer.transform(data); - logger.info("ReadEntity,{},{}", entityId, resultContent); - if (!(resultContent.getData() instanceof JsonNode)) { - throw new RuntimeException("Unknown response object " + resultContent); - } - JsonNode node = (JsonNode) resultContent.getData(); - JsonNode entityNode = node.get(entityName); - return entityNode != null ? entityNode : node; + } + + private String getTemplateUrlFromRequest( + HttpServletRequest request, + String entityName + ) { + if ( + externalTemplatesEnabled && + !StringUtils.isEmpty(request.getHeader(Template)) + ) { + return request.getHeader(Template); } - - @RequestMapping(value = "/api/v1/{entityName}", method = RequestMethod.GET) - public ResponseEntity getEntityByToken(@PathVariable String entityName, HttpServletRequest request, - @RequestHeader(required = false) String viewTemplateId) throws RecordNotFoundException { - ResponseParams responseParams = new ResponseParams(); - Response response = new Response(Response.API_ID.GET, "OK", responseParams); + if ( + definitionsManager.getCertificateTemplates(entityName) != null && + definitionsManager.getCertificateTemplates(entityName).size() > 0 && + !StringUtils.isEmpty(request.getHeader(TemplateKey)) + ) { + String templateUri = definitionsManager + .getCertificateTemplates(entityName) + .getOrDefault(request.getHeader(TemplateKey), null); + if (!StringUtils.isEmpty(templateUri)) { try { - checkEntityNameInDefinitionManager(entityName); - String userId = registryHelper.getUserId(request, entityName); - if (!Strings.isEmpty(userId)) { - JsonNode responseFromDb = registryHelper.searchEntitiesByUserId(entityName, userId, viewTemplateId); - JsonNode entities = responseFromDb.get(entityName); - if (entities.size() > 0) { - return new ResponseEntity<>(entities, HttpStatus.OK); - } else { - responseParams.setErrmsg("No record found"); - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } - } else { - responseParams.setErrmsg("User id is empty"); - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } - } catch (RecordNotFoundException e) { - createSchemaNotFoundResponse(e.getMessage(),responseParams); - response = new Response(Response.API_ID.GET, "ERROR", responseParams); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + if (templateUri.startsWith(MINIO_URI_PREFIX)) { + return fileStorageService.getSignedUrl( + templateUri.substring(MINIO_URI_PREFIX.length()) + ); + } else if ( + templateUri.startsWith(HTTP_URI_PREFIX) || + templateUri.startsWith(HTTPS_URI_PREFIX) + ) { + return templateUri; + } } catch (Exception e) { - logger.error("Exception in controller while searching entities !", e); - response.setResult(""); - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - responseParams.setErrmsg(e.getMessage()); - return new ResponseEntity<>(response, HttpStatus.UNAUTHORIZED); + logger.error( + "Exception while parsing certificate templates DID urls", + e + ); + return null; } + } } - - //TODO: check the usage and deprecate the api if not used - @GetMapping(value = "/api/v1/{entity}/{entityId}/attestationProperties") - public ResponseEntity getEntityForAttestation( - @PathVariable String entity, - @PathVariable String entityId + return null; + } + + @RequestMapping( + value = "/api/v1/{entityName}/{entityId}", + method = RequestMethod.GET + ) + public ResponseEntity getEntity( + @PathVariable String entityName, + @PathVariable String entityId, + @RequestHeader HttpHeaders header, + HttpServletRequest request, + @RequestHeader(required = false) String viewTemplateId + ) { + boolean requireLDResponse = false; + boolean requireVCResponse = false; + for (MediaType t : header.getAccept()) { + if (t.toString().equals(Constants.LD_JSON_MEDIA_TYPE)) { + requireLDResponse = true; + break; + } else if (t.toString().equals(Constants.VC_JSON_MEDIA_TYPE)) { + requireVCResponse = true; + } + } + if ( + registryHelper.doesEntityOperationRequireAuthorization(entityName) && + securityEnabled ) { - ResponseParams responseParams = new ResponseParams(); + try { + registryHelper.authorize(entityName, entityId, request); + } catch (Exception e) { try { - JsonNode resultNode = registryHelper.readEntity("", entity, entityId, false, null, false); - ObjectNode objectNode = objectMapper.createObjectNode(); - objectNode.set("entity", resultNode.get(entity)); - checkEntityNameInDefinitionManager(entity); - List attestationPolicies = definitionsManager.getDefinition(entity) - .getOsSchemaConfiguration() - .getAttestationPolicies(); - objectNode.set("attestationPolicies", objectMapper.convertValue(attestationPolicies, JsonNode.class)); - return new ResponseEntity<>(objectNode, HttpStatus.OK); - - } catch (RecordNotFoundException re) { - createSchemaNotFoundResponse(re.getMessage(), responseParams); - Response response = new Response(Response.API_ID.GET, "ERROR", responseParams); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + registryHelper.authorizeAttestor(entityName, request); + } catch (Exception exceptionFromAuthorizeAttestor) { + return new ResponseEntity<>(HttpStatus.FORBIDDEN); } - catch (Exception e) { - e.printStackTrace(); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); - } - + } } - - //TODO: check the usage and deprecate the api if not used - @RequestMapping(value = "/api/v1/{entityName}/{entityId}", method = RequestMethod.PATCH) - public ResponseEntity attestEntity( - @PathVariable String entityName, - @PathVariable String entityId, - @RequestHeader HttpHeaders header, - @RequestBody JsonNode rootNode - ) throws Exception { - ResponseParams responseParams = new ResponseParams(); - try { - checkEntityNameInDefinitionManager(entityName); - } catch (RecordNotFoundException re) { - createSchemaNotFoundResponse(re.getMessage(),responseParams); - Response response = new Response(Response.API_ID.PATCH, "ERROR", responseParams); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } - - logger.info("Attestation request for {}", rootNode.get("fieldPaths")); - JsonNode nodePath = rootNode.get("jsonPaths"); - if (nodePath instanceof ArrayNode) { - Iterator elements = ((ArrayNode) nodePath).elements(); - ArrayList paths = new ArrayList<>(); - for (Iterator it = elements; it.hasNext(); ) { - JsonNode e = it.next(); - paths.add(e.textValue()); - } - JsonNode node = registryHelper.readEntity("admin", entityName, entityId, false, null, false); - registryHelper.attestEntity(entityName, node, paths.toArray(new String[]{}), "admin"); - } - return null; + ResponseParams responseParams = new ResponseParams(); + Response response = new Response( + Response.API_ID.READ, + "OK", + responseParams + ); + try { + checkEntityNameInDefinitionManager(entityName); + String readerUserId = getUserId(entityName, request); + JsonNode node = getEntityJsonNode( + entityName, + entityId, + requireLDResponse, + readerUserId, + viewTemplateId + ); + if (requireLDResponse) { + addJsonLDSpec(node); + } else if (requireVCResponse) { + String vcString = node + .get(OSSystemFields._osSignedData.name()) + .textValue(); + return new ResponseEntity<>(vcString, HttpStatus.OK); + } + return new ResponseEntity<>(node, HttpStatus.OK); + } catch (RecordNotFoundException re) { + createSchemaNotFoundResponse(re.getMessage(), responseParams); + response = new Response(Response.API_ID.GET, "ERROR", responseParams); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } catch (Exception e) { + logger.error("Read Api Exception occurred ", e); + responseParams.setErrmsg(e.getMessage()); + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); } - - //TODO: check the usage and deprecate the api if not used - @RequestMapping(value = "/api/v1/system/{property}/{propertyId}", method = RequestMethod.POST) - public ResponseEntity updateProperty( - @PathVariable String property, - @PathVariable String propertyId, - @RequestBody JsonNode requestBody) { - logger.info("Got system request for the property {} {}", property, propertyId); - ((ObjectNode) requestBody).put(uuidPropertyName, propertyId); - ObjectNode newRootNode = objectMapper.createObjectNode(); - - ResponseParams responseParams = new ResponseParams(); - newRootNode.set(property, requestBody); - try { - String response = registryHelper.updateProperty(newRootNode, ""); - responseParams.setStatus(Response.Status.SUCCESSFUL); - responseParams.setResultList(Collections.singletonList(response)); - return new ResponseEntity<>(responseParams, HttpStatus.OK); - } catch (Exception exception) { - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - responseParams.setErrmsg(exception.getMessage()); - exception.printStackTrace(); - return new ResponseEntity<>(responseParams, HttpStatus.INTERNAL_SERVER_ERROR); - } + } + + private String getUserId(String entityName, HttpServletRequest request) + throws Exception { + return registryHelper.getUserId(request, entityName); + } + + private void addJsonLDSpec(JsonNode node) {} + + private JsonNode getEntityJsonNode( + @PathVariable String entityName, + @PathVariable String entityId, + boolean requireLDResponse, + String userId, + String viewTemplateId + ) throws Exception { + JsonNode resultNode = registryHelper.readEntity( + userId, + entityName, + entityId, + false, + viewTemplateManager.getViewTemplateById(viewTemplateId), + false + ); + Data data = new Data<>(resultNode); + Configuration config = configurationHelper.getResponseConfiguration( + requireLDResponse + ); + ITransformer responseTransformer = transformer.getInstance(config); + Data resultContent = responseTransformer.transform(data); + logger.info("ReadEntity,{},{}", entityId, resultContent); + if (!(resultContent.getData() instanceof JsonNode)) { + throw new RuntimeException("Unknown response object " + resultContent); } - - //TODO: API called by claim-ms, need to be blocked from external access - @RequestMapping(value = "/api/v1/{property}/{propertyId}/attestation/{attestationName}/{attestationId}", method = RequestMethod.PUT) - public ResponseEntity updateAttestationProperty( - @PathVariable String property, - @PathVariable String propertyId, - @PathVariable String attestationName, - @PathVariable String attestationId, - @RequestBody JsonNode requestBody) { - logger.info("Got system request to update attestation property {} {} {} {}", property, propertyId, attestationName, attestationId); - ((ObjectNode) requestBody).put(uuidPropertyName, propertyId); - ObjectNode newRootNode = objectMapper.createObjectNode(); - - ResponseParams responseParams = new ResponseParams(); - newRootNode.set(property, requestBody); - try { - logger.info("updateAttestationProperty: {}", requestBody); - PluginResponseMessage pluginResponseMessage = objectMapper.convertValue(requestBody, PluginResponseMessage.class); - registryHelper.updateState(pluginResponseMessage); - responseParams.setStatus(Response.Status.SUCCESSFUL); - responseParams.setResultList(Collections.singletonList("response")); - return new ResponseEntity<>(responseParams, HttpStatus.OK); - } catch (Exception exception) { - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - responseParams.setErrmsg(exception.getMessage()); - exception.printStackTrace(); - return new ResponseEntity<>(responseParams, HttpStatus.INTERNAL_SERVER_ERROR); + JsonNode node = (JsonNode) resultContent.getData(); + JsonNode entityNode = node.get(entityName); + return entityNode != null ? entityNode : node; + } + + @RequestMapping(value = "/api/v1/{entityName}", method = RequestMethod.GET) + public ResponseEntity getEntityByToken( + @PathVariable String entityName, + HttpServletRequest request, + @RequestHeader(required = false) String viewTemplateId + ) throws RecordNotFoundException { + ResponseParams responseParams = new ResponseParams(); + Response response = new Response(Response.API_ID.GET, "OK", responseParams); + try { + checkEntityNameInDefinitionManager(entityName); + String userId = registryHelper.getUserId(request, entityName); + if (!Strings.isEmpty(userId)) { + JsonNode responseFromDb = registryHelper.searchEntitiesByUserId( + entityName, + userId, + viewTemplateId + ); + JsonNode entities = responseFromDb.get(entityName); + if (entities.size() > 0) { + return new ResponseEntity<>(entities, HttpStatus.OK); + } else { + responseParams.setErrmsg("No record found"); + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); } + } else { + responseParams.setErrmsg("User id is empty"); + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } + } catch (RecordNotFoundException e) { + createSchemaNotFoundResponse(e.getMessage(), responseParams); + response = new Response(Response.API_ID.GET, "ERROR", responseParams); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } catch (Exception e) { + logger.error("Exception in controller while searching entities !", e); + response.setResult(""); + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + responseParams.setErrmsg(e.getMessage()); + return new ResponseEntity<>(response, HttpStatus.UNAUTHORIZED); } - - @Deprecated - @RequestMapping(value = "/api/v1/{entityName}/sign", method = RequestMethod.GET) - public ResponseEntity getSignedEntityByToken(@PathVariable String entityName, HttpServletRequest request) { - ResponseParams responseParams = new ResponseParams(); - Response response = new Response(Response.API_ID.SEARCH, "OK", responseParams); - try { - checkEntityNameInDefinitionManager(entityName); - JsonNode result = registryHelper.getRequestedUserDetails(request, entityName); - if (result.get(entityName).size() > 0) { - Object credentialTemplate = definitionsManager.getCredentialTemplate(entityName); - Object signedCredentials = registryHelper.getSignedDoc(result.get(entityName).get(0), credentialTemplate); - return new ResponseEntity<>(signedCredentials, HttpStatus.OK); - } else { - responseParams.setErrmsg("Entity not found"); - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } - } catch (RecordNotFoundException re) { - createSchemaNotFoundResponse(re.getMessage(), responseParams); - response = new Response(Response.API_ID.GET, "ERROR", responseParams); - return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); - } catch (Exception e) { - logger.error("Exception in controller while searching entities !", e); - response.setResult(""); - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - responseParams.setErrmsg(e.getMessage()); - } - return new ResponseEntity<>(response, HttpStatus.OK); + } + + //TODO: check the usage and deprecate the api if not used + @GetMapping(value = "/api/v1/{entity}/{entityId}/attestationProperties") + public ResponseEntity getEntityForAttestation( + @PathVariable String entity, + @PathVariable String entityId + ) { + ResponseParams responseParams = new ResponseParams(); + try { + JsonNode resultNode = registryHelper.readEntity( + "", + entity, + entityId, + false, + null, + false + ); + ObjectNode objectNode = objectMapper.createObjectNode(); + objectNode.set("entity", resultNode.get(entity)); + checkEntityNameInDefinitionManager(entity); + List attestationPolicies = definitionsManager + .getDefinition(entity) + .getOsSchemaConfiguration() + .getAttestationPolicies(); + objectNode.set( + "attestationPolicies", + objectMapper.convertValue(attestationPolicies, JsonNode.class) + ); + return new ResponseEntity<>(objectNode, HttpStatus.OK); + } catch (RecordNotFoundException re) { + createSchemaNotFoundResponse(re.getMessage(), responseParams); + Response response = new Response( + Response.API_ID.GET, + "ERROR", + responseParams + ); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } catch (Exception e) { + e.printStackTrace(); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + //TODO: check the usage and deprecate the api if not used + @RequestMapping( + value = "/api/v1/{entityName}/{entityId}", + method = RequestMethod.PATCH + ) + public ResponseEntity attestEntity( + @PathVariable String entityName, + @PathVariable String entityId, + @RequestHeader HttpHeaders header, + @RequestBody JsonNode rootNode + ) throws Exception { + ResponseParams responseParams = new ResponseParams(); + try { + checkEntityNameInDefinitionManager(entityName); + } catch (RecordNotFoundException re) { + createSchemaNotFoundResponse(re.getMessage(), responseParams); + Response response = new Response( + Response.API_ID.PATCH, + "ERROR", + responseParams + ); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); } - @GetMapping(value = "/api/v1/{entityName}/{entityId}/attestation/{attestationName}/{attestationId}", - produces = {MediaType.APPLICATION_PDF_VALUE, MediaType.TEXT_HTML_VALUE, Constants.SVG_MEDIA_TYPE, MediaType.APPLICATION_JSON_VALUE}) - public ResponseEntity getAttestationCertificate(HttpServletRequest request, @PathVariable String entityName, @PathVariable String entityId, - @PathVariable String attestationName, @PathVariable String attestationId) { - ResponseParams responseParams = new ResponseParams(); - try { - checkEntityNameInDefinitionManager(entityName); - String readerUserId = getUserId(entityName, request); - JsonNode node = registryHelper.readEntity(readerUserId, entityName, entityId, false, null, false) - .get(entityName).get(attestationName); - JsonNode attestationNode = getAttestationSignedData(attestationId, node); - return new ResponseEntity<>(certificateService.getCertificate(attestationNode, - entityName, - entityId, - request.getHeader(HttpHeaders.ACCEPT), - getTemplateUrlFromRequest(request, entityName), - getAttestationNode(attestationId, node) - ), HttpStatus.OK); - - } catch (RecordNotFoundException re) { - createSchemaNotFoundResponse(re.getMessage(), responseParams); - Response response = new Response(Response.API_ID.GET, "ERROR", responseParams); - try { - return new ResponseEntity<>(objectMapper.writeValueAsString(response), HttpStatus.NOT_FOUND); - } catch (JsonProcessingException e) { - return new ResponseEntity<>(HttpStatus.NOT_FOUND); - } - } catch (AttestationNotFoundException e) { - logger.error(e.getMessage()); - return new ResponseEntity<>(HttpStatus.NOT_FOUND); - } catch (Exception e) { - e.printStackTrace(); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } + logger.info("Attestation request for {}", rootNode.get("fieldPaths")); + JsonNode nodePath = rootNode.get("jsonPaths"); + if (nodePath instanceof ArrayNode) { + Iterator elements = ((ArrayNode) nodePath).elements(); + ArrayList paths = new ArrayList<>(); + for (Iterator it = elements; it.hasNext();) { + JsonNode e = it.next(); + paths.add(e.textValue()); + } + JsonNode node = registryHelper.readEntity( + "admin", + entityName, + entityId, + false, + null, + false + ); + registryHelper.attestEntity( + entityName, + node, + paths.toArray(new String[] {}), + "admin" + ); } - @RequestMapping(value = "/api/v1/{entityName}/{entityId}/revoke", method = RequestMethod.POST) - public ResponseEntity revokeACredential ( - HttpServletRequest request, - @PathVariable String entityName, - @PathVariable String entityId, - @RequestHeader HttpHeaders headers - ){ - String userId = USER_ANONYMOUS; - logger.info("Revoking the entityType {} with {} Id",entityName, entityId); - // Check fot Authorisation - if (registryHelper.doesEntityOperationRequireAuthorization(entityName)) { - try { - userId = registryHelper.authorize(entityName, entityId, request); - } catch (Exception e) { - return createUnauthorizedExceptionResponse(e); - } - } - ResponseParams responseParams = new ResponseParams(); - Response response = new Response(Response.API_ID.REVOKE, "OK", responseParams); - try { - String tag = "RegistryController.revokeAnExistingCredential " + entityName; - watch.start(tag); - JsonNode existingEntityNode = getEntityJsonNode(entityName, entityId,false, userId, null); - String signedData = existingEntityNode.get(OSSystemFields._osSignedData.name()).asText(); - if (signedData.equals(new String()) || signedData.equals(null)) { - throw new RecordNotFoundException("Credential is already revoked"); - } - JsonNode revokedEntity = registryHelper.revokeAnEntity( entityName ,entityId, userId, existingEntityNode); - if (revokedEntity != null) { - registryHelper.revokeExistingCredentials(entityName, entityId, userId, signedData); - } - responseParams.setErrmsg(""); - responseParams.setStatus(Response.Status.SUCCESSFUL); - watch.stop(tag); - return new ResponseEntity<>(response, HttpStatus.OK); - } catch (Exception e) { - logger.error("Registry Controller: Exception while revoking an entity:", e); - responseParams.setStatus(Response.Status.UNSUCCESSFUL); - responseParams.setErrmsg(e.getMessage()); - return new ResponseEntity<>(response,HttpStatus.INTERNAL_SERVER_ERROR); - } + return null; + } + + //TODO: check the usage and deprecate the api if not used + @RequestMapping( + value = "/api/v1/system/{property}/{propertyId}", + method = RequestMethod.POST + ) + public ResponseEntity updateProperty( + @PathVariable String property, + @PathVariable String propertyId, + @RequestBody JsonNode requestBody + ) { + logger.info( + "Got system request for the property {} {}", + property, + propertyId + ); + ((ObjectNode) requestBody).put(uuidPropertyName, propertyId); + ObjectNode newRootNode = objectMapper.createObjectNode(); + + ResponseParams responseParams = new ResponseParams(); + newRootNode.set(property, requestBody); + try { + String response = registryHelper.updateProperty(newRootNode, ""); + responseParams.setStatus(Response.Status.SUCCESSFUL); + responseParams.setResultList(Collections.singletonList(response)); + return new ResponseEntity<>(responseParams, HttpStatus.OK); + } catch (Exception exception) { + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + responseParams.setErrmsg(exception.getMessage()); + exception.printStackTrace(); + return new ResponseEntity<>( + responseParams, + HttpStatus.INTERNAL_SERVER_ERROR + ); + } + } + + //TODO: API called by claim-ms, need to be blocked from external access + @RequestMapping( + value = "/api/v1/{property}/{propertyId}/attestation/{attestationName}/{attestationId}", + method = RequestMethod.PUT + ) + public ResponseEntity updateAttestationProperty( + @PathVariable String property, + @PathVariable String propertyId, + @PathVariable String attestationName, + @PathVariable String attestationId, + @RequestBody JsonNode requestBody + ) { + logger.info( + "Got system request to update attestation property {} {} {} {}", + property, + propertyId, + attestationName, + attestationId + ); + ((ObjectNode) requestBody).put(uuidPropertyName, propertyId); + ObjectNode newRootNode = objectMapper.createObjectNode(); + + ResponseParams responseParams = new ResponseParams(); + newRootNode.set(property, requestBody); + try { + logger.info("updateAttestationProperty: {}", requestBody); + PluginResponseMessage pluginResponseMessage = objectMapper.convertValue( + requestBody, + PluginResponseMessage.class + ); + registryHelper.updateState(pluginResponseMessage); + responseParams.setStatus(Response.Status.SUCCESSFUL); + responseParams.setResultList(Collections.singletonList("response")); + return new ResponseEntity<>(responseParams, HttpStatus.OK); + } catch (Exception exception) { + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + responseParams.setErrmsg(exception.getMessage()); + exception.printStackTrace(); + return new ResponseEntity<>( + responseParams, + HttpStatus.INTERNAL_SERVER_ERROR + ); + } + } + + @Deprecated + @RequestMapping( + value = "/api/v1/{entityName}/sign", + method = RequestMethod.GET + ) + public ResponseEntity getSignedEntityByToken( + @PathVariable String entityName, + HttpServletRequest request + ) { + ResponseParams responseParams = new ResponseParams(); + Response response = new Response( + Response.API_ID.SEARCH, + "OK", + responseParams + ); + try { + checkEntityNameInDefinitionManager(entityName); + JsonNode result = registryHelper.getRequestedUserDetails( + request, + entityName + ); + if (result.get(entityName).size() > 0) { + Object credentialTemplate = definitionsManager.getCredentialTemplate( + entityName + ); + Object signedCredentials = registryHelper.getSignedDoc( + result.get(entityName).get(0), + credentialTemplate + ); + return new ResponseEntity<>(signedCredentials, HttpStatus.OK); + } else { + responseParams.setErrmsg("Entity not found"); + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } + } catch (RecordNotFoundException re) { + createSchemaNotFoundResponse(re.getMessage(), responseParams); + response = new Response(Response.API_ID.GET, "ERROR", responseParams); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } catch (Exception e) { + logger.error("Exception in controller while searching entities !", e); + response.setResult(""); + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + responseParams.setErrmsg(e.getMessage()); + } + return new ResponseEntity<>(response, HttpStatus.OK); + } + + @GetMapping( + value = "/api/v1/{entityName}/{entityId}/attestation/{attestationName}/{attestationId}", + produces = { + MediaType.APPLICATION_PDF_VALUE, + MediaType.TEXT_HTML_VALUE, + Constants.SVG_MEDIA_TYPE, + MediaType.APPLICATION_JSON_VALUE, + } + ) + public ResponseEntity getAttestationCertificate( + HttpServletRequest request, + @PathVariable String entityName, + @PathVariable String entityId, + @PathVariable String attestationName, + @PathVariable String attestationId + ) { + ResponseParams responseParams = new ResponseParams(); + try { + checkEntityNameInDefinitionManager(entityName); + String readerUserId = getUserId(entityName, request); + JsonNode node = registryHelper + .readEntity(readerUserId, entityName, entityId, false, null, false) + .get(entityName) + .get(attestationName); + JsonNode attestationNode = getAttestationSignedData(attestationId, node); + return new ResponseEntity<>( + certificateService.getCertificate( + attestationNode, + entityName, + entityId, + request.getHeader(HttpHeaders.ACCEPT), + getTemplateUrlFromRequest(request, entityName), + getAttestationNode(attestationId, node) + ), + HttpStatus.OK + ); + } catch (RecordNotFoundException re) { + createSchemaNotFoundResponse(re.getMessage(), responseParams); + Response response = new Response( + Response.API_ID.GET, + "ERROR", + responseParams + ); + try { + return new ResponseEntity<>( + objectMapper.writeValueAsString(response), + HttpStatus.NOT_FOUND + ); + } catch (JsonProcessingException e) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } catch (AttestationNotFoundException e) { + logger.error(e.getMessage()); + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } catch (Exception e) { + e.printStackTrace(); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + } + + @RequestMapping( + value = "/api/v1/{entityName}/{entityId}/revoke", + method = RequestMethod.POST + ) + public ResponseEntity revokeACredential( + HttpServletRequest request, + @PathVariable String entityName, + @PathVariable String entityId, + @RequestHeader HttpHeaders headers + ) { + String userId = USER_ANONYMOUS; + logger.info("Revoking the entityType {} with {} Id", entityName, entityId); + // Check fot Authorisation + if (registryHelper.doesEntityOperationRequireAuthorization(entityName)) { + try { + userId = registryHelper.authorize(entityName, entityId, request); + } catch (Exception e) { + return createUnauthorizedExceptionResponse(e); + } + } + ResponseParams responseParams = new ResponseParams(); + Response response = new Response( + Response.API_ID.REVOKE, + "OK", + responseParams + ); + try { + String tag = + "RegistryController.revokeAnExistingCredential " + entityName; + watch.start(tag); + JsonNode existingEntityNode = getEntityJsonNode( + entityName, + entityId, + false, + userId, + null + ); + String signedData = existingEntityNode + .get(OSSystemFields._osSignedData.name()) + .asText(); + if (signedData.equals(new String()) || signedData.equals(null)) { + throw new RecordNotFoundException("Credential is already revoked"); + } + JsonNode revokedEntity = registryHelper.revokeAnEntity( + entityName, + entityId, + userId, + existingEntityNode + ); + if (revokedEntity != null) { + registryHelper.revokeExistingCredentials( + entityName, + entityId, + userId, + signedData + ); + } + responseParams.setErrmsg(""); + responseParams.setStatus(Response.Status.SUCCESSFUL); + watch.stop(tag); + return new ResponseEntity<>(response, HttpStatus.OK); + } catch (Exception e) { + logger.error( + "Registry Controller: Exception while revoking an entity:", + e + ); + responseParams.setStatus(Response.Status.UNSUCCESSFUL); + responseParams.setErrmsg(e.getMessage()); + return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); } + } } diff --git a/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java b/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java index 8522b4555..600f65504 100644 --- a/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java +++ b/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java @@ -93,7 +93,9 @@ public class RegistryHelper { @Value("${notification.service.enabled}") boolean notificationEnabled; @Value("${invite.required_validation_enabled}") boolean skipRequiredValidationForInvite = true; @Value("${invite.signature_enabled}") boolean skipSignatureForInvite = true; - @Value("${cord.schemaURL}") String cord_schema_url; + @Value("${cord.issuer_schema_url}") String cord_schema_url; + @Value("${cord.issuer_registry_url}") String cord_registry_url; + @Autowired private NotificationHelper notificationHelper; @Autowired @@ -197,7 +199,7 @@ public JsonNode removeFormatAttr(JsonNode requestBody) { /** * REUSBALE METHOD FOR POST API CALLS */ - public void apiHelper(JsonNode obj,String url){ + public JsonNode apiHelper(JsonNode obj,String url) throws Exception{ WebClient.Builder builder = WebClient.builder(); try{ Mono responseMono = builder.build() @@ -214,7 +216,6 @@ public void apiHelper(JsonNode obj,String url){ }); JsonNode response = responseMono.block(); - logger.info("RESPONSE {}",response); }catch(Exception e){ logger.error("Exception occurred !" , e); } @@ -223,20 +224,15 @@ public void apiHelper(JsonNode obj,String url){ /** * Anchors schema to the CORD CHAIN */ - public void anchorSchemaAPI(JsonNode obj){ - // apiHelper(obj,"http://172.24.0.1:5106/api/v1/schema"); - apiHelper(obj,cord_schema_url); // considering issuer agent running in local - } - - - /** - * Anchors registry to the chain , - * Before calling this api, schema must be created - - */ - public void anchorRegistryAPI(){ + public JsonNode anchorSchemaAPI(JsonNode obj) throws Exception{ + JsonNode schema=apiHelper(obj,cord_schema_url); + logger.info("ANCHORED SCHEMA {}",schema); + return schema; + } + + /** * calls validation and then persists the record to registry. * diff --git a/java/registry/src/main/resources/application.yml b/java/registry/src/main/resources/application.yml index 4509edb97..1d442b296 100644 --- a/java/registry/src/main/resources/application.yml +++ b/java/registry/src/main/resources/application.yml @@ -66,7 +66,9 @@ registry: host: ${redis_host:localhost} port: ${redis_port:6379} cord: - schemaURL: ${cord_schema_url:http://localhost:5106/api/v1/schema} + issuer_schema_url: ${cord_schema_url:http://localhost:5106/api/v1/schema} + issuer_registry_url : ${cord_registry_url:http://localhost:5106/api/v1/registry} + issuer_credential_url: ${cord_credential_url:http://localhost:5106/api/v1/cred} workflow: enabled: ${workflow.enable:true}