Subversion Repositories SmartDukaan

Rev

Rev 21469 | Rev 21730 | Go to most recent revision | Details | Compare with Previous | 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;
21426 ashik.ali 6
import java.util.HashSet;
21278 ashik.ali 7
import java.util.Iterator;
21277 ashik.ali 8
import java.util.Map;
21282 ashik.ali 9
import java.util.Set;
21277 ashik.ali 10
 
21278 ashik.ali 11
import org.springframework.beans.factory.annotation.Autowired;
21282 ashik.ali 12
import org.springframework.stereotype.Component;
21278 ashik.ali 13
 
21277 ashik.ali 14
import com.fasterxml.jackson.core.JsonProcessingException;
15
import com.fasterxml.jackson.databind.JsonNode;
16
import com.fasterxml.jackson.databind.ObjectMapper;
21556 ashik.ali 17
import com.spice.profitmandi.common.enumuration.SchemeType;
21277 ashik.ali 18
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
19
import com.spice.profitmandi.common.model.ProfitMandiConstants;
20
import com.spice.profitmandi.common.util.JWTUtil;
21556 ashik.ali 21
import com.spice.profitmandi.common.web.client.RestClient;
21282 ashik.ali 22
import com.spice.profitmandi.dao.entity.Role;
21277 ashik.ali 23
import com.spice.profitmandi.dao.entity.SocialUser;
24
import com.spice.profitmandi.dao.entity.User;
21278 ashik.ali 25
import com.spice.profitmandi.dao.enumuration.Gender;
21277 ashik.ali 26
import com.spice.profitmandi.dao.enumuration.SocialType;
27
import com.spice.profitmandi.dao.repository.SocialUserRepository;
28
import com.spice.profitmandi.dao.repository.UserRepository;
29
 
21282 ashik.ali 30
@Component
21277 ashik.ali 31
public class GoogleLoginProcessor {
21282 ashik.ali 32
 
21556 ashik.ali 33
	private static final String V1_HOST_NAME = "content.googleapis.com";
34
	private static final String V1_URI = "/plus/v1/people/me";
35
	private static final String V3_HOST_NAME = "www.googleapis.com";
36
	private static final String V3_URI = "/oauth2/v3/tokeninfo";
21277 ashik.ali 37
	private static final int PORT_NUMBER = 443;
38
	private final ObjectMapper objectMapper = new ObjectMapper();
39
 
21278 ashik.ali 40
	@Autowired
41
	SocialUserRepository socialUserRepository;
21277 ashik.ali 42
 
21278 ashik.ali 43
	@Autowired
44
	UserRepository userRepository;
45
 
21277 ashik.ali 46
	public Map<String, Object> process(Map<String, Object> map) throws ProfitMandiBusinessException{
21556 ashik.ali 47
		RestClient restClient = new RestClient(SchemeType.HTTPS, V1_HOST_NAME, PORT_NUMBER);
21277 ashik.ali 48
		Map<String, String> params = new HashMap<>();
21278 ashik.ali 49
		params.put(ProfitMandiConstants.ACCESS_TOKEN, map.get(ProfitMandiConstants.TOKEN).toString());
21556 ashik.ali 50
		String responseString = restClient.get(V1_URI, params);
21277 ashik.ali 51
		try {
21278 ashik.ali 52
			JsonNode rootNode = objectMapper.readTree(responseString);
21277 ashik.ali 53
			SocialUser socialUser = new SocialUser();
21278 ashik.ali 54
			if(rootNode.has("emails")){
55
				JsonNode emails = rootNode.get("emails");
56
				if(emails.isArray()){
21286 ashik.ali 57
					Iterator<JsonNode> emailsIterator = emails.elements();
21278 ashik.ali 58
					if(emailsIterator.hasNext()){
59
						JsonNode email = emailsIterator.next();
60
						if(email.has("value")){
61
							socialUser.setEmailId(email.get("value").asText());
62
						}
63
					}
64
				}
21286 ashik.ali 65
				//socialUser.setEmailId(rootNode.get("email").asText());
21277 ashik.ali 66
			}
21278 ashik.ali 67
			if(rootNode.has("displayName")){
68
				socialUser.setName(rootNode.get("displayName").asText());
21277 ashik.ali 69
			}
21278 ashik.ali 70
			if(rootNode.has("gender")){
71
				String genderName = rootNode.get("gender").asText();
72
				switch(genderName){
73
					case "male":{
74
						socialUser.setGender(Gender.MALE);
75
						break;
76
					}case "female":{
77
						socialUser.setGender(Gender.FEMALE);
78
						break;
79
					}
80
				}
81
			}
21277 ashik.ali 82
			socialUser.setCreateTimestamp(LocalDateTime.now());
83
			socialUser.setType(SocialType.GOOGLE);
84
			socialUser.setUpdateTimestamp(LocalDateTime.now());
85
			socialUserRepository.persist(socialUser);
86
			Map<String, Object> responseMap = new HashMap<>(2);
87
			try{
88
				User user = userRepository.selectByEmailId(socialUser.getEmailId());
21426 ashik.ali 89
				//Set<Role> roles = user.getRoles();
90
				Set<Role> roles = new HashSet<>();
91
				String[] roleNames = new String[roles.size()];
21282 ashik.ali 92
				int index = 0;
93
				for(Role role : roles){
21426 ashik.ali 94
					roleNames[index++] = role.getName();
21282 ashik.ali 95
				}
21426 ashik.ali 96
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleNames));
21277 ashik.ali 97
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
21440 ashik.ali 98
			}catch (ProfitMandiBusinessException profitMandiBusinessException) {
21469 amit.gupta 99
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(socialUser.getEmailId()));
21277 ashik.ali 100
				responseMap.put(ProfitMandiConstants.REGISTERED, false);
101
			}
102
			return responseMap;
103
		}catch (JsonProcessingException jsonProcessingException) {
104
			//LOGGER.error("Json parse exception of "+json,jsonProcessingException);
105
			throw new ProfitMandiBusinessException("", "", "VE_1001");
106
		}catch (IOException ioException) {
107
			//LOGGER.error("IO Exception occurred while parsing json String");
108
			throw new ProfitMandiBusinessException("", "", "VE_1001");
109
		}
110
	}
21556 ashik.ali 111
 
112
	public Map<String, Object> getFofoDetail(String token) throws ProfitMandiBusinessException{
113
		RestClient restClient = new RestClient(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER);
114
		Map<String, String> params = new HashMap<>();
115
		params.put(ProfitMandiConstants.ID_TOKEN, token);
116
		String responseString = restClient.get(V3_URI, params);
117
		try {
118
			JsonNode rootNode = objectMapper.readTree(responseString);
119
			try{
120
				User user = userRepository.selectByEmailId(rootNode.get("email").asText());
121
				Map<String, Object> responseMap = new HashMap<>(2);
122
				responseMap.put(ProfitMandiConstants.FOFO_ID, user.getId());
123
				responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
124
				return responseMap;
125
			}catch (ProfitMandiBusinessException profitMandiBusinessException) {
126
				throw new ProfitMandiBusinessException(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(), "");
127
			}
128
 
129
		}catch (JsonProcessingException jsonProcessingException) {
130
			throw new ProfitMandiBusinessException("", "", "VE_1001");
131
		}catch (IOException ioException) {
132
			throw new ProfitMandiBusinessException("", "", "VE_1001");
133
		}
134
	}
21277 ashik.ali 135
}