no.dusken.aranea.web.control.FormController.java Source code

Java tutorial

Introduction

Here is the source code for no.dusken.aranea.web.control.FormController.java

Source

/*
 Copyright 2006 - 2010 Under Dusken
    
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
    
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package no.dusken.aranea.web.control;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import no.dusken.aranea.util.MailCleaner;
import no.dusken.common.model.Mail;
import no.dusken.common.util.MailSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author Marvin B. Lillehaug <lillehau@underdusken.no>
 */
public class FormController extends SimpleFormController {

    private MailCleaner mailCleaner;

    private ImageCaptchaService imageCaptchaService;

    private MailSender mailSender;

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private Boolean captchaEnabled;

    protected Map referenceData(HttpServletRequest httpServletRequest) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("captchaString", UUID.randomUUID().toString());
        map.put("captchaEnabled", captchaEnabled);
        return map;
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws ServletRequestBindingException {
        return new Mail();
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object object,
            BindException errors) throws Exception {
        Mail mail = (Mail) object;
        // this content does not change that often, allow one hour cache
        response.setHeader("Cache-Control", "max-age=3600");
        String hidden = ServletRequestUtils.getStringParameter(request, "info", null); //this is a hidden field,
        if (hidden != null && !hidden.equals("")) { //if filled, tis teh botz!
            log.info("Botfield filled in contestform. IP: " + request.getRemoteAddr());
            return new ModelAndView("redirect:");
        }
        String id = ServletRequestUtils.getStringParameter(request, "captchaID", null);
        String input = ServletRequestUtils.getStringParameter(request, "captchaText", null);

        //mailCleaner.cleanMail(mail);

        Boolean correct = true;
        if (id != null) {
            try {
                correct = imageCaptchaService.validateResponseForID(id, input);
            } catch (CaptchaServiceException e) { //When first pressing wrong key, then refreshing the captchaid is wrong
                correct = false;

            }
        }

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("tlf", ServletRequestUtils.getStringParameter(request, "tlf", ""));

        if (correct || !captchaEnabled) {
            mailSender.sendEmail(mail, map, "no/dusken/aranea/base/web/mail/template.vm");
            return new ModelAndView("no/dusken/aranea/base/web/form/success");
        } else {
            map.put("CaptchaError", true);
            log.info("Input of wrong captcha");
            // view form
            map.put("captchaString", UUID.randomUUID().toString());
            map.put("entity", mail);
            logger.info("Input of wrong captcha in form");
            return new ModelAndView("no/dusken/aranea/base/web/form/form", map);
        }
    }

    @Required
    public void setImageCaptchaService(ImageCaptchaService imageCaptchaService) {
        this.imageCaptchaService = imageCaptchaService;
    }

    @Autowired
    @Required
    public void setMailCleaner(MailCleaner mailCleaner) {
        this.mailCleaner = mailCleaner;
    }

    @Required
    public void setCaptchaEnabled(Boolean captchaEnabled) {
        this.captchaEnabled = captchaEnabled;
    }

    @Autowired
    @Required
    public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
    }
}