Project

General

Profile

Download (3.06 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.util.List;
19

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

    
27
    @SuppressWarnings("unused")
28
    private static final String TERM_SERVICE_DEV = "dev-gfbio.bgbm.org";
29
    private static final String TERM_SERVICE = "terminologies.gfbio.org";
30
    private static final int TIMEOUT = 10000;
31

    
32
    public String request() {
33

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

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

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

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

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

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

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

    
93
    }
94
}
(1-1/9)