Skip to content

Commit

Permalink
SAK-32315 (Bug fix) : Problem uploading file to Urkund with multipart… (
Browse files Browse the repository at this point in the history
sakaiproject#4067)

* SAK-32315 (Bug fix) : Problem uploading file to Urkund with multipart post http request

+ Refactor : Included UrkundSubmission object to better manage
parameters

* Pull request : changes requested
  • Loading branch information
frasese authored and jonespm committed Mar 13, 2017
1 parent f43c718 commit 344e415
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,18 @@ public void init() {
/*
* Utility Methods below
*/
//TODO : let the caller fill the UrkundSubmission to avoid extra parameters in this method
public UrkundSubmissionData uploadFile(String submitterEmail, String externalId, String filename, byte[] filecontent, String mimeType) {
String jsonResponse = UrkundAPIUtil.postDocument(apiURL, submitterEmail, externalId, filename, filecontent, mimeType, receiverAddress, username, password, connTimeout);
UrkundSubmission submission = new UrkundSubmission();
submission.setFilename(filename);
submission.setMimeType(mimeType);
submission.setContent(filecontent);
submission.setSubmitterEmail(submitterEmail);
submission.setSubject(""); // Insert site name?
submission.setMessage(""); // Insert assignment title?
submission.setAnon(false);

String jsonResponse = UrkundAPIUtil.postDocument(apiURL, receiverAddress, externalId, submission, username, password, connTimeout);
return getSubmissionData(externalId, jsonResponse);
}
public List<UrkundSubmissionData> getReports(String externalId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**********************************************************************************
*
* Copyright (c) 2017 Sakai Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**********************************************************************************/
package org.sakaiproject.contentreview.urkund;

import lombok.Getter;
import lombok.Setter;

import java.util.Base64;


public class UrkundSubmission {

@Getter @Setter private String submitterEmail;
@Getter @Setter private String filename;
@Getter @Setter private String mimeType;
@Getter @Setter private byte[] content;
@Getter @Setter private String subject = "";
@Getter @Setter private String message = "";
@Getter @Setter private boolean anon = false;
@Getter @Setter private String language = "en-US";

public String getFilenameEncoded() {
try {
return new String(Base64.getEncoder().encode(filename.getBytes("UTF-8")));
}catch(Exception e){}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;


import org.sakaiproject.contentreview.urkund.UrkundSubmission;

/**
* This is a utility class for wrapping the Rest calls to the Urkund Service
Expand All @@ -44,7 +42,7 @@
public class UrkundAPIUtil {
private static final Logger log = LoggerFactory.getLogger(UrkundAPIUtil.class);

public static String postDocument(String baseUrl, String submitterEmail, String externalId, String filename, byte[] filecontent, String mimeType, String receiverAddress, String urkundUsername, String urkundPassword, int timeout){
public static String postDocument(String baseUrl, String receiverAddress, String externalId, UrkundSubmission submission, String urkundUsername, String urkundPassword, int timeout){
String ret = null;

RequestConfig.Builder requestBuilder = RequestConfig.custom();
Expand All @@ -58,22 +56,23 @@ public static String postDocument(String baseUrl, String submitterEmail, String

HttpPost httppost = new HttpPost(baseUrl+"submissions/"+receiverAddress+"/"+externalId);
//------------------------------------------------------------
MultipartEntityBuilder meBuilder = MultipartEntityBuilder.create();
meBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

meBuilder.addBinaryBody("file", filecontent, ContentType.create(mimeType), filename);
EntityBuilder eBuilder = EntityBuilder.create();
eBuilder.setBinary(submission.getContent());

httppost.setEntity(meBuilder.build());
httppost.setEntity(eBuilder.build());
//------------------------------------------------------------
if(StringUtils.isNotBlank(urkundUsername) && StringUtils.isNotBlank(urkundPassword)) {
addAuthorization(httppost, urkundUsername, urkundPassword);
}
//------------------------------------------------------------
httppost.addHeader("Accept", "application/json");
httppost.addHeader("Content-Type", mimeType);
httppost.addHeader("x-urkund-filename", new String(Base64.getEncoder().encode(filename.getBytes("UTF-8"))));
httppost.addHeader("x-urkund-submitter", submitterEmail);
httppost.addHeader("x-urkund-anonymous", "false");
httppost.addHeader("Content-Type", submission.getMimeType());
httppost.addHeader("Accept-Language", submission.getLanguage());
httppost.addHeader("x-urkund-filename", submission.getFilenameEncoded());
httppost.addHeader("x-urkund-submitter", submission.getSubmitterEmail());
httppost.addHeader("x-urkund-anonymous", Boolean.toString(submission.isAnon()));
httppost.addHeader("x-urkund-subject", submission.getSubject());
httppost.addHeader("x-urkund-message", submission.getMessage());
//------------------------------------------------------------

HttpResponse response = httpClient.execute(httppost);
Expand Down

0 comments on commit 344e415

Please sign in to comment.