Subversion Repositories SmartDukaan

Rev

Rev 23612 | Rev 25244 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 23612 Rev 25011
Line 21... Line 21...
21
import org.apache.http.conn.HttpHostConnectException;
21
import org.apache.http.conn.HttpHostConnectException;
22
import org.apache.http.entity.ContentType;
22
import org.apache.http.entity.ContentType;
23
import org.apache.http.entity.StringEntity;
23
import org.apache.http.entity.StringEntity;
24
import org.apache.http.impl.client.HttpClients;
24
import org.apache.http.impl.client.HttpClients;
25
import org.apache.http.message.BasicNameValuePair;
25
import org.apache.http.message.BasicNameValuePair;
26
import org.apache.logging.log4j.Logger;
-
 
27
import org.apache.logging.log4j.LogManager;
26
import org.apache.logging.log4j.LogManager;
-
 
27
import org.apache.logging.log4j.Logger;
28
import org.springframework.http.HttpStatus;
28
import org.springframework.http.HttpStatus;
29
import org.springframework.stereotype.Component;
29
import org.springframework.stereotype.Component;
30
import org.springframework.web.util.UriComponentsBuilder;
30
import org.springframework.web.util.UriComponentsBuilder;
31
 
31
 
32
import com.spice.profitmandi.common.ResponseCodeHolder;
32
import com.spice.profitmandi.common.ResponseCodeHolder;
Line 34... Line 34...
34
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
34
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
35
import com.spice.profitmandi.common.util.StringUtils;
35
import com.spice.profitmandi.common.util.StringUtils;
36
 
36
 
37
@Component
37
@Component
38
public class RestClient {
38
public class RestClient {
39
	
39
 
40
	private static final Logger LOGGER = LogManager.getLogger(RestClient.class);
40
	private static final Logger LOGGER = LogManager.getLogger(RestClient.class);
41
	
41
 
42
	private HttpClient httpClient;
42
	private HttpClient httpClient;
43
	
43
 
44
	public RestClient() {
44
	public RestClient() {
45
		this.httpClient = HttpClients.createDefault();
45
		this.httpClient = HttpClients.createDefault();
46
	}
46
	}
47
	
47
 
48
	public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params, Map<String, String> headers)
48
	public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
49
		throws ProfitMandiBusinessException, HttpHostConnectException{
49
			Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
50
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString() : scheme.getValue()  + hostName + ":" + port + "/" + uri;
50
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
-
 
51
				: scheme.getValue() + hostName + ":" + port + "/" + uri;
51
		return this.get(url, params, headers);
52
		return this.get(url, params, headers);
52
	}
53
	}
53
	
54
 
54
	public String get(String url, Map<String, String> params, Map<String, String> headers)
55
	public String get(String url, Map<String, String> params, Map<String, String> headers)
55
			throws ProfitMandiBusinessException, HttpHostConnectException{
56
			throws ProfitMandiBusinessException, HttpHostConnectException {
56
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
57
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
57
		Set<String> keys = params.keySet();
58
		Set<String> keys = params.keySet();
58
		for(String key : keys){
59
		for (String key : keys) {
59
			builder.queryParam(key, params.get(key));
60
			builder.queryParam(key, params.get(key));
60
		}
61
		}
61
		HttpGet request = new HttpGet(builder.build().encode().toUri());
62
		HttpGet request = new HttpGet(builder.build().encode().toUri());
62
		for(Map.Entry<String, String> entry : headers.entrySet()){
63
		for (Map.Entry<String, String> entry : headers.entrySet()) {
63
			request.setHeader(entry.getKey(), entry.getValue());
64
			request.setHeader(entry.getKey(), entry.getValue());
64
		}
65
		}
65
		return this.execute(request);
66
		return this.execute(request);
66
	}
67
	}
67
 
68
 
68
	public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params)
69
	public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params)
69
			throws ProfitMandiBusinessException, HttpHostConnectException{
70
			throws ProfitMandiBusinessException, HttpHostConnectException {
70
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString() : scheme.getValue()  + hostName + ":" + port + "/" + uri;
71
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
-
 
72
				: scheme.getValue() + hostName + ":" + port + "/" + uri;
71
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
73
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
72
		if(params != null){
74
		if (params != null) {
73
			Set<String> keys = params.keySet();
75
			Set<String> keys = params.keySet();
74
			for(String key : keys){
76
			for (String key : keys) {
75
				builder.queryParam(key, params.get(key));
77
				builder.queryParam(key, params.get(key));
76
			}
78
			}
77
		}
79
		}
78
		HttpGet request = new HttpGet(builder.build().encode().toUri());
80
		HttpGet request = new HttpGet(builder.build().encode().toUri());
79
		return this.execute(request);
81
		return this.execute(request);
80
	}
82
	}
81
	
83
 
82
	public HttpResponse getResponse(String url, Map<String, String> params, Map<String, String> headers)
