easycare.web.InfoController.java Source code

Java tutorial

Introduction

Here is the source code for easycare.web.InfoController.java

Source

package easycare.web;

import easycare.common.ApplicationMessageSource;
import easycare.common.LocaleResolverSupport;
import easycare.service.ApplicationSettings;
import easycare.service.ContentResourcesService;
import easycare.web.service.ExternalResourceService;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/info")
public class InfoController {

    private static final Logger log = LoggerFactory.getLogger(InfoController.class);

    public static final String REQUIREMENTS = "requirements";
    public static final String ACCESS = "access";
    public static final String EXTERNAL_REGISTRATION_FORM_URL = "externalRegistrationFormUrl";
    public static final String COUNTRY_CODES = "countryCodes";
    public static final String GOOGLE_ANALYTICS_ENABLED = "googleAnalyticsEnabled";
    public static final String LANGUAGE_BROWSER_CODE = "languageBrowserCode";
    public static final String WELCOME_PAGE = "welcome-page";

    private static final String LICENSE_AGREEMENT_URL = "/licenseagreement";
    private static final String LICENSE_AGREEMENT_VIEW = "user-license-agreement";
    private static final String LICENSE_AGREEMENT_PROPERTY = "license.agreement";
    private static final String LICENSE_AGREEMENT = "licenseAgreement";
    private static final String APAC_DEPLOYMENT = "APAC";

    @Autowired
    private ContentResourcesService contentResourcesService;

    @Value("${easycare.registration.form.url}")
    protected String registrationFormUrl = "";

    @Autowired
    protected ApplicationMessageSource messageSource;

    @Autowired
    private ApplicationSettings applicationSettings;

    @Autowired
    protected ExternalResourceService externalResourceService;

    @Autowired
    private LocaleResolverSupport localeResolverSupport;

    @Autowired(required = false)
    protected ServletContext servletContext;

    @RequestMapping("/requirements")
    public ModelAndView showRequirements() {
        ModelAndView mav = new ModelAndView(REQUIREMENTS);
        return mav;
    }

    @RequestMapping("/access")
    public ModelAndView showAccess() {
        String fullRegistrationFormUrl = externalResourceService.getResourceUrl(WELCOME_PAGE,
                getRegistrationFormUrlByBrowserLanguage());
        String url = servletContext
                .getRealPath(fullRegistrationFormUrl.replace("/" + servletContext.getServletContextName(), ""));
        if (url == null || !new File(url).exists()) {
            fullRegistrationFormUrl = externalResourceService.getResourceUrl(WELCOME_PAGE, registrationFormUrl);
        }
        String installationIdentifier = applicationSettings.getInstallationIdentifier();
        ModelAndView mav = new ModelAndView(ACCESS);
        mav.addObject(EXTERNAL_REGISTRATION_FORM_URL, fullRegistrationFormUrl);
        if (installationIdentifier.equalsIgnoreCase(APAC_DEPLOYMENT)) {
            String[] countryCode = { "jp", "au" };
            mav.addObject(COUNTRY_CODES, countryCode);
        } else {
            String[] countryCode = { "us" };
            mav.addObject(COUNTRY_CODES, countryCode);
        }
        return mav;
    }

    private String getRegistrationFormUrlByBrowserLanguage() {
        String registrationFormExtension = FilenameUtils.getExtension(registrationFormUrl);
        String registrationFormName = registrationFormUrl.replace("." + registrationFormExtension, "");
        String languageBrowser = localeResolverSupport.resolveLocale().getLanguage();
        return registrationFormName + "_" + languageBrowser + "." + registrationFormExtension;
    }

    @RequestMapping(LICENSE_AGREEMENT_URL)
    public ModelAndView licenseAgreement() {
        ModelAndView mav = new ModelAndView(LICENSE_AGREEMENT_VIEW);
        try {
            String licenseAgreementFileName = contentResourcesService.getProperty(LICENSE_AGREEMENT_PROPERTY);
            String licenseAgreementText = IOUtils
                    .toString(new ClassPathResource(licenseAgreementFileName).getInputStream());

            mav.addObject(LICENSE_AGREEMENT, licenseAgreementText);
        } catch (IOException e) {
            log.warn("Cannot load the License Agreement file", e);
        }
        return mav;
    }

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}