Project

General

Profile

Download (2.99 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.taxeditor.view.webimport.termimport.requests;
10

    
11
import java.io.BufferedReader;
12
import java.io.IOException;
13
import java.io.InputStreamReader;
14
import java.net.HttpURLConnection;
15
import java.net.URI;
16
import java.net.URISyntaxException;
17
import java.net.URL;
18
import java.net.URLEncoder;
19
import java.util.List;
20

    
21
/**
22
 * @author pplitzner
23
 * @since Apr 23, 2018
24
 *
25
 */
26
public abstract class AbstractTerminologyServiceRequest {
27

    
28
    private static final int TIMEOUT = 10000;
29

    
30
    public String request() {
31

    
32
        try {
33
            String queryString = "format=json&";
34
            String pathString = "/api/terminologies/"
35
                    + (getTerminologyId()!=null?getTerminologyId()+"/":"")
36
                    + (getServiceUri()!=null?getServiceUri()+"/":"");
37

    
38
            List<RequestParameter> serviceParameters = getServiceParameters();
39
            if(serviceParameters!=null){
40
                StringBuilder params = new StringBuilder();
41
                serviceParameters.forEach(param->params.append(param.key+"="+param.value+"&"));
42
                queryString += params.toString();
43
            }
44
            queryString = URLEncoder.encode(queryString, "UTF-8");
45
            URI uri = new URI("https", "dev-gfbio.bgbm.org", pathString, queryString, null);
46
            URL url = uri.toURL();
47
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
48
            conn.setRequestProperty("Accept", "application/json");
49
            conn.setConnectTimeout(TIMEOUT*6);
50
            conn.setReadTimeout(TIMEOUT*6);
51

    
52
            if (conn.getResponseCode() != 200) {
53
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
54
            }
55
            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
56

    
57
            StringBuilder  builder = new StringBuilder();
58
            String line;
59
            while ((line = br.readLine()) != null) {
60
                builder.append(line);
61
            }
62
            conn.disconnect();
63
            return builder.toString();
64

    
65
        } catch (IOException e) {
66
            e.printStackTrace();
67
        } catch (URISyntaxException e) {
68
            e.printStackTrace();
69
        }
70
        return null;
71
    }
72

    
73
    protected abstract String getServiceUri();
74
    protected abstract String getTerminologyId();
75
    protected abstract List<RequestParameter> getServiceParameters();
76

    
77
    protected class RequestParameter{
78
        String key;
79
        String value;
80
        public RequestParameter(String key, String value) {
81
            super();
82
            this.key = key;
83
            this.value = value;
84
        }
85
        public String getKey() {
86
            return key;
87
        }
88
        public String getValue() {
89
            return value;
90
        }
91

    
92
    }
93
}
(1-1/9)