Subversion Repositories SmartDukaan

Rev

Rev 31637 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21561 ashik.ali 1
package com.spice.profitmandi.web.util;
2
 
3
import java.io.IOException;
4
import java.util.HashMap;
5
import java.util.Map;
6
 
23505 ashik.ali 7
import org.apache.http.conn.HttpHostConnectException;
31637 tejbeer 8
import org.apache.logging.log4j.LogManager;
9
import org.apache.logging.log4j.Logger;
22217 ashik.ali 10
import org.springframework.http.HttpHeaders;
11
import org.springframework.http.MediaType;
21561 ashik.ali 12
import org.springframework.stereotype.Component;
13
 
14
import com.fasterxml.jackson.core.JsonProcessingException;
15
import com.fasterxml.jackson.databind.JsonNode;
16
import com.fasterxml.jackson.databind.ObjectMapper;
17
import com.spice.profitmandi.common.enumuration.SchemeType;
18
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
19
import com.spice.profitmandi.common.model.ProfitMandiConstants;
20
import com.spice.profitmandi.common.web.client.RestClient;
31637 tejbeer 21
import com.spice.profitmandi.web.controller.LoginController;
21561 ashik.ali 22
 
23
@Component
22111 ashik.ali 24
public class GoogleTokenUtil {
31637 tejbeer 25
 
21561 ashik.ali 26
	private static final String V3_HOST_NAME = "www.googleapis.com";
23528 ashik.ali 27
	private static final String V3_URI = "oauth2/v3/tokeninfo";
21561 ashik.ali 28
	private static final int PORT_NUMBER = 443;
29
	private final ObjectMapper objectMapper = new ObjectMapper();
31637 tejbeer 30
 
31
	private static final Logger LOGGER = LogManager.getLogger(GoogleTokenUtil.class);
32
 
33
	public String getEmailId(String token) throws ProfitMandiBusinessException {
23528 ashik.ali 34
		RestClient restClient = new RestClient();
21561 ashik.ali 35
		Map<String, String> params = new HashMap<>();
36
		params.put(ProfitMandiConstants.ID_TOKEN, token);
22217 ashik.ali 37
		Map<String, String> headers = new HashMap<>(1);
38
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
23505 ashik.ali 39
		String responseString = null;
21561 ashik.ali 40
		try {
23528 ashik.ali 41
			responseString = restClient.get(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER, V3_URI, params, headers);
23505 ashik.ali 42
		} catch (HttpHostConnectException e) {
43
			throw new ProfitMandiBusinessException("googleServerResponse", responseString, "GE_1006");
44
		}
45
		try {
21561 ashik.ali 46
			JsonNode rootNode = objectMapper.readTree(responseString);
31637 tejbeer 47
 
48
			LOGGER.info("rootNode {}", rootNode);
22111 ashik.ali 49
			return rootNode.get("email").asText();
31637 tejbeer 50
 
51
		} catch (JsonProcessingException jsonProcessingException) {
21561 ashik.ali 52
			throw new ProfitMandiBusinessException("", "", "VE_1001");
53
		}
54
	}
55
}