diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleAPIRestTemplate.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleAPIRestTemplate.java index b18ea6e5..8ea56b6e 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleAPIRestTemplate.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleAPIRestTemplate.java @@ -63,8 +63,9 @@ public interface MoodleAPIRestTemplate { String uploadMultiPart( String uploadEndpoint, - MultiValueMap multiPartAttributes, - MultiValueMap queryAttributes); + String quizId, + String fileName, + byte[] configData); /** This maps a Moodle warning JSON object */ diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleRestTemplateFactoryImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleRestTemplateFactoryImpl.java index 1ad3f02c..92d0f861 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleRestTemplateFactoryImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleRestTemplateFactoryImpl.java @@ -26,12 +26,7 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.plugin.Mood import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; +import org.springframework.http.*; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -429,31 +424,60 @@ public class MoodleRestTemplateFactoryImpl implements MoodleRestTemplateFactory @Override public String uploadMultiPart( final String uploadEndpoint, - final MultiValueMap multiPartAttributes, - final MultiValueMap queryAttributes) { + final String quizId, + final String fileName, + final byte[] configData) { final LmsSetup lmsSetup = this.apiTemplateDataSupplier.getLmsSetup(); final StringBuilder uri = new StringBuilder(lmsSetup.lmsApiUrl + uploadEndpoint); getAccessToken(); - queryAttributes.add("token", this.accessToken.toString()); - queryAttributes.forEach((key, values) -> { - if (values.isEmpty()) { - return; - } - if (uri.toString().contains("?")) { - uri.append("&").append(key).append("=").append(values.get(0)); - } else { - uri.append("?").append(key).append("=").append(values.get(0)); - } - }); + final HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.MULTIPART_FORM_DATA); + final MultiValueMap fileMap = new LinkedMultiValueMap<>(); + final ContentDisposition contentDisposition = ContentDisposition + .builder("form-data") + .name("file") + .filename(fileName) + .build(); + fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString()); + final HttpEntity fileEntity = new HttpEntity<>(configData, fileMap); - log.info("Upload to Moodle url: {}", uri.toString()); + final MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("token", this.accessToken.toString()); + body.add("quizid", quizId); + body.add("file", fileEntity); - return super.postForObject( + final HttpEntity> requestEntity = + new HttpEntity<>(body, headers); + + final ResponseEntity exchange = super.exchange( uri.toString(), - multiPartAttributes, + HttpMethod.POST, + requestEntity, String.class); + + return exchange.getBody(); + +// multiPartAttributes.add("token", this.accessToken.toString()); +// +// queryAttributes.forEach((key, values) -> { +// if (values.isEmpty()) { +// return; +// } +// if (uri.toString().contains("?")) { +// uri.append("&").append(key).append("=").append(values.get(0)); +// } else { +// uri.append("?").append(key).append("=").append(values.get(0)); +// } +// }); +// +// log.info("Upload to Moodle url: {}", uri.toString()); +// +// return super.postForObject( +// uri.toString(), +// multiPartAttributes, +// String.class); } private String doRequest( diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/plugin/MoodlePluginFullIntegration.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/plugin/MoodlePluginFullIntegration.java index d9f90930..abcc41c8 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/plugin/MoodlePluginFullIntegration.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/plugin/MoodlePluginFullIntegration.java @@ -246,27 +246,28 @@ public class MoodlePluginFullIntegration implements FullLmsIntegrationAPI { final String quizId = MoodleUtils.getQuizId(exam.externalId); final String fileName = getConnectionConfigFileName(exam); - final MultiValueMap multiPartAttributes = new LinkedMultiValueMap<>(); - multiPartAttributes.add("quizid", quizId); - multiPartAttributes.add("name", fileName); - multiPartAttributes.add("filename", fileName); +// final MultiValueMap multiPartAttributes = new LinkedMultiValueMap<>(); +// multiPartAttributes.add("quizid", quizId); +// multiPartAttributes.add("name", fileName); +// multiPartAttributes.add("filename", fileName); - final MultiValueMap queryAttributes = new LinkedMultiValueMap<>(); - queryAttributes.add("quizid", quizId); - final ByteArrayResource contentsAsResource = new ByteArrayResource(configData) { - @Override - public String getFilename() { - return fileName; // Filename has to be returned in order to be able to post. - } - }; - - multiPartAttributes.add("file", contentsAsResource); +// final MultiValueMap queryAttributes = new LinkedMultiValueMap<>(); +// //queryAttributes.add("quizid", quizId); +// final ByteArrayResource contentsAsResource = new ByteArrayResource(configData) { +// @Override +// public String getFilename() { +// return fileName; // Filename has to be returned in order to be able to post. +// } +// }; +// +// multiPartAttributes.add("file", contentsAsResource); final MoodleAPIRestTemplate rest = getRestTemplate().getOrThrow(); final String response = rest.uploadMultiPart( UPLOAD_ENDPOINT, - multiPartAttributes, - queryAttributes); + quizId, + fileName, + configData); if (response != null) { log.info("Upload Connection Configuration to Moodle: quizid: {}, fileName: {} response: {}", quizId, fileName, response );