3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.ext
.common
;
12 import java
.io
.IOException
;
13 import java
.io
.InputStream
;
14 import java
.net
.MalformedURLException
;
16 import java
.net
.URISyntaxException
;
18 import java
.util
.HashMap
;
19 import java
.util
.List
;
22 import org
.apache
.http
.HttpEntity
;
23 import org
.apache
.http
.HttpResponse
;
24 import org
.apache
.http
.NameValuePair
;
25 import org
.apache
.http
.client
.ClientProtocolException
;
26 import org
.apache
.http
.message
.BasicNameValuePair
;
27 import org
.apache
.log4j
.Logger
;
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
38 public class ServiceWrapperBase
<T
extends CdmBase
> {
40 public static final Logger logger
= Logger
.getLogger(ServiceWrapperBase
.class);
44 private final boolean followRedirects
= true;
46 protected Map
<String
, SchemaAdapterBase
<T
>> schemaAdapterMap
;
49 * @param baseUrl the baseUrl to set
51 public void setBaseUrl(String baseUrl
) {
53 this.baseUrl
= new URL(baseUrl
);
54 } catch (MalformedURLException e
) {
62 public String
getBaseUrl() {
63 return baseUrl
.toString();
67 * @param schemaAdapterMap the schemaAdapterMap to set
69 public void setSchemaAdapterMap(Map
<String
, SchemaAdapterBase
<T
>> schemaAdapterMap
) {
70 this.schemaAdapterMap
= schemaAdapterMap
;
73 public void addSchemaAdapter(SchemaAdapterBase schemaAdapter
){
74 if(schemaAdapterMap
== null){
75 schemaAdapterMap
= new HashMap
<String
, SchemaAdapterBase
<T
>>();
77 schemaAdapterMap
.put(schemaAdapter
.getShortName(), schemaAdapter
);
82 * @return the schemaAdapterMap
84 public Map
<String
, SchemaAdapterBase
<T
>> getSchemaAdapterMap() {
85 return schemaAdapterMap
;
89 * Send an HTTP GET request to the given URI.
90 * @param uri the URI of this HTTP request
91 * @param requestHeaders the parameters (name-value pairs) of the connection added to the header of the request
92 * @return the response as an {@link InputStream}
93 * @throws ClientProtocolException
96 protected InputStream
executeHttpGet(URI uri
, Map
<String
, String
> requestHeaders
) throws ClientProtocolException
, IOException
{
97 return executeHttp(uri
, requestHeaders
, HttpMethod
.GET
, null);
101 * Send an HTTP POST request to the given URI.
102 * @param uri the URI of this HTTP request
103 * @param requestHeaders the parameters (name-value pairs) of the connection added to the header of the request
104 * @param entity the {@link HttpEntity} attached to a HTTP POST request
105 * @return the response as an {@link InputStream}
106 * @throws ClientProtocolException
107 * @throws IOException
109 protected InputStream
executeHttpPost(URI uri
, Map
<String
, String
> requestHeaders
, HttpEntity httpEntity
) throws ClientProtocolException
, IOException
{
110 return executeHttp(uri
, requestHeaders
, HttpMethod
.POST
, httpEntity
);
114 * @param uri the URI of this HTTP request
115 * @param requestHeaders the parameters (name-value pairs) of the connection added to the header of the request
116 * @param httpMethod defines if method is POST or GET
117 * @param entity the {@link HttpEntity} attached to a HTTP POST request
118 * @return the response as an {@link InputStream}
119 * @throws IOException
120 * @throws ClientProtocolException
122 private InputStream
executeHttp(URI uri
, Map
<String
, String
> requestHeaders
, HttpMethod httpMethod
, HttpEntity entity
) throws IOException
, ClientProtocolException
{
123 logger
.debug("sending "+httpMethod
+" request: " + uri
);
125 HttpResponse response
= UriUtils
.getResponseByType(uri
, requestHeaders
, httpMethod
, entity
);
127 if(UriUtils
.isOk(response
)){
128 InputStream stream
= response
.getEntity().getContent();
131 logger
.error("HTTP Reponse code is not = 200 (OK): " + UriUtils
.getStatus(response
));
137 * Adds a {@link BasicNameValuePair} to the given {@link List}.
138 * @param listOfPairs the list to add the name-value pair to
139 * @param name the name
140 * @param value the value
142 public static void addNameValuePairTo(List
<NameValuePair
> listOfPairs
, String name
, String value
){
144 listOfPairs
.add(new BasicNameValuePair(name
, value
));
149 * Adds a {@link BasicNameValuePair} to the given {@link List}.
150 * @param listOfPairs the list to add the name-value pair to
151 * @param name the name
152 * @param value the String representation of the object (toString())
154 public static void addNameValuePairTo(List
<NameValuePair
> listOfPairs
, String name
, Object value
){
156 listOfPairs
.add(new BasicNameValuePair(name
, value
.toString()));
162 * Creates a {@link URI} based on the {@link ServiceWrapperBase#baseUrl} and the given subPath and qParams
163 * @param subPath the sub path of the URI to be created
164 * @param qparams the parameters added as GET parameters to the URI
165 * @return a URI consisting of the baseURL, the subPath and qParams
166 * @throws URISyntaxException
168 protected URI
createUri(String subPath
, List
<NameValuePair
> qparams
) throws URISyntaxException
{
170 return UriUtils
.createUri(baseUrl
, subPath
, qparams
, null);
172 // String path = baseUrl.getPath();
173 // if(subPath != null){
174 // if(!path.endsWith("/")){
177 // if(subPath.startsWith("/")){
178 // subPath = subPath.substring(1);
183 // URI uri = URIUtils.createURI(baseUrl.getProtocol(),
184 // baseUrl.getHost(), baseUrl.getPort(), path, URLEncodedUtils.format(qparams, "UTF-8"), null);