Java tutorial
/* * @(#)DigitalSignUtil.java 2009-6-30 * * Copyright 2008 LINKAGE, Inc. All rights reserved. * LINKAGE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.linkage.crm.csb.sign; import java.io.File; import java.io.FileInputStream; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.servlet.ServletContext; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.web.context.ServletContextAware; /** * Util. * SSO. * * packageExample. * * NOTE:-Dconfig.dir/SPRINGconfigDir * keystore. * * springsingleton. * * @author * @version 1.0.0, 2009-6-30 * @since 1.0 */ public final class DigitalSignUtil implements ServletContextAware { private static Log log = LogFactory.getLog(DigitalSignUtil.class); private ServletContext servletContext; private String configDir; private ICAPSigner uaSigner; //UA. private Map verifierMap = new HashMap(); //--ICAPSigner. public void setServletContext(ServletContext sc) { this.servletContext = sc; } /** * @param uaSigner the uaSigner to set */ public void setUaSigner(ICAPSigner uaSigner) { this.uaSigner = uaSigner; } /** * .xml. * ${config.dir}/keystore/ua/. * * @param xml capxml.DigitalSign<DigitalSign/> * @throws IOException . * @throws IllegalArgumentException if xml is empty. * @return xml. */ public String signature(String xml) throws SignException { return uaSigner.signatureCAP(xml); } /** * srcSysId,. * * @param reqCap * string req. * @return srcSysId or null if the reqCap not valid */ private String getSrcSysId(String reqCap, String key) { // String key = "SrcSysID"; if (reqCap != null && reqCap.indexOf(key) != -1) { int id1 = reqCap.indexOf(key) + key.length() + 1; String sub1 = reqCap.substring(id1); int id2 = sub1.indexOf("<"); String result = sub1.substring(0, id2); return result; } return null; } /** * . * @param code 5.:10.3 * @param xml xml. * @return true/false. * @throws IOException . */ public boolean verify(String xml, String key, String srcSysId) throws SignException { if (StringUtils.isEmpty(xml)) { throw new IllegalArgumentException("xml to be verified is null"); } boolean result = false; ICAPSigner verifier = null; if (srcSysId == null || srcSysId.equals("")) { srcSysId = getSrcSysId(xml, key); } log.debug("srcSysId==" + srcSysId); if (srcSysId != null) { //SP. verifier = (ICAPSigner) verifierMap.get(srcSysId); if (verifier == null) { // srcSysIdpwd. Properties props = new Properties(); String spConfigFile = null; try { System.out.println(this.servletContext.getRealPath(String.valueOf(File.separatorChar))); System.out.println(this.servletContext.getContextPath()); configDir = this.servletContext.getRealPath(String.valueOf(File.separatorChar)) + "/META-INF"; spConfigFile = configDir + File.separator + "keystore" + File.separator + srcSysId + File.separator + "pwd"; props.load(new FileInputStream(new File(spConfigFile))); log.debug("spConfigFile" + spConfigFile); log.debug("" + srcSysId + "." + props); } catch (Exception e) { throw new SignException(SignException.TYPE_VRF, "SP" + spConfigFile, e); } String fileName = null, alias = null, password = null, certFileName = null; if (props != null && props.size() > 0) { fileName = props.getProperty("filename"); alias = props.getProperty("alias"); password = props.getProperty("password"); certFileName = props.getProperty("certfile"); log.debug("%%%%%%%%%%%%%%%%" + fileName + "===" + alias + "===" + password + "===" + certFileName); { verifier = new DefaultCAPSigner(password, alias, configDir + File.separator + "keystore" + File.separator + srcSysId + File.separator + fileName, // configDir + File.separator + "keystore" + File.separator + srcSysId + File.separator + certFileName); // verifierMap.put(srcSysId, verifier); } } else { log.error(",srcSysId=" + srcSysId); } } } if (verifier != null) { result = verifier.verifyCAP(xml); } else { log.debug("SignException"); throw new SignException(SignException.TYPE_VRF, "" + srcSysId + "", new NullPointerException("verifier for " + srcSysId + " is null")); } return result; } }