Java tutorial
/* * Copyright 2005-2013 shopxx.net. All rights reserved. * Support: http://www.shopxx.net * License: http://www.shopxx.net/license */ package cn.shengyuan.yun.admin.system.controller; import cn.shengyuan.tools.util.StringUtil; import com.google.code.kaptcha.Constants; import com.google.code.kaptcha.Producer; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.context.ServletContextAware; import javax.annotation.Resource; import javax.imageio.ImageIO; import javax.servlet.ServletContext; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * Controller - * * @author SHOP++ Team * @version 3.0 */ @Controller("commonController") @RequestMapping("/admin/common") public class CommonController implements ServletContextAware { @Value("${system.name}") private String systemName; @Value("${system.version}") private String systemVersion; @Value("${system.description}") private String systemDescription; @Value("${system.show_powered}") private Boolean systemShowPowered; @Resource private Producer captchaProducer; /** servletContext */ private ServletContext servletContext; public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; } /** * */ @RequestMapping(value = "/main", method = RequestMethod.GET) public String main() { return "/common/main"; } /** * */ @RequestMapping(value = "/index", method = RequestMethod.GET) public String index(ModelMap model) { Map<String, Object> options = new HashMap<String, Object>(); model.addAttribute("systemName", systemName); model.addAttribute("systemVersion", systemVersion); model.addAttribute("systemDescription", systemDescription); model.addAttribute("systemShowPowered", systemShowPowered); model.addAttribute("javaVersion", System.getProperty("java.version")); model.addAttribute("javaHome", System.getProperty("java.home")); model.addAttribute("osName", System.getProperty("os.name")); model.addAttribute("osArch", System.getProperty("os.arch")); model.addAttribute("serverInfo", servletContext.getServerInfo()); model.addAttribute("servletVersion", servletContext.getMajorVersion() + "." + servletContext.getMinorVersion()); Map<String, Object> map = new HashMap<String, Object>(); map.put("receiverRead", false); return "/common/index"; } /** * */ @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 = "/captchaImage", method = RequestMethod.GET) public String captchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setDateHeader("Expires", 0); // Set standard HTTP/1.1 no-cache headers. response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); // Set IE extended HTTP/1.1 no-cache headers (use addHeader). response.addHeader("Cache-Control", "post-check=0, pre-check=0"); // Set standard HTTP/1.0 no-cache header. response.setHeader("Pragma", "no-cache"); // return a jpeg response.setContentType("image/jpeg"); // create the text for the image String capText = captchaProducer.createText(); // store the text in the session request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); // create the image with the text BufferedImage bi = captchaProducer.createImage(capText); ServletOutputStream out = response.getOutputStream(); // write the data out ImageIO.write(bi, "jpg", out); try { out.flush(); } finally { out.close(); } return null; } /** * ?? */ @RequestMapping(value = "/captcha", method = RequestMethod.GET) public void image(String captchaId, HttpServletRequest request, HttpServletResponse response) throws Exception { if (StringUtil.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(); servletOutputStream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(servletOutputStream); } } /** * ?? */ @RequestMapping("/error") public String error() { return "/common/error"; } /** * ?? */ @RequestMapping("/resource_not_found") public String resourceNotFound() { return "/common/resource_not_found"; } /** * ?? */ @RequestMapping("/unauthorized") public String unauthorized(HttpServletRequest request, HttpServletResponse response) { String requestType = request.getHeader("X-Requested-With"); if (requestType != null && requestType.equalsIgnoreCase("XMLHttpRequest")) { response.addHeader("loginStatus", "unauthorized"); try { response.sendError(HttpServletResponse.SC_FORBIDDEN); } catch (IOException e) { e.printStackTrace(); } return null; } return "/common/unauthorized"; } }