-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestWithBody.java
267 lines (180 loc) · 6.05 KB
/
RequestWithBody.java
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import java.net.*;
import java.util.*;
import java.io.*;
/**
* This method represent a Request with body.
* Body kinds: form-data, Json, binary-file.
* You can't sent a 'GET' method with this class
*
*
*
* @author Mohammad Mahdi Malmasi
* @version 0.2.5
*/
public class RequestWithBody extends Request
{
/* Feilds */
// kind of the request body
private String bodyKind;
// data of body
private String bodyDatas;
// body of the request
private RequestBody requestBody;
/* Constructor */
/**
* Create a new Request with body and given details
* Method of this Request can't be "GET" (becuse it has body)
*
*
* @param name : a name for this request
* @param group : name of the this request group
* @param url : url for send this requst
* @param method : method of this request (POST, PUT, DELETE)
* @param headers : a {@code String} of request headers in this format: "header:value;header1:value1; ..."
* @param requestBodyKind : kind of the body requset
* @param bodyDatas : datas to send as rquest body
* @param query : a {@code String} of request query in this format: "name=value&name1=value1& ..."
* @param followRedirect : set it {@code true} if you want follow redirect
* @param terminalShow : show the result on terminal or not
* @param reqDetails : user given args for this requset
*
* @throws IOException
*/
public RequestWithBody(String name,
String group,
String url,
String method,
String headers,
String requestBodyKind,
String bodyDatas,
String query,
boolean followRedirect,
boolean terminalShow,
String reqDetails) throws IOException
{
super(name, group, url, (headers == null ? "": headers), query, followRedirect, terminalShow, reqDetails);
super.setRequestKind(method);
this.bodyKind = requestBodyKind;
this.bodyDatas = bodyDatas;
setBodyObject();
}
/* Methods */
// * getter methods *
/**
* @return the content type of this requset
*/
public String getContentType()
{
if (requestBody != null)
return this.requestBody.getContentType();
return "no body content";
}
/**
* @return the content length of this request in byte
*/
public Long getContentLength()
{
return this.requestBody.getContentLength();
}
/**
* This method send the request and read the body
*/
@Override
public void run()
{
HttpURLConnection.setFollowRedirects(false);
if (check)
{
try{ url = new URL(urlString); }
catch(MalformedURLException e) { Out.printErrors("internet"); }
}
try{ connection = (HttpURLConnection) url.openConnection(); }
catch (IOException e) { Out.printErrors("internet"); }
try{ connection.setRequestMethod(RequestKinds.getKind(requestKind)); }
catch (ProtocolException | SecurityException e) { return; }
connection.setDoOutput(true);
setHeaders();
long startTime = System.nanoTime(), endTime = 0;
if (requestBody != null)
{
requestBody.build();
try
{
requestBody.set(connection.getOutputStream());
endTime = System.nanoTime();
}
catch(IOException e) { Out.printErrors("reqbody"); }
}
try
{
connection.connect();
responseCode = connection.getResponseCode();
if (endTime == 0)
endTime = System.nanoTime();
responseMessage = connection.getResponseMessage();
responseSize = connection.getContentLengthLong();
readResponseHeaders();
setResponseBodyFormat();
}
catch(IOException e) { Out.printErrors("internet"); }
responseTime = (endTime - startTime) / 1000000;
if (check)
{
Out.printRequestDetails(this);
check = false;
}
if (showResponseHeaders)
Out.printResponseDetails(this);
if (followRedirect && (responseCode/100 == 3))
{
String newUrl = connection.getHeaderField("Location");
try { this.url = new URL(newUrl); }
catch (MalformedURLException e) { Out.printErrors("internet"); }
this.run();
}
if (connection.getErrorStream() != null)
{
try{ responseSize = Out.printResponseBody(connection.getErrorStream()); }
catch(IOException e){}
}
else
{
try{ responseSize = Out.printResponseBody(connection.getInputStream()); }
catch(IOException e){}
}
connection.disconnect();
}
/**
* This method set the headers of the request
*/
@Override
protected void setHeaders()
{
super.setHeaders();
if (bodyKind != null)
{
connection.setRequestProperty("Content-Type", this.getContentType());
super.addHeader("Content-Type", this.getContentType());
}
}
// this method set the request body object
private void setBodyObject()
{
if (bodyKind == null)
return;
switch(bodyKind.toLowerCase())
{
case "form-data":
this.requestBody = new FormDataBody(this.bodyDatas);
break;
case "json":
this.requestBody = new JsonBody(this.bodyDatas);
break;
case "binary-file":
this.requestBody = new BinaryFileBody(this.bodyDatas);
break;
default:
this.requestBody = null;
}
}
}