Java tutorial
/* * Copyright 2005-2013 shopxx.net. All rights reserved. * Support: http://www.shopxx.net * License: http://www.shopxx.net/license */ package net.groupbuy.controller.admin; import java.io.FileOutputStream; import java.io.OutputStream; import java.net.ConnectException; import java.net.UnknownHostException; import java.util.Properties; import javax.annotation.Resource; import javax.mail.MessagingException; import net.groupbuy.CommonAttributes; import net.groupbuy.Message; import net.groupbuy.Setting; import net.groupbuy.FileInfo.FileType; import net.groupbuy.Setting.AccountLockType; import net.groupbuy.Setting.CaptchaType; import net.groupbuy.Setting.ConsultationAuthority; import net.groupbuy.Setting.ReviewAuthority; import net.groupbuy.Setting.RoundType; import net.groupbuy.Setting.StockAllocationTime; import net.groupbuy.Setting.WatermarkPosition; import net.groupbuy.service.CacheService; import net.groupbuy.service.FileService; import net.groupbuy.service.MailService; import net.groupbuy.service.StaticService; import net.groupbuy.util.SettingUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.mail.MailAuthenticationException; import org.springframework.mail.MailSendException; 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.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.sun.mail.smtp.SMTPSendFailedException; import com.sun.mail.smtp.SMTPSenderFailedException; /** * Controller - * * @author SHOP++ Team * @version 3.0 */ @Controller("adminstingController") @RequestMapping("/admin/setting") public class SettingController extends BaseController { @Resource(name = "fileServiceImpl") private FileService fileService; @Resource(name = "mailServiceImpl") private MailService mailService; @Resource(name = "cacheServiceImpl") private CacheService cacheService; @Resource(name = "staticServiceImpl") private StaticService staticService; /** * */ @RequestMapping(value = "/mail_test", method = RequestMethod.POST) public @ResponseBody Message mailTest(String smtpFromMail, String smtpHost, Integer smtpPort, String smtpUsername, String smtpPassword, String toMail) { if (StringUtils.isEmpty(toMail)) { return ERROR_MESSAGE; } Setting setting = SettingUtils.get(); if (StringUtils.isEmpty(smtpPassword)) { smtpPassword = setting.getSmtpPassword(); } try { if (!isValid(Setting.class, "smtpFromMail", smtpFromMail) || !isValid(Setting.class, "smtpHost", smtpHost) || !isValid(Setting.class, "smtpPort", smtpPort) || !isValid(Setting.class, "smtpUsername", smtpUsername)) { return ERROR_MESSAGE; } mailService.sendTestMail(smtpFromMail, smtpHost, smtpPort, smtpUsername, smtpPassword, toMail); } catch (MailSendException e) { Exception[] messageExceptions = e.getMessageExceptions(); if (messageExceptions != null) { for (Exception exception : messageExceptions) { if (exception instanceof SMTPSendFailedException) { SMTPSendFailedException smtpSendFailedException = (SMTPSendFailedException) exception; Exception nextException = smtpSendFailedException.getNextException(); if (nextException instanceof SMTPSenderFailedException) { return Message.error("admin.setting.mailTestSenderFailed"); } } else if (exception instanceof MessagingException) { MessagingException messagingException = (MessagingException) exception; Exception nextException = messagingException.getNextException(); if (nextException instanceof UnknownHostException) { return Message.error("admin.setting.mailTestUnknownHost"); } else if (nextException instanceof ConnectException) { return Message.error("admin.setting.mailTestConnect"); } } } } return Message.error("admin.setting.mailTestError"); } catch (MailAuthenticationException e) { return Message.error("admin.setting.mailTestAuthentication"); } catch (Exception e) { return Message.error("admin.setting.mailTestError"); } return Message.success("admin.setting.mailTestSuccess"); } /** * */ @RequestMapping(value = "/edit", method = RequestMethod.GET) public String edit(ModelMap model) { model.addAttribute("watermarkPositions", WatermarkPosition.values()); model.addAttribute("roundTypes", RoundType.values()); model.addAttribute("captchaTypes", CaptchaType.values()); model.addAttribute("accountLockTypes", AccountLockType.values()); model.addAttribute("stockAllocationTimes", StockAllocationTime.values()); model.addAttribute("reviewAuthorities", ReviewAuthority.values()); model.addAttribute("consultationAuthorities", ConsultationAuthority.values()); return "/admin/setting/edit"; } /** * */ @RequestMapping(value = "/update", method = RequestMethod.POST) public String update(Setting setting, MultipartFile watermarkImageFile, RedirectAttributes redirectAttributes) { if (!isValid(setting)) { return ERROR_VIEW; } if (setting.getUsernameMinLength() > setting.getUsernameMaxLength() || setting.getPasswordMinLength() > setting.getPasswordMinLength()) { return ERROR_VIEW; } Setting srcSetting = SettingUtils.get(); if (StringUtils.isEmpty(setting.getSmtpPassword())) { setting.setSmtpPassword(srcSetting.getSmtpPassword()); } if (watermarkImageFile != null && !watermarkImageFile.isEmpty()) { if (!fileService.isValid(FileType.image, watermarkImageFile)) { addFlashMessage(redirectAttributes, Message.error("admin.upload.invalid")); return "redirect:edit.jhtml"; } String watermarkImage = fileService.uploadLocal(FileType.image, watermarkImageFile); setting.setWatermarkImage(watermarkImage); } else { setting.setWatermarkImage(srcSetting.getWatermarkImage()); } setting.setCnzzSiteId(srcSetting.getCnzzSiteId()); setting.setCnzzPassword(srcSetting.getCnzzPassword()); SettingUtils.set(setting); cacheService.clear(); staticService.buildIndex(); staticService.buildOther(); OutputStream outputStream = null; try { org.springframework.core.io.Resource resource = new ClassPathResource( CommonAttributes.SHOPXX_PROPERTIES_PATH); Properties properties = PropertiesLoaderUtils.loadProperties(resource); String templateUpdateDelay = properties.getProperty("template.update_delay"); String messageCacheSeconds = properties.getProperty("message.cache_seconds"); if (setting.getIsDevelopmentEnabled()) { if (!templateUpdateDelay.equals("0") || !messageCacheSeconds.equals("0")) { outputStream = new FileOutputStream(resource.getFile()); properties.setProperty("template.update_delay", "0"); properties.setProperty("message.cache_seconds", "0"); properties.store(outputStream, "SHOP++ PROPERTIES"); } } else { if (templateUpdateDelay.equals("0") || messageCacheSeconds.equals("0")) { outputStream = new FileOutputStream(resource.getFile()); properties.setProperty("template.update_delay", "3600"); properties.setProperty("message.cache_seconds", "3600"); properties.store(outputStream, "SHOP++ PROPERTIES"); } } } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(outputStream); } addFlashMessage(redirectAttributes, SUCCESS_MESSAGE); return "redirect:edit.jhtml"; } }