1
|
package eu.dnetlib.openaire.usermanagement;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import com.google.gson.GsonBuilder;
|
5
|
import org.apache.commons.io.IOUtils;
|
6
|
import org.apache.http.HttpHeaders;
|
7
|
import org.apache.http.HttpResponse;
|
8
|
import org.apache.http.client.methods.HttpPost;
|
9
|
import org.apache.http.entity.StringEntity;
|
10
|
import org.apache.http.impl.client.CloseableHttpClient;
|
11
|
import org.apache.http.impl.client.HttpClients;
|
12
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
13
|
import org.springframework.beans.factory.annotation.Value;
|
14
|
import org.springframework.security.core.context.SecurityContextHolder;
|
15
|
|
16
|
import javax.servlet.ServletException;
|
17
|
import javax.servlet.http.HttpServlet;
|
18
|
import javax.servlet.http.HttpServletRequest;
|
19
|
import javax.servlet.http.HttpServletResponse;
|
20
|
import java.io.IOException;
|
21
|
import java.nio.charset.StandardCharsets;
|
22
|
|
23
|
public class RegisterServiceServlet extends HttpServlet {
|
24
|
|
25
|
@Value("${oidc.issuer}")
|
26
|
private String issuer;
|
27
|
|
28
|
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
29
|
throws ServletException, IOException {
|
30
|
response.setContentType("text/html");
|
31
|
request.getRequestDispatcher("./registerService.jsp").include(request, response);
|
32
|
}
|
33
|
|
34
|
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
35
|
throws ServletException, IOException {
|
36
|
response.setContentType("text/html");
|
37
|
|
38
|
String name = request.getParameter("first_name").trim();
|
39
|
String description = request.getParameter("description").trim();
|
40
|
|
41
|
if ( name!= null && !name.isEmpty()){
|
42
|
ServiceRequest serviceJSON = new ServiceRequest();
|
43
|
serviceJSON.setClientName(name);
|
44
|
serviceJSON.setClientDescription(description);
|
45
|
|
46
|
GsonBuilder builder = new GsonBuilder();
|
47
|
builder.serializeNulls();
|
48
|
Gson gson = builder.create();
|
49
|
String json = gson.toJson(serviceJSON);
|
50
|
|
51
|
System.out.println(json);
|
52
|
|
53
|
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
54
|
|
55
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
56
|
//TODO fix this
|
57
|
HttpPost httppost = new HttpPost("https://openaire-dev.aai-dev.grnet.gr/oidc/api/clients");
|
58
|
|
59
|
httppost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
60
|
httppost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + authentication.getAccessTokenValue());
|
61
|
StringEntity params = new StringEntity(json.toString());
|
62
|
httppost.setEntity(params);
|
63
|
HttpResponse httpResponse = null;
|
64
|
httpResponse = httpclient.execute(httppost);
|
65
|
|
66
|
String serverMessage = IOUtils.toString(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8.name());
|
67
|
|
68
|
ServiceResponse serviceResponse = new Gson().fromJson(serverMessage, ServiceResponse.class);
|
69
|
System.out.println("The created id is " + serviceResponse.getId());
|
70
|
}
|
71
|
|
72
|
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
73
|
|
74
|
request.getSession().setAttribute("accessToken", authentication.getAccessTokenValue());
|
75
|
request.getSession().setAttribute("refreshToken", authentication.getRefreshTokenValue());
|
76
|
|
77
|
request.getRequestDispatcher("./serviceTokens.jsp").include(request, response);
|
78
|
}
|
79
|
}
|