find-bugs

This commit is contained in:
anhefti 2019-07-04 16:33:48 +02:00
parent 5551c7d7c2
commit 7ad1e20316
2 changed files with 55 additions and 18 deletions

View file

@ -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<String, String> 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<String, String> 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<String, String> 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<String, String> getAdditionalAttributes() {
return this.additionalAttributes;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();

View file

@ -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<String, String> 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<String, String> 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<String, String> 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()));
}
}