com.kaanha.reports.web.controller.ReportController.java Source code

Java tutorial

Introduction

Here is the source code for com.kaanha.reports.web.controller.ReportController.java

Source

package com.kaanha.reports.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.kaanha.reports.core.domain.Tenant;
import com.kaanha.reports.core.domain.User;
import com.kaanha.reports.core.exception.AccessDeniedException;
import com.kaanha.reports.core.service.EmailService;
import com.kaanha.reports.core.service.JIRASessionService;
import com.kaanha.reports.core.service.ReportService;
import com.kaanha.reports.core.service.TenantService;
import com.kaanha.reports.core.service.UserService;
import com.kaanha.reports.core.service.ValidatorService;

@Controller
@RequestMapping("/report")
public class ReportController {

    @Autowired
    ReportService reportService;

    @Autowired
    ValidatorService validatorService;

    @Autowired
    UserService userService;

    @Autowired
    EmailService emailService;

    @Autowired
    JIRASessionService jiraSessionService;

    @Autowired
    TenantService tenantService;

    @ModelAttribute("validatedRequest")
    public ValidatedRequest validate(@RequestParam(required = true) String jwt,
            @RequestParam(value = "user_id", required = true) String username,
            @RequestParam(required = false) String link, @RequestParam(required = false) String lic,
            HttpServletRequest request) throws Exception {
        if (StringUtils.isBlank(link) && !request.getPathInfo().endsWith("hosted")) {
            if (lic != null) {
                if ("none".equals(lic)) {
                    throw new AccessDeniedException(
                            "Sorry, we are unable to detect an active license. Please make sure that you have an active subscription to the 'All-In-One JIRA Reports' plugin. If you were using a trial version, then your trial period may have expired. Please contact your JIRA administrator to renew the license.");
                }
            }
            Tenant tenant = validatorService.validateJWT(jwt);
            User user = userService.findOrSaveUser(username, tenant);
            ValidatedRequest validatedRequest = new ValidatedRequest();
            validatedRequest.setTenant(tenant);
            validatedRequest.setUser(user);
            return validatedRequest;
        }
        return null;
    }
}

class ValidatedRequest {
    Tenant tenant;
    User user;

    public Tenant getTenant() {
        return tenant;
    }

    public void setTenant(Tenant tenant) {
        this.tenant = tenant;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}