Rev 21277 | Rev 21282 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.processor;import java.io.IOException;import java.time.LocalDateTime;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.common.util.JWTUtil;import com.spice.profitmandi.dao.entity.SocialUser;import com.spice.profitmandi.dao.entity.User;import com.spice.profitmandi.dao.enumuration.Gender;import com.spice.profitmandi.dao.enumuration.SocialType;import com.spice.profitmandi.dao.repository.SocialUserRepository;import com.spice.profitmandi.dao.repository.UserRepository;import com.spice.profitmandi.web.client.RestClient;import com.spice.profitmandi.web.enumuration.SchemeType;public class GoogleLoginProcessor {//https://content.googleapis.com/plus/v1/people/me?access_tokenprivate static final String HOST_NAME = "content.googleapis.com";private static final String URI = "/plus/v1/people/me";private static final int PORT_NUMBER = 443;private final ObjectMapper objectMapper = new ObjectMapper();@AutowiredSocialUserRepository socialUserRepository;@AutowiredUserRepository userRepository;public Map<String, Object> process(Map<String, Object> map) throws ProfitMandiBusinessException{RestClient restClient = new RestClient(SchemeType.HTTPS, HOST_NAME, PORT_NUMBER);Map<String, String> params = new HashMap<>();params.put(ProfitMandiConstants.ACCESS_TOKEN, map.get(ProfitMandiConstants.TOKEN).toString());String responseString = restClient.get(URI, params);try {JsonNode rootNode = objectMapper.readTree(responseString);SocialUser socialUser = new SocialUser();if(rootNode.has("emails")){JsonNode emails = rootNode.get("emails");if(emails.isArray()){Iterator<JsonNode> emailsIterator = rootNode.elements();if(emailsIterator.hasNext()){JsonNode email = emailsIterator.next();if(email.has("value")){socialUser.setEmailId(email.get("value").asText());}}}socialUser.setEmailId(rootNode.get("email").asText());}if(rootNode.has("displayName")){socialUser.setName(rootNode.get("displayName").asText());}if(rootNode.has("gender")){String genderName = rootNode.get("gender").asText();switch(genderName){case "male":{socialUser.setGender(Gender.MALE);break;}case "female":{socialUser.setGender(Gender.FEMALE);break;}}}socialUser.setCreateTimestamp(LocalDateTime.now());socialUser.setType(SocialType.GOOGLE);socialUser.setUpdateTimestamp(LocalDateTime.now());socialUserRepository.persist(socialUser);Map<String, Object> responseMap = new HashMap<>(2);try{User user = userRepository.selectByEmailId(socialUser.getEmailId());responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId()));responseMap.put(ProfitMandiConstants.REGISTERED, true);}catch (ProfitMandiBusinessException pmbe) {responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(-1));responseMap.put(ProfitMandiConstants.REGISTERED, false);}return responseMap;}catch (JsonProcessingException jsonProcessingException) {//LOGGER.error("Json parse exception of "+json,jsonProcessingException);throw new ProfitMandiBusinessException("", "", "VE_1001");}catch (IOException ioException) {//LOGGER.error("IO Exception occurred while parsing json String");throw new ProfitMandiBusinessException("", "", "VE_1001");}}}