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