1
|
package eu.dnetlib.data.claims.utils;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import org.apache.log4j.Logger;
|
5
|
|
6
|
import java.io.BufferedReader;
|
7
|
import java.io.InputStreamReader;
|
8
|
import java.io.StringReader;
|
9
|
import java.net.HttpURLConnection;
|
10
|
import java.net.URL;
|
11
|
import java.util.List;
|
12
|
|
13
|
public class ManagerUtils {
|
14
|
private static String registryAPIForManagerEmails;
|
15
|
private String id;
|
16
|
private String email;
|
17
|
private String name;
|
18
|
private String memberSince;
|
19
|
|
20
|
private final static Logger log = Logger.getLogger(ManagerUtils.class);
|
21
|
|
22
|
public String getRegistryAPIForManagerEmails() { return registryAPIForManagerEmails; }
|
23
|
public void setRegistryAPIForManagerEmails(String registryAPIForManagerEmails) { ManagerUtils.registryAPIForManagerEmails = registryAPIForManagerEmails; }
|
24
|
|
25
|
public String getName() {
|
26
|
return name;
|
27
|
}
|
28
|
public void setName(String name) {
|
29
|
this.name = name;
|
30
|
}
|
31
|
|
32
|
public String getId() {
|
33
|
return id;
|
34
|
}
|
35
|
|
36
|
public void setId(String id) {
|
37
|
this.id = id;
|
38
|
}
|
39
|
|
40
|
public String getEmail() {
|
41
|
return email;
|
42
|
}
|
43
|
|
44
|
public void setEmail(String email) {
|
45
|
this.email = email;
|
46
|
}
|
47
|
|
48
|
public String getMemberSince() {
|
49
|
return memberSince;
|
50
|
}
|
51
|
|
52
|
public void setMemberSince(String memberSince) {
|
53
|
this.memberSince = memberSince;
|
54
|
}
|
55
|
|
56
|
public List<ManagerUtils> getManagersByEmail(String community) {
|
57
|
String url = registryAPIForManagerEmails.replace("{community}", community);
|
58
|
URL obj = null;
|
59
|
String responseStr = null;
|
60
|
log.debug("Registry url is " + url);
|
61
|
|
62
|
try {
|
63
|
obj = new URL(url);
|
64
|
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
65
|
log.debug("Get manager emails from registry response code is: " + con.getResponseCode());
|
66
|
if (con.getResponseCode() != 200) {
|
67
|
return null;
|
68
|
}
|
69
|
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
70
|
StringBuffer response = new StringBuffer();
|
71
|
String inputLine;
|
72
|
while ((inputLine = in.readLine()) != null) {
|
73
|
response.append(inputLine).append("\n");
|
74
|
}
|
75
|
in.close();
|
76
|
responseStr = response.toString();
|
77
|
} catch (Exception e) {
|
78
|
log.error("An error occured while trying to fetch manager emails from registry ", e);
|
79
|
return null;
|
80
|
}
|
81
|
return json2ManagerUtils(responseStr);
|
82
|
}
|
83
|
|
84
|
class ManagersResponse {
|
85
|
public List<ManagerUtils> response;
|
86
|
|
87
|
public List<ManagerUtils> getResponse() {
|
88
|
return response;
|
89
|
}
|
90
|
|
91
|
public void setResponse(List<ManagerUtils> response) {
|
92
|
this.response = response;
|
93
|
}
|
94
|
}
|
95
|
|
96
|
private List<ManagerUtils> json2ManagerUtils(String json){
|
97
|
|
98
|
log.debug("Try to create List<ManagerUtils> from json: "+json);
|
99
|
if (json == null){
|
100
|
return null;
|
101
|
}
|
102
|
|
103
|
BufferedReader br = new BufferedReader(new StringReader(json));
|
104
|
//convert the json string back to object
|
105
|
Gson gson = new Gson();
|
106
|
ManagersResponse managersResponse = null;
|
107
|
List<ManagerUtils> managerUtilsList = null;
|
108
|
|
109
|
try {
|
110
|
managersResponse = gson.fromJson(br, ManagersResponse.class);
|
111
|
managerUtilsList = managersResponse.getResponse();
|
112
|
}catch(Exception e){
|
113
|
log.debug("Error in parsing json response. Given json is : "+json, e);
|
114
|
return null;
|
115
|
}
|
116
|
|
117
|
log.debug("Original response.........: "+managersResponse.toString());
|
118
|
|
119
|
|
120
|
|
121
|
return managerUtilsList;
|
122
|
}
|
123
|
|
124
|
public Boolean isCommunityManager(String communityId, String userMail) {
|
125
|
List<ManagerUtils> managerUtils = this.getManagersByEmail(communityId);
|
126
|
if(managerUtils != null) {
|
127
|
for(ManagerUtils manager : managerUtils) {
|
128
|
if(manager.getEmail().equals(userMail)) {
|
129
|
return true;
|
130
|
}
|
131
|
}
|
132
|
}
|
133
|
return false;
|
134
|
}
|
135
|
}
|