Project

General

Profile

1
package eu.dnetlib.data.statsmanager;
2

    
3

    
4
import org.apache.log4j.Logger;
5
import org.apache.tools.ant.util.Base64Converter;
6

    
7
import javax.net.ssl.HttpsURLConnection;
8
import java.io.BufferedReader;
9
import java.io.InputStreamReader;
10
import java.net.URL;
11
import java.net.URLConnection;
12
import java.util.HashMap;
13
import java.util.Map;
14

    
15

    
16
public class CacheController {
17

    
18
    private HashMap<String, URL> actions;
19

    
20
    private Logger log = Logger.getLogger(this.getClass());
21

    
22
    private String httpsCredentials;
23

    
24
    public CacheController() {
25
    }
26

    
27
    public void executeCommand(String action, Map<String, String> parameters) throws Exception {
28

    
29

    
30
        /*if (cacheURL != null && !cacheURL.isEmpty()) {
31
            log.debug("existing cache url " + this.getActions().get(action));
32
            String actionUrl = this.getActions().get(action).toString();
33
            actionUrl = actionUrl.substring(actionUrl.lastIndexOf("/"), actionUrl.length());
34
            url = new URL(cacheURL + actionUrl);
35
            log.info("creds for url " + url + "  " + credentials);
36
            executeRemoteScript(url, credentials);
37

    
38
        } else {
39
          */
40

    
41
        String urlString = this.actions.get(action).toString();
42
        for (Map.Entry<String, String> e : parameters.entrySet()) {
43
            if (!e.getKey().equals("cache")&&!e.getKey().equals("error")) {
44
                urlString += "&" + e.getKey() + "=" + e.getValue();
45

    
46

    
47
            }
48
        }
49
        // credentials = this.cacheCredInfo.get(url.toString().substring(0, url.toString().lastIndexOf("/")));
50
        executeRemoteScript(new URL(urlString));
51
    }
52

    
53
    private void executeRemoteScript(URL url) throws Exception {
54
        try {
55

    
56

    
57
            Base64Converter converter = new Base64Converter();
58
            BufferedReader in = null;
59

    
60
            if (url.toString().startsWith("https://")) {
61

    
62
                String encoding = converter.encode(httpsCredentials.getBytes("UTF-8"));
63

    
64
                log.debug("Using https : " + url.toString());
65
                HttpsURLConnection yc = (HttpsURLConnection) url.openConnection();
66
                yc.setRequestProperty("Authorization", String.format("Basic %s", encoding));
67
                in = new BufferedReader(
68
                        new InputStreamReader(
69
                                yc.getInputStream()));
70
            } else {
71
                  log.debug("Using normal url : " + url.toString());
72
                URLConnection yc = (URLConnection) url.openConnection();
73

    
74
                in
75
                        = new BufferedReader(
76
                        new InputStreamReader(
77
                                yc.getInputStream()));
78

    
79
            }
80

    
81
            String inputLine;
82

    
83
            while ((inputLine = in.readLine()) != null)
84
                log.debug(inputLine);
85

    
86

    
87
            in.close();
88

    
89

    
90
        } catch (Exception e) {
91
            log.error("Error while calling php script over http. Reason: " + e);
92
            throw new Exception(e);
93
        }
94
    }
95

    
96
    public HashMap<String, URL> getActions() {
97
        return actions;
98
    }
99

    
100
    public void setActions(HashMap<String, URL> actions) {
101
        this.actions = actions;
102
    }
103

    
104
    public CacheController(HashMap<String, URL> actions) {
105
        this.actions = actions;
106
    }
107

    
108

    
109
    public String getHttpsCredentials() {
110
        return httpsCredentials;
111
    }
112

    
113
    public void setHttpsCredentials(String httpsCredentials) {
114
        this.httpsCredentials = httpsCredentials;
115
    }
116
}
(1-1/8)