Merge branch 'release/5.19.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / webimport / termimport / requests / AbstractTerminologyServiceRequest.java
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 public abstract class AbstractTerminologyServiceRequest {
25
26 @SuppressWarnings("unused")
27 private static final String TERM_SERVICE_DEV = "dev-gfbio.bgbm.org";
28 private static final String TERM_SERVICE = "terminologies.gfbio.org";
29 private static final int TIMEOUT = 10000;
30
31 public String request() {
32
33 try {
34 String queryString = "format=json&";
35 String pathString = "/api/terminologies/"
36 + (getTerminologyId()!=null?getTerminologyId()+"/":"")
37 + (getServiceUri()!=null?getServiceUri():"");
38
39 List<RequestParameter> serviceParameters = getServiceParameters();
40 if(serviceParameters!=null){
41 StringBuilder params = new StringBuilder();
42 serviceParameters.forEach(param->params.append(param.key+"="+param.value+"&"));
43 queryString += params.toString();
44 }
45 URI uri = new URI("https", TERM_SERVICE, pathString, queryString, null);
46 URL url = uri.toURL();
47 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
48 conn.setRequestProperty("Accept", "application/json");
49 conn.setConnectTimeout(TIMEOUT*9);
50 conn.setReadTimeout(TIMEOUT*9);
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 }