84
	public HttpResponse getResponse(String url, Map<String, String> params, Map<String, String> headers)
83
			throws ProfitMandiBusinessException, HttpHostConnectException {
85
			throws ProfitMandiBusinessException, HttpHostConnectException {
84
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
86
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
85
		Set<String> keys = params.keySet();
87
		Set<String> keys = params.keySet();
86
		for(String key : keys){
88
		for (String key : keys) {
87
			builder.queryParam(key, params.get(key));
89
			builder.queryParam(key, params.get(key));
88
		}
90
		}
89
		HttpGet request = new HttpGet(builder.build().encode().toUri());
91
		HttpGet request = new HttpGet(builder.build().encode().toUri());
90
		for(Map.Entry<String, String> entry : headers.entrySet()){
92
		for (Map.Entry<String, String> entry : headers.entrySet()) {
91
			request.setHeader(entry.getKey(), entry.getValue());
93
			request.setHeader(entry.getKey(), entry.getValue());
92
		}
94
		}
93
		try {
95
		try {
94
			LOGGER.info("Request uri is  {}", request.getURI().toString());
96
			LOGGER.info("Request uri is  {}", request.getURI().toString());
95
			HttpResponse response = httpClient.execute(request);
97
			HttpResponse response = httpClient.execute(request);
96
			LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
98
			LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
97
			if(response.getStatusLine().getStatusCode() == HttpStatus.OK.value()){
99
			if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
98
				return response;
100
				return response;
99
			}else{
101
			} else {
100
				throw new ProfitMandiBusinessException("", "", "GE_1005");
102
				throw new ProfitMandiBusinessException("", "", "GE_1005");
101
			}
103
			}
102
		} catch(HttpHostConnectException httpHostConnectException) {
104
		} catch (HttpHostConnectException httpHostConnectException) {
103
			LOGGER.error("Connection Timeout Exception", httpHostConnectException);
105
			LOGGER.error("Connection Timeout Exception", httpHostConnectException);
104
			throw httpHostConnectException;
106
			throw httpHostConnectException;
105
		} catch (ClientProtocolException e) {
107
		} catch (ClientProtocolException e) {
106
			LOGGER.error("Client Error : ",e);
108
			LOGGER.error("Client Error : ", e);
107
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
109
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
108
		} catch (IOException e) {
110
		} catch (IOException e) {
109
			LOGGER.error("IO Error : ", e);
111
			LOGGER.error("IO Error : ", e);
110
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
112
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
111
		}
113
		}
112
	}
114
	}
113
	
115
 
114
	public String execute(HttpUriRequest request)
-
 
115
			throws ProfitMandiBusinessException, HttpHostConnectException{
116
	public String execute(HttpUriRequest request) throws ProfitMandiBusinessException, HttpHostConnectException {
116
		LOGGER.info("Connecting to server at url {}",request.getURI());
117
		LOGGER.info("Connecting to server at url {}", request.getURI());
117
		try {
118
		try {
118
			HttpResponse response = httpClient.execute(request);
119
			HttpResponse response = httpClient.execute(request);
119
			String responseString = this.toString(response.getEntity().getContent());
120
			String responseString = this.toString(response.getEntity().getContent());
120
			LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
121
			LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
121
			if(response.getStatusLine().getStatusCode() == HttpStatus.OK.value()){
122
			if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
122
				return responseString;
123
				return responseString;
123
			}else{
124
			} else {
124
				throw new ProfitMandiBusinessException("", "", "GE_1005");
125
				throw new ProfitMandiBusinessException("", "", "GE_1005");
125
			}
126
			}
126
		}catch(HttpHostConnectException httpHostConnectException) {
127
		} catch (HttpHostConnectException httpHostConnectException) {
127
			LOGGER.error("Connection Timeout Exception", httpHostConnectException);
128
			LOGGER.error("Connection Timeout Exception", httpHostConnectException);
128
			throw httpHostConnectException;
129
			throw httpHostConnectException;
129
		} catch (ClientProtocolException e) {
130
		} catch (ClientProtocolException e) {
130
			LOGGER.error("Client Error : ",e);
131
			LOGGER.error("Client Error : ", e);
131
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
132
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
132
		} catch (IOException e) {
133
		} catch (IOException e) {
133
			LOGGER.error("IO Error : ", e);
134
			LOGGER.error("IO Error : ", e);
134
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
135
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
135
		} 
136
		}
136
	}
137
	}
137
	
138
 
138
	public String post(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params, Map<String, String> headers)
139
	public String post(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
139
			throws ProfitMandiBusinessException, HttpHostConnectException{
140
			Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
140
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString() : scheme.getValue()  + hostName + ":" + port + "/" + uri;
141
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
-
 
142
				: scheme.getValue() + hostName + ":" + port + "/" + uri;
141
		return this.post(url, params, headers);
143
		return this.post(url, params, headers);
142
	}
144
	}
