Rev 21278 | 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.Map;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.SocialType;import com.spice.profitmandi.dao.repository.SocialUserRepository;import com.spice.profitmandi.dao.repository.SocialUserRepositoryImpl;import com.spice.profitmandi.dao.repository.UserRepository;import com.spice.profitmandi.dao.repository.UserRepositoryImpl;import com.spice.profitmandi.web.client.RestClient;import com.spice.profitmandi.web.enumuration.SchemeType;public class GoogleLoginProcessor {private static final String HOST_NAME = "www.googleapis.com";private static final String URI = "oauth2/v3/tokeninfo";private static final int PORT_NUMBER = 443;private final ObjectMapper objectMapper = new ObjectMapper();private GoogleLoginProcessor(){}private static GoogleLoginProcessor googleLoginProcessor;public static GoogleLoginProcessor getInstance(){if(googleLoginProcessor == null){googleLoginProcessor = new GoogleLoginProcessor();}return googleLoginProcessor;}private final SocialUserRepository socialUserRepository = SocialUserRepositoryImpl.getInstance();private final UserRepository userRepository = UserRepositoryImpl.getInstance();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.ID_TOKEN, map.get(ProfitMandiConstants.TOKEN).toString());String responseString = restClient.get(URI, params);try {JsonNode jsonNode = objectMapper.readTree(responseString);SocialUser socialUser = new SocialUser();if(jsonNode.has("email")){socialUser.setEmailId(jsonNode.get("email").asText());}if(jsonNode.has("name")){socialUser.setName(jsonNode.get("name").asText());}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");}}}