1
|
package eu.dnetlib.openaire.usermanagement;
|
2
|
|
3
|
import eu.dnetlib.openaire.user.pojos.RegisteredService;
|
4
|
import eu.dnetlib.openaire.usermanagement.utils.RegisteredServicesUtils;
|
5
|
import eu.dnetlib.openaire.usermanagement.utils.TokenUtils;
|
6
|
import org.apache.http.HttpResponse;
|
7
|
import org.apache.log4j.Logger;
|
8
|
import org.bouncycastle.util.Integers;
|
9
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
11
|
import org.springframework.security.core.context.SecurityContextHolder;
|
12
|
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
|
13
|
|
14
|
import javax.servlet.ServletConfig;
|
15
|
import javax.servlet.ServletException;
|
16
|
import javax.servlet.http.HttpServlet;
|
17
|
import javax.servlet.http.HttpServletRequest;
|
18
|
import javax.servlet.http.HttpServletResponse;
|
19
|
import java.io.IOException;
|
20
|
import java.sql.SQLException;
|
21
|
|
22
|
public class EditRegisteredService extends HttpServlet {
|
23
|
|
24
|
@Autowired
|
25
|
private RegisteredServicesUtils registeredServicesUtils;
|
26
|
private static final Logger logger = Logger.getLogger(EditRegisteredService.class);
|
27
|
|
28
|
public void init(ServletConfig config) throws ServletException {
|
29
|
super.init(config);
|
30
|
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
|
31
|
config.getServletContext());
|
32
|
}
|
33
|
|
34
|
@Override
|
35
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
36
|
request.getSession().setAttribute("authenticated",
|
37
|
!SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()
|
38
|
.equals("anonymousUser"));
|
39
|
|
40
|
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.
|
41
|
getContext().getAuthentication();
|
42
|
|
43
|
String accessToken = authentication.getAccessTokenValue();
|
44
|
String serviceId = request.getParameter("id");
|
45
|
|
46
|
int serviceIdInt = Integer.parseInt(serviceId);
|
47
|
|
48
|
try {
|
49
|
if (!registeredServicesUtils.isAuthorized(authentication.getSub(), serviceIdInt)) {
|
50
|
request.getSession().setAttribute("message", "You have no permission to edit the service.");
|
51
|
response.sendRedirect("./registeredServices");
|
52
|
}
|
53
|
} catch (SQLException sqle) {
|
54
|
logger.error("Unable to access service with id " + serviceId, sqle);
|
55
|
request.getSession().setAttribute("message", "There was an error accessing your service.");
|
56
|
response.sendRedirect("./registeredServices");
|
57
|
|
58
|
} catch (NumberFormatException nfe) {
|
59
|
logger.error("Unable to access service with id " + serviceId, nfe);
|
60
|
request.getSession().setAttribute("message", "Service with id " + serviceId + " does not exist.");
|
61
|
response.sendRedirect("./registeredServices");
|
62
|
}
|
63
|
|
64
|
if (serviceId == null || serviceId.isEmpty()) { //TODO WRONG MESSAGE
|
65
|
request.getSession().setAttribute("message", "Service with id " + serviceId + " does not exist.");
|
66
|
}
|
67
|
|
68
|
RegisteredService registeredService = null;
|
69
|
try {
|
70
|
registeredService = registeredServicesUtils.getRegisteredServiceDao().fetchRegisteredServiceById(Integer.parseInt(serviceId));
|
71
|
|
72
|
} catch (SQLException sqle) {
|
73
|
logger.error("Unable to access service with id " + serviceId, sqle);
|
74
|
request.getSession().setAttribute("message", "There was an error accessing your service.");
|
75
|
response.sendRedirect("./registeredServices");
|
76
|
}
|
77
|
|
78
|
if (registeredService != null && registeredService.getAai_id() != null) {
|
79
|
ServiceResponse serviceResponse = TokenUtils.getRegisteredService(registeredService.getAai_id(), accessToken);
|
80
|
request.getSession().setAttribute("serviceId", serviceResponse.getId());
|
81
|
System.out.println("service client name " + serviceResponse.getClientName());
|
82
|
request.getSession().setAttribute("first_name", serviceResponse.getClientName());
|
83
|
System.out.println("service client description " + serviceResponse.getClientDescription());
|
84
|
request.getSession().setAttribute("description", serviceResponse.getClientDescription());
|
85
|
|
86
|
} else {
|
87
|
request.getSession().setAttribute("message", "Service with id " + serviceId + " does not exist.");
|
88
|
}
|
89
|
response.setContentType("text/html");
|
90
|
request.getRequestDispatcher("./editRegisteredService.jsp").include(request, response);
|
91
|
|
92
|
}
|
93
|
|
94
|
@Override
|
95
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
96
|
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
97
|
String accessToken = authentication.getAccessTokenValue();
|
98
|
|
99
|
String serviceId = request.getParameter("serviceId");
|
100
|
String name = request.getParameter("first_name");
|
101
|
String description = request.getParameter("description");
|
102
|
|
103
|
HttpResponse resp = TokenUtils.updateService(serviceId, authentication.getSub(), name, description, authentication.getUserInfo().getEmail(), accessToken);
|
104
|
|
105
|
if (resp.getStatusLine().getStatusCode()==200) {
|
106
|
|
107
|
RegisteredService registeredService = new RegisteredService();
|
108
|
registeredService.setName(name);
|
109
|
registeredService.setAai_id(serviceId);
|
110
|
try {
|
111
|
registeredServicesUtils.getRegisteredServiceDao().update(registeredService);
|
112
|
} catch (SQLException sqle) {
|
113
|
logger.error("Unable to contact db.", sqle);
|
114
|
request.getSession().setAttribute("message", "Fail to delete the service. Please try again later.");
|
115
|
response.setContentType("text/html");
|
116
|
request.getRequestDispatcher("./registeredServices.jsp").include(request, response);
|
117
|
}
|
118
|
|
119
|
request.getSession().setAttribute("success", "Your service with name '" + name + "' was successfully updated");
|
120
|
}
|
121
|
|
122
|
response.sendRedirect("./registeredServices");
|
123
|
|
124
|
}
|
125
|
}
|