Java tutorial
package com.esoft.yeepay.user.service.impl; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.annotation.Resource; import javax.faces.context.FacesContext; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; import org.hibernate.LockMode; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.esoft.archer.system.service.SpringSecurityService; import com.esoft.archer.user.model.Role; import com.esoft.archer.user.model.User; import com.esoft.archer.user.service.UserService; import com.esoft.archer.user.service.impl.UserBO; import com.esoft.core.annotations.Logger; import com.esoft.core.jsf.util.FacesUtil; import com.esoft.core.util.Dom4jUtil; import com.esoft.core.util.IdGenerator; import com.esoft.core.util.MapUtil; import com.esoft.jdp2p.trusteeship.TrusteeshipConstants; import com.esoft.jdp2p.trusteeship.exception.TrusteeshipReturnException; import com.esoft.jdp2p.trusteeship.model.TrusteeshipAccount; import com.esoft.jdp2p.trusteeship.model.TrusteeshipOperation; import com.esoft.jdp2p.trusteeship.service.impl.TrusteeshipOperationBO; import com.esoft.yeepay.sign.CFCASignUtil; import com.esoft.yeepay.trusteeship.YeePayConstants; import com.esoft.yeepay.trusteeship.service.impl.YeePayOperationServiceAbs; import com.esoft.yeepay.user.model.YeepayCorpInfo; @Service("yeePayCorpAccountOperation") public class YeePayCorpAccountOperation extends YeePayOperationServiceAbs<YeepayCorpInfo> { @Resource TrusteeshipOperationBO trusteeshipOperationBO; @Resource HibernateTemplate ht; @Resource UserService userService; @Resource private SpringSecurityService springSecurityService; @Logger static Log log; @Resource private UserBO userBO; @Override @Transactional(rollbackFor = Exception.class) public TrusteeshipOperation createOperation(YeepayCorpInfo yci, FacesContext fc) throws IOException { ht.saveOrUpdate(yci); User user = yci.getUser(); StringBuffer content = new StringBuffer(); content.append("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"); content.append("<request platformNo='" + YeePayConstants.Config.MER_CODE + "'>"); // ?? content.append("<requestNo>" + YeePayConstants.RequestNoPre.ENTERPRISE_REGISTER + yci.getSeq() + "a" + user.getId() + "</requestNo>"); // ?:? content.append("<platformUserNo>" + user.getId() + "</platformUserNo>"); // ??? content.append("<enterpriseName>" + yci.getEnterpriseName() + "</enterpriseName>"); // ?? content.append("<bankLicense>" + yci.getBankLicense() + "</bankLicense>"); // ? content.append("<orgNo>" + yci.getOrgNo() + "</orgNo>"); // ?? content.append("<businessLicense>" + yci.getBusinessLicense() + "</businessLicense>"); // ? content.append("<taxNo>" + yci.getTaxNo() + "</taxNo>"); // ?? content.append("<legal>" + yci.getLegal() + "</legal>"); // ?? content.append("<legalIdNo>" + yci.getLegalIdNo() + "</legalIdNo>"); // ?? content.append("<contact>" + yci.getContact() + "</contact>"); // ?? content.append("<contactPhone>" + yci.getContactPhone() + "</contactPhone>"); // ? content.append("<email>" + yci.getEmail() + "</email>"); // content.append("<memberClassType>" + yci.getMemberClassType() + "</memberClassType>"); // URL content.append("<callbackUrl>" + YeePayConstants.ResponseWebUrl.PRE_RESPONSE_URL + YeePayConstants.OperationType.ENTERPRISE_REGISTER + "</callbackUrl>"); // ? URL content.append("<notifyUrl>" + YeePayConstants.ResponseS2SUrl.PRE_RESPONSE_URL + YeePayConstants.OperationType.ENTERPRISE_REGISTER + "</notifyUrl>"); content.append("</request>"); log.debug(content.toString()); // ? Map<String, String> params = new HashMap<String, String>(); params.put("req", content.toString()); String sign = CFCASignUtil.sign(content.toString()); log.debug(sign); params.put("sign", sign); TrusteeshipOperation to = new TrusteeshipOperation(); to.setId(IdGenerator.randomUUID()); to.setMarkId(user.getId()); to.setOperator(user.getId()); to.setRequestUrl(YeePayConstants.RequestUrl.ENTERPRISE_REGISTER); to.setRequestData(MapUtil.mapToString(params)); to.setStatus(TrusteeshipConstants.Status.UN_SEND); to.setType(YeePayConstants.OperationType.ENTERPRISE_REGISTER); to.setTrusteeship("yeepay"); trusteeshipOperationBO.save(to); try { super.sendOperation(to.getId(), "utf-8", fc); } catch (IOException e) { e.printStackTrace(); } ht.evict(user); user = ht.get(User.class, user.getId()); userBO.addRole(user, new Role("WAIT_CONFIRM")); springSecurityService.refreshLoginUserAuthorities(user.getId()); return null; } @Override @Transactional(rollbackFor = Exception.class, noRollbackFor = TrusteeshipReturnException.class) public void receiveOperationPostCallback(ServletRequest request) throws TrusteeshipReturnException { try { request.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } // ?? xml? String respXML = request.getParameter("resp"); log.debug(respXML.toString()); // ?? String sign = request.getParameter("sign"); boolean flag = CFCASignUtil.isVerifySign(respXML, sign); if (flag) { // ?? @SuppressWarnings("unchecked") Map<String, String> resultMap = Dom4jUtil.xmltoMap(respXML); // ?? userId String requestNo = resultMap.get("requestNo").substring(resultMap.get("requestNo").indexOf("a") + 1); // ? String code = resultMap.get("code"); String description = resultMap.get("description"); TrusteeshipOperation to = trusteeshipOperationBO.get(YeePayConstants.OperationType.ENTERPRISE_REGISTER, requestNo, requestNo, "yeepay"); ht.evict(to); to = ht.get(TrusteeshipOperation.class, to.getId(), LockMode.UPGRADE); to.setResponseTime(new Date()); to.setResponseData(respXML); // ? ???? User user = ht.get(User.class, requestNo); if ("1".equals(code)) { if (user != null) { TrusteeshipAccount ta = ht.get(TrusteeshipAccount.class, user.getId()); if (ta == null) { ta = new TrusteeshipAccount(); ta.setId(user.getId()); ta.setUser(user); } ta.setAccountId(user.getId()); ta.setCreateTime(new Date()); ta.setStatus(TrusteeshipConstants.Status.PASSED); ta.setTrusteeship("yeepay"); ht.saveOrUpdate(ta); userBO.removeRole(user, new Role("WAIT_CONFIRM")); userBO.addRole(user, new Role("LOANER")); // ?? springSecurityService.refreshLoginUserAuthorities(user.getId()); to.setStatus(TrusteeshipConstants.Status.PASSED); ht.merge(to); } } else { to.setStatus(TrusteeshipConstants.Status.REFUSED); ht.merge(to); userBO.removeRole(user, new Role("WAIT_CONFIRM")); // ?? springSecurityService.refreshLoginUserAuthorities(user.getId()); if ("0".equals(code)) { throw new TrusteeshipReturnException(description); } // throw new TrusteeshipReturnException(code + ":" + description); } } } @Override @Transactional(rollbackFor = Exception.class) public void receiveOperationS2SCallback(ServletRequest request, ServletResponse response) { try { request.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } // ?? xml? String notifyxml = request.getParameter("notify"); // ?? String sign = request.getParameter("sign"); boolean flag = CFCASignUtil.isVerifySign(notifyxml, sign); if (flag) { // ?? @SuppressWarnings("unchecked") Map<String, String> resultMap = Dom4jUtil.xmltoMap(notifyxml); String code = resultMap.get("code"); String message = resultMap.get("message"); String platformUserNo = resultMap.get("platformUserNo"); TrusteeshipOperation to = trusteeshipOperationBO.get(YeePayConstants.OperationType.ENTERPRISE_REGISTER, platformUserNo, platformUserNo, "yeepay"); ht.evict(to); to = ht.get(TrusteeshipOperation.class, to.getId(), LockMode.UPGRADE); to.setResponseTime(new Date()); to.setResponseData(notifyxml); User user = ht.get(User.class, platformUserNo); log.info("code:" + code); if ("1".equals(code)) { if (user != null) { TrusteeshipAccount ta = ht.get(TrusteeshipAccount.class, user.getId()); if (ta == null) { ta = new TrusteeshipAccount(); ta.setId(user.getId()); ta.setUser(user); } ta.setAccountId(user.getId()); ta.setCreateTime(new Date()); ta.setStatus(TrusteeshipConstants.Status.PASSED); ta.setTrusteeship("yeepay"); ht.saveOrUpdate(ta); userBO.removeRole(user, new Role("WAIT_CONFIRM")); userBO.addRole(user, new Role("LOANER")); // ?? springSecurityService.refreshLoginUserAuthorities(user.getId()); to.setStatus(TrusteeshipConstants.Status.PASSED); ht.merge(to); } } else if ("0".equals(code) || "104".equals(code)) { to.setStatus(TrusteeshipConstants.Status.REFUSED); ht.merge(to); userBO.removeRole(user, new Role("WAIT_CONFIRM")); // ?? springSecurityService.refreshLoginUserAuthorities(user.getId()); } else { // throw new RuntimeException(new TrusteeshipReturnException(code + ":" + message)); } } try { response.getWriter().write("SUCCESS"); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } } }