shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.walker.remote;
 
import com.walker.api.client.ResponseData;
import com.walker.infrastructure.utils.StringUtils;
import org.apache.hc.client5.http.cookie.CookieStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.Map;
 
/**
 * 同步远程调用任务实现。
 * @author shikeying
 * @date 2015-9-2
 *
 */
public abstract class RemoteSyncTask<T> 
//implements ContextAware 
{
 
    private AbstractByteCoder contentCoder;
    
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    
    // 本次请求的简单参数,此参数优先处理,如果不存在才会处理其他参数
    private Map<String, String> simpleData;
    
    // 是否提示网络未连接状态,默认提示
//    private boolean showNetworkStatusTip = true;
    
    // 设置标记,是否服务端出现连接异常
    private boolean serverFailed = false;
    
    public boolean isServerFailed() {
        return serverFailed;
    }
 
    /**
     * 返回简单的请求参数,此参数优先处理,如果不存在才会处理其他参数
     * @return
     */
    public Map<String, String> getSimpleData() {
        return simpleData;
    }
    
    protected AbstractByteCoder getContentCoder() {
        return contentCoder;
    }
    
    public void setSimpleData(Map<String, String> simpleData) {
        this.simpleData = simpleData;
    }
 
    public void setContext() {
//        this.context = context;
//        message = ToastMessage.getInstance(context);
    }
    
    public RemoteSyncTask<T> setContentCoder(AbstractByteCoder contentCoder) {
        this.contentCoder = contentCoder;
        return this;
    }
    
    public T execute(String url, String jsonData){
        if(StringUtils.isEmpty(url)){
            throw new RemoteAccessorException("缺少请求http调用参数");
        }
//        if(!AppUtils.isActiveConnected()){
//            return null;
//        }
        String result = null;
        try{
            result = requestForData(simpleData, url, jsonData);
        } catch(RemoteAccessorException e){
            e.printStackTrace();
            if(e.isServerFailed()){
                serverFailed = true;
                logger.debug("服务端连接异常");
            } else
                logger.debug(e.getMessage(), e);
        }
        
        if(result != null){
            return processDataUI(result);
        } else {
            responseEmpty();
            return null;
        }
    }
    
    public T execute(String url, String jsonData, CookieStore cookieStore){
        if(StringUtils.isEmpty(url)){
            throw new RemoteAccessorException("缺少请求http调用参数");
        }
        ResultData result = null;
        try{
            result = requestForDataAndCookie(simpleData, url, jsonData, cookieStore);
        } catch(RemoteAccessorException e){
            e.printStackTrace();
            if(e.isServerFailed()){
                serverFailed = true;
                logger.debug("服务端连接异常");
            } else
                logger.debug(e.getMessage(), e);
        }
        
        if(result != null){
            return processDataUI(result.getHtml());
        } else {
            responseEmpty();
            return null;
        }
    }
    
    public T execute(String url, Map<String, String> simpleData, String jsonData
            , String methodType, String contentType, Map<String, String> header){
        if(StringUtils.isEmpty(url)){
            throw new RemoteAccessorException("缺少请求http调用参数");
        }
        String result = null;
        try{
            result = requestForData(simpleData, url, jsonData, methodType, contentType, header);
        } catch(RemoteAccessorException e){
            e.printStackTrace();
            if(e.isServerFailed()){
                serverFailed = true;
                logger.debug("服务端连接异常");
            } else
                logger.debug(e.getMessage(), e);
        }
        
        if(result != null){
            return processDataUI(result);
        } else {
            responseEmpty();
            return null;
        }
    }
    
    /**
     * 返回调用结果之后,回掉该函数,可以操作UI界面处理数据
     * @param data
     */
    protected T processDataUI(String data){
//        if(data instanceof String){
            String result = data.toString();
            if(!StringUtils.isEmpty(result)){
//                ResponseData<T> responseData = createResponseData(result);
                logger.debug("============= 执行了方法:createResponseData(),result = " + result);
                if(responseData == null){
                    throw new IllegalStateException("please implementation createResponseData()!");
                }
                responseData.toObjectFromJson(result);
                
                if(!responseData.getStatus()){
                    logger.debug(responseData.getMessage());
                }
                return (responseData.getResultData());
            }
//        } 
//        else if(data instanceof Uri){
//            // 对于图片等文件形式的内容,统一返回URI
////            Uri uri = (Uri)data;
////            success((T)uri);
//            throw new UnsupportedOperationException("不支持的数据返回类型:URI");
//            
//        } else if(data instanceof ResultDownload){
//            // 下载文件,返回特定对象
////            ResultDownload result = (ResultDownload)data;
////            success((T)result);
//            throw new UnsupportedOperationException("不支持的数据返回类型:ResultDownload");
//        }
        return null;
    }
 
    /**
     * 具体的请求远程数据调用操作,由子类负责实现。
     * @param simpleData 提交的简单参数
//     * @param params 其他参数,params[0] = url, params[1] = json字符串参数
     * @param url
     * @param jsonData
     * @return
     */
    protected abstract String requestForData(Map<String, String> simpleData
            , String url, String jsonData) throws RemoteAccessorException;
    
    /**
     * 时克英修改,增加更多请求参数,支持不同方法,如:put/post/delete等
     * @param simpleData
     * @param url
     * @param jsonData
     * @param contentType
     * @param header
     * @return
     * @throws RemoteAccessorException
     */
    protected abstract String requestForData(Map<String, String> simpleData
            , String url, String jsonData
            , String methodType, String contentType, Map<String, String> header) throws RemoteAccessorException;
    
    protected abstract ResultData requestForDataAndCookie(Map<String, String> simpleData
            , String url, String jsonData
            , CookieStore cookieStore) throws RemoteAccessorException;
    
    /**
     * 响应返回空数据之后,调用该函数。
     */
    protected abstract void responseEmpty();
    
    /**
     * 创建一个特定的响应对象,该对象中包含的业务返回的pojo对象。
     * @param jsonObj
     * @return
     */
//    protected abstract <T> ResponseData<T> createResponseData(String jsonObj);
    
    private ResponseData<T> responseData;
    public void setResponseData(ResponseData<T> responseData){
        this.responseData = responseData;
    }
}