Subversion Repositories SmartDukaan

Rev

Rev 21278 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21277 ashik.ali 1
package com.spice.profitmandi.web.processor;
2
 
3
import java.io.IOException;
4
import java.time.LocalDateTime;
5
import java.util.HashMap;
6
import java.util.Map;
7
 
8
import com.fasterxml.jackson.core.JsonProcessingException;
9
import com.fasterxml.jackson.databind.JsonNode;
10
import com.fasterxml.jackson.databind.ObjectMapper;
11
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
12
import com.spice.profitmandi.common.model.ProfitMandiConstants;
13
import com.spice.profitmandi.common.util.JWTUtil;
14
import com.spice.profitmandi.dao.entity.SocialUser;
15
import com.spice.profitmandi.dao.entity.User;
16
import com.spice.profitmandi.dao.enumuration.SocialType;
17
import com.spice.profitmandi.dao.repository.SocialUserRepository;
18
import com.spice.profitmandi.dao.repository.SocialUserRepositoryImpl;
19
import com.spice.profitmandi.dao.repository.UserRepository;
20
import com.spice.profitmandi.dao.repository.UserRepositoryImpl;
21
import com.spice.profitmandi.web.client.RestClient;
22
import com.spice.profitmandi.web.enumuration.SchemeType;
23
 
24
public class GoogleLoginProcessor {
25
	private static final String HOST_NAME = "www.googleapis.com";
26
	private static final String URI = "oauth2/v3/tokeninfo";
27
	private static final int PORT_NUMBER = 443;
28
	private final ObjectMapper objectMapper = new ObjectMapper();
29
	private GoogleLoginProcessor(){
30
 
31
	}
32
	private static GoogleLoginProcessor googleLoginProcessor;
33
	public static GoogleLoginProcessor getInstance(){
34
		if(googleLoginProcessor == null){
35
			googleLoginProcessor = new GoogleLoginProcessor();
36
		}
37
		return googleLoginProcessor;
38
	}
39
 
40
	private final SocialUserRepository socialUserRepository = SocialUserRepositoryImpl.getInstance();
41
	private final UserRepository userRepository = UserRepositoryImpl.getInstance();
42
 
43
	public Map<String, Object> process(Map<String, Object> map) throws ProfitMandiBusinessException{
44
		RestClient restClient = new RestClient(SchemeType.HTTPS, HOST_NAME, PORT_NUMBER);
45
		Map<String, String> params = new HashMap<>();
46
		params.put(ProfitMandiConstants.ID_TOKEN, map.get(ProfitMandiConstants.TOKEN).toString());
47
		String responseString = restClient.get(URI, params);
48
		try {
49
			JsonNode jsonNode = objectMapper.readTree(responseString);
50
			SocialUser socialUser = new SocialUser();
51
			if(jsonNode.has("email")){
52
				socialUser.setEmailId(jsonNode.get("email").asText());
53
			}
54
			if(jsonNode.has("name")){
55
				socialUser.setName(jsonNode.get("name").asText());
56
			}
57
			socialUser.setCreateTimestamp(LocalDateTime.now());
58
			socialUser.setType(SocialType.GOOGLE);
59
			socialUser.setUpdateTimestamp(LocalDateTime.now());
60
			socialUserRepository.persist(socialUser);
61
			Map<String, Object> responseMap = new HashMap<>(2);
62
			try{
63
				User user = userRepository.selectByEmailId(socialUser.getEmailId());
64
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId()));
65
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
66
			}catch (ProfitMandiBusinessException pmbe) {
67
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(-1));
68
				responseMap.put(ProfitMandiConstants.REGISTERED, false);
69
			}
70
			return responseMap;
71
		}catch (JsonProcessingException jsonProcessingException) {
72
			//LOGGER.error("Json parse exception of "+json,jsonProcessingException);
73
			throw new ProfitMandiBusinessException("", "", "VE_1001");
74
		}catch (IOException ioException) {
75
			//LOGGER.error("IO Exception occurred while parsing json String");
76
			throw new ProfitMandiBusinessException("", "", "VE_1001");
77
		}
78
	}
79
}