Project

General

Profile

Download (2.9 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
    private static final int TIMEOUT = 10000;
28

    
29
    public String request() {
30

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

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

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

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

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

    
71
    protected abstract String getServiceUri();
72
    protected abstract String getTerminologyId();
73
    protected abstract List<RequestParameter> getServiceParameters();
74

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

    
90
    }
91
}
(1-1/9)