com.abc.turkey.web.unfreeze.UnfreezeController.java Source code

Java tutorial

Introduction

Here is the source code for com.abc.turkey.web.unfreeze.UnfreezeController.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.abc.turkey.web.unfreeze;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.abc.turkey.service.ServiceException;
import com.abc.turkey.service.account.BonusService;
import com.abc.turkey.service.account.ShiroDbRealm.ShiroUser;
import com.abc.turkey.service.unfreeze.UnfreezeProxy;
import com.abc.turkey.web.support.JsonResponse;

/**
 * Controller
 * 
 * @author calvin
 */
@Controller
@RequestMapping(value = "/unfreeze")
public class UnfreezeController {
    private static final String captcha_prefix = "https://ssl.captcha.qq.com/";

    private static Logger logger = LoggerFactory.getLogger(UnfreezeController.class);

    private CloseableHttpClient httpClient = HttpClients.createDefault();

    @Autowired
    private BonusService bonusService;

    @RequestMapping(method = RequestMethod.GET)
    public String show() {
        return "unfreeze/show";
    }

    // ??
    @RequestMapping(value = "/captcha")
    public String captcha() {
        return "unfreeze/captcha";
    }

    // ?sig
    @RequestMapping(value = "/cap_union_getsig_new")
    public void getNewSig(HttpServletRequest request, HttpServletResponse response) {
        String url = captcha_prefix + "cap_union_getsig_new?" + request.getQueryString();
        HttpGet httpGet = new HttpGet(url);
        try {
            CloseableHttpResponse response2 = httpClient.execute(httpGet);
            response2.getEntity().writeTo(response.getOutputStream());
            response2.close();
        } catch (Exception e) {
            throw new ServiceException(e.getMessage(), e.getCause());
        }
    }

    // sig??
    @RequestMapping(value = "/cap_union_getcapbysig_new")
    public void getCaptchaImage(HttpServletRequest request, HttpServletResponse response) {
        String url = captcha_prefix + "cap_union_getcapbysig_new?" + request.getQueryString();
        HttpGet httpGet = new HttpGet(url);
        try {
            CloseableHttpResponse response2 = httpClient.execute(httpGet);
            response2.getEntity().writeTo(response.getOutputStream());
            response2.close();
        } catch (Exception e) {
            throw new ServiceException(e.getMessage(), e.getCause());
        }
    }

    // ????
    @RequestMapping(value = "/cap_union_verify_new")
    public void capVerify(HttpServletRequest request, HttpServletResponse response) {
        String url = captcha_prefix + "cap_union_verify_new?" + request.getQueryString();
        HttpGet httpGet = new HttpGet(url);
        try {
            CloseableHttpResponse response2 = httpClient.execute(httpGet);
            response2.getEntity().writeTo(response.getOutputStream());
            response2.close();
        } catch (Exception e) {
            throw new ServiceException(e.getMessage(), e.getCause());
        }
    }

    @RequestMapping(value = "/deal", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse dealNumbers(String number, String ticket, String randstr) {
        ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
        JsonResponse response = new JsonResponse();
        try {
            UnfreezeProxy proxy = new UnfreezeProxy();
            proxy.unfreeze(number, ticket, randstr);
        } catch (Exception e) {
            logger.error(e.toString());
            response.setStatus("fail");
            if (e instanceof ServiceException) {
                response.setResult(e.getMessage());
            } else {
                response.setResult("");
            }
            return response;
        }
        bonusService.consume(user.id, 1);
        int newBonus = bonusService.getBonus(user.id);
        response.setStatus("ok");
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("msg", "???");
        result.put("bonus", newBonus);
        response.setResult(result);
        return response;
    }

}