From 07bbf6c49bad2a64ebb932b0149870ac6cf9edbb Mon Sep 17 00:00:00 2001 From: anhefti Date: Tue, 25 Jun 2024 14:43:47 +0200 Subject: [PATCH] SEBSERV-553 fixed file upload --- .../impl/moodle/MoodleAPIRestTemplate.java | 3 ++- .../moodle/MoodleRestTemplateFactoryImpl.java | 27 +++++++++++-------- .../plugin/MoodlePluginFullIntegration.java | 8 +++++- 3 files changed, 25 insertions(+), 13 deletions(-) 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 e37fb957..b18ea6e5 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,7 +63,8 @@ public interface MoodleAPIRestTemplate { String uploadMultiPart( String uploadEndpoint, - MultiValueMap multiPartAttributes); + MultiValueMap multiPartAttributes, + MultiValueMap queryAttributes); /** 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 98ad514a..5a2207f6 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 @@ -8,10 +8,7 @@ package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle; -import javax.net.ssl.SSLContext; import java.net.URLDecoder; -import java.net.URLEncoder; -import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -27,10 +24,6 @@ import java.util.stream.Collectors; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.plugin.MoodlePluginCourseRestriction; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.plugin.MoodlePluginFullIntegration; import org.apache.commons.lang3.StringUtils; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.ssl.TrustStrategy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpEntity; @@ -436,15 +429,27 @@ public class MoodleRestTemplateFactoryImpl implements MoodleRestTemplateFactory @Override public String uploadMultiPart( final String uploadEndpoint, - final MultiValueMap multiPartAttributes) { + final MultiValueMap multiPartAttributes, + final MultiValueMap queryAttributes) { final LmsSetup lmsSetup = this.apiTemplateDataSupplier.getLmsSetup(); - final String uri = lmsSetup.lmsApiUrl + uploadEndpoint; + final StringBuilder uri = new StringBuilder(lmsSetup.lmsApiUrl + uploadEndpoint); getAccessToken(); - multiPartAttributes.add("token", this.accessToken); + 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)); + } + }); return super.postForObject( - uri, + uri.toString(), multiPartAttributes, String.class); } 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 7cf3ae7b..486bf3c5 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 @@ -250,6 +250,9 @@ public class MoodlePluginFullIntegration implements FullLmsIntegrationAPI { multiPartAttributes.add("quizid", quizId); multiPartAttributes.add("name", fileName); multiPartAttributes.add("filename", fileName); + + final MultiValueMap queryAttributes = new LinkedMultiValueMap<>(); + multiPartAttributes.add("quizid", quizId); final ByteArrayResource contentsAsResource = new ByteArrayResource(configData) { @Override public String getFilename() { @@ -260,7 +263,10 @@ public class MoodlePluginFullIntegration implements FullLmsIntegrationAPI { multiPartAttributes.add("file", contentsAsResource); final MoodleAPIRestTemplate rest = getRestTemplate().getOrThrow(); - final String response = rest.uploadMultiPart(UPLOAD_ENDPOINT, multiPartAttributes); + final String response = rest.uploadMultiPart( + UPLOAD_ENDPOINT, + multiPartAttributes, + queryAttributes); if (response != null) { log.info("Upload Connection Configuration to Moodle: quizid: {}, fileName: {} response: {}", quizId, fileName, response );