Skip to content

Commit cb4f966

Browse files
committed
refs #302 - PATCH request
- added Patch request
1 parent 4645db6 commit cb4f966

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

javalite-common/src/main/java/org/javalite/http/Http.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,46 @@ public static Delete delete(String url, int connectTimeout, int readTimeout) {
177177
}
178178

179179

180+
/**
181+
* Executes a PATCH request.
182+
*
183+
* @param uri url of resource.
184+
* @param content content to be posted.
185+
* @return {@link Patch} object.
186+
*/
187+
public static Patch patch(String uri, String content) {
188+
return patch(uri, content.getBytes(), CONNECTION_TIMEOUT, READ_TIMEOUT);
189+
}
190+
191+
/**
192+
* Executes a PATCH request.
193+
*
194+
* @param uri url of resource.
195+
* @param content content to be posted.
196+
* @return {@link Patch} object.
197+
*/
198+
public static Patch patch(String uri, byte[] content) {
199+
return patch(uri, content, CONNECTION_TIMEOUT, READ_TIMEOUT);
200+
}
201+
202+
/**
203+
* Executes a PATCH request.
204+
*
205+
* @param url url of resource.
206+
* @param content content to be posted.
207+
* @param connectTimeout connection timeout in milliseconds.
208+
* @param readTimeout read timeout in milliseconds.
209+
* @return {@link Patch} object.
210+
*/
211+
public static Patch patch(String url, byte[] content, int connectTimeout, int readTimeout) {
212+
213+
try {
214+
return new Patch(url, content, connectTimeout, readTimeout);
215+
} catch (Exception e) {
216+
throw new HttpException("Failed URL: " + url, e);
217+
}
218+
}
219+
180220

181221
/**
182222
* Converts a map to URL- encoded content. This is a convenience method which can be used in combination
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.javalite.http;
2+
3+
import java.io.OutputStream;
4+
5+
/**
6+
* Executes a PATCH request.
7+
*
8+
* @author Mikhail Chachkouski
9+
*/
10+
public class Patch extends Request<Patch> {
11+
12+
private final byte[] content;
13+
14+
/**
15+
* Constructor for making PATCH requests.
16+
*
17+
* @param uri URI of resource.
18+
* @param content content to be posted to the resource.
19+
* @param connectTimeout connection timeout.
20+
* @param readTimeout read timeout.
21+
*/
22+
public Patch(String uri, byte[] content, int connectTimeout, int readTimeout) {
23+
super(uri, connectTimeout, readTimeout);
24+
this.content = content;
25+
}
26+
27+
@Override
28+
public Patch doConnect() {
29+
try {
30+
this.connection.setDoInput(true);
31+
this.connection.setDoOutput(true);
32+
this.connection.setUseCaches(false);
33+
this.connection.setRequestMethod("POST");
34+
this.connection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
35+
OutputStream out = this.connection.getOutputStream();
36+
out.write(this.content);
37+
out.flush();
38+
return this;
39+
} catch (Exception e) {
40+
throw new HttpException("Failed URL: " + url, e);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)