Subversion Repositories SmartDukaan

Rev

Rev 21277 | Rev 21282 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21277 Rev 21278
Line 1... Line 1...
1
package com.spice.profitmandi.web.processor;
1
package com.spice.profitmandi.web.processor;
2
 
2
 
3
import java.io.IOException;
3
import java.io.IOException;
4
import java.time.LocalDateTime;
4
import java.time.LocalDateTime;
5
import java.util.HashMap;
5
import java.util.HashMap;
-
 
6
import java.util.Iterator;
6
import java.util.Map;
7
import java.util.Map;
7
 
8
 
-
 
9
import org.springframework.beans.factory.annotation.Autowired;
-
 
10
 
8
import com.fasterxml.jackson.core.JsonProcessingException;
11
import com.fasterxml.jackson.core.JsonProcessingException;
9
import com.fasterxml.jackson.databind.JsonNode;
12
import com.fasterxml.jackson.databind.JsonNode;
10
import com.fasterxml.jackson.databind.ObjectMapper;
13
import com.fasterxml.jackson.databind.ObjectMapper;
11
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
14
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
12
import com.spice.profitmandi.common.model.ProfitMandiConstants;
15
import com.spice.profitmandi.common.model.ProfitMandiConstants;
13
import com.spice.profitmandi.common.util.JWTUtil;
16
import com.spice.profitmandi.common.util.JWTUtil;
14
import com.spice.profitmandi.dao.entity.SocialUser;
17
import com.spice.profitmandi.dao.entity.SocialUser;
15
import com.spice.profitmandi.dao.entity.User;
18
import com.spice.profitmandi.dao.entity.User;
-
 
19
import com.spice.profitmandi.dao.enumuration.Gender;
16
import com.spice.profitmandi.dao.enumuration.SocialType;
20
import com.spice.profitmandi.dao.enumuration.SocialType;
17
import com.spice.profitmandi.dao.repository.SocialUserRepository;
21
import com.spice.profitmandi.dao.repository.SocialUserRepository;
18
import com.spice.profitmandi.dao.repository.SocialUserRepositoryImpl;
-
 
19
import com.spice.profitmandi.dao.repository.UserRepository;
22
import com.spice.profitmandi.dao.repository.UserRepository;
20
import com.spice.profitmandi.dao.repository.UserRepositoryImpl;
-
 
21
import com.spice.profitmandi.web.client.RestClient;
23
import com.spice.profitmandi.web.client.RestClient;
22
import com.spice.profitmandi.web.enumuration.SchemeType;
24
import com.spice.profitmandi.web.enumuration.SchemeType;
23
 
25
 
24
public class GoogleLoginProcessor {
26
public class GoogleLoginProcessor {
-
 
27
	//https://content.googleapis.com/plus/v1/people/me?access_token
25
	private static final String HOST_NAME = "www.googleapis.com";
28
	private static final String HOST_NAME = "content.googleapis.com";
26
	private static final String URI = "oauth2/v3/tokeninfo";
29
	private static final String URI = "/plus/v1/people/me";
27
	private static final int PORT_NUMBER = 443;
30
	private static final int PORT_NUMBER = 443;
28
	private final ObjectMapper objectMapper = new ObjectMapper();
31
	private final ObjectMapper objectMapper = new ObjectMapper();
29
	private GoogleLoginProcessor(){
-
 
30
		
-
 
31
	}
-
 
32
	private static GoogleLoginProcessor googleLoginProcessor;
-
 
33
	public static GoogleLoginProcessor getInstance(){
-
 
34
		if(googleLoginProcessor == null){
-
 
35
			googleLoginProcessor = new GoogleLoginProcessor();
-
 
36
		}
-
 
37
		return googleLoginProcessor;
-
 
38
	}
-
 
39
	
32
	
-
 
33
	@Autowired
40
	private final SocialUserRepository socialUserRepository = SocialUserRepositoryImpl.getInstance();
34
	SocialUserRepository socialUserRepository;
-
 
35
	
-
 
36
	@Autowired
41
	private final UserRepository userRepository = UserRepositoryImpl.getInstance();
37
	UserRepository userRepository;
42
	
38
	
43
	public Map<String, Object> process(Map<String, Object> map) throws ProfitMandiBusinessException{
39
	public Map<String, Object> process(Map<String, Object> map) throws ProfitMandiBusinessException{
44
		RestClient restClient = new RestClient(SchemeType.HTTPS, HOST_NAME, PORT_NUMBER);
40
		RestClient restClient = new RestClient(SchemeType.HTTPS, HOST_NAME, PORT_NUMBER);
45
		Map<String, String> params = new HashMap<>();
41
		Map<String, String> params = new HashMap<>();
46
		params.put(ProfitMandiConstants.ID_TOKEN, map.get(ProfitMandiConstants.TOKEN).toString());
42
		params.put(ProfitMandiConstants.ACCESS_TOKEN, map.get(ProfitMandiConstants.TOKEN).toString());
47
		String responseString = restClient.get(URI, params);
43
		String responseString = restClient.get(URI, params);
48
		try {
44
		try {
49
			JsonNode jsonNode = objectMapper.readTree(responseString);
45
			JsonNode rootNode = objectMapper.readTree(responseString);
50
			SocialUser socialUser = new SocialUser();
46
			SocialUser socialUser = new SocialUser();
51
			if(jsonNode.has("email")){
47
			if(rootNode.has("emails")){
-
 
48
				JsonNode emails = rootNode.get("emails");
-
 
49
				if(emails.isArray()){
-
 
50
					Iterator<JsonNode> emailsIterator = rootNode.elements();
-
 
51
					if(emailsIterator.hasNext()){
-
 
52
						JsonNode email = emailsIterator.next();
-
 
53
						if(email.has("value")){
-
 
54
							socialUser.setEmailId(email.get("value").asText());
-
 
55
						}
-
 
56
					}
-
 
57
				}
52
				socialUser.setEmailId(jsonNode.get("email").asText());
58
				socialUser.setEmailId(rootNode.get("email").asText());
-
 
59
			}
-
 
60
			if(rootNode.has("displayName")){
-
 
61
				socialUser.setName(rootNode.get("displayName").asText());
53
			}
62
			}
54
			if(jsonNode.has("name")){
63
			if(rootNode.has("gender")){
55
				socialUser.setName(jsonNode.get("name").asText());
64
				String genderName = rootNode.get("gender").asText();
-
 
65
				switch(genderName){
-
 
66
					case "male":{
-
 
67
						socialUser.setGender(Gender.MALE);
-
 
68
						break;
-
 
69
					}case "female":{
-
 
70
						socialUser.setGender(Gender.FEMALE);
-
 
71
						break;
-
 
72
					}
-
 
73
				}
56
			}
74
			}
57
			socialUser.setCreateTimestamp(LocalDateTime.now());
75
			socialUser.setCreateTimestamp(LocalDateTime.now());
58
			socialUser.setType(SocialType.GOOGLE);
76
			socialUser.setType(SocialType.GOOGLE);
59
			socialUser.setUpdateTimestamp(LocalDateTime.now());
77
			socialUser.setUpdateTimestamp(LocalDateTime.now());
60
			socialUserRepository.persist(socialUser);
78
			socialUserRepository.persist(socialUser);