Rev 21440 | Rev 21556 | 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.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Set;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;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.Role;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;@Componentpublic class GoogleLoginProcessor {private 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 = emails.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());//Set<Role> roles = user.getRoles();Set<Role> roles = new HashSet<>();String[] roleNames = new String[roles.size()];int index = 0;for(Role role : roles){roleNames[index++] = role.getName();}responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleNames));responseMap.put(ProfitMandiConstants.REGISTERED, true);}catch (ProfitMandiBusinessException profitMandiBusinessException) {responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(socialUser.getEmailId()));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");}}}