| 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;
|
| 21278 |
ashik.ali |
6 |
import java.util.Iterator;
|
| 22011 |
ashik.ali |
7 |
import java.util.List;
|
| 21277 |
ashik.ali |
8 |
import java.util.Map;
|
|
|
9 |
|
| 21278 |
ashik.ali |
10 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 22355 |
ashik.ali |
11 |
import org.springframework.http.HttpHeaders;
|
|
|
12 |
import org.springframework.http.MediaType;
|
| 21282 |
ashik.ali |
13 |
import org.springframework.stereotype.Component;
|
| 21278 |
ashik.ali |
14 |
|
| 21277 |
ashik.ali |
15 |
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
16 |
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
17 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
| 21556 |
ashik.ali |
18 |
import com.spice.profitmandi.common.enumuration.SchemeType;
|
| 21277 |
ashik.ali |
19 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 22931 |
ashik.ali |
20 |
import com.spice.profitmandi.common.model.GoogleLoginRequest;
|
| 21277 |
ashik.ali |
21 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
22 |
import com.spice.profitmandi.common.util.JWTUtil;
|
| 21556 |
ashik.ali |
23 |
import com.spice.profitmandi.common.web.client.RestClient;
|
| 21735 |
ashik.ali |
24 |
import com.spice.profitmandi.dao.entity.dtr.SocialUser;
|
|
|
25 |
import com.spice.profitmandi.dao.entity.dtr.User;
|
| 22011 |
ashik.ali |
26 |
import com.spice.profitmandi.dao.entity.dtr.UserRole;
|
| 21735 |
ashik.ali |
27 |
import com.spice.profitmandi.dao.enumuration.dtr.Gender;
|
|
|
28 |
import com.spice.profitmandi.dao.enumuration.dtr.SocialType;
|
|
|
29 |
import com.spice.profitmandi.dao.repository.dtr.SocialUserRepository;
|
|
|
30 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
| 22011 |
ashik.ali |
31 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
| 21277 |
ashik.ali |
32 |
|
| 22011 |
ashik.ali |
33 |
|
| 21282 |
ashik.ali |
34 |
@Component
|
| 21277 |
ashik.ali |
35 |
public class GoogleLoginProcessor {
|
| 21282 |
ashik.ali |
36 |
|
| 21556 |
ashik.ali |
37 |
private static final String V1_HOST_NAME = "content.googleapis.com";
|
|
|
38 |
private static final String V1_URI = "/plus/v1/people/me";
|
|
|
39 |
private static final String V3_HOST_NAME = "www.googleapis.com";
|
|
|
40 |
private static final String V3_URI = "/oauth2/v3/tokeninfo";
|
| 21277 |
ashik.ali |
41 |
private static final int PORT_NUMBER = 443;
|
|
|
42 |
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
43 |
|
| 21278 |
ashik.ali |
44 |
@Autowired
|
| 22931 |
ashik.ali |
45 |
private SocialUserRepository socialUserRepository;
|
| 21277 |
ashik.ali |
46 |
|
| 21278 |
ashik.ali |
47 |
@Autowired
|
| 22931 |
ashik.ali |
48 |
private UserRepository userRepository;
|
| 21278 |
ashik.ali |
49 |
|
| 22011 |
ashik.ali |
50 |
@Autowired
|
| 22931 |
ashik.ali |
51 |
private UserRoleRepository userRoleRepository;
|
| 22011 |
ashik.ali |
52 |
|
| 22931 |
ashik.ali |
53 |
public Map<String, Object> process(GoogleLoginRequest googleLoginRequest) throws ProfitMandiBusinessException{
|
| 21556 |
ashik.ali |
54 |
RestClient restClient = new RestClient(SchemeType.HTTPS, V1_HOST_NAME, PORT_NUMBER);
|
| 21277 |
ashik.ali |
55 |
Map<String, String> params = new HashMap<>();
|
| 22931 |
ashik.ali |
56 |
params.put(ProfitMandiConstants.ACCESS_TOKEN, googleLoginRequest.getToken());
|
| 22355 |
ashik.ali |
57 |
Map<String, String> headers = new HashMap<>(1);
|
|
|
58 |
headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
|
|
59 |
String responseString = restClient.get(V1_URI, params, headers);
|
| 21277 |
ashik.ali |
60 |
try {
|
| 21278 |
ashik.ali |
61 |
JsonNode rootNode = objectMapper.readTree(responseString);
|
| 21277 |
ashik.ali |
62 |
SocialUser socialUser = new SocialUser();
|
| 21278 |
ashik.ali |
63 |
if(rootNode.has("emails")){
|
|
|
64 |
JsonNode emails = rootNode.get("emails");
|
|
|
65 |
if(emails.isArray()){
|
| 21286 |
ashik.ali |
66 |
Iterator<JsonNode> emailsIterator = emails.elements();
|
| 21278 |
ashik.ali |
67 |
if(emailsIterator.hasNext()){
|
|
|
68 |
JsonNode email = emailsIterator.next();
|
|
|
69 |
if(email.has("value")){
|
|
|
70 |
socialUser.setEmailId(email.get("value").asText());
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
}
|
| 21286 |
ashik.ali |
74 |
//socialUser.setEmailId(rootNode.get("email").asText());
|
| 21277 |
ashik.ali |
75 |
}
|
| 22024 |
ashik.ali |
76 |
if(!socialUserRepository.isExistByEmailId(socialUser.getEmailId())){
|
|
|
77 |
if(rootNode.has("displayName")){
|
|
|
78 |
socialUser.setName(rootNode.get("displayName").asText());
|
|
|
79 |
}
|
|
|
80 |
if(rootNode.has("gender")){
|
|
|
81 |
String genderName = rootNode.get("gender").asText();
|
|
|
82 |
switch(genderName){
|
|
|
83 |
case "male":{
|
|
|
84 |
socialUser.setGender(Gender.MALE);
|
|
|
85 |
break;
|
|
|
86 |
}case "female":{
|
|
|
87 |
socialUser.setGender(Gender.FEMALE);
|
|
|
88 |
break;
|
|
|
89 |
}
|
| 21278 |
ashik.ali |
90 |
}
|
|
|
91 |
}
|
| 22024 |
ashik.ali |
92 |
socialUser.setCreateTimestamp(LocalDateTime.now());
|
|
|
93 |
socialUser.setType(SocialType.GOOGLE);
|
|
|
94 |
socialUser.setUpdateTimestamp(LocalDateTime.now());
|
|
|
95 |
socialUserRepository.persist(socialUser);
|
| 21278 |
ashik.ali |
96 |
}
|
| 22024 |
ashik.ali |
97 |
|
| 21277 |
ashik.ali |
98 |
Map<String, Object> responseMap = new HashMap<>(2);
|
| 23204 |
ashik.ali |
99 |
User user = null;
|
| 21277 |
ashik.ali |
100 |
try{
|
| 23204 |
ashik.ali |
101 |
user = userRepository.selectByEmailId(socialUser.getEmailId());
|
|
|
102 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
103 |
|
|
|
104 |
}
|
|
|
105 |
if(user == null){
|
|
|
106 |
try{
|
|
|
107 |
user = userRepository.selectByEmailId(socialUser.getEmailId());
|
|
|
108 |
}catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
109 |
responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(socialUser.getEmailId()));
|
|
|
110 |
responseMap.put(ProfitMandiConstants.REGISTERED, false);
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
if(user != null){
|
| 21426 |
ashik.ali |
114 |
//Set<Role> roles = user.getRoles();
|
| 22011 |
ashik.ali |
115 |
List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
|
|
|
116 |
//Set<Role> roles = new HashSet<>();
|
|
|
117 |
String[] roleTypes = new String[userRoles.size()];
|
| 21282 |
ashik.ali |
118 |
int index = 0;
|
| 22011 |
ashik.ali |
119 |
for (UserRole userRole : userRoles) {
|
|
|
120 |
roleTypes[index++] = userRole.getRoleType().toString();
|
| 21282 |
ashik.ali |
121 |
}
|
| 22011 |
ashik.ali |
122 |
responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleTypes));
|
| 21277 |
ashik.ali |
123 |
responseMap.put(ProfitMandiConstants.REGISTERED, true);
|
|
|
124 |
}
|
| 23204 |
ashik.ali |
125 |
|
| 21277 |
ashik.ali |
126 |
return responseMap;
|
|
|
127 |
}catch (JsonProcessingException jsonProcessingException) {
|
|
|
128 |
//LOGGER.error("Json parse exception of "+json,jsonProcessingException);
|
|
|
129 |
throw new ProfitMandiBusinessException("", "", "VE_1001");
|
|
|
130 |
}catch (IOException ioException) {
|
|
|
131 |
//LOGGER.error("IO Exception occurred while parsing json String");
|
|
|
132 |
throw new ProfitMandiBusinessException("", "", "VE_1001");
|
|
|
133 |
}
|
|
|
134 |
}
|
| 21556 |
ashik.ali |
135 |
|
|
|
136 |
public Map<String, Object> getFofoDetail(String token) throws ProfitMandiBusinessException{
|
|
|
137 |
RestClient restClient = new RestClient(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER);
|
|
|
138 |
Map<String, String> params = new HashMap<>();
|
|
|
139 |
params.put(ProfitMandiConstants.ID_TOKEN, token);
|
| 22355 |
ashik.ali |
140 |
Map<String, String> headers = new HashMap<>(1);
|
|
|
141 |
headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
|
|
142 |
String responseString = restClient.get(V3_URI, params, headers);
|
| 21556 |
ashik.ali |
143 |
try {
|
|
|
144 |
JsonNode rootNode = objectMapper.readTree(responseString);
|
| 23204 |
ashik.ali |
145 |
User user = null;
|
| 21556 |
ashik.ali |
146 |
try{
|
| 23204 |
ashik.ali |
147 |
user = userRepository.selectByEmailId(rootNode.get("email").asText());
|
|
|
148 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
149 |
|
|
|
150 |
}
|
|
|
151 |
if(user == null){
|
|
|
152 |
user = userRepository.selectBySecondryEmailId(rootNode.get("email").asText());
|
|
|
153 |
}
|
|
|
154 |
if(user != null){
|
| 21556 |
ashik.ali |
155 |
Map<String, Object> responseMap = new HashMap<>(2);
|
|
|
156 |
responseMap.put(ProfitMandiConstants.FOFO_ID, user.getId());
|
|
|
157 |
responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
|
|
|
158 |
return responseMap;
|
|
|
159 |
}
|
| 23204 |
ashik.ali |
160 |
|
| 21556 |
ashik.ali |
161 |
}catch (JsonProcessingException jsonProcessingException) {
|
|
|
162 |
throw new ProfitMandiBusinessException("", "", "VE_1001");
|
|
|
163 |
}catch (IOException ioException) {
|
|
|
164 |
throw new ProfitMandiBusinessException("", "", "VE_1001");
|
|
|
165 |
}
|
| 23204 |
ashik.ali |
166 |
return null;
|
| 21556 |
ashik.ali |
167 |
}
|
| 21277 |
ashik.ali |
168 |
}
|