Project

General

Profile

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.ArrayList;
12
import java.util.List;
13

    
14
public class CommunityUtils {
15
    private static String communityAPI;
16
    String name;
17
    List<String> managers = new ArrayList<String>();
18
    private final static Logger log = Logger.getLogger(CommunityUtils.class);
19

    
20
    public String getCommunityAPI() { return communityAPI; }
21
    public void setCommunityAPI(String communityAPI) { CommunityUtils.communityAPI = communityAPI; }
22

    
23
    public String getName() {
24
        return name;
25
    }
26
    public void setName(String name) {
27
        this.name = name;
28
    }
29

    
30
    public List<String> getManagers() {
31
        return managers;
32
    }
33
    public void setManagers(List<String> managers) {
34
        this.managers = managers;
35
    }
36

    
37
    public  CommunityUtils getCommunityInfo(String community) {
38
        String url = communityAPI + community;
39
        URL obj = null;
40
        String responseStr = null;
41
        log.debug("Community info url is " + url);
42

    
43
        try {
44
            obj = new URL(url);
45
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
46
            log.debug("User info response code is: " + con.getResponseCode());
47
            if (con.getResponseCode() != 200) {
48
                return null;
49
            }
50
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
51
            StringBuffer response = new StringBuffer();
52
            String inputLine;
53
            while ((inputLine = in.readLine()) != null) {
54
                response.append(inputLine).append("\n");
55
            }
56
            in.close();
57
            responseStr = response.toString();
58
        } catch (Exception e) {
59
            log.error("An error occured while trying to fetch user info ", e);
60
            return null;
61
        }
62
        return json2CommunityInfo(responseStr);
63
    }
64
    private CommunityUtils json2CommunityInfo(String json){
65

    
66
        log.debug("Try to create CommunityInfo class from json: "+json);
67
        if (json == null){
68
            return null;
69
        }
70

    
71
        BufferedReader br = new BufferedReader(new StringReader(json));
72
        //convert the json string back to object
73
        Gson gson = new Gson();
74
        CommunityUtils communityInfo = null;
75
        try {
76
            communityInfo = gson.fromJson(br, CommunityUtils.class);
77
        }catch(Exception e){
78
            log.debug("Error in parsing json response. Given json is : "+json, e);
79
            return null;
80
        }
81

    
82
        log.debug("Original response.........: "+communityInfo.toString());
83

    
84

    
85

    
86
        return communityInfo;
87
    }
88
    public Boolean isCommunityManager(String communityId, String userMail){
89
        CommunityUtils community = this.getCommunityInfo(communityId);
90
        return (community!=null && community.managers.indexOf(userMail) !=-1);
91
    }
92
}
(4-4/9)