Project

General

Profile

« Previous | Next » 

Revision 59517

Added RegisteredServicesUtils

View differences:

modules/dnet-openaire-users/trunk/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServiceServlet.java
1 1
package eu.dnetlib.openaire.usermanagement;
2 2

  
3 3
import com.google.gson.Gson;
4
import com.google.gson.GsonBuilder;
4
import eu.dnetlib.openaire.user.pojos.RegisteredService;
5
import eu.dnetlib.openaire.user.utils.VerificationActions;
6
import eu.dnetlib.openaire.usermanagement.utils.RegisteredServicesUtils;
5 7
import eu.dnetlib.openaire.usermanagement.utils.TokenUtils;
6
import org.apache.commons.io.IOUtils;
7
import org.apache.http.HttpHeaders;
8
import org.apache.http.HttpResponse;
9
import org.apache.http.client.methods.HttpPost;
10
import org.apache.http.entity.StringEntity;
11
import org.apache.http.impl.client.CloseableHttpClient;
12
import org.apache.http.impl.client.HttpClients;
8
import org.apache.log4j.Logger;
13 9
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
14
import org.springframework.beans.factory.annotation.Value;
10
import org.springframework.beans.factory.annotation.Autowired;
15 11
import org.springframework.security.core.context.SecurityContextHolder;
12
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
16 13

  
14
import javax.servlet.ServletConfig;
17 15
import javax.servlet.ServletException;
18 16
import javax.servlet.http.HttpServlet;
19 17
import javax.servlet.http.HttpServletRequest;
20 18
import javax.servlet.http.HttpServletResponse;
21 19
import java.io.IOException;
22
import java.nio.charset.StandardCharsets;
20
import java.sql.SQLException;
23 21

  
22

  
24 23
public class RegisterServiceServlet extends HttpServlet {
25 24

  
26
    @Value("${oidc.issuer}")
27
    private String issuer;
25
    private Logger logger = Logger.getLogger(RegisterServiceServlet.class);
28 26

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

  
33
    @Autowired
34
    private RegisteredServicesUtils registeredServicesUtils;
35

  
29 36
    public void doGet(HttpServletRequest request, HttpServletResponse response)
30 37
            throws ServletException, IOException {
31 38
        response.setContentType("text/html");
......
44 51
        String email = authentication.getUserInfo().getEmail();
45 52
        String accessToken = authentication.getAccessTokenValue();
46 53

  
54
        ServiceResponse serviceResponse = null;
55

  
47 56
        if ( name!= null && !name.isEmpty() && userid != null && !userid.isEmpty() &&
48
                    email != null && email.isEmpty()) {
57
                    email != null && !email.isEmpty()) {
49 58
            String serverMessage = TokenUtils.registerService(userid, name, description, email, accessToken);
50
            ServiceResponse serviceResponse = new Gson().fromJson(serverMessage, ServiceResponse.class);
51 59

  
52
            System.out.println("The created id is " + serviceResponse.getId());
60
            if (serverMessage == null) {
61
                request.getSession().setAttribute("message", "There was an error registering your service. Please try again later.");
62
                response.sendRedirect("./registeredServices");
63
            }
64

  
65
            serviceResponse = new Gson().fromJson(serverMessage, ServiceResponse.class);
66

  
53 67
        }
54 68

  
69
        String aai_id = serviceResponse.getId();
70
        String owner = userid;
71

  
72
        RegisteredService registeredService = new RegisteredService(aai_id, owner, name);
73

  
74
        try {
75
            System.out.println("I WILL CONTACT THE DB");
76
            registeredServicesUtils.addRegistedService(registeredService);
77

  
78
        } catch (SQLException sqle) {
79
            System.out.println("SQLE SKATA " +  sqle.getMessage());
80
            logger.error("SQLE SKATA", sqle);
81
            request.getSession().setAttribute("message", "There was an error registering your service. Please try again later.");
82
            response.sendRedirect("/registeredServices");
83
        }
84

  
55 85
        request.getSession().setAttribute("accessToken", authentication.getAccessTokenValue());
56 86
        request.getSession().setAttribute("refreshToken", authentication.getRefreshTokenValue());
57 87

  
58
        request.getRequestDispatcher("./serviceTokens.jsp").include(request, response);
88
        request.getRequestDispatcher("./registeredServices.jsp").include(request, response);
59 89
    }
60 90
}
modules/dnet-openaire-users/trunk/src/main/java/eu/dnetlib/openaire/usermanagement/utils/TokenUtils.java
41 41
        CloseableHttpClient httpclient = HttpClients.createDefault();
42 42
        HttpResponse httpResponse = httpclient.execute(httppost);
43 43

  
44
        return IOUtils.toString(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8.name());
44
        System.out.println("HTTP RESPONSE " + httpResponse.getStatusLine().getStatusCode());
45
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
46
            return IOUtils.toString(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8.name());
47
        }
48

  
49
        return null;
45 50
    }
46 51

  
47 52
    public static HttpResponse updateService(String serviceId, String userId, String name, String description, String email, String accessToken) throws IOException {
modules/dnet-openaire-users/trunk/src/main/java/eu/dnetlib/openaire/usermanagement/utils/RegisteredServicesUtils.java
1
package eu.dnetlib.openaire.usermanagement.utils;
2

  
3
import eu.dnetlib.openaire.user.pojos.RegisteredService;
4
import eu.dnetlib.openaire.user.registeredService.RegisteredServiceDao;
5
import eu.dnetlib.openaire.user.registeredService.RegisteredServiceSQL;
6
import org.springframework.stereotype.Component;
7

  
8
import java.sql.SQLException;
9

  
10
@Component
11
public class RegisteredServicesUtils {
12

  
13
    RegisteredServiceDao registeredServiceDao = new RegisteredServiceSQL();
14

  
15
    public RegisteredServiceDao getRegisteredServiceDao() {
16
        return registeredServiceDao;
17
    }
18

  
19
    public void setRegisteredServiceDao(RegisteredServiceDao registeredServiceDao) {
20
        this.registeredServiceDao = registeredServiceDao;
21
    }
22

  
23
    public void addRegistedService(RegisteredService registeredService) throws SQLException {
24
        System.out.println("I AM HEEEEEEEEERE :P");
25
        registeredServiceDao.insertRegisteredService(registeredService);
26
    }
27

  
28
}
modules/dnet-openaire-users/trunk/src/main/java/eu/dnetlib/openaire/usermanagement/EditRegisteredService.java
15 15

  
16 16
    @Override
17 17
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
18
        String serviceId = request.getParameter("id");
18

  
19 19
        OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
20
        //AuthenticationUtils.checkAuthenticationAndRedirect(authentication, response);
20 21
        String accessToken = authentication.getAccessTokenValue();
22

  
23
        String serviceId = request.getParameter("id");
24
        if (serviceId == null || serviceId.isEmpty()) {
25
            request.getSession().setAttribute("message", "No selected service is provided.");
26
        }
27

  
21 28
        ServiceResponse serviceResponse = TokenUtils.getRegisteredService(serviceId, accessToken);
22 29
        request.getSession().setAttribute("serviceId", serviceResponse.getId());
23 30
        request.getSession().setAttribute("first_name", serviceResponse.getClientName());

Also available in: Unified diff