Subversion Repositories SmartDukaan

Rev

Rev 22217 | Rev 23528 | Go to most recent revision | 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;
22217 ashik.ali 8
import org.springframework.http.HttpHeaders;
9
import org.springframework.http.MediaType;
21561 ashik.ali 10
import org.springframework.stereotype.Component;
11
 
12
import com.fasterxml.jackson.core.JsonProcessingException;
13
import com.fasterxml.jackson.databind.JsonNode;
14
import com.fasterxml.jackson.databind.ObjectMapper;
15
import com.spice.profitmandi.common.enumuration.SchemeType;
16
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
17
import com.spice.profitmandi.common.model.ProfitMandiConstants;
18
import com.spice.profitmandi.common.web.client.RestClient;
19
 
20
@Component
22111 ashik.ali 21
public class GoogleTokenUtil {
21561 ashik.ali 22
 
23
	private static final String V3_HOST_NAME = "www.googleapis.com";
24
	private static final String V3_URI = "/oauth2/v3/tokeninfo";
25
	private static final int PORT_NUMBER = 443;
26
	private final ObjectMapper objectMapper = new ObjectMapper();
27
 
22111 ashik.ali 28
	public String getEmailId(String token) throws ProfitMandiBusinessException{
21561 ashik.ali 29
		RestClient restClient = new RestClient(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER);
30
		Map<String, String> params = new HashMap<>();
31
		params.put(ProfitMandiConstants.ID_TOKEN, token);
22217 ashik.ali 32
		Map<String, String> headers = new HashMap<>(1);
33
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
23505 ashik.ali 34
		String responseString = null;
21561 ashik.ali 35
		try {
23505 ashik.ali 36
			responseString = restClient.get(V3_URI, params, headers);
37
		} catch (HttpHostConnectException e) {
38
			throw new ProfitMandiBusinessException("googleServerResponse", responseString, "GE_1006");
39
		}
40
		try {
21561 ashik.ali 41
			JsonNode rootNode = objectMapper.readTree(responseString);
22111 ashik.ali 42
			return rootNode.get("email").asText();
43
 
21561 ashik.ali 44
		}catch (JsonProcessingException jsonProcessingException) {
45
			throw new ProfitMandiBusinessException("", "", "VE_1001");
46
		}catch (IOException ioException) {
47
			throw new ProfitMandiBusinessException("", "", "VE_1001");
48
		}
49
	}
50
}