NIMSDK-AOS  10.10.13
V2NIMProxyRequest.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.v2.passthrough.model;
2 
3 import android.text.TextUtils;
4 import com.netease.nimlib.log.NimLog;
6 import com.netease.nimlib.util.StringUtil;
7 import java.io.Serializable;
8 
9 /**
10  * 代理的http请求参数
11  */
12 public class V2NIMProxyRequest implements Serializable {
13 
14  private static final String TAG = "V2NIMProxyRequest";
15 
16 
17  /**
18  * 映射一个upstream host,不传用默认配置
19  */
20  private String zone;
21 
22  /**
23  * url中除了host的path path不能为空, 为空返回参数错误
24  */
25  private final String path;
26 
27  /**
28  * 请求方式 GET请求, 如果body非空, 则返回参数错误 POST/PUT, 如果body为空,范围参数错误
29  */
31 
32  /**
33  * 请求头,必须要求json格式 非json格式返回参数错误
34  */
35  private String header;
36 
37  /**
38  * 请求体
39  */
40  private String body;
41 
42 
43  /**
44  * 构造函数
45  *
46  * @param zone 映射一个upstream host, 不传使用默认配置
47  * @param path url中除了host的path
48  * @param method 请求方式
49  * @param header 请求头
50  * @param body 请求体
51  */
52  public V2NIMProxyRequest(String zone, String path, V2NIMProxyRequestMethod method, String header, String body) {
53  this.zone = zone;
54  this.path = path;
55  this.method = method;
56  this.header = header;
57  this.body = body;
58  }
59 
60  /**
61  * 构造函数
62  *
63  * @param path url中除了host的path
64  * @param method 请求方式
65  * @param header 请求头
66  * @param body 请求体
67  */
68  public V2NIMProxyRequest(String path, V2NIMProxyRequestMethod method, String header, String body) {
69  this(null, path, method, header, body);
70  }
71 
72  /**
73  * 构造函数
74  *
75  * @param path url中除了host的path
76  * @param header 请求头
77  * @param body 请求体
78  */
79  public V2NIMProxyRequest(String path, String header, String body) {
81  }
82 
83  /**
84  * 构造函数
85  *
86  * @param path url中除了host的path
87  */
88  public V2NIMProxyRequest(String path) {
89  this(path, null, null);
90  }
91 
92  private V2NIMProxyRequest() {
93  this.path = null;
95  }
96 
97  /**
98  * 获取zone
99  *
100  * @return zone 映射一个upstream host
101  */
102  public String getZone() {
103  return zone;
104  }
105 
106  /**
107  * 设置zone
108  *
109  * @param zone 映射一个upstream host
110  */
111  public void setZone(String zone) {
112  this.zone = zone;
113  }
114 
115  /**
116  * 获取path
117  *
118  * @return path url中除了host的path
119  */
120  public String getPath() {
121  return path;
122  }
123 
124  /**
125  * 获取method
126  *
127  * @return method 请求方式
128  */
130  if (method == null) {
132  }
133  return method;
134  }
135 
136  /**
137  * 设置method
138  *
139  * @param method 请求方式
140  */
141  public void setMethod(V2NIMProxyRequestMethod method) {
142  this.method = method;
143  }
144 
145  /**
146  * 获取header
147  *
148  * @return header 请求头
149  */
150  public String getHeader() {
151  return header;
152  }
153 
154  /**
155  * 设置header
156  *
157  * @param header 请求头
158  */
159  public void setHeader(String header) {
160  this.header = header;
161  }
162 
163  /**
164  * 获取body
165  *
166  * @return body 请求体
167  */
168  public String getBody() {
169  return body;
170  }
171 
172  /**
173  * 设置body
174  *
175  * @param body 请求体
176  */
177  public void setBody(String body) {
178  this.body = body;
179  }
180 
181  /**
182  * 判断是否有效
183  *
184  * @return
185  */
186  public boolean isValid() {
187  if (TextUtils.isEmpty(path)) {
188  NimLog.e(TAG, "path is empty");
189  return false;
190  }
191  if (getMethod() == V2NIMProxyRequestMethod.V2NIM_PROXY_REQUEST_METHOD_GET && !TextUtils.isEmpty(body)) {
192  NimLog.e(TAG, "body is not empty when method is GET");
193  return false;
194  }
195  if (getMethod() == V2NIMProxyRequestMethod.V2NIM_PROXY_REQUEST_METHOD_POST && TextUtils.isEmpty(body)) {
196  NimLog.e(TAG, "body is empty when method is POST");
197  return false;
198  }
199  if (getMethod() == V2NIMProxyRequestMethod.V2NIM_PROXY_REQUEST_METHOD_PUT && TextUtils.isEmpty(body)) {
200  NimLog.e(TAG, "body is empty when method is PUT");
201  return false;
202  }
203  if (!TextUtils.isEmpty(header) && !StringUtil.isJson(header)) {
204  NimLog.e(TAG, "header is not json");
205  return false;
206  }
207  return true;
208  }
209 
210  /**
211  * 构建请求参数Builder
212  */
213  public static class Builder {
214 
215  private final V2NIMProxyRequest params;
216 
217  /**
218  * 构造函数
219  *
220  * @param path url中除了host的path
221  */
222  public Builder(String path) {
223  params = new V2NIMProxyRequest(path);
224  }
225 
226  /**
227  * 设置zone
228  *
229  * @param zone 映射一个upstream host
230  */
231  public Builder withZone(String zone) {
232  params.setZone(zone);
233  return this;
234  }
235 
236  /**
237  * 设置method
238  *
239  * @param method 请求方式
240  */
241  public Builder withMethod(V2NIMProxyRequestMethod method) {
242  params.setMethod(method);
243  return this;
244  }
245 
246  /**
247  * 设置header
248  *
249  * @param header 请求头
250  */
251  public Builder withHeader(String header) {
252  params.setHeader(header);
253  return this;
254  }
255 
256  /**
257  * 设置body
258  *
259  * @param body 请求体
260  */
261  public Builder withBody(String body) {
262  params.setBody(body);
263  return this;
264  }
265 
266  /**
267  * 构建请求参数
268  *
269  * @return 请求参数
270  */
271  public V2NIMProxyRequest build() {
272  return params;
273  }
274  }
275 
276 }
void setMethod(V2NIMProxyRequestMethod method)
设置method
V2NIMProxyRequest(String zone, String path, V2NIMProxyRequestMethod method, String header, String body)
构造函数
V2NIMProxyRequest(String path, V2NIMProxyRequestMethod method, String header, String body)
构造函数
V2NIMProxyRequest(String path, String header, String body)
构造函数