Skip to content

Commit

Permalink
🐛 make sure the right content type are set when creating developer
Browse files Browse the repository at this point in the history
 profile type credentials
  • Loading branch information
Wei Li committed Jul 7, 2017
1 parent 4a2e61a commit 8058758
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -421,19 +422,23 @@ public void post_multipart_form_json(String path, Map<String, Object> data, bool
HttpPost request;
if (data != null) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
for (Map.Entry<String, Object> entry : data.entrySet()) {
String fieldName = entry.getKey();
Object fieldValue = entry.getValue();
if (fieldValue instanceof String) {
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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

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

Map<String, Object> formFields = new HashMap<String, Object>();
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;
}

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


Map<String, Object> formFields = new HashMap<String, Object>();
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());
Expand Down

0 comments on commit 8058758

Please sign in to comment.