From 97fa962311e938ec39008ae84c0401bdbce6f33e Mon Sep 17 00:00:00 2001 From: Greg Schueler Date: Sun, 9 Mar 2014 17:17:06 -0700 Subject: [PATCH] Support request content sent directly via file or stream --- src/main/java/org/rundeck/api/ApiCall.java | 9 ++++ .../java/org/rundeck/api/ApiPathBuilder.java | 51 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/main/java/org/rundeck/api/ApiCall.java b/src/main/java/org/rundeck/api/ApiCall.java index f3e7596..e285dee 100644 --- a/src/main/java/org/rundeck/api/ApiCall.java +++ b/src/main/java/org/rundeck/api/ApiCall.java @@ -24,7 +24,9 @@ import org.apache.http.client.methods.*; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.ssl.TrustStrategy; +import org.apache.http.entity.BasicHttpEntity; import org.apache.http.entity.EntityTemplate; +import org.apache.http.entity.FileEntity; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.InputStreamBody; @@ -298,6 +300,13 @@ class ApiCall { } catch (UnsupportedEncodingException e) { throw new RundeckApiException("Unsupported encoding: " + e.getMessage(), e); } + }else if(apiPath.getContentStream() !=null && apiPath.getContentType()!=null){ + BasicHttpEntity entity = new BasicHttpEntity(); + entity.setContent(apiPath.getContentStream()); + entity.setContentType(apiPath.getContentType()); + httpPost.setEntity(entity); + }else if(apiPath.getContentFile() !=null && apiPath.getContentType()!=null){ + httpPost.setEntity(new FileEntity(apiPath.getContentFile(), apiPath.getContentType())); }else if(apiPath.getXmlDocument()!=null) { httpPost.setHeader("Content-Type", "application/xml"); httpPost.setEntity(new EntityTemplate(new DocumentContentProducer(apiPath.getXmlDocument()))); diff --git a/src/main/java/org/rundeck/api/ApiPathBuilder.java b/src/main/java/org/rundeck/api/ApiPathBuilder.java index bf27ae9..18a6194 100644 --- a/src/main/java/org/rundeck/api/ApiPathBuilder.java +++ b/src/main/java/org/rundeck/api/ApiPathBuilder.java @@ -15,6 +15,7 @@ */ package org.rundeck.api; +import java.io.File; import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; @@ -46,6 +47,9 @@ class ApiPathBuilder { private final Map attachments; private final List form = new ArrayList(); private Document xmlDocument; + private InputStream contentStream; + private File contentFile; + private String contentType; /** Marker for using the right separator between parameters ("?" or "&") */ private boolean firstParamDone = false; @@ -59,6 +63,10 @@ class ApiPathBuilder { public ApiPathBuilder(String... paths) { apiPath = new StringBuilder(); attachments = new HashMap(); + paths(paths); + } + + public ApiPathBuilder paths(String... paths) { if (paths != null) { for (String path : paths) { if (StringUtils.isNotBlank(path)) { @@ -66,6 +74,7 @@ class ApiPathBuilder { } } } + return this; } /** @@ -269,6 +278,36 @@ class ApiPathBuilder { } return this; } + /** + * When POSTing a request, use the given {@link InputStream} as the content of the request. This + * will only add the stream if it is not null. + * + * @param contentType MIME content type ofr hte request + * @param stream content stream + * @return this, for method chaining + */ + public ApiPathBuilder content(final String contentType, final InputStream stream) { + if (stream != null && contentType != null) { + this.contentStream=stream; + this.contentType=contentType; + } + return this; + } + /** + * When POSTing a request, use the given {@link File} as the content of the request. This + * will only add the stream if it is not null. + * + * @param contentType MIME content type ofr hte request + * @param file content from a file + * @return this, for method chaining + */ + public ApiPathBuilder content(final String contentType, final File file) { + if (file != null && contentType != null) { + this.contentFile=file; + this.contentType=contentType; + } + return this; + } /** * When POSTing a request, add the given XMl Document as the content of the request. * @@ -352,6 +391,18 @@ class ApiPathBuilder { return xmlDocument; } + public InputStream getContentStream() { + return contentStream; + } + + public String getContentType() { + return contentType; + } + + public File getContentFile() { + return contentFile; + } + /** * BuildsParameters can add URL or POST parameters to an {@link ApiPathBuilder} *