1
|
package eu.dnetlib.openaire.usermanagement.utils;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import com.google.gson.GsonBuilder;
|
5
|
import com.google.gson.reflect.TypeToken;
|
6
|
import eu.dnetlib.openaire.usermanagement.ServiceRequest;
|
7
|
import eu.dnetlib.openaire.usermanagement.ServiceResponse;
|
8
|
import org.apache.commons.io.IOUtils;
|
9
|
import org.apache.http.HttpHeaders;
|
10
|
import org.apache.http.HttpResponse;
|
11
|
import org.apache.http.client.methods.HttpDelete;
|
12
|
import org.apache.http.client.methods.HttpGet;
|
13
|
import org.apache.http.client.methods.HttpPost;
|
14
|
import org.apache.http.client.methods.HttpPut;
|
15
|
import org.apache.http.entity.StringEntity;
|
16
|
import org.apache.http.impl.client.CloseableHttpClient;
|
17
|
import org.apache.http.impl.client.HttpClients;
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.springframework.beans.factory.annotation.Value;
|
20
|
import org.springframework.http.HttpStatus;
|
21
|
|
22
|
import javax.xml.ws.Service;
|
23
|
import java.io.IOException;
|
24
|
import java.io.UnsupportedEncodingException;
|
25
|
import java.lang.reflect.Type;
|
26
|
import java.nio.charset.StandardCharsets;
|
27
|
import java.util.Base64;
|
28
|
import java.util.List;
|
29
|
|
30
|
public class TokenUtils {
|
31
|
|
32
|
private Logger logger = Logger.getLogger(TokenUtils.class);
|
33
|
|
34
|
public static String registerService(String userId, String name, String description, String email, String accessToken)
|
35
|
throws IOException {
|
36
|
|
37
|
String json = createServiceJson(name, description);
|
38
|
|
39
|
//TODO fix this
|
40
|
HttpPost httppost = new HttpPost("https://openaire-dev.aai-dev.grnet.gr/oidc/api/clients");
|
41
|
httppost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
42
|
httppost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
|
43
|
StringEntity params = new StringEntity(json.toString());
|
44
|
httppost.setEntity(params);
|
45
|
|
46
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
47
|
HttpResponse httpResponse = httpclient.execute(httppost);
|
48
|
|
49
|
System.out.println("HTTP RESPONSE " + httpResponse.getStatusLine().getStatusCode());
|
50
|
if (httpResponse.getStatusLine().getStatusCode() == 200) {
|
51
|
return IOUtils.toString(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8.name());
|
52
|
}
|
53
|
|
54
|
return null;
|
55
|
}
|
56
|
|
57
|
public static HttpResponse updateService(String serviceId, String userId, String name, String description, String email, String accessToken) throws IOException {
|
58
|
System.out.println("Updated name " + name);
|
59
|
System.out.println("Updated description " + description);
|
60
|
String json = createServiceJson(name, description);
|
61
|
|
62
|
HttpPut httpPut = new HttpPut("https://openaire-dev.aai-dev.grnet.gr/oidc/api/clients/"+serviceId);
|
63
|
httpPut.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
64
|
httpPut.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
|
65
|
StringEntity params = new StringEntity(json.toString());
|
66
|
httpPut.setEntity(params);
|
67
|
|
68
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
69
|
return httpclient.execute(httpPut);
|
70
|
}
|
71
|
|
72
|
public static HttpResponse deleteService(String serviceId, String accessToken) throws IOException {
|
73
|
|
74
|
System.out.println("DELETE " + "https://openaire-dev.aai-dev.grnet.gr/oidc/api/clients/"+serviceId);
|
75
|
HttpDelete httpDelete = new HttpDelete("https://openaire-dev.aai-dev.grnet.gr/oidc/api/clients/"+serviceId);
|
76
|
httpDelete.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
77
|
httpDelete.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
|
78
|
|
79
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
80
|
return httpclient.execute(httpDelete);
|
81
|
}
|
82
|
|
83
|
public static ServiceResponse getRegisteredService(String serviceId, String accessToken) throws IOException {
|
84
|
HttpGet httpGet = new HttpGet("https://openaire-dev.aai-dev.grnet.gr/oidc/api/clients/"+serviceId);
|
85
|
httpGet.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
|
86
|
|
87
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
88
|
HttpResponse httpResponse = httpclient.execute(httpGet);
|
89
|
String registeredService = IOUtils.toString(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8.name());
|
90
|
System.out.println(registeredService);
|
91
|
return new Gson().fromJson(registeredService,ServiceResponse.class);
|
92
|
}
|
93
|
|
94
|
public void viewRegisteredServices(List<String> serviceIds, String accessToken) throws IOException {
|
95
|
for (String serviceId: serviceIds) {
|
96
|
getRegisteredService(serviceId, accessToken);
|
97
|
}
|
98
|
}
|
99
|
|
100
|
private static String createServiceJson(String name, String description) {
|
101
|
ServiceRequest serviceJSON = new ServiceRequest();
|
102
|
serviceJSON.setClientName(name);
|
103
|
serviceJSON.setClientDescription(description);
|
104
|
|
105
|
GsonBuilder builder = new GsonBuilder();
|
106
|
builder.serializeNulls();
|
107
|
Gson gson = builder.create();
|
108
|
|
109
|
System.out.println("Created json " +serviceJSON);
|
110
|
return gson.toJson(serviceJSON);
|
111
|
}
|
112
|
}
|