diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/FormBinaryField.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/FormBinaryField.java new file mode 100644 index 00000000..72d25b7e --- /dev/null +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/FormBinaryField.java @@ -0,0 +1,25 @@ +package com.offbytwo.jenkins.client; + +public class FormBinaryField { + private String fileName; + private String contentType; + private byte[] content; + + public FormBinaryField(String fileName, String contentType, byte[] content) { + this.fileName = fileName; + this.contentType = contentType; + this.content = content; + } + + public String getFileName() { + return fileName; + } + + public String getContentType() { + return contentType; + } + + public byte[] getContent() { + return content; + } +} diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java index c2eaf3b1..a1fe527b 100755 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java @@ -30,6 +30,7 @@ import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; +import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.client.BasicCredentialsProvider; @@ -421,6 +422,7 @@ public void post_multipart_form_json(String path, Map data, bool HttpPost request; if (data != null) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); for (Map.Entry entry : data.entrySet()) { String fieldName = entry.getKey(); Object fieldValue = entry.getValue(); @@ -428,12 +430,15 @@ public void post_multipart_form_json(String path, Map data, bool builder.addTextBody(fieldName, (String) fieldValue); } else if (fieldValue instanceof byte[]) { builder.addBinaryBody(fieldName, (byte[]) fieldValue); + } else if (fieldValue instanceof FormBinaryField) { + FormBinaryField binaryField = (FormBinaryField) fieldValue; + builder.addBinaryBody(fieldName, binaryField.getContent(), ContentType.create(binaryField.getContentType()), binaryField.getFileName()); } else if (fieldValue instanceof File) { builder.addBinaryBody(fieldName, (File) fieldValue); } else if (fieldValue instanceof InputStream) { builder.addBinaryBody(fieldName, (InputStream) fieldValue); } else { - throw new IllegalArgumentException("type of field " + fieldName + " is not String, byte[], File or InputStream"); + builder.addTextBody(fieldName, JSONObject.fromObject(fieldValue).toString()); } } request = new HttpPost(noapi(path)); diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/credentials/AppleDeveloperProfileCredential.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/credentials/AppleDeveloperProfileCredential.java index 8b84ab8a..a1f30dd5 100644 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/credentials/AppleDeveloperProfileCredential.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/credentials/AppleDeveloperProfileCredential.java @@ -1,5 +1,6 @@ package com.offbytwo.jenkins.model.credentials; +import com.offbytwo.jenkins.client.FormBinaryField; import net.sf.json.JSONObject; import java.util.HashMap; @@ -17,6 +18,9 @@ public class AppleDeveloperProfileCredential extends Credential { private static final String FILE_ZERO_FIELD_NAME = "file0"; private static final String FILE_ONE_FIELD_NAME = "file1"; + private static final String DEFAULT_DEV_PROFILE_NAME = "developerProfile.zip"; + private static final String DEFAULT_DEV_PROFULE_CONTENT_TYPE = "application/zip"; + private String password; private byte[] developerProfileContent; @@ -72,14 +76,14 @@ public Map dataForCreate() { jsonData.put("credentials", credentialMap); Map formFields = new HashMap(); - formFields.put(FILE_ZERO_FIELD_NAME, this.getDeveloperProfileContent()); + formFields.put(FILE_ZERO_FIELD_NAME, new FormBinaryField(DEFAULT_DEV_PROFILE_NAME, DEFAULT_DEV_PROFULE_CONTENT_TYPE, this.getDeveloperProfileContent())); formFields.put("_.scope", SCOPE_GLOBAL); formFields.put("_.password", this.getPassword()); formFields.put("_.id", this.getId()); formFields.put("_.description", this.getDescription()); formFields.put("stapler-class", BASECLASS); formFields.put("$class", BASECLASS); - formFields.put("json", JSONObject.fromObject(jsonData).toString()); + formFields.put("json", jsonData); return formFields; } @@ -95,7 +99,7 @@ public Map dataForUpdate() { Map formFields = new HashMap(); - formFields.put(FILE_ONE_FIELD_NAME, this.getDeveloperProfileContent()); + formFields.put(FILE_ONE_FIELD_NAME, new FormBinaryField(DEFAULT_DEV_PROFILE_NAME, DEFAULT_DEV_PROFULE_CONTENT_TYPE, this.getDeveloperProfileContent())); formFields.put("_.", "on"); formFields.put("_.password", this.getPassword()); formFields.put("_.id", this.getId());