2 * Copyright (C) 2009 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
.cdm
.ext
.common
;
11 import java
.io
.IOException
;
12 import java
.io
.InputStream
;
13 import java
.net
.MalformedURLException
;
14 import java
.net
.URISyntaxException
;
16 import java
.util
.HashMap
;
17 import java
.util
.List
;
20 import org
.apache
.http
.HttpEntity
;
21 import org
.apache
.http
.HttpResponse
;
22 import org
.apache
.http
.NameValuePair
;
23 import org
.apache
.http
.client
.ClientProtocolException
;
24 import org
.apache
.http
.message
.BasicNameValuePair
;
25 import org
.apache
.logging
.log4j
.LogManager
;
26 import org
.apache
.logging
.log4j
.Logger
;
28 import eu
.etaxonomy
.cdm
.common
.URI
;
29 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
30 import eu
.etaxonomy
.cdm
.common
.UriUtils
.HttpMethod
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
34 * @author a.kohlbecker
37 public class ServiceWrapperBase
<T
extends CdmBase
> {
39 private static final Logger logger
= LogManager
.getLogger();
43 protected Map
<String
, SchemaAdapterBase
<T
>> schemaAdapterMap
;
45 public void setBaseUrl(String baseUrl
) {
47 this.baseUrl
= new URL(baseUrl
);
48 } catch (MalformedURLException e
) {
52 public String
getBaseUrl() {
53 return baseUrl
.toString();
56 public void setSchemaAdapterMap(Map
<String
, SchemaAdapterBase
<T
>> schemaAdapterMap
) {
57 this.schemaAdapterMap
= schemaAdapterMap
;
60 public void addSchemaAdapter(SchemaAdapterBase schemaAdapter
){
61 if(schemaAdapterMap
== null){
62 schemaAdapterMap
= new HashMap
<>();
64 schemaAdapterMap
.put(schemaAdapter
.getShortName(), schemaAdapter
);
67 public Map
<String
, SchemaAdapterBase
<T
>> getSchemaAdapterMap() {
68 return schemaAdapterMap
;
72 * Send an HTTP GET request to the given URI.
73 * @param uri the URI of this HTTP request
74 * @param requestHeaders the parameters (name-value pairs) of the connection added to the header of the request
75 * @return the response as an {@link InputStream}
76 * @throws ClientProtocolException
79 protected InputStream
executeHttpGet(URI uri
, Map
<String
, String
> requestHeaders
) throws ClientProtocolException
, IOException
{
80 return executeHttp(uri
, requestHeaders
, HttpMethod
.GET
, null);
84 * Send an HTTP POST request to the given URI.
85 * @param uri the URI of this HTTP request
86 * @param requestHeaders the parameters (name-value pairs) of the connection added to the header of the request
87 * @param entity the {@link HttpEntity} attached to a HTTP POST request
88 * @return the response as an {@link InputStream}
89 * @throws ClientProtocolException
92 protected InputStream
executeHttpPost(URI uri
, Map
<String
, String
> requestHeaders
, HttpEntity httpEntity
) throws ClientProtocolException
, IOException
{
93 return executeHttp(uri
, requestHeaders
, HttpMethod
.POST
, httpEntity
);
97 * @param uri the URI of this HTTP request
98 * @param requestHeaders the parameters (name-value pairs) of the connection added to the header of the request
99 * @param httpMethod defines if method is POST or GET
100 * @param entity the {@link HttpEntity} attached to a HTTP POST request
101 * @return the response as an {@link InputStream}
102 * @throws IOException
103 * @throws ClientProtocolException
105 private InputStream
executeHttp(URI uri
, Map
<String
, String
> requestHeaders
, HttpMethod httpMethod
, HttpEntity entity
) throws IOException
, ClientProtocolException
{
106 logger
.debug("sending "+httpMethod
+" request: " + uri
);
108 HttpResponse response
= UriUtils
.getResponseByType(uri
, requestHeaders
, httpMethod
, entity
);
110 if(UriUtils
.isOk(response
)){
111 InputStream stream
= response
.getEntity().getContent();
114 logger
.error("HTTP Reponse code is not = 200 (OK): " + UriUtils
.getStatus(response
));
120 * Adds a {@link BasicNameValuePair} to the given {@link List}.
121 * @param listOfPairs the list to add the name-value pair to
122 * @param name the name
123 * @param value the value
125 public static void addNameValuePairTo(List
<NameValuePair
> listOfPairs
, String name
, String value
){
127 listOfPairs
.add(new BasicNameValuePair(name
, value
));
132 * Adds a {@link BasicNameValuePair} to the given {@link List}.
133 * @param listOfPairs the list to add the name-value pair to
134 * @param name the name
135 * @param value the String representation of the object (toString())
137 public static void addNameValuePairTo(List
<NameValuePair
> listOfPairs
, String name
, Object value
){
139 listOfPairs
.add(new BasicNameValuePair(name
, value
.toString()));
144 * Creates a {@link URI} based on the {@link ServiceWrapperBase#baseUrl} and the given subPath and qParams
145 * @param subPath the sub path of the URI to be created
146 * @param qparams the parameters added as GET parameters to the URI
147 * @return a URI consisting of the baseURL, the subPath and qParams
148 * @throws URISyntaxException
150 protected URI
createUri(String subPath
, List
<NameValuePair
> qparams
) throws URISyntaxException
{
152 return UriUtils
.createUri(baseUrl
, subPath
, qparams
, null);
154 // String path = baseUrl.getPath();
155 // if(subPath != null){
156 // if(!path.endsWith("/")){
159 // if(subPath.startsWith("/")){
160 // subPath = subPath.substring(1);
165 // URI uri = URIUtils.createURI(baseUrl.getProtocol(),
166 // baseUrl.getHost(), baseUrl.getPort(), path, URLEncodedUtils.format(qparams, "UTF-8"), null);