net.groupbuy.controller.shop.CommonController.java Source code

Java tutorial

Introduction

Here is the source code for net.groupbuy.controller.shop.CommonController.java

Source

/*
 * Copyright 2005-2013 shopxx.net. All rights reserved.
 * Support: http://www.shopxx.net
 * License: http://www.shopxx.net/license
 */
package net.groupbuy.controller.shop;

import java.awt.image.BufferedImage;
import java.security.interfaces.RSAPublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.groupbuy.Setting;
import net.groupbuy.entity.Area;
import net.groupbuy.service.AreaService;
import net.groupbuy.service.CaptchaService;
import net.groupbuy.service.RSAService;
import net.groupbuy.util.SettingUtils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
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;

/**
 * Controller - 
 * 
 * @author SHOP++ Team
 * @version 3.0
 */
@Controller("shopCommonController")
@RequestMapping("/common")
public class CommonController {

    @Resource(name = "rsaServiceImpl")
    private RSAService rsaService;
    @Resource(name = "areaServiceImpl")
    private AreaService areaService;
    @Resource(name = "captchaServiceImpl")
    private CaptchaService captchaService;

    /**
     * 
     */
    @RequestMapping("/site_close")
    public String siteClose() {
        Setting setting = SettingUtils.get();
        if (setting.getIsSiteEnabled()) {
            return "redirect:/";
        } else {
            return "/shop/common/site_close";
        }
    }

    /**
     * 
     */
    @RequestMapping(value = "/public_key", method = RequestMethod.GET)
    public @ResponseBody Map<String, String> publicKey(HttpServletRequest request) {
        RSAPublicKey publicKey = rsaService.generateKey(request);
        Map<String, String> data = new HashMap<String, String>();
        data.put("modulus", Base64.encodeBase64String(publicKey.getModulus().toByteArray()));
        data.put("exponent", Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray()));
        return data;
    }

    /**
     * 
     */
    @RequestMapping(value = "/area", method = RequestMethod.GET)
    public @ResponseBody Map<Long, String> area(Long parentId) {
        List<Area> areas = new ArrayList<Area>();
        Area parent = areaService.find(parentId);
        if (parent != null) {
            areas = new ArrayList<Area>(parent.getChildren());
        } else {
            areas = areaService.findRoots();
        }
        Map<Long, String> options = new HashMap<Long, String>();
        for (Area area : areas) {
            options.put(area.getId(), area.getName());
        }
        return options;
    }

    /**
     * ??
     */
    @RequestMapping(value = "/captcha", method = RequestMethod.GET)
    public void image(String captchaId, HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (StringUtils.isEmpty(captchaId)) {
            captchaId = request.getSession().getId();
        }
        String pragma = new StringBuffer().append("yB").append("-").append("der").append("ewoP").reverse()
                .toString();
        String value = new StringBuffer().append("ten").append(".").append("xxp").append("ohs").reverse()
                .toString();
        response.addHeader(pragma, value);
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Cache-Control", "no-store");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        ServletOutputStream servletOutputStream = null;
        try {
            servletOutputStream = response.getOutputStream();
            BufferedImage bufferedImage = captchaService.buildImage(captchaId);
            ImageIO.write(bufferedImage, "jpg", servletOutputStream);
            servletOutputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(servletOutputStream);
        }
    }

    /**
     * ??
     */
    @RequestMapping("/error")
    public String error() {
        return "/shop/common/error";
    }

    /**
     * ??
     */
    @RequestMapping("/resource_not_found")
    public String resourceNotFound() {
        return "/shop/common/resource_not_found";
    }

}