From 66706b74c737e3cdb5ab0886843e353527b43091 Mon Sep 17 00:00:00 2001 From: Konstantinos Triantafyllou Date: Tue, 10 Nov 2020 09:58:13 +0000 Subject: [PATCH] [Users]: Fix update service --- .../RegisterServiceServlet.java | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServiceServlet.java b/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServiceServlet.java index 8f09f34..0507a68 100644 --- a/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServiceServlet.java +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServiceServlet.java @@ -151,7 +151,7 @@ public class RegisterServiceServlet extends HttpServlet { String serviceId = request.getParameter("id"); String name = request.getParameter("first_name").trim(); - if (name == null || name.isEmpty()) { + if (name.isEmpty()) { request.getSession().setAttribute("first_name_error", true); canProceed = false; } @@ -192,15 +192,6 @@ public class RegisterServiceServlet extends HttpServlet { } String userid = authentication.getSub(); String email = authentication.getUserInfo().getEmail(); - String serverRequestJSON; - if (keyType.equals("uri")){ - serverRequestJSON = createServiceJson(name, email, jwksUri); - } else { - serverRequestJSON = createServiceJson(name, email, jwks); - } - - System.out.println("SERVER JSON " + serverRequestJSON); - ServiceResponse serviceResponse = null; if (nameIsValid(name) && userInfoIsValid(userid, email) && keyIsValid(keyType, jwksUri, jwksString) && canProceed) { @@ -210,7 +201,14 @@ public class RegisterServiceServlet extends HttpServlet { if(mode.equals("create")) { //Careful! Redirects in method checkNumberOfRegisteredServices(request, response, authentication); + String serverRequestJSON; + if (keyType.equals("uri")){ + serverRequestJSON = createServiceJson(null, name, email, jwksUri); + } else { + serverRequestJSON = createServiceJson(null, name, email, jwks); + } + System.out.println("SERVER JSON " + serverRequestJSON); serverMessage = tokenUtils.registerService(serverRequestJSON); logger.debug(serverMessage); @@ -237,8 +235,7 @@ public class RegisterServiceServlet extends HttpServlet { return; } } else { - int serviceIdInt = 0; - + int serviceIdInt = 0; if (serviceId == null || serviceId.isEmpty()) { //TODO WRONG MESSAGE request.getSession().setAttribute("message", "Service with id " + serviceId + " does not exist."); response.sendRedirect("./registeredServices"); @@ -252,13 +249,16 @@ public class RegisterServiceServlet extends HttpServlet { response.sendRedirect("./registeredServices"); } - RegisteredService registeredService = null; - registeredService = registeredServicesUtils.getRegisteredServiceDao().fetchRegisteredServiceById(serviceIdInt); - + RegisteredService registeredService = registeredServicesUtils.getRegisteredServiceDao().fetchRegisteredServiceById(serviceIdInt); if (registeredService != null && registeredService.getClientId() != null) { - serviceResponse = tokenUtils.getRegisteredService(registeredService.getClientId(), registeredService.getRegistrationAccessToken()); + String serverRequestJSON; + if (keyType.equals("uri")){ + serverRequestJSON = createServiceJson(registeredService.getClientId(), name, email, jwksUri); + } else { + serverRequestJSON = createServiceJson(registeredService.getClientId(), name, email, jwks); + } + System.out.println("SERVER JSON " + serverRequestJSON); HttpResponse resp = tokenUtils.updateService(registeredService.getClientId(), serverRequestJSON, registeredService.getRegistrationAccessToken()); - if (resp.getStatusLine().getStatusCode()==200) { System.out.println("NAME >>>>" + name); registeredService.setName(name); @@ -275,7 +275,7 @@ public class RegisterServiceServlet extends HttpServlet { } request.getSession().setAttribute("success", "Your service with name " + name + " was successfully updated.The id of your service " + - "is " + serviceResponse.getClientId()); + "is " + registeredService.getClientId()); } } else { @@ -358,8 +358,9 @@ public class RegisterServiceServlet extends HttpServlet { } } - private static String createServiceJson(String name, String email, String jwksURI) { + private static String createServiceJson(String clientId, String name, String email, String jwksURI) { ServiceRequest serviceJSON = new ServiceRequest(); + serviceJSON.setClientId(clientId); serviceJSON.setClientName(name); serviceJSON.setContacts(new String[]{email}); serviceJSON.setJwksUri(jwksURI); @@ -371,8 +372,9 @@ public class RegisterServiceServlet extends HttpServlet { return gson.toJson(serviceJSON); } - private static String createServiceJson(String name, String email, Jwks jwks) { + private static String createServiceJson(String clientId, String name, String email, Jwks jwks) { ServiceRequest serviceJSON = new ServiceRequest(); + serviceJSON.setClientId(clientId); serviceJSON.setClientName(name); serviceJSON.setContacts(new String[]{email}); serviceJSON.setJwks(jwks);