Skip to content

Commit 9daa93d

Browse files
author
Shashi Ranjan
committed
Merge branch 'marcelocyreno-master'
2 parents f37caf9 + be953b5 commit 9daa93d

File tree

1 file changed

+141
-124
lines changed

1 file changed

+141
-124
lines changed

src/main/java/com/mashape/unirest/request/HttpRequestWithBody.java

Lines changed: 141 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ a copy of this software and associated documentation files (the
2525

2626
package com.mashape.unirest.request;
2727

28+
import org.apache.http.entity.ContentType;
29+
import org.apache.http.entity.mime.content.InputStreamBody;
30+
2831
import com.mashape.unirest.http.HttpMethod;
2932
import com.mashape.unirest.http.JsonNode;
3033
import com.mashape.unirest.http.ObjectMapper;
@@ -33,137 +36,151 @@ a copy of this software and associated documentation files (the
3336
import com.mashape.unirest.request.body.MultipartBody;
3437
import com.mashape.unirest.request.body.RawBody;
3538
import com.mashape.unirest.request.body.RequestBodyEntity;
39+
3640
import org.json.JSONArray;
3741
import org.json.JSONObject;
3842

3943
import java.io.File;
44+
import java.io.InputStream;
4045
import java.util.Collection;
4146
import java.util.Map;
4247
import java.util.Map.Entry;
4348

4449
public class HttpRequestWithBody extends HttpRequest {
4550

46-
public HttpRequestWithBody(HttpMethod method, String url) {
47-
super(method, url);
48-
}
49-
50-
@Override
51-
public HttpRequestWithBody routeParam(String name, String value) {
52-
super.routeParam(name, value);
53-
return this;
54-
}
55-
56-
@Override
57-
public HttpRequestWithBody header(String name, String value) {
58-
return (HttpRequestWithBody) super.header(name, value);
59-
}
60-
61-
@Override
62-
public HttpRequestWithBody headers(Map<String, String> headers) {
63-
return (HttpRequestWithBody) super.headers(headers);
64-
}
65-
66-
@Override
67-
public HttpRequestWithBody basicAuth(String username, String password) {
68-
super.basicAuth(username, password);
69-
return this;
70-
}
71-
72-
@Override
73-
public HttpRequestWithBody queryString(Map<String, Object> parameters) {
74-
return (HttpRequestWithBody) super.queryString(parameters);
75-
}
76-
77-
@Override
78-
public HttpRequestWithBody queryString(String name, Object value) {
79-
return (HttpRequestWithBody) super.queryString(name, value);
80-
}
81-
82-
public MultipartBody field(String name, Collection<?> value) {
83-
MultipartBody body = new MultipartBody(this).field(name, value);
84-
this.body = body;
85-
return body;
86-
}
87-
88-
public MultipartBody field(String name, Object value) {
89-
return field(name, value, null);
90-
}
91-
92-
public MultipartBody field(String name, File file) {
93-
return field(name, file, null);
94-
}
95-
96-
public MultipartBody field(String name, Object value, String contentType) {
97-
MultipartBody body = new MultipartBody(this).field(name, (value == null) ? "" : value.toString(), contentType);
98-
this.body = body;
99-
return body;
100-
}
101-
102-
public MultipartBody field(String name, File file, String contentType) {
103-
MultipartBody body = new MultipartBody(this).field(name, file, contentType);
104-
this.body = body;
105-
return body;
106-
}
107-
108-
public MultipartBody fields(Map<String, Object> parameters) {
109-
MultipartBody body = new MultipartBody(this);
110-
if (parameters != null) {
111-
for (Entry<String, Object> param : parameters.entrySet()) {
112-
if (param.getValue() instanceof File) {
113-
body.field(param.getKey(), (File) param.getValue());
114-
} else {
115-
body.field(param.getKey(), (param.getValue() == null) ? "" : param.getValue().toString());
116-
}
117-
}
118-
}
119-
this.body = body;
120-
return body;
121-
}
122-
123-
public RequestBodyEntity body(JsonNode body) {
124-
return body(body.toString());
125-
}
126-
127-
public RequestBodyEntity body(String body) {
128-
RequestBodyEntity b = new RequestBodyEntity(this).body(body);
129-
this.body = b;
130-
return b;
131-
}
132-
133-
public RequestBodyEntity body(Object body) {
134-
ObjectMapper objectMapper = (ObjectMapper) Options.getOption(Option.OBJECT_MAPPER);
135-
136-
if (objectMapper == null) {
137-
throw new RuntimeException("Serialization Impossible. Can't find an ObjectMapper implementation.");
138-
}
139-
140-
return body(objectMapper.writeValue(body));
141-
}
142-
143-
public RawBody body(byte[] body) {
144-
RawBody b = new RawBody(this).body(body);
145-
this.body = b;
146-
return b;
147-
}
148-
149-
/**
150-
* Sugar method for body operation
151-
*
152-
* @param body raw org.JSONObject
153-
* @return RequestBodyEntity instance
154-
*/
155-
public RequestBodyEntity body(JSONObject body) {
156-
return body(body.toString());
157-
}
158-
159-
/**
160-
* Sugar method for body operation
161-
*
162-
* @param body raw org.JSONArray
163-
* @return RequestBodyEntity instance
164-
*/
165-
public RequestBodyEntity body(JSONArray body) {
166-
return body(body.toString());
167-
}
168-
169-
}
51+
public HttpRequestWithBody(HttpMethod method, String url) {
52+
super(method, url);
53+
}
54+
55+
@Override
56+
public HttpRequestWithBody routeParam(String name, String value) {
57+
super.routeParam(name, value);
58+
return this;
59+
}
60+
61+
@Override
62+
public HttpRequestWithBody header(String name, String value) {
63+
return (HttpRequestWithBody) super.header(name, value);
64+
}
65+
66+
@Override
67+
public HttpRequestWithBody headers(Map<String, String> headers) {
68+
return (HttpRequestWithBody) super.headers(headers);
69+
}
70+
71+
@Override
72+
public HttpRequestWithBody basicAuth(String username, String password) {
73+
super.basicAuth(username, password);
74+
return this;
75+
}
76+
77+
@Override
78+
public HttpRequestWithBody queryString(Map<String, Object> parameters) {
79+
return (HttpRequestWithBody) super.queryString(parameters);
80+
}
81+
82+
@Override
83+
public HttpRequestWithBody queryString(String name, Object value) {
84+
return (HttpRequestWithBody) super.queryString(name, value);
85+
}
86+
87+
public MultipartBody field(String name, Collection<?> value) {
88+
MultipartBody body = new MultipartBody(this).field(name, value);
89+
this.body = body;
90+
return body;
91+
}
92+
93+
public MultipartBody field(String name, Object value) {
94+
return field(name, value, null);
95+
}
96+
97+
public MultipartBody field(String name, File file) {
98+
return field(name, file, null);
99+
}
100+
101+
public MultipartBody field(String name, Object value, String contentType) {
102+
MultipartBody body = new MultipartBody(this).field(name, (value == null) ? "" : value.toString(), contentType);
103+
this.body = body;
104+
return body;
105+
}
106+
107+
public MultipartBody field(String name, File file, String contentType) {
108+
MultipartBody body = new MultipartBody(this).field(name, file, contentType);
109+
this.body = body;
110+
return body;
111+
}
112+
113+
public MultipartBody fields(Map<String, Object> parameters) {
114+
MultipartBody body = new MultipartBody(this);
115+
if (parameters != null) {
116+
for (Entry<String, Object> param : parameters.entrySet()) {
117+
if (param.getValue() instanceof File) {
118+
body.field(param.getKey(), (File) param.getValue());
119+
} else {
120+
body.field(param.getKey(), (param.getValue() == null) ? "" : param.getValue().toString());
121+
}
122+
}
123+
}
124+
this.body = body;
125+
return body;
126+
}
127+
128+
public MultipartBody field(String name, InputStream stream, ContentType contentType, String fileName) {
129+
InputStreamBody inputStreamBody = new InputStreamBody(stream, contentType, fileName);
130+
MultipartBody body = new MultipartBody(this).field(name, inputStreamBody, true, contentType.toString());
131+
this.body = body;
132+
return body;
133+
}
134+
135+
public MultipartBody field(String name, InputStream stream, String fileName) {
136+
MultipartBody body = field(name, stream, ContentType.APPLICATION_OCTET_STREAM, fileName);
137+
this.body = body;
138+
return body;
139+
}
140+
141+
public RequestBodyEntity body(JsonNode body) {
142+
return body(body.toString());
143+
}
144+
145+
public RequestBodyEntity body(String body) {
146+
RequestBodyEntity b = new RequestBodyEntity(this).body(body);
147+
this.body = b;
148+
return b;
149+
}
150+
151+
public RequestBodyEntity body(Object body) {
152+
ObjectMapper objectMapper = (ObjectMapper) Options.getOption(Option.OBJECT_MAPPER);
153+
154+
if (objectMapper == null) {
155+
throw new RuntimeException("Serialization Impossible. Can't find an ObjectMapper implementation.");
156+
}
157+
158+
return body(objectMapper.writeValue(body));
159+
}
160+
161+
public RawBody body(byte[] body) {
162+
RawBody b = new RawBody(this).body(body);
163+
this.body = b;
164+
return b;
165+
}
166+
167+
/**
168+
* Sugar method for body operation
169+
*
170+
* @param body raw org.JSONObject
171+
* @return RequestBodyEntity instance
172+
*/
173+
public RequestBodyEntity body(JSONObject body) {
174+
return body(body.toString());
175+
}
176+
177+
/**
178+
* Sugar method for body operation
179+
*
180+
* @param body raw org.JSONArray
181+
* @return RequestBodyEntity instance
182+
*/
183+
public RequestBodyEntity body(JSONArray body) {
184+
return body(body.toString());
185+
}
186+
}

0 commit comments

Comments
 (0)