Subversion Repositories SmartDukaan

Rev

Rev 23526 | Rev 23568 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21557 ashik.ali 1
package com.spice.profitmandi.common.web.client;
2
 
3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.io.InputStreamReader;
22215 ashik.ali 7
import java.io.UnsupportedEncodingException;
8
import java.util.ArrayList;
9
import java.util.List;
21557 ashik.ali 10
import java.util.Map;
11
import java.util.Set;
12
 
13
import org.apache.http.HttpResponse;
22215 ashik.ali 14
import org.apache.http.NameValuePair;
21557 ashik.ali 15
import org.apache.http.client.ClientProtocolException;
16
import org.apache.http.client.HttpClient;
22215 ashik.ali 17
import org.apache.http.client.entity.UrlEncodedFormEntity;
21557 ashik.ali 18
import org.apache.http.client.methods.HttpGet;
22215 ashik.ali 19
import org.apache.http.client.methods.HttpPost;
20
import org.apache.http.client.methods.HttpUriRequest;
23502 ashik.ali 21
import org.apache.http.conn.HttpHostConnectException;
22
import org.apache.http.entity.ContentType;
23
import org.apache.http.entity.StringEntity;
21557 ashik.ali 24
import org.apache.http.impl.client.HttpClients;
22215 ashik.ali 25
import org.apache.http.message.BasicNameValuePair;
21557 ashik.ali 26
import org.slf4j.Logger;
27
import org.slf4j.LoggerFactory;
28
import org.springframework.http.HttpStatus;
23526 ashik.ali 29
import org.springframework.stereotype.Component;
21557 ashik.ali 30
import org.springframework.web.util.UriComponentsBuilder;
31
 
32
import com.spice.profitmandi.common.ResponseCodeHolder;
33
import com.spice.profitmandi.common.enumuration.SchemeType;
34
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23502 ashik.ali 35
import com.spice.profitmandi.common.util.StringUtils;
21557 ashik.ali 36
 
23526 ashik.ali 37
@Component
21557 ashik.ali 38
public class RestClient {
39
 
40
	private static final Logger LOGGER = LoggerFactory.getLogger(RestClient.class);
41
 
42
	private HttpClient httpClient;
23526 ashik.ali 43
 
44
	public RestClient() {
21557 ashik.ali 45
		this.httpClient = HttpClients.createDefault();
46
	}
23526 ashik.ali 47
 
48
	public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params, Map<String, String> headers)
23502 ashik.ali 49
		throws ProfitMandiBusinessException, HttpHostConnectException{
23526 ashik.ali 50
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString() : scheme.getValue()  + hostName + ":" + port + "/" + uri;
23561 ashik.ali 51
		return this.get(url, params, headers);
52
	}
53
 
54
	public String get(String url, Map<String, String> params, Map<String, String> headers)
55
			throws ProfitMandiBusinessException, HttpHostConnectException{
23526 ashik.ali 56
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
21557 ashik.ali 57
		Set<String> keys = params.keySet();
58
		for(String key : keys){
22341 amit.gupta 59
			builder.queryParam(key, params.get(key));
21557 ashik.ali 60
		}
22345 amit.gupta 61
		HttpGet request = new HttpGet(builder.build().encode().toUri());
22215 ashik.ali 62
		for(Map.Entry<String, String> entry : headers.entrySet()){
22339 amit.gupta 63
			request.setHeader(entry.getKey(), entry.getValue());
22215 ashik.ali 64
		}
65
		return this.execute(request);
66
	}
22233 amit.gupta 67
 
23526 ashik.ali 68
	public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params)
23502 ashik.ali 69
			throws ProfitMandiBusinessException, HttpHostConnectException{
23526 ashik.ali 70
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString() : scheme.getValue()  + hostName + ":" + port + "/" + uri;
71
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
72
		if(params != null){
23509 amit.gupta 73
			Set<String> keys = params.keySet();
74
			for(String key : keys){
75
				builder.queryParam(key, params.get(key));
76
			}
22233 amit.gupta 77
		}
22343 amit.gupta 78
		HttpGet request = new HttpGet(builder.build().encode().toUri());
22233 amit.gupta 79
		return this.execute(request);
80
	}
22215 ashik.ali 81
 
82
	public String execute(HttpUriRequest request)
