cleanup
[cdmlib.git] / cdmlib-ext / src / main / java / eu / etaxonomy / cdm / ext / common / ServiceWrapperBase.java
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.URISyntaxException;
15 import java.net.URL;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
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;
27
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;
32
33 /**
34 * @author a.kohlbecker
35 * @since 24.08.2010
36 */
37 public class ServiceWrapperBase<T extends CdmBase> {
38
39 private static final Logger logger = LogManager.getLogger();
40
41 private URL baseUrl;
42
43 protected Map<String, SchemaAdapterBase<T>> schemaAdapterMap;
44
45 public void setBaseUrl(String baseUrl) {
46 try {
47 this.baseUrl = new URL(baseUrl);
48 } catch (MalformedURLException e) {
49 logger.error(e);
50 }
51 }
52 public String getBaseUrl() {
53 return baseUrl.toString();
54 }
55
56 public void setSchemaAdapterMap(Map<String, SchemaAdapterBase<T>> schemaAdapterMap) {
57 this.schemaAdapterMap = schemaAdapterMap;
58 }
59
60 public void addSchemaAdapter(SchemaAdapterBase schemaAdapter){
61 if(schemaAdapterMap == null){
62 schemaAdapterMap = new HashMap<>();
63 }
64 schemaAdapterMap.put(schemaAdapter.getShortName(), schemaAdapter);
65 }
66
67 public Map<String, SchemaAdapterBase<T>> getSchemaAdapterMap() {
68 return schemaAdapterMap;
69 }
70
71 /**
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
77 * @throws IOException
78 */
79 protected InputStream executeHttpGet(URI uri, Map<String, String> requestHeaders) throws ClientProtocolException, IOException{
80 return executeHttp(uri, requestHeaders, HttpMethod.GET, null);
81 }
82
83 /**
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
90 * @throws IOException
91 */
92 protected InputStream executeHttpPost(URI uri, Map<String, String> requestHeaders, HttpEntity httpEntity) throws ClientProtocolException, IOException{
93 return executeHttp(uri, requestHeaders, HttpMethod.POST, httpEntity);
94 }
95
96 /**
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
104 */
105 private InputStream executeHttp(URI uri, Map<String, String> requestHeaders, HttpMethod httpMethod, HttpEntity entity) throws IOException, ClientProtocolException {
106 logger.debug("sending "+httpMethod+" request: " + uri);
107
108 HttpResponse response = UriUtils.getResponseByType(uri, requestHeaders, httpMethod, entity);
109
110 if(UriUtils.isOk(response)){
111 InputStream stream = response.getEntity().getContent();
112 return stream;
113 } else {
114 logger.error("HTTP Reponse code is not = 200 (OK): " + UriUtils.getStatus(response));
115 return null;
116 }
117 }
118
119 /**
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
124 */
125 public static void addNameValuePairTo(List<NameValuePair> listOfPairs, String name, String value){
126 if(value != null){
127 listOfPairs.add(new BasicNameValuePair(name, value));
128 }
129 }
130
131 /**
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())
136 */
137 public static void addNameValuePairTo(List<NameValuePair> listOfPairs, String name, Object value){
138 if(value != null){
139 listOfPairs.add(new BasicNameValuePair(name, value.toString()));
140 }
141 }
142
143 /**
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
149 */
150 protected URI createUri(String subPath, List<NameValuePair> qparams) throws URISyntaxException {
151
152 return UriUtils.createUri(baseUrl, subPath, qparams, null);
153
154 // String path = baseUrl.getPath();
155 // if(subPath != null){
156 // if(!path.endsWith("/")){
157 // path += "/";
158 // }
159 // if(subPath.startsWith("/")){
160 // subPath = subPath.substring(1);
161 // }
162 // path += subPath;
163 // }
164 //
165 // URI uri = URIUtils.createURI(baseUrl.getProtocol(),
166 // baseUrl.getHost(), baseUrl.getPort(), path, URLEncodedUtils.format(qparams, "UTF-8"), null);
167 //
168 // return uri;
169 }
170 }