com.flyfish.oauth.common.sync.SyncSender Maven / Gradle / Ivy
package com.flyfish.oauth.common.sync;
import com.flyfish.oauth.client.RestClient;
import com.flyfish.oauth.common.OAuthContext;
import com.flyfish.oauth.configuration.OAuth2SsoProperties;
import com.flyfish.oauth.configuration.sync.SyncUserProvider;
import com.flyfish.oauth.configuration.sync.user.AbstractUserProvider;
import com.flyfish.oauth.domain.raw.LocalUser;
import com.flyfish.oauth.filter.SyncUserFilter;
import com.flyfish.oauth.utils.CastUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* 同步发送者,需要主动发送时调用的逻辑
*
* @author wangyu
*/
@Slf4j
public class SyncSender {
/**
* 发送,返回状态码
*
* @return 结果
*/
public static boolean send(List users) {
if (CollectionUtils.isNotEmpty(users)) {
SyncUserProvider provider = SyncUserFilter.sharedProvider();
if (provider instanceof AbstractUserProvider) {
AbstractUserProvider standardProvider = CastUtils.cast(provider);
List localUsers = standardProvider.transform(users);
return innerSend(localUsers);
}
}
return false;
}
public static boolean sendAll() {
SyncUserProvider provider = SyncUserFilter.sharedProvider();
if (null != provider) {
return innerSend(provider.provide());
}
return false;
}
private static boolean innerSend(List users) {
OAuth2SsoProperties properties = OAuthContext.getProperties();
if (CollectionUtils.isEmpty(users) || null == properties) {
return false;
}
try {
Map result = RestClient.create()
.method(RestClient.HttpMethod.PUT)
.url(properties.getSyncUri())
.withCredentials()
.body(users)
.build()
.executeForMap();
return result != null && BooleanUtils.isTrue((Boolean) result.get("success"));
} catch (IOException e) {
log.error("【OAuth Rest】同步用户失败!", e);
} catch (IllegalStateException e) {
log.error("【OAuth Rest】通信时发生异常,请检查配置!");
}
return false;
}
}