diff --git a/app/controllers/iop/collaboration/impl/CollaborationController.java b/app/controllers/iop/collaboration/impl/CollaborationController.java index eafb90b79..6a71f1bf6 100644 --- a/app/controllers/iop/collaboration/impl/CollaborationController.java +++ b/app/controllers/iop/collaboration/impl/CollaborationController.java @@ -13,8 +13,6 @@ import java.net.MalformedURLException; import java.net.URI; import java.net.URL; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Map; import java.util.Optional; @@ -35,6 +33,7 @@ import org.slf4j.LoggerFactory; import play.libs.Json; import play.libs.ws.WSClient; +import play.libs.ws.WSRequest; import play.libs.ws.WSResponse; import play.mvc.Result; import util.config.ConfigReader; @@ -68,22 +67,20 @@ Optional parseUrl() { } } - Optional parseUrlWithSearchParam(String filter, boolean anonymous) { - try { - if (filter == null) { - return Optional.empty(); - } - String paramStr = String.format( - "filter=%s&anonymous=%s", - URLEncoder.encode(filter, StandardCharsets.UTF_8), - anonymous - ); - URI uri = URI.create(String.format("%s/api/exams/search?%s", configReader.getIopHost(), paramStr)); - return Optional.of(uri.toURL()); - } catch (MalformedURLException e) { - logger.error("Malformed URL", e); - return Optional.empty(); - } + WSRequest getSearchRequest(Optional filter) { + String host = configReader.getIopHost(); + return filter + .map(s -> { + URI uri = URI.create(String.format("%s/api/exams/search", host)); + return wsClient + .url(uri.toString()) + .addQueryParameter("filter", s) + .addQueryParameter("anonymous", "false"); + }) + .orElseGet(() -> { + URI uri = URI.create(String.format("%s/api/exams", host)); + return wsClient.url(uri.toString()); + }); } protected CompletionStage> downloadExam(CollaborativeExam ce) { diff --git a/app/controllers/iop/collaboration/impl/CollaborativeEnrolmentController.java b/app/controllers/iop/collaboration/impl/CollaborativeEnrolmentController.java index aadcc2e45..d4810c800 100644 --- a/app/controllers/iop/collaboration/impl/CollaborativeEnrolmentController.java +++ b/app/controllers/iop/collaboration/impl/CollaborativeEnrolmentController.java @@ -6,7 +6,6 @@ import io.ebean.Transaction; import io.ebean.text.PathProperties; import io.vavr.control.Either; -import java.net.URL; import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -68,12 +67,7 @@ private Either checkExam(Exam exam, User user) { @Restrict({ @Group("STUDENT") }) public CompletionStage searchExams(Optional filter) { - Optional url = filter.orElse("").isEmpty() ? parseUrl() : parseUrlWithSearchParam(filter.get(), false); - if (url.isEmpty()) { - return wrapAsPromise(internalServerError("i18n_internal_error")); - } - - WSRequest request = wsClient.url(url.get().toString()); + WSRequest request = getSearchRequest(filter); String homeOrg = configReader.getHomeOrganisationRef(); Function onSuccess = response -> findExamsToProcess(response) diff --git a/app/controllers/iop/collaboration/impl/CollaborativeExamController.java b/app/controllers/iop/collaboration/impl/CollaborativeExamController.java index 9faadca22..ca183ad41 100644 --- a/app/controllers/iop/collaboration/impl/CollaborativeExamController.java +++ b/app/controllers/iop/collaboration/impl/CollaborativeExamController.java @@ -101,13 +101,8 @@ private Exam prepareDraft(User user) { @Authenticated @Restrict({ @Group("ADMIN"), @Group("TEACHER") }) public CompletionStage searchExams(Http.Request request, final Optional filter) { - Optional url = filter.orElse("").isEmpty() ? parseUrl() : parseUrlWithSearchParam(filter.get(), false); - if (url.isEmpty()) { - return wrapAsPromise(internalServerError("i18n_internal_error")); - } - + WSRequest wsRequest = getSearchRequest(filter); User user = request.attrs().get(Attrs.AUTHENTICATED_USER); - WSRequest wsRequest = wsClient.url(url.get().toString()); String homeOrg = configReader.getHomeOrganisationRef(); Function onSuccess = response ->