1
|
/**
|
2
|
* Copyright (C) 2009 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.cdm.ext.common;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.io.InputStream;
|
13
|
import java.net.MalformedURLException;
|
14
|
import java.net.URI;
|
15
|
import java.net.URISyntaxException;
|
16
|
import java.net.URL;
|
17
|
import java.util.HashMap;
|
18
|
import java.util.List;
|
19
|
import java.util.Map;
|
20
|
|
21
|
import org.apache.http.HttpEntity;
|
22
|
import org.apache.http.HttpResponse;
|
23
|
import org.apache.http.NameValuePair;
|
24
|
import org.apache.http.client.ClientProtocolException;
|
25
|
import org.apache.http.message.BasicNameValuePair;
|
26
|
import org.apache.log4j.Logger;
|
27
|
|
28
|
import eu.etaxonomy.cdm.common.UriUtils;
|
29
|
import eu.etaxonomy.cdm.common.UriUtils.HttpMethod;
|
30
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
31
|
|
32
|
/**
|
33
|
* @author a.kohlbecker
|
34
|
* @since 24.08.2010
|
35
|
*/
|
36
|
public class ServiceWrapperBase<T extends CdmBase> {
|
37
|
|
38
|
public static final Logger logger = Logger.getLogger(ServiceWrapperBase.class);
|
39
|
|
40
|
private URL baseUrl;
|
41
|
|
42
|
private final boolean followRedirects = true;
|
43
|
|
44
|
protected Map<String, SchemaAdapterBase<T>> schemaAdapterMap;
|
45
|
|
46
|
/**
|
47
|
* @param baseUrl the baseUrl to set
|
48
|
*/
|
49
|
public void setBaseUrl(String baseUrl) {
|
50
|
try {
|
51
|
this.baseUrl = new URL(baseUrl);
|
52
|
} catch (MalformedURLException e) {
|
53
|
logger.error(e);
|
54
|
}
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* @return the baseUrl
|
59
|
*/
|
60
|
public String getBaseUrl() {
|
61
|
return baseUrl.toString();
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* @param schemaAdapterMap the schemaAdapterMap to set
|
66
|
*/
|
67
|
public void setSchemaAdapterMap(Map<String, SchemaAdapterBase<T>> schemaAdapterMap) {
|
68
|
this.schemaAdapterMap = schemaAdapterMap;
|
69
|
}
|
70
|
|
71
|
public void addSchemaAdapter(SchemaAdapterBase schemaAdapter){
|
72
|
if(schemaAdapterMap == null){
|
73
|
schemaAdapterMap = new HashMap<>();
|
74
|
}
|
75
|
schemaAdapterMap.put(schemaAdapter.getShortName(), schemaAdapter);
|
76
|
}
|
77
|
|
78
|
public Map<String, SchemaAdapterBase<T>> getSchemaAdapterMap() {
|
79
|
return schemaAdapterMap;
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Send an HTTP GET request to the given URI.
|
84
|
* @param uri the URI of this HTTP request
|
85
|
* @param requestHeaders the parameters (name-value pairs) of the connection added to the header of the request
|
86
|
* @return the response as an {@link InputStream}
|
87
|
* @throws ClientProtocolException
|
88
|
* @throws IOException
|
89
|
*/
|
90
|
protected InputStream executeHttpGet(URI uri, Map<String, String> requestHeaders) throws ClientProtocolException, IOException{
|
91
|
return executeHttp(uri, requestHeaders, HttpMethod.GET, null);
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* Send an HTTP POST request to the given URI.
|
96
|
* @param uri the URI of this HTTP request
|
97
|
* @param requestHeaders the parameters (name-value pairs) of the connection added to the header of the request
|
98
|
* @param entity the {@link HttpEntity} attached to a HTTP POST request
|
99
|
* @return the response as an {@link InputStream}
|
100
|
* @throws ClientProtocolException
|
101
|
* @throws IOException
|
102
|
*/
|
103
|
protected InputStream executeHttpPost(URI uri, Map<String, String> requestHeaders, HttpEntity httpEntity) throws ClientProtocolException, IOException{
|
104
|
return executeHttp(uri, requestHeaders, HttpMethod.POST, httpEntity);
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* @param uri the URI of this HTTP request
|
109
|
* @param requestHeaders the parameters (name-value pairs) of the connection added to the header of the request
|
110
|
* @param httpMethod defines if method is POST or GET
|
111
|
* @param entity the {@link HttpEntity} attached to a HTTP POST request
|
112
|
* @return the response as an {@link InputStream}
|
113
|
* @throws IOException
|
114
|
* @throws ClientProtocolException
|
115
|
*/
|
116
|
private InputStream executeHttp(URI uri, Map<String, String> requestHeaders, HttpMethod httpMethod, HttpEntity entity) throws IOException, ClientProtocolException {
|
117
|
logger.debug("sending "+httpMethod+" request: " + uri);
|
118
|
|
119
|
HttpResponse response = UriUtils.getResponseByType(uri, requestHeaders, httpMethod, entity);
|
120
|
|
121
|
if(UriUtils.isOk(response)){
|
122
|
InputStream stream = response.getEntity().getContent();
|
123
|
return stream;
|
124
|
} else {
|
125
|
logger.error("HTTP Reponse code is not = 200 (OK): " + UriUtils.getStatus(response));
|
126
|
return null;
|
127
|
}
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* Adds a {@link BasicNameValuePair} to the given {@link List}.
|
132
|
* @param listOfPairs the list to add the name-value pair to
|
133
|
* @param name the name
|
134
|
* @param value the value
|
135
|
*/
|
136
|
public static void addNameValuePairTo(List<NameValuePair> listOfPairs, String name, String value){
|
137
|
if(value != null){
|
138
|
listOfPairs.add(new BasicNameValuePair(name, value));
|
139
|
}
|
140
|
}
|
141
|
|
142
|
/**
|
143
|
* Adds a {@link BasicNameValuePair} to the given {@link List}.
|
144
|
* @param listOfPairs the list to add the name-value pair to
|
145
|
* @param name the name
|
146
|
* @param value the String representation of the object (toString())
|
147
|
*/
|
148
|
public static void addNameValuePairTo(List<NameValuePair> listOfPairs, String name, Object value){
|
149
|
if(value != null){
|
150
|
listOfPairs.add(new BasicNameValuePair(name, value.toString()));
|
151
|
}
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* Creates a {@link URI} based on the {@link ServiceWrapperBase#baseUrl} and the given subPath and qParams
|
156
|
* @param subPath the sub path of the URI to be created
|
157
|
* @param qparams the parameters added as GET parameters to the URI
|
158
|
* @return a URI consisting of the baseURL, the subPath and qParams
|
159
|
* @throws URISyntaxException
|
160
|
*/
|
161
|
protected URI createUri(String subPath, List<NameValuePair> qparams) throws URISyntaxException {
|
162
|
|
163
|
return UriUtils.createUri(baseUrl, subPath, qparams, null);
|
164
|
|
165
|
// String path = baseUrl.getPath();
|
166
|
// if(subPath != null){
|
167
|
// if(!path.endsWith("/")){
|
168
|
// path += "/";
|
169
|
// }
|
170
|
// if(subPath.startsWith("/")){
|
171
|
// subPath = subPath.substring(1);
|
172
|
// }
|
173
|
// path += subPath;
|
174
|
// }
|
175
|
//
|
176
|
// URI uri = URIUtils.createURI(baseUrl.getProtocol(),
|
177
|
// baseUrl.getHost(), baseUrl.getPort(), path, URLEncodedUtils.format(qparams, "UTF-8"), null);
|
178
|
//
|
179
|
// return uri;
|
180
|
}
|
181
|
}
|