[Users]: Fix update service

This commit is contained in:
Konstantinos Triantafyllou 2020-11-10 09:58:13 +00:00
parent 8bb0f9e44f
commit 66706b74c7
1 changed files with 22 additions and 20 deletions

View File

@ -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);