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