Subversion Repositories SmartDukaan

Rev

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

Rev 26656 Rev 27179
Line 100... Line 100...
100
		Set<String> keys = params.keySet();
100
		Set<String> keys = params.keySet();
101
		for (String key : keys) {
101
		for (String key : keys) {
102
			builder.queryParam(key, params.get(key));
102
			builder.queryParam(key, params.get(key));
103
		}
103
		}
104
		HttpGet request = new HttpGet(builder.build().encode().toUri());
104
		HttpGet request = new HttpGet(builder.build().encode().toUri());
105
		if(headers != null) {
105
		if (headers != null) {
106
			for (Map.Entry<String, String> entry : headers.entrySet()) {
106
			for (Map.Entry<String, String> entry : headers.entrySet()) {
107
				request.setHeader(entry.getKey(), entry.getValue());
107
				request.setHeader(entry.getKey(), entry.getValue());
108
			}
108
			}
109
		}
109
		}
110
		try {
110
		try {
Line 150... Line 150...
150
			LOGGER.error("IO Error : ", e);
150
			LOGGER.error("IO Error : ", e);
151
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
151
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
152
		}
152
		}
153
	}
153
	}
154
 
154
 
-
 
155
	public String executeJson(HttpUriRequest request) throws ProfitMandiBusinessException, HttpHostConnectException {
-
 
156
		LOGGER.info("Connecting to server at url {}", request.getURI());
-
 
157
		try {
-
 
158
			HttpResponse response = httpClient.execute(request);
-
 
159
			String responseString = this.toString(response.getEntity().getContent());
-
 
160
			LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
-
 
161
			return responseString;
-
 
162
		} catch (HttpHostConnectException httpHostConnectException) {
-
 
163
			LOGGER.error("Connection Timeout Exception", httpHostConnectException);
-
 
164
			throw httpHostConnectException;
-
 
165
		} catch (ClientProtocolException e) {
-
 
166
			LOGGER.error("Client Error : ", e);
-
 
167
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
-
 
168
		} catch (IOException e) {
-
 
169
			LOGGER.error("IO Error : ", e);
-
 
170
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
-
 
171
		}
-
 
172
	}
-
 
173
 
155
	public String post(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
174
	public String post(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
156
			Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
175
			Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
157
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
176
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
158
				: scheme.getValue() + hostName + ":" + port + "/" + uri;
177
				: scheme.getValue() + hostName + ":" + port + "/" + uri;
159
		return this.post(url, params, headers);
178
		return this.post(url, params, headers);
Line 182... Line 201...
182
 
201
 
183
		return this.execute(request);
202
		return this.execute(request);
184
 
203
 
185
	}
204
	}
186
 
205
 
187
	public String postJson(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
-
 
188
			Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
-
 
189
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
-
 
190
				: scheme.getValue() + hostName + ":" + port + "/" + uri;
-
 
191
		return this.postJson(url, params, headers);
-
 
192
	}
-
 
193
 
-
 
194
 
-
 
195
	public String postJson(String url, Object object, Map<String, String> headers)
206
	public String postJson(String url, Object object, Map<String, String> headers)
196
			throws ProfitMandiBusinessException, HttpHostConnectException {
207
			throws ProfitMandiBusinessException, HttpHostConnectException {
197
		String jsonString;
208
		String jsonString;
198
		try {
209
		try {
199
			jsonString = objectMapper.writeValueAsString(object);
210
			jsonString = objectMapper.writeValueAsString(object);
200
			LOGGER.info("JSON String - {}", jsonString);
211
			LOGGER.info("JSON String - {}", jsonString);
201
		} catch(Exception e) {
212
		} catch (Exception e) {
202
			e.printStackTrace();
213
			e.printStackTrace();
203
			throw new ProfitMandiBusinessException("Json Object", object.toString(), "Could not write as String");
214
			throw new ProfitMandiBusinessException("Json Object", object.toString(), "Could not write as String");
204
		}
215
		}
205
		StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
216
		StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
206
 
217
 
Line 209... Line 220...
209
		HttpPost request = new HttpPost(builder.build().encode().toUri());
220
		HttpPost request = new HttpPost(builder.build().encode().toUri());
210
		for (Map.Entry<String, String> entry : headers.entrySet()) {
221
		for (Map.Entry<String, String> entry : headers.entrySet()) {
211
			request.setHeader(entry.getKey(), entry.getValue());
222
			request.setHeader(entry.getKey(), entry.getValue());
212
		}
223
		}
213
		request.setEntity(requestEntity);
224
		request.setEntity(requestEntity);
214
		return this.execute(request);
225
		return this.executeJson(request);
215
	}
226
	}
216
 
227
 
217
	private String toString(InputStream inputStream) {
228
	private String toString(InputStream inputStream) {
218
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
229
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
219
		StringBuilder responseString = new StringBuilder();
230
		StringBuilder responseString = new StringBuilder();
Line 224... Line 235...
224
			}
235
			}
225
			inputStream.close();
236
			inputStream.close();
226
		} catch (IOException e) {
237
		} catch (IOException e) {
227
			throw new RuntimeException();
238
			throw new RuntimeException();
228
		} finally {
239
		} finally {
229
			
240
 
230
		}
241
		}
231
		return responseString.toString();
242
		return responseString.toString();
232
	}
243
	}
233
}
244
}
234
245