Project

General

Profile

Download (2.76 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
    public String request() {
27

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

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

    
45
            if (conn.getResponseCode() != 200) {
46
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
47
            }
48
            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
49

    
50
            StringBuilder  builder = new StringBuilder();
51
            String line;
52
            while ((line = br.readLine()) != null) {
53
                builder.append(line);
54
            }
55
            conn.disconnect();
56
            return builder.toString();
57

    
58
        } catch (IOException e) {
59
            e.printStackTrace();
60
        } catch (URISyntaxException e) {
61
            e.printStackTrace();
62
        }
63
        return null;
64
    }
65

    
66
    protected abstract String getServiceUri();
67
    protected abstract String getTerminologyId();
68
    protected abstract List<RequestParameter> getServiceParameters();
69

    
70
    protected class RequestParameter{
71
        String key;
72
        String value;
73
        public RequestParameter(String key, String value) {
74
            super();
75
            this.key = key;
76
            this.value = value;
77
        }
78
        public String getKey() {
79
            return key;
80
        }
81
        public String getValue() {
82
            return value;
83
        }
84

    
85
    }
86
}
(1-1/8)