From 7ad1e20316446416257abf4d771367108228c31f Mon Sep 17 00:00:00 2001 From: anhefti Date: Thu, 4 Jul 2019 16:33:48 +0200 Subject: [PATCH] find-bugs --- .../sebserver/gbl/model/exam/QuizData.java | 33 ++++++++++++++- .../lms/impl/OpenEdxLmsAPITemplate.java | 40 +++++++++++-------- 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/model/exam/QuizData.java b/src/main/java/ch/ethz/seb/sebserver/gbl/model/exam/QuizData.java index 30fbaaab..e4180d08 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/model/exam/QuizData.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/model/exam/QuizData.java @@ -8,7 +8,9 @@ package ch.ethz.seb.sebserver.gbl.model.exam; +import java.util.Collections; import java.util.Comparator; +import java.util.Map; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; @@ -23,11 +25,14 @@ import ch.ethz.seb.sebserver.gbl.model.Domain; import ch.ethz.seb.sebserver.gbl.model.GrantEntity; import ch.ethz.seb.sebserver.gbl.model.PageSortOrder; import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup.LmsType; +import ch.ethz.seb.sebserver.gbl.util.Utils; public final class QuizData implements GrantEntity { public static final String FILTER_ATTR_START_TIME = "start_timestamp"; + public static final String ATTR_ADDITIONAL_ATTRIBUTES = "ADDITIONAL_ATTRIBUTES"; + public static final String QUIZ_ATTR_ID = "quiz_id"; public static final String QUIZ_ATTR_INSTITUION_ID = Domain.EXAM.ATTR_INSTITUTION_ID; public static final String QUIZ_ATTR_LMS_SETUP_ID = "lms_setup_id"; @@ -65,6 +70,9 @@ public final class QuizData implements GrantEntity { @JsonProperty(QUIZ_ATTR_START_URL) public final String startURL; + @JsonProperty(ATTR_ADDITIONAL_ATTRIBUTES) + public final Map additionalAttributes; + @JsonCreator public QuizData( @JsonProperty(QUIZ_ATTR_ID) final String id, @@ -75,7 +83,8 @@ public final class QuizData implements GrantEntity { @JsonProperty(QUIZ_ATTR_DESCRIPTION) final String description, @JsonProperty(QUIZ_ATTR_START_TIME) final DateTime startTime, @JsonProperty(QUIZ_ATTR_END_TIME) final DateTime endTime, - @JsonProperty(QUIZ_ATTR_START_URL) final String startURL) { + @JsonProperty(QUIZ_ATTR_START_URL) final String startURL, + @JsonProperty(ATTR_ADDITIONAL_ATTRIBUTES) final Map additionalAttributes) { this.id = id; this.institutionId = institutionId; @@ -86,6 +95,7 @@ public final class QuizData implements GrantEntity { this.startTime = startTime; this.endTime = endTime; this.startURL = startURL; + this.additionalAttributes = Utils.immutableMapOf(additionalAttributes); } public QuizData( @@ -99,6 +109,22 @@ public final class QuizData implements GrantEntity { final String endTime, final String startURL) { + this(id, institutionId, lmsSetupId, lmsType, name, description, + startTime, endTime, startURL, Collections.emptyMap()); + } + + public QuizData( + final String id, + final Long institutionId, + final Long lmsSetupId, + final LmsType lmsType, + final String name, + final String description, + final String startTime, + final String endTime, + final String startURL, + final Map additionalAttributes) { + this.id = id; this.institutionId = institutionId; this.lmsSetupId = lmsSetupId; @@ -116,6 +142,7 @@ public final class QuizData implements GrantEntity { .toDateTime(DateTimeZone.UTC) : null; this.startURL = startURL; + this.additionalAttributes = Utils.immutableMapOf(additionalAttributes); } @Override @@ -170,6 +197,10 @@ public final class QuizData implements GrantEntity { return this.startURL; } + public Map getAdditionalAttributes() { + return this.additionalAttributes; + } + @Override public String toString() { final StringBuilder builder = new StringBuilder(); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/OpenEdxLmsAPITemplate.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/OpenEdxLmsAPITemplate.java index 97df31cd..b2bd4045 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/OpenEdxLmsAPITemplate.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/OpenEdxLmsAPITemplate.java @@ -12,9 +12,11 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -123,13 +125,13 @@ final class OpenEdxLmsAPITemplate implements LmsAPITemplate { try { this.getEdxPage(this.lmsSetup.lmsApiUrl + OPEN_EDX_DEFAULT_COURSE_ENDPOINT); - } catch (final Exception e) { + } catch (final RuntimeException e) { if (this.restTemplate != null) { this.restTemplate.setAuthenticator(new EdxOAuth2RequestAuthenticator()); } try { this.getEdxPage(this.lmsSetup.lmsApiUrl + OPEN_EDX_DEFAULT_COURSE_ENDPOINT); - } catch (final Exception ee) { + } catch (final RuntimeException ee) { return LmsSetupTestResult.ofQuizRequestError(ee.getMessage()); } } @@ -244,9 +246,11 @@ final class OpenEdxLmsAPITemplate implements LmsAPITemplate { EdXPage page = getEdxPage(pageURI).getBody(); if (page != null) { collector.addAll(page.results); - while (StringUtils.isNotBlank(page.next)) { + while (page != null && StringUtils.isNotBlank(page.next)) { page = getEdxPage(page.next).getBody(); - collector.addAll(page.results); + if (page != null) { + collector.addAll(page.results); + } } } @@ -267,6 +271,8 @@ final class OpenEdxLmsAPITemplate implements LmsAPITemplate { final CourseData courseData) { final String startURI = lmsSetup.lmsApiUrl + OPEN_EDX_DEFAULT_COURSE_START_URL_PREFIX + courseData.id; + final Map additionalAttrs = new HashMap<>(); + additionalAttrs.put("blocks_url", courseData.blocks_url); return new QuizData( courseData.id, lmsSetup.getInstitutionId(), @@ -291,7 +297,6 @@ final class OpenEdxLmsAPITemplate implements LmsAPITemplate { /** Maps the OpenEdX course API course data */ static final class CourseData { public String id; - public String course_id; public String name; public String short_description; public String blocks_url; @@ -312,18 +317,21 @@ final class OpenEdxLmsAPITemplate implements LmsAPITemplate { AccessDeniedException, OAuth2AccessDeniedException { - final ClientCredentialsResourceDetails resource = (ClientCredentialsResourceDetails) details; - final HttpHeaders headers = new HttpHeaders(); - headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE); + if (details instanceof ClientCredentialsResourceDetails) { + final ClientCredentialsResourceDetails resource = (ClientCredentialsResourceDetails) details; + final HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE); - final MultiValueMap params = new LinkedMultiValueMap<>(); - params.add("grant_type", "client_credentials"); - //params.add("token_type", "jwt"); - params.add("client_id", resource.getClientId()); - params.add("client_secret", resource.getClientSecret()); + final MultiValueMap params = new LinkedMultiValueMap<>(); + params.add("grant_type", "client_credentials"); + params.add("client_id", resource.getClientId()); + params.add("client_secret", resource.getClientSecret()); - final OAuth2AccessToken retrieveToken = retrieveToken(request, resource, params, headers); - return retrieveToken; + final OAuth2AccessToken retrieveToken = retrieveToken(request, resource, params, headers); + return retrieveToken; + } else { + return super.obtainAccessToken(details, request); + } } } @@ -341,8 +349,6 @@ final class OpenEdxLmsAPITemplate implements LmsAPITemplate { } request.getHeaders().set("Authorization", String.format("%s %s", "Bearer", accessToken.getValue())); - - //request.getHeaders().set("Authorization", String.format("%s %s", "JWT", accessToken.getValue())); } }