Project

General

Profile

Download (6.15 KB) Statistics
| Branch: | Tag: | Revision:
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
	public static final Logger logger = LogManager.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<>();
75
		}
76
		schemaAdapterMap.put(schemaAdapter.getShortName(), schemaAdapter);
77
	}
78

    
79
	public Map<String, SchemaAdapterBase<T>> getSchemaAdapterMap() {
80
		return schemaAdapterMap;
81
	}
82

    
83
    /**
84
     * Send an HTTP GET 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
     * @return the response as an {@link InputStream}
88
     * @throws ClientProtocolException
89
     * @throws IOException
90
     */
91
	protected InputStream executeHttpGet(URI uri, Map<String, String> requestHeaders) throws ClientProtocolException, IOException{
92
        return executeHttp(uri, requestHeaders, HttpMethod.GET, null);
93
	}
94

    
95
	/**
96
	 * Send an HTTP POST request to the given URI.
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 entity the {@link HttpEntity} attached to a HTTP POST request
100
     * @return the response as an {@link InputStream}
101
	 * @throws ClientProtocolException
102
	 * @throws IOException
103
	 */
104
	protected InputStream executeHttpPost(URI uri, Map<String, String> requestHeaders, HttpEntity httpEntity) throws ClientProtocolException, IOException{
105
	    return executeHttp(uri, requestHeaders, HttpMethod.POST, httpEntity);
106
	}
107

    
108
    /**
109
     * @param uri the URI of this HTTP request
110
     * @param requestHeaders the parameters (name-value pairs) of the connection added to the header of the request
111
     * @param httpMethod defines if method is POST or GET
112
     * @param entity the {@link HttpEntity} attached to a HTTP POST request
113
     * @return the response as an {@link InputStream}
114
     * @throws IOException
115
     * @throws ClientProtocolException
116
     */
117
    private InputStream executeHttp(URI uri, Map<String, String> requestHeaders, HttpMethod httpMethod, HttpEntity entity) throws IOException, ClientProtocolException {
118
        logger.debug("sending "+httpMethod+" request: " + uri);
119

    
120
	    HttpResponse response = UriUtils.getResponseByType(uri, requestHeaders, httpMethod, entity);
121

    
122
	    if(UriUtils.isOk(response)){
123
	        InputStream stream = response.getEntity().getContent();
124
	        return stream;
125
	    } else {
126
	        logger.error("HTTP Reponse code is not = 200 (OK): " + UriUtils.getStatus(response));
127
	        return null;
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 value
136
     */
137
	public static void addNameValuePairTo(List<NameValuePair> listOfPairs, String name, String value){
138
		if(value != null){
139
		    listOfPairs.add(new BasicNameValuePair(name, value));
140
		}
141
	}
142

    
143
	/**
144
     * Adds a {@link BasicNameValuePair} to the given {@link List}.
145
     * @param listOfPairs the list to add the name-value pair to
146
     * @param name the name
147
     * @param value the String representation of the object (toString())
148
     */
149
	public static void addNameValuePairTo(List<NameValuePair> listOfPairs, String name, Object value){
150
		if(value != null){
151
		    listOfPairs.add(new BasicNameValuePair(name, value.toString()));
152
		}
153
	}
154

    
155
	/**
156
	 * Creates a {@link URI} based on the {@link ServiceWrapperBase#baseUrl} and the given subPath and qParams
157
	 * @param subPath the sub path of the URI to be created
158
	 * @param qparams the parameters added as GET parameters to the URI
159
	 * @return a URI consisting of the baseURL, the subPath and qParams
160
	 * @throws URISyntaxException
161
	 */
162
	protected URI createUri(String subPath, List<NameValuePair> qparams) throws	URISyntaxException {
163

    
164
		return UriUtils.createUri(baseUrl, subPath, qparams, null);
165

    
166
//		String path = baseUrl.getPath();
167
//		if(subPath != null){
168
//			if(!path.endsWith("/")){
169
//				path += "/";
170
//			}
171
//			if(subPath.startsWith("/")){
172
//				subPath = subPath.substring(1);
173
//			}
174
//			path += subPath;
175
//		}
176
//
177
//		URI uri = URIUtils.createURI(baseUrl.getProtocol(),
178
//				baseUrl.getHost(), baseUrl.getPort(), path, URLEncodedUtils.format(qparams, "UTF-8"), null);
179
//
180
//		return uri;
181
	}
182
}
(2-2/2)