@@ -129,6 +129,8 @@ public <T extends BaseData> T navigate(Class<T> tClass, Link l, HashMap<String,
129
129
result = sendRequest (Verb .PUT , this .baseUrl + uri , tClass , null );
130
130
} else if (l .getMethod ().equals ("POST" )) {
131
131
result = sendRequest (Verb .POST , this .baseUrl + uri , tClass , null );
132
+ } else if (l .getMethod ().equals ("DELETE" )) {
133
+ result = sendRequest (Verb .DELETE , this .baseUrl + uri , tClass , new Object ());
132
134
} else {
133
135
result = sendRequest (Verb .GET , uri , tClass , null );
134
136
}
@@ -170,6 +172,11 @@ public <T extends BaseData> T navigate(Class<T> tClass, Link l, Object object) t
170
172
String url = this .baseUrl + l .getUri ();
171
173
result = sendRequest (Verb .PATCH , url , tClass , object );
172
174
}
175
+
176
+ if (l .getMethod ().equals ("DELETE" )) {
177
+ String url = this .baseUrl + l .getUri ();
178
+ result = sendRequest (Verb .DELETE , url , tClass , object );
179
+ }
173
180
if (isTimed ()) {
174
181
end = System .currentTimeMillis ();
175
182
long time = end - start ;
@@ -198,7 +205,7 @@ private <T extends BaseData> T sendRequest(Verb verb, String url, Class<T> tClas
198
205
try {
199
206
serverAddress = new URL (url );
200
207
connection = (HttpURLConnection ) serverAddress .openConnection ();
201
- boolean doOutput = (verb != Verb . GET );
208
+ boolean doOutput = doOutput (verb );
202
209
connection .setDoOutput (doOutput );
203
210
connection .setRequestMethod (method (verb ));
204
211
connection .setInstanceFollowRedirects (false );
@@ -270,6 +277,10 @@ else if (verb == Verb.POST && object == null) {
270
277
return (T ) objectCache .getObject (url );
271
278
}
272
279
280
+ if (connection .getResponseCode () == HttpURLConnection .HTTP_NO_CONTENT ) {
281
+ return (T ) new ResponseData ();
282
+ }
283
+
273
284
if (connection .getResponseCode () >= HttpURLConnection .HTTP_BAD_REQUEST && connection .getResponseCode () < HttpURLConnection .HTTP_INTERNAL_ERROR ) {
274
285
System .out .println ("ErrorCode: " + connection .getResponseCode () + " " + connection .getResponseMessage () +
275
286
" " + url + ", verb: " + verb );
@@ -342,16 +353,18 @@ private <T extends BaseData> T sendRequestWithAddedHeaders(Verb verb, String url
342
353
serverAddress = new URL (url );
343
354
connection = (HttpURLConnection ) serverAddress .openConnection ();
344
355
connection .setInstanceFollowRedirects (false );
345
- connection .setDoOutput (true );
356
+ boolean doOutput = doOutput (verb );
357
+ connection .setDoOutput (doOutput );
346
358
connection .setRequestMethod (method (verb ));
347
359
connection .setRequestProperty ("Authorization" , headers .get ("authorization" ));
348
360
connection .addRequestProperty ("Accept" , "application/json" );
349
- connection .addRequestProperty ("Content-Length" , "0" );
350
-
351
- OutputStreamWriter os = new OutputStreamWriter (connection .getOutputStream ());
352
- os .write ("" );
353
- os .flush ();
354
- os .close ();
361
+ if (doOutput ){
362
+ connection .addRequestProperty ("Content-Length" , "0" );
363
+ OutputStreamWriter os = new OutputStreamWriter (connection .getOutputStream ());
364
+ os .write ("" );
365
+ os .flush ();
366
+ os .close ();
367
+ }
355
368
connection .connect ();
356
369
357
370
if (connection .getResponseCode () == HttpURLConnection .HTTP_SEE_OTHER || connection .getResponseCode () == HttpURLConnection .HTTP_CREATED ) {
@@ -459,6 +472,15 @@ private Link parseLocationLinkFromString(String s) {
459
472
return new Link ("location" , s , "GET" , true );
460
473
}
461
474
475
+ private boolean doOutput (Verb verb ) {
476
+ switch (verb ) {
477
+ case GET :
478
+ case DELETE :
479
+ return false ;
480
+ default :
481
+ return true ;
482
+ }
483
+ }
462
484
private enum Verb {
463
485
GET , PUT , POST , DELETE , PATCH
464
486
}
0 commit comments