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