Search in sources :

Example 1 with LtiLaunchRequestDTO

use of de.tum.in.www1.artemis.web.rest.dto.LtiLaunchRequestDTO in project ArTEMiS by ls1intum.

the class LtiResource method launch.

/**
 * POST  /lti/launch/:exerciseId : Launch the exercise app using request by a LTI consumer. Redirects the user to the exercise on success.
 *
 * @param launchRequest the LTI launch request (ExerciseLtiConfigurationDTO)
 * @param exerciseId    the id of the exercise the user wants to open
 * @param request       HTTP request
 * @param response      HTTP response
 */
@PostMapping(value = "/lti/launch/{exerciseId}", produces = MediaType.APPLICATION_JSON_VALUE)
public void launch(@ModelAttribute LtiLaunchRequestDTO launchRequest, @PathVariable("exerciseId") Long exerciseId, HttpServletRequest request, HttpServletResponse response) throws IOException {
    log.debug("Launch request : {}", launchRequest);
    // Verify request
    if (!ltiService.verifyRequest(request)) {
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Bad or expired request. Please try again.");
        return;
    }
    // Check if exercise ID is valid
    Exercise exercise = exerciseRepository.findOne(exerciseId);
    if (exercise == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "Exercise not found");
        return;
    }
    // Handle the launch request using LtiService
    try {
        ltiService.handleLaunchRequest(launchRequest, exercise);
    } catch (Exception ex) {
        log.error("Error during LIT launch request of exercise " + exercise.getTitle() + ": " + ex.getMessage());
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
        return;
    }
    // If the current user was created within the last 15 seconds, we just created the user
    // Display a welcome message to the user
    Boolean isNewUser = SecurityUtils.isAuthenticated() && TimeUnit.SECONDS.toMinutes(ZonedDateTime.now().toEpochSecond() - userService.getUser().getCreatedDate().toEpochMilli() * 1000) < 15;
    String redirectUrl = // "http"
    request.getScheme() + // "://"
    "://" + // "myhost"
    request.getServerName() + (request.getServerPort() != 80 && request.getServerPort() != 443 ? ":" + request.getServerPort() : "") + "/#/courses/" + exercise.getCourse().getId() + "/exercise/" + exercise.getId() + (isNewUser ? "?welcome" : "") + (!SecurityUtils.isAuthenticated() ? "?login" : "");
    response.sendRedirect(redirectUrl);
}
Also used : Exercise(de.tum.in.www1.artemis.domain.Exercise) IOException(java.io.IOException)

Example 2 with LtiLaunchRequestDTO

use of de.tum.in.www1.artemis.web.rest.dto.LtiLaunchRequestDTO in project ArTEMiS by ls1intum.

the class LtiService method handleLaunchRequestForSession.

/**
 * Handle launch request which was initiated earlier by a LTI consumer
 *
 * @param sessionId
 */
public void handleLaunchRequestForSession(String sessionId) {
    if (launchRequestForSession.containsKey(sessionId)) {
        log.debug("Found LTI launchRequest for session ID {}", sessionId);
        LtiLaunchRequestDTO launchRequest = launchRequestForSession.get(sessionId).getLeft();
        Exercise exercise = launchRequestForSession.get(sessionId).getRight();
        onSuccessfulLtiAuthentication(launchRequest, exercise);
        // clean up
        launchRequestForSession.remove(sessionId);
    }
}
Also used : LtiLaunchRequestDTO(de.tum.in.www1.artemis.web.rest.dto.LtiLaunchRequestDTO)

Aggregations

Exercise (de.tum.in.www1.artemis.domain.Exercise)1 LtiLaunchRequestDTO (de.tum.in.www1.artemis.web.rest.dto.LtiLaunchRequestDTO)1 IOException (java.io.IOException)1