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

com.github.nezha.httpfetch.HttpApiConfiguration Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package com.github.nezha.httpfetch;

import com.github.nezha.httpfetch.chains.*;
import com.github.nezha.httpfetch.convertor.DefaultResponseGeneratorConvertor;
import com.github.nezha.httpfetch.convertor.ResponseGeneratorConvertor;
import com.github.nezha.httpfetch.resolver.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by daiqiang on 16/12/6.
 */
public class HttpApiConfiguration {

    private List sourceReaders;

    /**
     * 请求拦截器,做切面处理
     */
    private List chains;

    /**
     * 结果集处理类,如果需要不同的结果转换时,可以继承并注册
     */
    private List convertors;

    /**
     * 入参处理类,如果需要扩展参数的转换方式时可以继承并注册
     */
    private List parameterResolvers;

    private Map urlAlias;

    public void init() {
        chains = new ArrayList<>();
        convertors = new ArrayList<>();
        parameterResolvers = new ArrayList<>();
        urlAlias = new HashMap<>();

        if (!sourceReaders.isEmpty()) {
            for (SourceReader reader : sourceReaders) {
                if (reader != null) {
                    if (reader.getChains() != null) {
                        chains.addAll(reader.getChains());
                    }
                    if (reader.getConvertors() != null) {
                        convertors.addAll(reader.getConvertors());
                    }
                    if (reader.getParameterResolvers() != null) {
                        parameterResolvers.addAll(reader.getParameterResolvers());
                    }
                    if (reader.getUrlAlias() != null) {
                        urlAlias.putAll(reader.getUrlAlias());
                    }
                }
            }
        }

        //默认的链
        chains.add(new BeanParamParseChain());
        chains.add(new UrlWrapperChain(this));
        chains.add(new ParameterResolverChain(this));
        chains.add(new GenerateResponseChain(this));
        chains.add(new ExecuteRequestChain());

        //默认参数解析类
        parameterResolvers.add(new UrlParameterResolver());
        parameterResolvers.add(new ArrayParameterResolver());
        parameterResolvers.add(new RequestBodyParameterResolver());
        parameterResolvers.add(new DefaultMethodParameterResolver());

        //默认结果解析器
        convertors.add(new DefaultResponseGeneratorConvertor());

    }

    public List getChains() {
        return chains;
    }

    public List getConvertors() {
        return convertors;
    }

    public List getParameterResolvers() {
        return parameterResolvers;
    }

    public Map getUrlAlias() {
        return urlAlias;
    }

    public List getSourceReaders() {
        return sourceReaders;
    }

    public void setSourceReaders(List sourceReaders) {
        this.sourceReaders = sourceReaders;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy