com.iterzp.momo.service.impl.CaptchaServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.iterzp.momo.service.impl.CaptchaServiceImpl.java

Source

/*
 * Copyright 2005-2013 iterzp.com. All rights reserved.
 * Support: http://www.iterzp.com
 * License: http://www.iterzp.com/license
 */
package com.iterzp.momo.service.impl;

import java.awt.image.BufferedImage;

import javax.annotation.Resource;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.iterzp.momo.entity.Setting;
import com.iterzp.momo.entity.Setting.CaptchaType;
import com.iterzp.momo.service.CaptchaService;
import com.iterzp.momo.utils.SettingUtils;

/**
 * ?????
 * @author canfly
 * @version 1.0
 */
@Service("captchaServiceImpl")
public class CaptchaServiceImpl implements CaptchaService {

    @Resource(name = "imageCaptchaService")
    private com.octo.captcha.service.CaptchaService imageCaptchaService;

    @Override
    public BufferedImage buildImage(String captchaId) {
        return (BufferedImage) imageCaptchaService.getChallengeForID(captchaId);
    }

    @Override
    public boolean isValid(CaptchaType captchaType, String captchaId, String captcha) {
        Setting setting = SettingUtils.get();
        if (captchaType == null || ArrayUtils.contains(setting.getCaptchaTypes(), captchaType)) {
            if (StringUtils.isNotEmpty(captchaId) && StringUtils.isNotEmpty(captcha)) {
                try {
                    return imageCaptchaService.validateResponseForID(captchaId, captcha.toUpperCase());
                } catch (Exception e) {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

}