Project

General

Profile

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.mitre.openid.connect.model.OIDCAuthenticationToken;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.security.core.context.SecurityContextHolder;
11
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
12

    
13
import javax.servlet.ServletConfig;
14
import javax.servlet.ServletException;
15
import javax.servlet.http.HttpServlet;
16
import javax.servlet.http.HttpServletRequest;
17
import javax.servlet.http.HttpServletResponse;
18
import java.io.IOException;
19
import java.sql.SQLException;
20

    
21
public class EditRegisteredService extends HttpServlet {
22

    
23
    @Autowired
24
    private RegisteredServicesUtils registeredServicesUtils;
25
    private static final Logger logger = Logger.getLogger(EditRegisteredService.class);
26

    
27
    public void init(ServletConfig config) throws ServletException {
28
        super.init(config);
29
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
30
                config.getServletContext());
31
    }
32

    
33
    @Override
34
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
35
        request.getSession().setAttribute("authenticated",
36
                !SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()
37
                        .equals("anonymousUser"));
38

    
39
        OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.
40
                getContext().getAuthentication();
41

    
42
        String accessToken = authentication.getAccessTokenValue();
43
        String serviceId = request.getParameter("id");
44

    
45
        int serviceIdInt = Integer.parseInt(serviceId);
46

    
47
        try {
48
            if (!registeredServicesUtils.isAuthorized(authentication.getSub(), serviceIdInt)) {
49
                request.getSession().setAttribute("message", "You have no permission to edit the service.");
50
                response.sendRedirect("./registeredServices");
51
            }
52
        } catch (SQLException sqle) {
53
            logger.error("Unable to access service with id " + serviceId, sqle);
54
            request.getSession().setAttribute("message", "There was an error accessing your service.");
55
            response.sendRedirect("./registeredServices");
56

    
57
        } catch (NumberFormatException nfe) {
58
            logger.error("Unable to access service with id " + serviceId, nfe);
59
            request.getSession().setAttribute("message", "Service with id " + serviceId + " does not exist.");
60
            response.sendRedirect("./registeredServices");
61
        }
62

    
63
        if (serviceId == null || serviceId.isEmpty()) { //TODO WRONG MESSAGE
64
            request.getSession().setAttribute("message", "Service with id " + serviceId + " does not exist.");
65
        }
66

    
67
        RegisteredService registeredService  = null;
68
        try {
69
            registeredService = registeredServicesUtils.getRegisteredServiceDao().fetchRegisteredServiceById(Integer.parseInt(serviceId));
70

    
71
        } catch (SQLException sqle) {
72
            logger.error("Unable to access service with id " + serviceId, sqle);
73
            request.getSession().setAttribute("message", "There was an error accessing your service.");
74
            response.sendRedirect("./registeredServices");
75
        }
76

    
77
        if (registeredService != null && registeredService.getAai_id() != null) {
78
            ServiceResponse serviceResponse = TokenUtils.getRegisteredService(registeredService.getAai_id(), accessToken);
79
            request.getSession().setAttribute("serviceId", serviceResponse.getId());
80
            System.out.println("service client name " + serviceResponse.getClientName());
81
            request.getSession().setAttribute("first_name", serviceResponse.getClientName());
82
            System.out.println("service client description " + serviceResponse.getClientDescription());
83
            request.getSession().setAttribute("description", serviceResponse.getClientDescription());
84

    
85
        } else {
86
            request.getSession().setAttribute("message", "Service with id " + serviceId + " does not exist.");
87
        }
88
        response.setContentType("text/html");
89
        request.getRequestDispatcher("./editRegisteredService.jsp").include(request, response);
90

    
91
    }
92

    
93
    @Override
94
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
95
        OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
96
        String accessToken = authentication.getAccessTokenValue();
97

    
98
        String serviceId = request.getParameter("serviceId");
99
        String name = request.getParameter("first_name");
100
        String description = request.getParameter("description");
101

    
102
        HttpResponse resp = TokenUtils.updateService(serviceId, authentication.getSub(), name, description, authentication.getUserInfo().getEmail(), accessToken);
103

    
104
        if (resp.getStatusLine().getStatusCode()==200) {
105

    
106
            RegisteredService registeredService = new RegisteredService();
107
            registeredService.setName(name);
108
            registeredService.setAai_id(serviceId);
109
            try {
110
                registeredServicesUtils.getRegisteredServiceDao().update(registeredService);
111
            } catch (SQLException sqle) {
112
                logger.error("Unable to contact db.", sqle);
113
                request.getSession().setAttribute("message", "Fail to delete the service. Please try again later.");
114
                response.setContentType("text/html");
115
                request.getRequestDispatcher("./registeredServices.jsp").include(request, response);
116
            }
117

    
118
            request.getSession().setAttribute("success", "Your service with name '" + name + "' was successfully updated");
119
        }
120

    
121
        response.sendRedirect("./registeredServices");
122

    
123
    }
124
}
(3-3/19)