Skip to content

Commit 8058758

Browse files
author
Wei Li
committed
🐛 make sure the right content type are set when creating developer
profile type credentials
1 parent 4a2e61a commit 8058758

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.offbytwo.jenkins.client;
2+
3+
public class FormBinaryField {
4+
private String fileName;
5+
private String contentType;
6+
private byte[] content;
7+
8+
public FormBinaryField(String fileName, String contentType, byte[] content) {
9+
this.fileName = fileName;
10+
this.contentType = contentType;
11+
this.content = content;
12+
}
13+
14+
public String getFileName() {
15+
return fileName;
16+
}
17+
18+
public String getContentType() {
19+
return contentType;
20+
}
21+
22+
public byte[] getContent() {
23+
return content;
24+
}
25+
}

jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.http.client.methods.HttpRequestBase;
3131
import org.apache.http.entity.ContentType;
3232
import org.apache.http.entity.StringEntity;
33+
import org.apache.http.entity.mime.HttpMultipartMode;
3334
import org.apache.http.entity.mime.MultipartEntityBuilder;
3435
import org.apache.http.impl.auth.BasicScheme;
3536
import org.apache.http.impl.client.BasicCredentialsProvider;
@@ -421,19 +422,23 @@ public void post_multipart_form_json(String path, Map<String, Object> data, bool
421422
HttpPost request;
422423
if (data != null) {
423424
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
425+
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
424426
for (Map.Entry<String, Object> entry : data.entrySet()) {
425427
String fieldName = entry.getKey();
426428
Object fieldValue = entry.getValue();
427429
if (fieldValue instanceof String) {
428430
builder.addTextBody(fieldName, (String) fieldValue);
429431
} else if (fieldValue instanceof byte[]) {
430432
builder.addBinaryBody(fieldName, (byte[]) fieldValue);
433+
} else if (fieldValue instanceof FormBinaryField) {
434+
FormBinaryField binaryField = (FormBinaryField) fieldValue;
435+
builder.addBinaryBody(fieldName, binaryField.getContent(), ContentType.create(binaryField.getContentType()), binaryField.getFileName());
431436
} else if (fieldValue instanceof File) {
432437
builder.addBinaryBody(fieldName, (File) fieldValue);
433438
} else if (fieldValue instanceof InputStream) {
434439
builder.addBinaryBody(fieldName, (InputStream) fieldValue);
435440
} else {
436-
throw new IllegalArgumentException("type of field " + fieldName + " is not String, byte[], File or InputStream");
441+
builder.addTextBody(fieldName, JSONObject.fromObject(fieldValue).toString());
437442
}
438443
}
439444
request = new HttpPost(noapi(path));

jenkins-client/src/main/java/com/offbytwo/jenkins/model/credentials/AppleDeveloperProfileCredential.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.offbytwo.jenkins.model.credentials;
22

3+
import com.offbytwo.jenkins.client.FormBinaryField;
34
import net.sf.json.JSONObject;
45

56
import java.util.HashMap;
@@ -17,6 +18,9 @@ public class AppleDeveloperProfileCredential extends Credential {
1718
private static final String FILE_ZERO_FIELD_NAME = "file0";
1819
private static final String FILE_ONE_FIELD_NAME = "file1";
1920

21+
private static final String DEFAULT_DEV_PROFILE_NAME = "developerProfile.zip";
22+
private static final String DEFAULT_DEV_PROFULE_CONTENT_TYPE = "application/zip";
23+
2024
private String password;
2125
private byte[] developerProfileContent;
2226

@@ -72,14 +76,14 @@ public Map<String, Object> dataForCreate() {
7276
jsonData.put("credentials", credentialMap);
7377

7478
Map<String, Object> formFields = new HashMap<String, Object>();
75-
formFields.put(FILE_ZERO_FIELD_NAME, this.getDeveloperProfileContent());
79+
formFields.put(FILE_ZERO_FIELD_NAME, new FormBinaryField(DEFAULT_DEV_PROFILE_NAME, DEFAULT_DEV_PROFULE_CONTENT_TYPE, this.getDeveloperProfileContent()));
7680
formFields.put("_.scope", SCOPE_GLOBAL);
7781
formFields.put("_.password", this.getPassword());
7882
formFields.put("_.id", this.getId());
7983
formFields.put("_.description", this.getDescription());
8084
formFields.put("stapler-class", BASECLASS);
8185
formFields.put("$class", BASECLASS);
82-
formFields.put("json", JSONObject.fromObject(jsonData).toString());
86+
formFields.put("json", jsonData);
8387
return formFields;
8488
}
8589

@@ -95,7 +99,7 @@ public Map<String, Object> dataForUpdate() {
9599

96100

97101
Map<String, Object> formFields = new HashMap<String, Object>();
98-
formFields.put(FILE_ONE_FIELD_NAME, this.getDeveloperProfileContent());
102+
formFields.put(FILE_ONE_FIELD_NAME, new FormBinaryField(DEFAULT_DEV_PROFILE_NAME, DEFAULT_DEV_PROFULE_CONTENT_TYPE, this.getDeveloperProfileContent()));
99103
formFields.put("_.", "on");
100104
formFields.put("_.password", this.getPassword());
101105
formFields.put("_.id", this.getId());

0 commit comments

Comments
 (0)