Project

General

Profile

1
package eu.dnetlib.repo.manager.utils;
2

    
3
import com.google.gson.JsonElement;
4
import com.google.gson.JsonObject;
5
import com.google.gson.JsonParser;
6
import org.apache.commons.codec.binary.Base64;
7
import org.apache.log4j.Logger;
8
import org.springframework.beans.factory.annotation.Value;
9
import org.springframework.http.*;
10
import org.springframework.stereotype.Component;
11
import org.springframework.web.client.RestTemplate;
12

    
13
import java.nio.charset.Charset;
14
import java.util.Map;
15

    
16
@Component
17
public class HttpUtils {
18

    
19
    private static final Logger logger = Logger.getLogger(HttpUtils.class);
20

    
21
    //TODO: refactor
22
    // TODO: To be changed the values
23
//    @Value("https://aai.openaire.eu/registry/")
24
    @Value("https://openaire-dev.aai-dev.grnet.gr/registry/")
25
    private String issuer;
26

    
27
    @Value("kostis30fylloy")
28
    private String user;
29

    
30
    @Value("fynhWc7F*2y4me4U")
31
    private String password;
32

    
33
    public JsonElement post(String path, JsonObject body) {
34
        RestTemplate restTemplate = new RestTemplate();
35
        HttpHeaders headers = createHeaders(user, password);
36
        headers.setContentType(MediaType.APPLICATION_JSON);
37
        HttpEntity<String> request = new HttpEntity<>(body.toString(), headers);
38
        ResponseEntity<String> responseEntity = restTemplate.exchange(issuer + path, HttpMethod.POST, request, String.class);
39
        if (responseEntity.getBody() != null) {
40
            return new JsonParser().parse(responseEntity.getBody());
41
        } else {
42
            return null;
43
        }
44
    }
45

    
46
    public JsonElement put(String path, JsonObject body) {
47
        RestTemplate restTemplate = new RestTemplate();
48
        HttpHeaders headers = createHeaders(user, password);
49
        headers.setContentType(MediaType.APPLICATION_JSON);
50
        HttpEntity<String> request = new HttpEntity<>(body.toString(), headers);
51
        ResponseEntity<String> responseEntity = restTemplate.exchange(issuer + path, HttpMethod.PUT, request, String.class);
52
        if (responseEntity.getBody() != null) {
53
            return new JsonParser().parse(responseEntity.getBody());
54
        } else {
55
            return null;
56
        }
57
    }
58

    
59
    public JsonElement get(String path, Map<String, String> params) {
60
        RestTemplate restTemplate = new RestTemplate();
61
        String url = issuer + path + ((params != null) ? createParams(params) : null);
62
        ResponseEntity<String> responseEntity = restTemplate.exchange
63
                (url, HttpMethod.GET, new HttpEntity<>(createHeaders(user, password)), String.class);
64
        if (responseEntity.getBody() != null) {
65
            return new JsonParser().parse(responseEntity.getBody());
66
        } else {
67
            return null;
68
        }
69
    }
70

    
71
    public JsonElement delete(String path) {
72
        RestTemplate restTemplate = new RestTemplate();
73
        String url = issuer + path;
74
        ResponseEntity<String> responseEntity = restTemplate.exchange
75
                (url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(user, password)), String.class);
76
        if (responseEntity.getBody() != null) {
77
            return new JsonParser().parse(responseEntity.getBody());
78
        } else {
79
            return null;
80
        }
81
    }
82

    
83

    
84
    private String createParams(Map<String, String> params) {
85
        StringBuilder ret = new StringBuilder("?");
86
        int count = 0;
87
        for (Map.Entry<String, String> param : params.entrySet()) {
88
            ret.append(param.getKey()).append("=").append(param.getValue());
89
            count++;
90
            if (count != params.entrySet().size()) {
91
                ret.append("&");
92
            }
93
        }
94
        return ret.toString();
95
    }
96

    
97
    private HttpHeaders createHeaders(String username, String password) {
98
        return new HttpHeaders() {{
99
            String auth = username + ":" + password;
100
            byte[] encodedAuth = Base64.encodeBase64(
101
                    auth.getBytes(Charset.forName("US-ASCII")));
102
            String authHeader = "Basic " + new String(encodedAuth);
103
            set("Authorization", authHeader);
104
        }};
105
    }
106
}
(4-4/6)