143
	
145
 
144
	public String post(String url, Map<String, String> params, Map<String, String> headers)
146
	public String post(String url, Map<String, String> params, Map<String, String> headers)
145
			throws ProfitMandiBusinessException, HttpHostConnectException{
147
			throws ProfitMandiBusinessException, HttpHostConnectException {
146
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
148
		//UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
147
		List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
149
		List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
148
		for(Map.Entry<String, String> entry : params.entrySet()){
150
		for (Map.Entry<String, String> entry : params.entrySet()) {
149
			bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
151
			bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
150
		}
152
		}
151
		
153
 
152
		LOGGER.info("Body Parameters {}", params);
154
		LOGGER.info("Body Parameters {}", params);
153
		HttpPost request = new HttpPost(builder.build().encode().toUri());
155
		HttpPost request = new HttpPost(url);
154
		for(Map.Entry<String, String> entry : headers.entrySet()){
156
		for (Map.Entry<String, String> entry : headers.entrySet()) {
155
			request.setHeader(entry.getKey(), entry.getValue());
157
			request.setHeader(entry.getKey(), entry.getValue());
156
		}
158
		}
157
		
159
 
158
		try{
160
		try {
159
			request.setEntity(new UrlEncodedFormEntity(bodyParameters));
161
			request.setEntity(new UrlEncodedFormEntity(bodyParameters));
160
		}catch (UnsupportedEncodingException unsupportedEncodingException) {
162
		} catch (UnsupportedEncodingException unsupportedEncodingException) {
161
			LOGGER.error("Encoding error : ", unsupportedEncodingException);
163
			LOGGER.error("Encoding error : ", unsupportedEncodingException);
162
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
164
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
163
		}
165
		}
164
		
-
 
165
		
166
 
166
		return this.execute(request);
167
		return this.execute(request);
167
		
168
 
168
	}
169
	}
169
	
170
 
170
	public String postJson(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params, Map<String, String> headers)
171
	public String postJson(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
171
			throws ProfitMandiBusinessException, HttpHostConnectException{
172
			Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
172
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString() : scheme.getValue()  + hostName + ":" + port + "/" + uri;
173
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
-
 
174
				: scheme.getValue() + hostName + ":" + port + "/" + uri;
173
		return this.postJson(url, params, headers);
175
		return this.postJson(url, params, headers);
174
	}
176
	}
175
	
177
 
176
	public String postJson(String url, Map<String, String> params, Map<String, String> headers)
178
	public String postJson(String url, Map<String, String> params, Map<String, String> headers)
177
			throws ProfitMandiBusinessException, HttpHostConnectException{
179
			throws ProfitMandiBusinessException, HttpHostConnectException {
178
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
180
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
179
		List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
181
		List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
180
		for(Map.Entry<String, String> entry : params.entrySet()){
182
		for (Map.Entry<String, String> entry : params.entrySet()) {
181
			bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
183
			bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
182
		}
184
		}
183
		
185
 
184
		String jsonString = null;
186
		String jsonString = null;
185
		try {
187
		try {
186
			jsonString = StringUtils.toString(params);
188
			jsonString = StringUtils.toString(params);
187
		} catch (Exception e) {
189
		} catch (Exception e) {
188
			e.printStackTrace();
190
			e.printStackTrace();
189
		}
191
		}
190
		
192
 
191
		StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
193
		StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
192
		
194
 
193
		LOGGER.info("Body {}", jsonString);
195
		LOGGER.info("Body {}", jsonString);
194
		HttpPost request = new HttpPost(builder.build().encode().toUri());
196
		HttpPost request = new HttpPost(builder.build().encode().toUri());
195
		for(Map.Entry<String, String> entry : headers.entrySet()){
197
		for (Map.Entry<String, String> entry : headers.entrySet()) {
196
			request.setHeader(entry.getKey(), entry.getValue());
198
			request.setHeader(entry.getKey(), entry.getValue());
197
		}
199
		}
198
		request.setEntity(requestEntity);
200
		request.setEntity(requestEntity);
199
		return this.execute(request);
201
		return this.execute(request);
200
	}
202
	}
201
	
203
 
202
	private String toString(InputStream inputStream){
204
	private String toString(InputStream inputStream) {
203
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
205
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
204
		StringBuilder responseString = new StringBuilder();
206
		StringBuilder responseString = new StringBuilder();
205
		String line = null;
207
		String line = null;
206
		try {
208
		try {
207
			while((line = reader.readLine()) != null){
209
			while ((line = reader.readLine()) != null) {
208
				responseString.append(line);
210
				responseString.append(line);
209
			}
211
			}
210
			inputStream.close();
212
			inputStream.close();
211
		} catch (IOException e) {
213
		} catch (IOException e) {
212
			throw new RuntimeException();
214
			throw new RuntimeException();