Support request content sent directly via file or stream

This commit is contained in:
Greg Schueler 2014-03-09 17:17:06 -07:00
parent bea99b1c97
commit 97fa962311
2 changed files with 60 additions and 0 deletions

View file

@ -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())));

View file

@ -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<String, InputStream> attachments;
private final List<NameValuePair> form = new ArrayList<NameValuePair>();
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<String, InputStream>();
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}
*