Subversion Repositories SmartDukaan

Rev

Rev 37388 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 37388 Rev 37661
Line 2... Line 2...
2
 
2
 
3
import com.fasterxml.jackson.databind.ObjectMapper;
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import com.spice.profitmandi.common.ResponseCodeHolder;
4
import com.spice.profitmandi.common.ResponseCodeHolder;
5
import com.spice.profitmandi.common.enumuration.SchemeType;
5
import com.spice.profitmandi.common.enumuration.SchemeType;
6
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
6
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
7
import com.spice.profitmandi.common.model.MandiiResponse;
7
import com.spice.profitmandi.common.model.RawHttpResponse;
8
import org.apache.http.HttpResponse;
8
import org.apache.http.HttpResponse;
9
import org.apache.http.NameValuePair;
9
import org.apache.http.NameValuePair;
10
import org.apache.http.client.ClientProtocolException;
10
import org.apache.http.client.ClientProtocolException;
11
import org.apache.http.client.HttpClient;
11
import org.apache.http.client.HttpClient;
12
import org.apache.http.client.config.RequestConfig;
12
import org.apache.http.client.config.RequestConfig;
Line 128... Line 128...
128
            }
128
            }
129
        }
129
        }
130
        return this.executeJson(request);
130
        return this.executeJson(request);
131
    }
131
    }
132
 
132
 
133
    public MandiiResponse getMandii(String url, Map<String, String> params, Map<String, String> headers)
-
 
134
            throws ProfitMandiBusinessException, HttpHostConnectException {
-
 
135
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
-
 
136
        if (params != null) {
-
 
137
            Set<String> keys = params.keySet();
-
 
138
            for (String key : keys) {
-
 
139
                builder.queryParam(key, params.get(key));
-
 
140
            }
-
 
141
        }
-
 
142
        HttpGet request = new HttpGet(builder.build().encode().toUri());
-
 
143
        for (Map.Entry<String, String> entry : headers.entrySet()) {
-
 
144
            request.setHeader(entry.getKey(), entry.getValue());
-
 
145
        }
-
 
146
        return this.executeMandii(request);
-
 
147
    }
-
 
148
 
-
 
149
    public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params)
133
    public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params)
150
            throws ProfitMandiBusinessException, HttpHostConnectException {
134
            throws ProfitMandiBusinessException, HttpHostConnectException {
151
        String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
135
        String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
152
                : scheme.getValue() + hostName + ":" + port + "/" + uri;
136
                : scheme.getValue() + hostName + ":" + port + "/" + uri;
153
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
137
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
Line 223... Line 207...
223
        } finally {
207
        } finally {
224
            if (response != null) EntityUtils.consumeQuietly(response.getEntity());
208
            if (response != null) EntityUtils.consumeQuietly(response.getEntity());
225
        }
209
        }
226
    }
210
    }
227
 
211
 
228
    public MandiiResponse executeMandii(HttpUriRequest request)
212
    public RawHttpResponse executeRaw(HttpUriRequest request)
229
            throws ProfitMandiBusinessException, HttpHostConnectException {
213
            throws ProfitMandiBusinessException, HttpHostConnectException {
230
        LOGGER.info("Connecting to server at url {}", request.getURI());
214
        LOGGER.info("Connecting to server at url {}", request.getURI());
231
        HttpResponse response = null;
215
        HttpResponse response = null;
232
        try {
216
        try {
233
            response = httpClient.execute(request);
217
            response = httpClient.execute(request);
234
            String responseString = this.toString(response.getEntity().getContent());
218
            String responseString = this.toString(response.getEntity().getContent());
235
            LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
219
            LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
236
 
220
 
237
            MandiiResponse mandiiResponse = new MandiiResponse();
221
            RawHttpResponse rawResponse = new RawHttpResponse();
238
            mandiiResponse.setResponseString(responseString);
222
            rawResponse.setResponseString(responseString);
239
            mandiiResponse.setStatusCode(response.getStatusLine().getStatusCode());
223
            rawResponse.setStatusCode(response.getStatusLine().getStatusCode());
240
 
224
 
241
            return mandiiResponse;
225
            return rawResponse;
242
        } catch (HttpHostConnectException httpHostConnectException) {
226
        } catch (HttpHostConnectException httpHostConnectException) {
243
            LOGGER.error("Connection Timeout Exception", httpHostConnectException);
227
            LOGGER.error("Connection Timeout Exception", httpHostConnectException);
244
            throw httpHostConnectException;
228
            throw httpHostConnectException;
245
        } catch (ClientProtocolException e) {
229
        } catch (ClientProtocolException e) {
246
            LOGGER.error("Client Error : ", e);
230
            LOGGER.error("Client Error : ", e);
Line 416... Line 400...
416
        }
400
        }
417
        request.setEntity(requestEntity);
401
        request.setEntity(requestEntity);
418
        return this.executeJson(request);
402
        return this.executeJson(request);
419
    }
403
    }
420
 
404
 
421
    public MandiiResponse postWithResponse(String url, String body, Map<String, String> headers)
405
    public RawHttpResponse postWithResponse(String url, String body, Map<String, String> headers)
422
            throws ProfitMandiBusinessException, HttpHostConnectException {
406
            throws ProfitMandiBusinessException, HttpHostConnectException {
423
        HttpPost request = new HttpPost(url);
407
        HttpPost request = new HttpPost(url);
424
        for (Map.Entry<String, String> entry : headers.entrySet()) {
408
        for (Map.Entry<String, String> entry : headers.entrySet()) {
425
            request.setHeader(entry.getKey(), entry.getValue());
409
            request.setHeader(entry.getKey(), entry.getValue());
426
        }
410
        }
Line 428... Line 412...
428
            request.setEntity(new StringEntity(body));
412
            request.setEntity(new StringEntity(body));
429
        } catch (UnsupportedEncodingException unsupportedEncodingException) {
413
        } catch (UnsupportedEncodingException unsupportedEncodingException) {
430
            LOGGER.error("Encoding error : ", unsupportedEncodingException);
414
            LOGGER.error("Encoding error : ", unsupportedEncodingException);
431
            throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
415
            throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
432
        }
416
        }
433
        return this.executeMandii(request);
417
        return this.executeRaw(request);
434
    }
418
    }
435
 
419
 
436
    public String patchJson(String url, Object object, Map<String, String> headers)
420
    public String patchJson(String url, Object object, Map<String, String> headers)
437
            throws ProfitMandiBusinessException, HttpHostConnectException {
421
            throws ProfitMandiBusinessException, HttpHostConnectException {
438
        String jsonString;
422
        String jsonString;