SEBSERV-553 fixed file upload

This commit is contained in:
anhefti 2024-06-25 14:43:47 +02:00
parent abe8bcd4d3
commit 07bbf6c49b
3 changed files with 25 additions and 13 deletions

View file

@ -63,7 +63,8 @@ public interface MoodleAPIRestTemplate {
String uploadMultiPart(
String uploadEndpoint,
MultiValueMap<String, Object> multiPartAttributes);
MultiValueMap<String, Object> multiPartAttributes,
MultiValueMap<String, String> queryAttributes);
/** This maps a Moodle warning JSON object */

View file

@ -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<String, Object> multiPartAttributes) {
final MultiValueMap<String, Object> multiPartAttributes,
final MultiValueMap<String, String> 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);
}

View file

@ -250,6 +250,9 @@ public class MoodlePluginFullIntegration implements FullLmsIntegrationAPI {
multiPartAttributes.add("quizid", quizId);
multiPartAttributes.add("name", fileName);
multiPartAttributes.add("filename", fileName);
final MultiValueMap<String, String> 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 );