Skip to content

Commit

Permalink
Merge pull request #141 from phuonghuynh/master
Browse files Browse the repository at this point in the history
Fix urgent issues when user login using G+ account
  • Loading branch information
phuonghuynh committed Jan 9, 2015
2 parents 884823e + 038f582 commit 7dffca5
Show file tree
Hide file tree
Showing 11 changed files with 353 additions and 358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,65 +30,65 @@
*/
public abstract class AbstractSocialService implements SocialService {

@Resource
protected UserService userService;
@Resource
protected UserService userService;

@Resource
protected Mapper dozerBeanMapper;
@Resource
protected Mapper dozerBeanMapper;

@Resource
protected TextEncryptor textEncryptor;
@Resource
protected TextEncryptor textEncryptor;

protected SocialConfig socialConfig;
protected SocialConfig socialConfig;

public AbstractSocialService(JsonConfigRepository jsonConfigRepository, SocialProvider socialProvider) {
socialConfig = jsonConfigRepository.getSocialConfig().stream()
.filter(config -> socialProvider == config.getProvider()).findFirst().get();
}
public AbstractSocialService(JsonConfigRepository jsonConfigRepository, SocialProvider socialProvider) {
socialConfig = jsonConfigRepository.getSocialConfig().stream()
.filter(config -> socialProvider == config.getProvider()).findFirst().get();
}

public com.techlooper.entity.AccessGrant getAccessGrant(String accessCode) {
AccessGrant access = getOAuth2ConnectionFactory().getOAuthOperations().exchangeForAccess(accessCode, socialConfig.getRedirectUri(), null);
return dozerBeanMapper.map(access, com.techlooper.entity.AccessGrant.class);
}
public com.techlooper.entity.AccessGrant getAccessGrant(String accessCode) {
AccessGrant access = getOAuth2ConnectionFactory().getOAuthOperations().exchangeForAccess(accessCode, socialConfig.getRedirectUri(), null);
return dozerBeanMapper.map(access, com.techlooper.entity.AccessGrant.class);
}

public com.techlooper.entity.AccessGrant getAccessGrant(String accessToken, String accessSecret) {
OAuth1Operations oAuthOperations = getOAuth1ConnectionFactory().getOAuthOperations();
if (Optional.ofNullable(accessToken).isPresent()) {
OAuthToken token = oAuthOperations.exchangeForAccessToken(
new AuthorizedRequestToken(new OAuthToken(accessToken, null), accessSecret), null);
return dozerBeanMapper.map(token, com.techlooper.entity.AccessGrant.class);
}
OAuthToken token = oAuthOperations.fetchRequestToken(socialConfig.getRedirectUri(), null);
String authorizeUrl = oAuthOperations.buildAuthorizeUrl(token.getValue(), OAuth1Parameters.NONE);
return accessGrant().withAuthorizeUrl(authorizeUrl).build();
public com.techlooper.entity.AccessGrant getAccessGrant(String accessToken, String accessSecret) {
OAuth1Operations oAuthOperations = getOAuth1ConnectionFactory().getOAuthOperations();
if (Optional.ofNullable(accessToken).isPresent()) {
OAuthToken token = oAuthOperations.exchangeForAccessToken(
new AuthorizedRequestToken(new OAuthToken(accessToken, null), accessSecret), null);
return dozerBeanMapper.map(token, com.techlooper.entity.AccessGrant.class);
}
OAuthToken token = oAuthOperations.fetchRequestToken(socialConfig.getRedirectUri(), null);
String authorizeUrl = oAuthOperations.buildAuthorizeUrl(token.getValue(), OAuth1Parameters.NONE);
return accessGrant().withAuthorizeUrl(authorizeUrl).build();
}

protected AccessGrant getAccessGrant(com.techlooper.entity.AccessGrant accessGrant) {
return new AccessGrant(accessGrant.getAccessToken(), accessGrant.getScope(), accessGrant.getRefreshToken(), accessGrant.getExpireTime());
}
protected AccessGrant getAccessGrant(com.techlooper.entity.AccessGrant accessGrant) {
return new AccessGrant(accessGrant.getAccessToken(), accessGrant.getScope(), accessGrant.getRefreshToken(), accessGrant.getExpireTime());
}

public OAuth2ConnectionFactory getOAuth2ConnectionFactory() {
throw new UnsupportedOperationException("Method is not supported");
}
public OAuth2ConnectionFactory getOAuth2ConnectionFactory() {
throw new UnsupportedOperationException("Method is not supported");
}

public OAuth1ConnectionFactory getOAuth1ConnectionFactory() {
throw new UnsupportedOperationException("Method is not supported");
}
public OAuth1ConnectionFactory getOAuth1ConnectionFactory() {
throw new UnsupportedOperationException("Method is not supported");
}

public abstract UserProfile getProfile(com.techlooper.entity.AccessGrant accessGrant);
public abstract UserProfile getProfile(com.techlooper.entity.AccessGrant accessGrant);

public UserEntity saveFootprint(com.techlooper.entity.AccessGrant accessGrant, String key) {
UserEntity entity = userService.findUserEntityByKey(key);
if (!Optional.ofNullable(entity).isPresent()) {
throw new EntityNotFoundException("Can not find User by key: " + key);
}
public UserEntity saveFootprint(com.techlooper.entity.AccessGrant accessGrant, String key) {
UserEntity entity = userService.findUserEntityByKey(key);
if (!Optional.ofNullable(entity).isPresent()) {
throw new EntityNotFoundException("Can not find User by key: " + key);
}

CompletableFuture.supplyAsync(() -> getProfile(accessGrant)).thenAccept((profile) -> {
userEntity(entity).withProfile(socialConfig.getProvider(), profile);
userService.save(entity);
});
CompletableFuture.supplyAsync(() -> getProfile(accessGrant)).thenAccept((profile) -> {
userEntity(entity).withProfile(socialConfig.getProvider(), profile);
userService.save(entity);
});

userEntity(entity).withProfile(socialConfig.getProvider(), null);
return entity;
}
userEntity(entity).withProfile(socialConfig.getProvider(), null);
return entity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.techlooper.entity.AccessGrant;
import com.techlooper.entity.UserEntity;
import com.techlooper.entity.UserProfile;
import com.techlooper.model.SocialProvider;
import com.techlooper.repository.JsonConfigRepository;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.support.OAuth2ConnectionFactory;
Expand Down Expand Up @@ -53,12 +52,12 @@ public UserEntity saveFootprint(AccessGrant accessGrant) {
com.techlooper.entity.FacebookProfile profileEntity = (com.techlooper.entity.FacebookProfile) getProfile(accessGrant);
UserEntity userEntity = Optional.ofNullable(userService.findById(profileEntity.getEmail())).orElse(new UserEntity());
UserEntity.UserEntityBuilder builder = userEntity(userEntity)
.withProfile(SocialProvider.FACEBOOK, profileEntity)
.withProfile(socialConfig.getProvider(), profileEntity)
.withAccessGrant(dozerBeanMapper.map(accessGrant, AccessGrant.class));
if (!Optional.ofNullable(userEntity.getEmailAddress()).isPresent()) {
dozerBeanMapper.map(profileEntity, userEntity);
builder.withId(profileEntity.getEmail())
.withLoginSource(SocialProvider.FACEBOOK);
.withLoginSource(socialConfig.getProvider());
}
userService.save(userEntity);
return userEntity;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/techlooper/service/impl/GitHubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import static com.techlooper.entity.UserEntity.UserEntityBuilder.userEntity;
import static com.techlooper.model.SocialProvider.GITHUB;
Expand Down Expand Up @@ -76,15 +75,15 @@ private UserEntity createUserEntity(AccessGrant accessGrant, Connection<GitHub>
com.techlooper.entity.GitHubUserProfile profileEntity = dozerBeanMapper.map(profile, com.techlooper.entity.GitHubUserProfile.class);
UserEntity userEntity = Optional.ofNullable(userService.findById(profileEntity.getEmail())).orElse(new UserEntity());
UserEntity.UserEntityBuilder builder = userEntity(userEntity)
.withProfile(GITHUB, profileEntity)
.withProfile(socialConfig.getProvider(), profileEntity)
.withAccessGrant(dozerBeanMapper.map(accessGrant, AccessGrant.class));
if (!Optional.ofNullable(userEntity.getEmailAddress()).isPresent()) {
String profileImageUrl = connection.getApi().restOperations()
.getForObject(socialConfig.getApiUrl().get("users"), JsonNode.class, profileEntity.getUsername()).get("avatar_url").asText();
profileEntity.setProfileImageUrl(profileImageUrl);
dozerBeanMapper.map(profileEntity, userEntity);
builder.withId(profileEntity.getEmail())
.withLoginSource(GITHUB);
.withLoginSource(socialConfig.getProvider());
}
return userEntity;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/techlooper/service/impl/GoogleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public UserEntity saveFootprint(AccessGrant accessGrant) {
Person profile = (Person) ((SimpleUserProfile) getProfile(accessGrant)).getActual();
UserEntity userEntity = Optional.ofNullable(userService.findById(profile.getAccountEmail())).orElse(new UserEntity());
UserEntity.UserEntityBuilder builder = userEntity(userEntity)
.withProfile(SocialProvider.GOOGLE, profile)
.withProfile(socialConfig.getProvider(), profile)
.withAccessGrant(dozerBeanMapper.map(accessGrant, AccessGrant.class));
if (!Optional.ofNullable(userEntity.getEmailAddress()).isPresent()) {
dozerBeanMapper.map(profile, userEntity);
builder.withId(profile.getAccountEmail())
.withLoginSource(SocialProvider.GITHUB);
.withLoginSource(socialConfig.getProvider());
}
userService.save(userEntity);
return userEntity;
Expand Down
152 changes: 76 additions & 76 deletions src/main/java/com/techlooper/service/impl/JobQueryBuilderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,80 +29,80 @@
@Component
public class JobQueryBuilderImpl implements JobQueryBuilder {

@Value("${elasticsearch.index.name}")
private String elasticSearchIndexName;

@Resource
private JsonConfigRepository jsonConfigRepository;

public FilterBuilder getTechnicalTermsQuery() {
BoolFilterBuilder boolFilter = FilterBuilders.boolFilter();
jsonConfigRepository.getSkillConfig().stream().map(this::getTechnicalTermQuery).forEach(boolFilter::should);
return boolFilter;
}

public FilterBuilder getTechnicalTermQuery(TechnicalTerm term) {
BoolFilterBuilder boolFilter = FilterBuilders.boolFilter();
term.getSearchTexts().stream().map(termName ->
FilterBuilders.queryFilter(QueryBuilders.multiMatchQuery(termName, SEARCH_JOB_FIELDS)
.operator(MatchQueryBuilder.Operator.AND))).forEach(boolFilter::should);
return boolFilter;
}

public FilterBuilder getTechnicalSkillQuery(Skill skill) {
return FilterBuilders.queryFilter(
QueryBuilders.multiMatchQuery(skill.getName(), SEARCH_JOB_FIELDS).operator(MatchQueryBuilder.Operator.AND)).cache(true);
}

public NativeSearchQueryBuilder getVietnamworksJobCountQuery() {
return new NativeSearchQueryBuilder().withIndices(elasticSearchIndexName).withTypes("job").withSearchType(SearchType.COUNT);
}

public AggregationBuilder getTechnicalTermAggregation(TechnicalTerm term) {
return AggregationBuilders.filter(term.getKey()).filter(this.getTechnicalTermQuery(term));
}

/**
* Constructs a query based on a specific period.
*
* @param skill is the detail of term, for example Java is a term and spring is a skill
* @param skillQuery {@link org.elasticsearch.index.query.QueryBuilder}
* @param histogramEnum {@link com.techlooper.model.HistogramEnum}
* @param total {@link com.techlooper.model.HistogramEnum#getTotal()}
* @return {@link org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder}
*/
private FilterAggregationBuilder getSkillIntervalAggregation(Skill skill, FilterBuilder skillQuery,
HistogramEnum histogramEnum, Integer total) {
String intervalDate = LocalDate.now().minusDays(total).format(DateTimeFormatter.ofPattern("YYYYMMdd"));
String to = "now-" + (total * histogramEnum.getPeriod()) + histogramEnum.getUnit();
RangeFilterBuilder approveDateQuery = FilterBuilders.rangeFilter("approvedDate").to(to).cache(true);
RangeFilterBuilder expiredDateQuery = FilterBuilders.rangeFilter("expiredDate").gte(to).cache(true);
BoolFilterBuilder filterQuery = FilterBuilders.boolFilter().must(skillQuery, approveDateQuery, expiredDateQuery);
return AggregationBuilders.filter(EncryptionUtils.encodeHexa(skill.getName()) + "-" + histogramEnum + "-" + intervalDate).filter(filterQuery);
}

public List<List<FilterAggregationBuilder>> toSkillAggregations(List<Skill> skills, HistogramEnum histogramEnum) {
Integer total = histogramEnum.getTotal();
return skills.stream().map(skill -> {
FilterBuilder skillQuery = this.getTechnicalSkillQuery(skill);
List<FilterAggregationBuilder> builders = new LinkedList<>();
for (int histogramEnumLengthCounter = 0; histogramEnumLengthCounter < total; ++histogramEnumLengthCounter) {
builders.add(this.getSkillIntervalAggregation(skill, skillQuery, histogramEnum, histogramEnumLengthCounter));
}
return builders;
}).collect(toList());
}

public FilterBuilder getExpiredDateQuery(String from) {
return FilterBuilders.rangeFilter("expiredDate").from(from).cache(true);
}

public FilterBuilder getTechnicalTermsQueryNotExpired() {
BoolFilterBuilder allTerms = (BoolFilterBuilder) this.getTechnicalTermsQuery();
return allTerms.must(this.getExpiredDateQuery("now"));
}

public FilterBuilder getTechnicalTermQueryNotExpired(TechnicalTerm term) {
return FilterBuilders.boolFilter().must(this.getTechnicalTermQuery(term), this.getExpiredDateQuery("now"));
}
@Value("${elasticsearch.index.name}")
private String elasticSearchIndexName;

@Resource
private JsonConfigRepository jsonConfigRepository;

public FilterBuilder getTechnicalTermsQuery() {
BoolFilterBuilder boolFilter = FilterBuilders.boolFilter();
jsonConfigRepository.getSkillConfig().stream().map(this::getTechnicalTermQuery).forEach(boolFilter::should);
return boolFilter;
}

public FilterBuilder getTechnicalTermQuery(TechnicalTerm term) {
BoolFilterBuilder boolFilter = FilterBuilders.boolFilter();
term.getSearchTexts().stream().map(termName ->
FilterBuilders.queryFilter(QueryBuilders.multiMatchQuery(termName, SEARCH_JOB_FIELDS)
.operator(MatchQueryBuilder.Operator.AND))).forEach(boolFilter::should);
return boolFilter;
}

public FilterBuilder getTechnicalSkillQuery(Skill skill) {
return FilterBuilders.queryFilter(
QueryBuilders.multiMatchQuery(skill.getName(), SEARCH_JOB_FIELDS).operator(MatchQueryBuilder.Operator.AND)).cache(true);
}

public NativeSearchQueryBuilder getVietnamworksJobCountQuery() {
return new NativeSearchQueryBuilder().withIndices(elasticSearchIndexName).withTypes("job").withSearchType(SearchType.COUNT);
}

public AggregationBuilder getTechnicalTermAggregation(TechnicalTerm term) {
return AggregationBuilders.filter(term.getKey()).filter(this.getTechnicalTermQuery(term));
}

/**
* Constructs a query based on a specific period.
*
* @param skill is the detail of term, for example Java is a term and spring is a skill
* @param skillQuery {@link org.elasticsearch.index.query.QueryBuilder}
* @param histogramEnum {@link com.techlooper.model.HistogramEnum}
* @param total {@link com.techlooper.model.HistogramEnum#getTotal()}
* @return {@link org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder}
*/
private FilterAggregationBuilder getSkillIntervalAggregation(Skill skill, FilterBuilder skillQuery,
HistogramEnum histogramEnum, Integer total) {
String intervalDate = LocalDate.now().minusDays(total).format(DateTimeFormatter.ofPattern("YYYYMMdd"));
String to = "now-" + (total * histogramEnum.getPeriod()) + histogramEnum.getUnit();
RangeFilterBuilder approveDateQuery = FilterBuilders.rangeFilter("approvedDate").to(to).cache(true);
RangeFilterBuilder expiredDateQuery = FilterBuilders.rangeFilter("expiredDate").gte(to).cache(true);
BoolFilterBuilder filterQuery = FilterBuilders.boolFilter().must(skillQuery, approveDateQuery, expiredDateQuery);
return AggregationBuilders.filter(EncryptionUtils.encodeHexa(skill.getName()) + "-" + histogramEnum + "-" + intervalDate).filter(filterQuery);
}

public List<List<FilterAggregationBuilder>> toSkillAggregations(List<Skill> skills, HistogramEnum histogramEnum) {
Integer total = histogramEnum.getTotal();
return skills.stream().map(skill -> {
FilterBuilder skillQuery = this.getTechnicalSkillQuery(skill);
List<FilterAggregationBuilder> builders = new LinkedList<>();
for (int histogramEnumLengthCounter = 0; histogramEnumLengthCounter < total; ++histogramEnumLengthCounter) {
builders.add(this.getSkillIntervalAggregation(skill, skillQuery, histogramEnum, histogramEnumLengthCounter));
}
return builders;
}).collect(toList());
}

public FilterBuilder getExpiredDateQuery(String from) {
return FilterBuilders.rangeFilter("expiredDate").from(from).cache(true);
}

public FilterBuilder getTechnicalTermsQueryNotExpired() {
BoolFilterBuilder allTerms = (BoolFilterBuilder) this.getTechnicalTermsQuery();
return allTerms.must(this.getExpiredDateQuery("now"));
}

public FilterBuilder getTechnicalTermQueryNotExpired(TechnicalTerm term) {
return FilterBuilders.boolFilter().must(this.getTechnicalTermQuery(term), this.getExpiredDateQuery("now"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public UserEntity saveFootprint(AccessGrant accessGrant) {
LinkedInProfile profileEntity = (LinkedInProfile) getProfile(accessGrant);
UserEntity entity = Optional.ofNullable(userService.findById(profileEntity.getEmailAddress())).orElse(new UserEntity());
UserEntityBuilder builder = userEntity(entity)
.withProfile(LINKEDIN, profileEntity)
.withProfile(socialConfig.getProvider(), profileEntity)
.withAccessGrant(dozerBeanMapper.map(accessGrant, AccessGrant.class));
if (!Optional.ofNullable(entity.getEmailAddress()).isPresent()) {
dozerBeanMapper.map(profileEntity, entity);
builder.withId(profileEntity.getEmailAddress())
.withLoginSource(LINKEDIN);
.withLoginSource(socialConfig.getProvider());
}
userService.save(entity);
return entity;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/techlooper/service/impl/TwitterService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.techlooper.entity.AccessGrant;
import com.techlooper.entity.UserEntity;
import com.techlooper.entity.UserProfile;
import com.techlooper.model.SocialProvider;
import com.techlooper.repository.JsonConfigRepository;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.support.OAuth1ConnectionFactory;
Expand Down Expand Up @@ -51,12 +50,12 @@ public UserEntity saveFootprint(AccessGrant accessGrant) {
String userId = TWITTER.name() + "-" + profile.getId();
UserEntity userEntity = Optional.ofNullable(userService.findById(userId)).orElse(new UserEntity());
UserEntity.UserEntityBuilder builder = userEntity(userEntity)
.withProfile(SocialProvider.TWITTER, profile)
.withProfile(socialConfig.getProvider(), profile)
.withAccessGrant(dozerBeanMapper.map(accessGrant, AccessGrant.class));
if (!Optional.ofNullable(userEntity.getEmailAddress()).isPresent()) {
dozerBeanMapper.map(profile, userEntity);
builder.withId(userId)
.withLoginSource(SocialProvider.TWITTER);
.withLoginSource(socialConfig.getProvider());
}
userService.save(userEntity);
return userEntity;
Expand Down
Loading

0 comments on commit 7dffca5

Please sign in to comment.