23502 ashik.ali 83
			throws ProfitMandiBusinessException, HttpHostConnectException{
22215 ashik.ali 84
		LOGGER.info("Connecting to server at url {}",request.getURI());
21557 ashik.ali 85
		try {
86
			HttpResponse response = httpClient.execute(request);
87
			String responseString = this.toString(response.getEntity().getContent());
22877 amit.gupta 88
			LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
21557 ashik.ali 89
			if(response.getStatusLine().getStatusCode() == HttpStatus.OK.value()){
90
				return responseString;
91
			}else{
22215 ashik.ali 92
				throw new ProfitMandiBusinessException("", "", "GE_1005");
21557 ashik.ali 93
			}
23502 ashik.ali 94
		}catch(HttpHostConnectException httpHostConnectException) {
95
			LOGGER.error("Connection Timeout Exception", httpHostConnectException);
96
			throw httpHostConnectException;
21557 ashik.ali 97
		} catch (ClientProtocolException e) {
22215 ashik.ali 98
			LOGGER.error("Client Error : ",e);
99
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
21557 ashik.ali 100
		} catch (IOException e) {
22215 ashik.ali 101
			LOGGER.error("IO Error : ", e);
102
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
23502 ashik.ali 103
		} 
21557 ashik.ali 104
	}
22215 ashik.ali 105
 
23526 ashik.ali 106
	public String post(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params, Map<String, String> headers)
23502 ashik.ali 107
			throws ProfitMandiBusinessException, HttpHostConnectException{
23526 ashik.ali 108
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString() : scheme.getValue()  + hostName + ":" + port + "/" + uri;
23561 ashik.ali 109
		return this.post(url, params, headers);
110
	}
111
 
112
	public String post(String url, Map<String, String> params, Map<String, String> headers)
113
			throws ProfitMandiBusinessException, HttpHostConnectException{
23526 ashik.ali 114
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
22215 ashik.ali 115
		List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
116
		for(Map.Entry<String, String> entry : params.entrySet()){
117
			bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
118
		}
119
 
120
		LOGGER.info("Body Parameters {}", params);
22343 amit.gupta 121
		HttpPost request = new HttpPost(builder.build().encode().toUri());
22215 ashik.ali 122
		for(Map.Entry<String, String> entry : headers.entrySet()){
123
			request.setHeader(entry.getKey(), entry.getValue());
124
		}
125
 
126
		try{
127
			request.setEntity(new UrlEncodedFormEntity(bodyParameters));
128
		}catch (UnsupportedEncodingException unsupportedEncodingException) {
129
			LOGGER.error("Encoding error : ", unsupportedEncodingException);
130
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
131
		}
132
 
133
 
134
		return this.execute(request);
135
 
136
	}
137
 
23526 ashik.ali 138
	public String postJson(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params, Map<String, String> headers)
23502 ashik.ali 139
			throws ProfitMandiBusinessException, HttpHostConnectException{
23526 ashik.ali 140
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString() : scheme.getValue()  + hostName + ":" + port + "/" + uri;
23561 ashik.ali 141
		return this.postJson(url, params, headers);
142
	}
143
 
144
	public String postJson(String url, Map<String, String> params, Map<String, String> headers)
145
			throws ProfitMandiBusinessException, HttpHostConnectException{
23526 ashik.ali 146
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
23502 ashik.ali 147
		List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
148
		for(Map.Entry<String, String> entry : params.entrySet()){
149
			bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
150
		}
151
 
152
		String jsonString = null;
153
		try {
154
			jsonString = StringUtils.toString(params);
155
		} catch (Exception e) {
156
			e.printStackTrace();
157
		}
158
 
23561 ashik.ali 159
		StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
23502 ashik.ali 160
 
161
		LOGGER.info("Body {}", jsonString);
162
		HttpPost request = new HttpPost(builder.build().encode().toUri());
163
		for(Map.Entry<String, String> entry : headers.entrySet()){
164
			request.setHeader(entry.getKey(), entry.getValue());
165
		}
166
		request.setEntity(requestEntity);
167
		return this.execute(request);
168
	}
169
 
21557 ashik.ali 170
	private String toString(InputStream inputStream){
171
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
172
		StringBuilder responseString = new StringBuilder();
173
		String line = null;
174
		try {
175
			while((line = reader.readLine()) != null){
176
				responseString.append(line);
177
			}
178
			inputStream.close();
179
		} catch (IOException e) {
180
			throw new RuntimeException();
181
		}
182
		return responseString.toString();
183
	}
184
}