Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
JENKINS-43147 - Added support to send body in delete method
  • Loading branch information
janario committed Mar 29, 2017
1 parent 18d0228 commit 670eefc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 58 deletions.
@@ -0,0 +1,22 @@
package jenkins.plugins.http_request.util;

import java.net.URI;

import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;

/**
* @author Janario Oliveira
* Add support to send body in delete method
*/
public class HttpBodyDelete extends HttpEntityEnclosingRequestBase {
public HttpBodyDelete(final String uri) {
super();
setURI(URI.create(uri));
}

@Override
public String getMethod() {
return HttpDelete.METHOD_NAME;
}
}
83 changes: 25 additions & 58 deletions src/main/java/jenkins/plugins/http_request/util/HttpClientUtil.java
Expand Up @@ -12,7 +12,7 @@
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPatch;
Expand Down Expand Up @@ -42,26 +42,29 @@ public HttpRequestBase createRequestBase(RequestAction requestAction) throws IOE
}

private HttpRequestBase doCreateRequestBase(RequestAction requestAction) throws IOException {
if (requestAction.getMode() == HttpMode.HEAD) {
return makeHead(requestAction);

//without entity
if (requestAction.getMode() == HttpMode.HEAD) {
return new HttpHead(getUrlWithParams(requestAction));
} else if (requestAction.getMode() == HttpMode.GET) {
return makeGet(requestAction);

} else if (requestAction.getMode() == HttpMode.POST) {
return makePost(requestAction);
return new HttpGet(getUrlWithParams(requestAction));
}

} else if (requestAction.getMode() == HttpMode.PUT) {
return makePut(requestAction);
//with entity
final String uri = requestAction.getUrl().toString();
HttpEntityEnclosingRequestBase http;

if (requestAction.getMode() == HttpMode.DELETE) {
http = new HttpBodyDelete(uri);
}else if (requestAction.getMode() == HttpMode.PUT) {
http = new HttpPut(uri);
} else if (requestAction.getMode() == HttpMode.PATCH) {
return makePatch(requestAction);

} else if (requestAction.getMode() == HttpMode.DELETE) {
return makeDelete(requestAction);
}
http = new HttpPatch(uri);
} else { //default post
http = new HttpPost(uri);
}

return makePost(requestAction);
http.setEntity(makeEntity(requestAction));
return http;
}

private HttpEntity makeEntity(RequestAction requestAction) throws
Expand All @@ -80,14 +83,14 @@ private HttpEntity makeEntity(RequestAction requestAction) throws
return toUrlEncoded(requestAction.getParams());
}

public HttpGet makeGet(RequestAction requestAction) throws IOException {
String url = requestAction.getUrl().toString();
private String getUrlWithParams(RequestAction requestAction) throws IOException {
String url = requestAction.getUrl().toString();

if (!requestAction.getParams().isEmpty()) {
if (!requestAction.getParams().isEmpty()) {
url = appendParamsToUrl(url, requestAction.getParams());
}
return new HttpGet(url);
}
}
return url;
}

private static UrlEncodedFormEntity toUrlEncoded(List<HttpRequestNameValuePair> params) throws UnsupportedEncodingException {
return new UrlEncodedFormEntity(params);
Expand Down Expand Up @@ -119,42 +122,6 @@ public static String paramsToString(List<HttpRequestNameValuePair> params) throw
}
}

public HttpHead makeHead(RequestAction requestAction) throws UnsupportedEncodingException {
final HttpHead httpHead = new HttpHead(requestAction.getUrl().toString());

return httpHead;
}

public HttpPost makePost(RequestAction requestAction) throws UnsupportedEncodingException {
final HttpEntity httpEntity = makeEntity(requestAction);
final HttpPost httpPost = new HttpPost(requestAction.getUrl().toString());
httpPost.setEntity(httpEntity);

return httpPost;
}

public HttpPut makePut(RequestAction requestAction) throws UnsupportedEncodingException {
final HttpEntity entity = makeEntity(requestAction);
final HttpPut httpPut = new HttpPut(requestAction.getUrl().toString());
httpPut.setEntity(entity);

return httpPut;
}

public HttpPatch makePatch(RequestAction requestAction) throws UnsupportedEncodingException {
final HttpEntity entity = makeEntity(requestAction);
final HttpPatch httpPatch = new HttpPatch(requestAction.getUrl().toString());
httpPatch.setEntity(entity);

return httpPatch;
}

public HttpDelete makeDelete(RequestAction requestAction) throws UnsupportedEncodingException {
final HttpDelete httpDelete = new HttpDelete(requestAction.getUrl().toString());

return httpDelete;
}

public HttpResponse execute(HttpClient client, HttpContext context, HttpRequestBase method,
PrintStream logger) throws IOException, InterruptedException {
logger.println("Sending request to url: " + method.getURI());
Expand Down

0 comments on commit 670eefc

Please sign in to comment.