Subversion Repositories SmartDukaan

Rev

Rev 31637 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.conn.HttpHostConnectException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spice.profitmandi.common.enumuration.SchemeType;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.client.RestClient;
import com.spice.profitmandi.web.controller.LoginController;

@Component
public class GoogleTokenUtil {

        private static final String V3_HOST_NAME = "www.googleapis.com";
        private static final String V3_URI = "oauth2/v3/tokeninfo";
        private static final int PORT_NUMBER = 443;
        private final ObjectMapper objectMapper = new ObjectMapper();

        private static final Logger LOGGER = LogManager.getLogger(GoogleTokenUtil.class);

        public String getEmailId(String token) throws ProfitMandiBusinessException {
                RestClient restClient = new RestClient();
                Map<String, String> params = new HashMap<>();
                params.put(ProfitMandiConstants.ID_TOKEN, token);
                Map<String, String> headers = new HashMap<>(1);
                headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
                String responseString = null;
                try {
                        responseString = restClient.get(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER, V3_URI, params, headers);
                } catch (HttpHostConnectException e) {
                        throw new ProfitMandiBusinessException("googleServerResponse", responseString, "GE_1006");
                }
                try {
                        JsonNode rootNode = objectMapper.readTree(responseString);

                        LOGGER.info("rootNode {}", rootNode);
                        return rootNode.get("email").asText();

                } catch (JsonProcessingException jsonProcessingException) {
                        throw new ProfitMandiBusinessException("", "", "VE_1001");
                }
        }
}