All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.flyfish.oauth.common.sync.AbstractSyncRequestAcceptor Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package com.flyfish.oauth.common.sync;

import com.flyfish.oauth.client.OAuth2SsoClientInitializer;
import com.flyfish.oauth.common.Function;
import com.flyfish.oauth.common.sync.support.CollectionComparator;
import com.flyfish.oauth.common.sync.support.SyncRequestEvent;
import com.flyfish.oauth.configuration.sync.SyncDomainService;
import com.flyfish.oauth.configuration.sync.SyncTransformer;
import com.flyfish.oauth.domain.raw.Domain;
import com.flyfish.oauth.utils.JacksonUtil;
import com.github.scribejava.core.utils.StreamUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 抽象类,实现了基础的查找定位功能
 *
 * @author wangyu
 */
public abstract class AbstractSyncRequestAcceptor implements SyncRequestAcceptor {

    private SyncRequestListener listener;

    /**
     * 比较并返回比较结果,根据不同实现策略进行不同的比较
     *
     * @param            泛型
     * @param originList    原数据
     * @param domainService 数据service
     * @return 结果
     */
    protected abstract  CollectionComparator compare(
            List originList, final SyncDomainService domainService) throws Exception;

    @Override
    public void sync(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doSync(request, response);
    }

    protected  Function identifierFunction(final SyncDomainService service) {
        return new Function() {
            @Override
            public Object apply(T data) {
                return service.identifier(data);
            }
        };
    }

    /**
     * 内部同步
     *
     * @param request 请求
     * @param      泛型,原
     * @param      泛型,现
     * @throws IOException 异常
     */
    private  void doSync(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 事件驱动,构建对象
        SyncRequestEvent.SyncRequestEventBuilder builder = SyncRequestEvent.builder()
                .acceptor(this)
                .request(request)
                .response(response);

        // 解析请求,判断同步类型,并解析出匹配规则
        SyncTransformer transformer = getTransformer(request);
        if (null == transformer) {
            listener.onFailed(builder.error(new IllegalArgumentException("解析规则不存在!")).build());
            return;
        }
        // 解析请求体
        String body = StreamUtils.getStreamContents(request.getInputStream());
        // 如果为空,丢弃
        if (StringUtils.isBlank(body)) {
            listener.onSuccess(builder.success(true).data(Collections.emptyList()).build());
            return;
        }
        // 继续解析
        List data = JacksonUtil.json2List(body, transformer.javaType());
        // 转换
        List parsedResults = transform(data, transformer);
        // 根据请求类型,进行比较处理(基于HttpMethod)
        try {
            CollectionComparator result = this.compare(parsedResults, transformer.syncService());
            listener.onSuccess(builder.success(true).data(parsedResults).comparator(result).build());
        } catch (Exception e) {
            e.printStackTrace();
            listener.onFailed(builder.data(parsedResults).error(e).build());
        }
    }

    @Override
    public void setListener(SyncRequestListener listener) {
        this.listener = listener;
    }

    /**
     * 转换列表
     *
     * @param data 数据
     * @param   原泛型
     * @param   先在型
     * @return 结果
     */
    private  List transform(List data, SyncTransformer transformer) {
        if (CollectionUtils.isNotEmpty(data)) {
            // 组成map,对共享数据以id组,方便同步组织时取得多级上级id
            Map domainMap = new HashMap<>();
            for (T domain : data) {
                domainMap.put(domain.getId(), domain);
            }
            // 解析结果进行转换
            List parsedResults = new ArrayList<>();
            for (T domain : data) {
                parsedResults.add(transformer.transform(domain, domainMap));
            }
            return parsedResults;
        }
        return Collections.emptyList();
    }

    @SuppressWarnings("unchecked")
    private  SyncTransformer getTransformer(HttpServletRequest request) {
        // 抽取uri
        String uri = StringUtils.substringAfterLast(request.getRequestURI(), "sync/");
        // 匹配
        return (SyncTransformer) OAuth2SsoClientInitializer.acceptTransform(uri);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy