Subversion Repositories SmartDukaan

Rev

Rev 22215 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.common.web.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;

import com.spice.profitmandi.common.ResponseCodeHolder;
import com.spice.profitmandi.common.enumuration.SchemeType;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;

public class RestClient {
        
        private static final Logger LOGGER = LoggerFactory.getLogger(RestClient.class);
        
        private HttpClient httpClient;
        private SchemeType scheme;
        private String hostName;
        private int port;
        private final String url;
        private HttpHeaders headers;
        public RestClient(SchemeType scheme, String hostName, int port){
                this.scheme = scheme;
                this.hostName = hostName;
                this.port = port;
                this.url = scheme.getValue()==null ? SchemeType.HTTP.toString() : scheme.getValue()  + hostName + ":" + port + "/";
                this.headers = new HttpHeaders();
                this.httpClient = HttpClients.createDefault();
                headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        }
        public SchemeType getScheme() {
                return scheme;
        }
        public String getHostName() {
                return hostName;
        }
        public int getPort() {
                return port;
        }
        public String getUrl() {
                return url;
        }
        public String get(String uri, Map<String, String> params)
                throws ProfitMandiBusinessException{
                UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
                Set<String> keys = params.keySet();
                for(String key : keys){
                        builder.queryParam(key, params.get(key));
                }
                HttpGet request = new HttpGet(builder.build().toUri());
                request.setHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
                LOGGER.info("Connecting to google server for token validation at url {}",request.getURI());
                try {
                        HttpResponse response = httpClient.execute(request);
                        String responseString = this.toString(response.getEntity().getContent());
                        LOGGER.info("Got response for token validation with responseCode {} and responseBody {}", response.getStatusLine().getStatusCode(), responseString);
                        if(response.getStatusLine().getStatusCode() == HttpStatus.OK.value()){
                                return responseString;
                        }else{
                                throw new ProfitMandiBusinessException(ProfitMandiConstants.TOKEN, params.get(ProfitMandiConstants.ACCESS_TOKEN), "USR_1005");
                        }
                } catch (ClientProtocolException e) {
                        throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1006"));
                } catch (IOException e) {
                        throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1006"));
                }
        }
        private String toString(InputStream inputStream){
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder responseString = new StringBuilder();
                String line = null;
                try {
                        while((line = reader.readLine()) != null){
                                responseString.append(line);
                        }
                        inputStream.close();
                } catch (IOException e) {
                        throw new RuntimeException();
                }
                return responseString.toString();
        }
}