2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.view
.webimport
.termimport
.requests
;
11 import java
.io
.BufferedReader
;
12 import java
.io
.IOException
;
13 import java
.io
.InputStreamReader
;
14 import java
.net
.HttpURLConnection
;
16 import java
.net
.URISyntaxException
;
18 import java
.util
.List
;
24 public abstract class AbstractTerminologyServiceRequest
{
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;
31 public String
request() {
34 String queryString
= "format=json&";
35 String pathString
= "/api/terminologies/"
36 + (getTerminologyId()!=null?
getTerminologyId()+"/":"")
37 + (getServiceUri()!=null?
getServiceUri():"");
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();
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);
52 if (conn
.getResponseCode() != 200) {
53 throw new RuntimeException("Failed : HTTP error code : " + conn
.getResponseCode());
55 BufferedReader br
= new BufferedReader(new InputStreamReader((conn
.getInputStream())));
57 StringBuilder builder
= new StringBuilder();
59 while ((line
= br
.readLine()) != null) {
63 return builder
.toString();
65 } catch (IOException e
) {
67 } catch (URISyntaxException e
) {
73 protected abstract String
getServiceUri();
74 protected abstract String
getTerminologyId();
75 protected abstract List
<RequestParameter
> getServiceParameters();
77 protected class RequestParameter
{
80 public RequestParameter(String key
, String value
) {
85 public String
getKey() {
88 public String
getValue() {