package com.orange.ecare.spring.mvc.controllers.jsp.vouchers;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.orange.ecare.spring.misc.CountingBean;
import com.orange.ecare.spring.mvc.controllers.abstracts.AbstractRestfulController;
import com.orange.ecare.spring.mvc.controllers.tokens.RequestTokens;
import com.orange.ecare.util.RequestUtils;
import com.orange.ecare.webservices.webmethods.clients.accountStatus.AccountStatusClient;
import com.orange.ecare.webservices.webmethods.clients.redeemVoucher.RedeemVoucherClient;
public class Vouchers extends AbstractRestfulController {
private Log log = LogFactory.getLog(Vouchers.class.getName());
//******************** Spring Injected ***************
private AccountStatusClient accountStatusClient;
private RedeemVoucherClient redeemVoucherClient;
private ImageCaptchaService captchaService;
private CountingBean captchaAttempts;
private CountingBean voucherAttempts;
private RequestTokens requestTokens;
public AccountStatusClient getAccountStatusClient() {return accountStatusClient;}
public void setAccountStatusClient(AccountStatusClient accountStatusClient) {this.accountStatusClient = accountStatusClient;}
public RedeemVoucherClient getRedeemVoucherClient() {return redeemVoucherClient;}
public void setRedeemVoucherClient(RedeemVoucherClient redeemVoucherClient) {this.redeemVoucherClient = redeemVoucherClient;}
public RequestTokens getRequestTokens() {return requestTokens;}
public void setRequestTokens(RequestTokens requestTokens) {this.requestTokens = requestTokens;}
public CountingBean getCaptchaAttempts() {return captchaAttempts;}
public CountingBean getVoucherAttempts() {return voucherAttempts;}
public void setVoucherAttempts(CountingBean voucherAttempts) {this.voucherAttempts = voucherAttempts;}
public void setCaptchaAttempts(CountingBean captchaAttempts) {this.captchaAttempts = captchaAttempts;}
public ImageCaptchaService getCaptchaService() {return captchaService;}
public void setCaptchaService(ImageCaptchaService captchaService) {this.captchaService = captchaService;}
//****************************************************
//********************* validation methods ***********
//****************************************************
protected boolean isCaptchaValid(HttpServletRequest req,String answer) throws Exception {
Boolean isResponseCorrect = getCaptchaService().validateResponseForID(req.getSession().getId(),answer);
return isResponseCorrect.booleanValue();
}
protected boolean isMsisdnValid(String msisdn) throws Exception {
return getAccountStatusClient().isValidMSISDN(msisdn);
}
protected boolean isVoucherValid(String msisdn, String voucher,Map model) throws Exception {
return getRedeemVoucherClient().processVoucher(msisdn, voucher);
}
//****************************************************
//****************************************************
//****************************************************
protected ModelAndView doPost(HttpServletRequest request, HttpServletResponse response, Map model) throws Exception {
log.debug("user submitted a voucher redemption request form");
//first check for double clicked submission using the token bean
if (!getRequestTokens().isTokenValid(request)) {
log.debug("user tried to redeem the same voucher twice");
return new ModelAndView("vouchers/badToken",model);
}
//Firstly check to see if the captcha challenge was good
String canswer = request.getParameter("captcha");
if (canswer==null || (!isCaptchaValid(request,canswer))) {
log.debug("user failed capcha they entered " + canswer);
//make tell the JSP that captcha failed
model.put("captchaError",new Boolean(true));
//increment the number of captcha attempts
getCaptchaAttempts().incAttempts(request);
//handle max captcha attempts
if (getCaptchaAttempts().isMaxAttempts(request)) {
log.warn("REPEATEDELY_FAILED_VOUCHER_CAPTCHAS (" + RequestUtils.getRemoteAddr(request) + ")");
return new ModelAndView("exception/maxCaptcha",model);
} else {
return new ModelAndView("vouchers/vouchers",model);
}
}
//now check that the MSISDN is valid
String msisdn = request.getParameter("msisdn");
if (msisdn==null || (!isMsisdnValid(msisdn))) {
log.debug("user entered an invalid msisdn : " + msisdn);
model.put("msisdnError",new Boolean(true));
return new ModelAndView("vouchers/vouchers",model);
}
//finally, attempt to redeem the voucher
String voucher = request.getParameter("voucherNumber");
if (voucher==null || (!isVoucherValid(msisdn,voucher,model))) {
log.debug("user entered an invalid voucher : " + voucher);
model.put("voucherError",new Boolean(true));
getVoucherAttempts().incAttempts(request);
if (getVoucherAttempts().isMaxAttempts(request)) {
log.warn("REPEATEDLY_FAILED_VOUCHER_REDEMPTION (" + RequestUtils.getRemoteAddr(request) + ")");
return new ModelAndView("exception/maxCaptcha",model);
} else {
return new ModelAndView("vouchers/vouchers",model);
}
}
//redemption succeeded
return new ModelAndView("vouchers/voucher_accepted",model);
}
protected ModelAndView doGet(HttpServletRequest request, HttpServletResponse response, Map model) throws Exception {
log.debug("User requested the displaying of the voucher redemption page");
//check to see if the user has reached the maximum captcha attempts
if (getCaptchaAttempts().isMaxAttempts(request)) {
log.warn("user has requested the voucher topup page to be displayed but has already had the maximum number of captcha attempts for this session");
return new ModelAndView("exception/maxCaptcha",model);
}
if (getVoucherAttempts().isMaxAttempts(request)) {
log.warn("user has requested the voucher topup page to be displayed but has already had the maximum number of voucher attempts for this session");
return new ModelAndView("exception/maxVoucher",model);
}
//reset the request token
getRequestTokens().resetToken(request);
return new ModelAndView("vouchers/vouchers",model);
}
}