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