com.seajas.search.attender.controller.ConfirmController.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.attender.controller.ConfirmController.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.attender.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.cxf.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.seajas.search.attender.model.profile.Profile;
import com.seajas.search.attender.model.profile.ProfileSubscriber;
import com.seajas.search.attender.service.attender.AttenderService;

/**
 * Confirm controller.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
@RequestMapping("/confirm/**")
@Controller
public class ConfirmController {
    /**
     * Attender service.
     */
    @Autowired
    private AttenderService attenderService;

    /**
     * Handle the request.
     * 
     * @param request
     * @param model
     * @return String
     */
    @RequestMapping(method = RequestMethod.GET)
    public String handleRequest(final HttpServletRequest request, final ModelMap model) throws Exception {
        String path = request.getPathInfo();

        if (!StringUtils.isEmpty(path) && path.length() > 9) {
            String[] uuidParts = path.substring(9).split("/");

            ProfileSubscriber subscriber;

            if (uuidParts.length > 1 && uuidParts[1].equalsIgnoreCase("disable"))
                subscriber = attenderService.unconfirmSubscriber(uuidParts[0]);
            else
                subscriber = attenderService.confirmSubscriber(uuidParts[0]);

            if (subscriber != null && uuidParts.length > 1 && uuidParts[1].equalsIgnoreCase("disable")) {
                model.put("uuid", uuidParts[0]);

                // TODO: Actually use the language in the JSPs

                model.put("language", subscriber.getEmailLanguage());

                return "unconfirm";
            } else if (subscriber != null) {
                Profile profile = attenderService.getProfileById(subscriber.getProfileId());

                model.put("uuid", uuidParts[0]);
                model.put("profileUUID", profile.getUniqueId());

                // TODO: Actually use the language in the JSPs

                model.put("language", subscriber.getEmailLanguage());

                return "confirm";
            } else {
                model.put("message", "Invalid confirmation ticket (" + uuidParts[0] + ") provided.");

                return "error";
            }
        } else {
            model.put("message", "No confirmation ticket provided.");

            return "error";
        }
    }
}