Skip to content

Commit d991938

Browse files
authored
Merge pull request #173 from warownia1/hanson1
Hanson1
2 parents 1f84f4d + 5132606 commit d991938

File tree

2 files changed

+77
-32
lines changed

2 files changed

+77
-32
lines changed

sources/net.sf.j2s.java.core/src/javajs/http/HttpClient.java

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,79 @@ public interface HttpRequest {
2626

2727
public HttpRequest addHeader(String name, String value);
2828

29-
public HttpRequest addParameter(String name, String value);
29+
/**
30+
* Add parameter which will be appended to the url query.
31+
*/
32+
public HttpRequest addQueryParameter(String name, String value);
3033

31-
public HttpRequest addFile(String name, File file);
34+
/**
35+
* Add parameter to the request form (either urlencoded or multipart).
36+
*/
37+
public HttpRequest addFormPart(String name, String value);
3238

33-
public HttpRequest addFile(String name, InputStream stream);
39+
/**
40+
* Add file form parameter with specified content type and file name to the
41+
* reqeust converting it into a multipart form .
42+
*/
43+
public HttpRequest addFilePart(String name, File file,
44+
String contentType, String fileName);
3445

35-
public HttpRequest addFormField(String name, Object data, String contentType, String fileName);
46+
/**
47+
* Add file form parameter to the request converting it into a multipart
48+
* form. Content type will be set to "application/octet-stream" and file
49+
* name to the name of the file.
50+
*/
51+
public default HttpRequest addFilePart(String name, File file) {
52+
return addFile(name, file, "application/octet-stream", file.getName());
53+
}
3654

37-
boolean removeFormField(String name);
55+
/**
56+
* Add file form parameter to the request converting it into a multipart
57+
* form.
58+
*/
59+
public HttpRequest addFilePart(String name, InputStream stream,
60+
String contentType, String fileName);
61+
62+
/**
63+
* Add file form parameter to the reqeust converting it into a multipart form.
64+
* The content type will be set to "application/octet-stream" and file name to "file".
65+
*/
66+
public default HttpRequest addFilePart(String name, InputStream stream) {
67+
return addFileParameter(name, stream, "application/octet-stream", "file")
68+
}
69+
70+
/**
71+
* Remove all query parameters having the specified name from the request.
72+
*/
73+
public HttpRequest clearQueryParameters(String name);
74+
75+
/**
76+
* Remove all form parameters having the specified name.
77+
*/
78+
public HttpRequest clearFormParts(String name);
3879

3980
/**
4081
* Send the request to the server and return the response.
4182
*/
4283
public HttpResponse execute() throws IOException;
4384

44-
public void executeAsync(Consumer<HttpResponse> success,
85+
public void executeAsync(Consumer<HttpResponse> success,
4586
BiConsumer<HttpResponse, ? super IOException> failure,
4687
BiConsumer<HttpResponse, ? super IOException> always);
4788
}
4889

4990
public interface HttpResponse extends Closeable {
91+
92+
/**
93+
* Get the status code of the response as per RFC 2616.
94+
*
95+
* @return
96+
*/
5097
public int getStatusCode();
5198

5299
/**
53-
* Get the response headers, combining same-name headers with commas, preserving
54-
* order, as per RFC 2616
100+
* Get the response headers, combining same-name headers with commas,
101+
* preserving order, as per RFC 2616
55102
*
56103
* @return
57104
*/
@@ -83,34 +130,33 @@ public default void close() throws IOException {
83130
}
84131

85132
/**
86-
* Initialises the GET request builder. Usually they have no request body and
133+
* Creates a new GET request. They usually have no body and
87134
* parameters are passed in the URL query.
88135
*/
89136
public HttpRequest get(URI uri);
90137

91138
/**
92-
* Initialises the GET request builder. They have no request body and parameters
93-
* are passed in the URL query. They are identical to GET requests, the only
94-
* difference is that the returned response contains headers only.
139+
* Creates a new HEAD request. They have no request body and
140+
* parameters are passed in the URL query. They are identical to GET requests,
141+
* except they receive no response body.
95142
*/
96143
public HttpRequest head(URI uri);
97144

98145
/**
99-
* Initialises the POST request builder. Usually they contain data in the
146+
* Creates a new POST request. Usually they pass data in the
100147
* request body either as a urlencoded form, a multipart form or raw bytes.
101-
* Currently, we only care about the multipart form.
148+
* Currently, we only care about the multipart and urlencoded forms.
102149
*/
103150
public HttpRequest post(URI uri);
104151

105152
/**
106-
* Initialises the PUT request builder which construct the same way as POST. The
107-
* only difference is the request method.
153+
* Creates a new PUT request which is constructed the same way as POST.
108154
*/
109155
public HttpRequest put(URI uri);
110156

111157
/**
112-
* Initialises the DELETE request builder. The DELETE requests have no body and
113-
* parameters are passed in the URL query, just like GET.
158+
* Creates a new DELETE request. THey have have no body
159+
* and parameters are passed in the URL query, just like GET.
114160
*/
115161
public HttpRequest delete(URI uri);
116162

sources/net.sf.j2s.java.core/src/javajs/http/SimpleHttpClient.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.function.Consumer;
2222
import java.util.function.Function;
2323

24+
2425
/**
2526
* SwingJS implementation of javajs.http.HttpClient and associated classes.
2627
*
@@ -203,29 +204,27 @@ public HttpRequest addHeader(String name, String value) {
203204
}
204205

205206
@Override
206-
public HttpRequest addParameter(String name, String value) {
207+
public HttpRequest addQueryParameter(String name, String value) {
208+
// TODO: implement adding parameters to url query
209+
}
210+
211+
@Override
212+
public HttpRequest addFormPart(String name, String value) {
207213
return addFormField(name, value, null, null);
208214
}
209215

210216
@Override
211-
public HttpRequest addFile(String name, File file) {
212-
return addFormField(name, toBytes(file), "application/octet-stream", file.getName());
217+
public HttpRequest addFilePart(String name, File file, String contentType, String fileName) {
218+
return addFormField(name, toBytes(file), contentType, fileName);
213219
}
214220

215221

216222
@Override
217-
public HttpRequest addFile(String name, InputStream stream) {
218-
return addFormField(name, toBytes(stream), "application/octet-stream", null);
223+
public HttpRequest addFilePart(String name, InputStream stream, String contentType, String fileName) {
224+
return addFormField(name, toBytes(stream), contentType, fileName);
219225
}
220226

221-
/**
222-
* @param name
223-
* @param data can be String, byte[], File, or InputStream
224-
* @param contentType
225-
* @param fileName
226-
*/
227-
@Override
228-
public HttpRequest addFormField(String name, Object data, String contentType, String fileName) {
227+
private HttpRequest addFormField(String name, Object data, String contentType, String fileName) {
229228
if (data == null) {
230229
removeFormField(name);
231230
return this;
@@ -237,7 +236,7 @@ public HttpRequest addFormField(String name, Object data, String contentType, St
237236
}
238237

239238
@Override
240-
public boolean removeFormField(String name) {
239+
public boolean clearParameter(String name) {
241240
if (formData != null)
242241
for (int i = 0; i < formData.size(); i++)
243242
if (formData.get(i).getName().equals(name)) {

0 commit comments

Comments
 (0)