removing unnesscary default baselayers - finally deleting commented lines
[cdmlib.git] / cdmlib-ext / src / main / java / eu / etaxonomy / cdm / ext / common / ServiceWrapperBase.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.cdm.ext.common;
11
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.net.MalformedURLException;
15 import java.net.URI;
16 import java.net.URISyntaxException;
17 import java.net.URL;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
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;
28
29 import eu.etaxonomy.cdm.common.UriUtils;
30 import eu.etaxonomy.cdm.common.UriUtils.HttpMethod;
31 import eu.etaxonomy.cdm.model.common.CdmBase;
32
33 /**
34 * @author a.kohlbecker
35 * @date 24.08.2010
36 *
37 */
38 public class ServiceWrapperBase<T extends CdmBase> {
39
40 public static final Logger logger = Logger.getLogger(ServiceWrapperBase.class);
41
42 private URL baseUrl;
43
44 private final boolean followRedirects = true;
45
46 protected Map<String, SchemaAdapterBase<T>> schemaAdapterMap;
47
48 /**
49 * @param baseUrl the baseUrl to set
50 */
51 public void setBaseUrl(String baseUrl) {
52 try {
53 this.baseUrl = new URL(baseUrl);
54 } catch (MalformedURLException e) {
55 logger.error(e);
56 }
57 }
58
59 /**
60 * @return the baseUrl
61 */
62 public String getBaseUrl() {
63 return baseUrl.toString();
64 }
65
66 /**
67 * @param schemaAdapterMap the schemaAdapterMap to set
68 */
69 public void setSchemaAdapterMap(Map<String, SchemaAdapterBase<T>> schemaAdapterMap) {
70 this.schemaAdapterMap = schemaAdapterMap;
71 }
72
73 public void addSchemaAdapter(SchemaAdapterBase schemaAdapter){
74 if(schemaAdapterMap == null){
75 schemaAdapterMap = new HashMap<String, SchemaAdapterBase<T>>();
76 }
77 schemaAdapterMap.put(schemaAdapter.getShortName(), schemaAdapter);
78 }
79
80
81 /**
82 * @return the schemaAdapterMap
83 */
84 public Map<String, SchemaAdapterBase<T>> getSchemaAdapterMap() {
85 return schemaAdapterMap;
86 }
87
88 /**
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
94 * @throws IOException
95 */
96 protected InputStream executeHttpGet(URI uri, Map<String, String> requestHeaders) throws ClientProtocolException, IOException{
97 return executeHttp(uri, requestHeaders, HttpMethod.GET, null);
98 }
99
100 /**
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
108 */
109 protected InputStream executeHttpPost(URI uri, Map<String, String> requestHeaders, HttpEntity httpEntity) throws ClientProtocolException, IOException{
110 return executeHttp(uri, requestHeaders, HttpMethod.POST, httpEntity);
111 }
112
113 /**
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
121 */
122 private InputStream executeHttp(URI uri, Map<String, String> requestHeaders, HttpMethod httpMethod, HttpEntity entity) throws IOException, ClientProtocolException {
123 logger.debug("sending "+httpMethod+" request: " + uri);
124
125 HttpResponse response = UriUtils.getResponseByType(uri, requestHeaders, httpMethod, entity);
126
127 if(UriUtils.isOk(response)){
128 InputStream stream = response.getEntity().getContent();
129 return stream;
130 } else {
131 logger.error("HTTP Reponse code is not = 200 (OK): " + UriUtils.getStatus(response));
132 return null;
133 }
134 }
135
136 /**
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
141 */
142 public static void addNameValuePairTo(List<NameValuePair> listOfPairs, String name, String value){
143 if(value != null){
144 listOfPairs.add(new BasicNameValuePair(name, value));
145 }
146 }
147
148 /**
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())
153 */
154 public static void addNameValuePairTo(List<NameValuePair> listOfPairs, String name, Object value){
155 if(value != null){
156 listOfPairs.add(new BasicNameValuePair(name, value.toString()));
157 }
158 }
159
160
161 /**
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
167 */
168 protected URI createUri(String subPath, List<NameValuePair> qparams) throws URISyntaxException {
169
170 return UriUtils.createUri(baseUrl, subPath, qparams, null);
171
172 // String path = baseUrl.getPath();
173 // if(subPath != null){
174 // if(!path.endsWith("/")){
175 // path += "/";
176 // }
177 // if(subPath.startsWith("/")){
178 // subPath = subPath.substring(1);
179 // }
180 // path += subPath;
181 // }
182 //
183 // URI uri = URIUtils.createURI(baseUrl.getProtocol(),
184 // baseUrl.getHost(), baseUrl.getPort(), path, URLEncodedUtils.format(qparams, "UTF-8"), null);
185 //
186 // return uri;
187 }
188
189
190 }