1 16 17 package samples.security; 18 19 import org.apache.axis.AxisFault; 20 import org.apache.axis.Handler; 21 import org.apache.axis.Message; 22 import org.apache.axis.MessageContext; 23 import org.apache.axis.components.logger.LogFactory; 24 import org.apache.axis.handlers.BasicHandler; 25 import org.apache.axis.utils.Messages; 26 import org.apache.commons.logging.Log; 27 import org.apache.xml.security.signature.XMLSignature; 28 import org.apache.xml.security.utils.Constants; 29 import org.apache.xpath.CachedXPathAPI; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Element ; 32 33 import java.io.FileWriter ; 34 import java.io.PrintWriter ; 35 36 public class LogHandler extends BasicHandler { 37 static Log log = 38 LogFactory.getLog(LogHandler.class.getName()); 39 40 static { 41 org.apache.xml.security.Init.init(); 42 } 43 44 public void invoke(MessageContext msgContext) throws AxisFault { 45 try { 46 System.out.println("Starting Server verification"); 47 48 Message inMsg = msgContext.getRequestMessage(); 49 Message outMsg = msgContext.getResponseMessage(); 50 51 53 Document doc = inMsg.getSOAPEnvelope().getAsDocument(); 54 String BaseURI = "http://xml-security"; 55 CachedXPathAPI xpathAPI = new CachedXPathAPI(); 56 57 Element nsctx = doc.createElement("nsctx"); 58 nsctx.setAttribute("xmlns:ds", Constants.SignatureSpecNS); 59 60 Element signatureElem = (Element ) xpathAPI.selectSingleNode(doc, 61 "//ds:Signature", nsctx); 62 63 if (signatureElem == null) { 65 System.out.println("The document is not signed"); 66 return; 67 } 68 69 XMLSignature sig = new XMLSignature(signatureElem, BaseURI); 70 71 boolean verify = sig.checkSignatureValue(sig.getKeyInfo().getPublicKey()); 72 System.out.println("Server verification complete."); 73 74 System.out.println("The signature is" + (verify 75 ? " " 76 : " not ") + "valid"); 77 } catch (Exception e) { 78 throw AxisFault.makeFault(e); 79 } 80 81 } 82 83 public void onFault(MessageContext msgContext) { 84 try { 85 Handler serviceHandler = msgContext.getService(); 86 String filename = (String ) getOption("filename"); 87 if ((filename == null) || (filename.equals(""))) 88 throw new AxisFault("Server.NoLogFile", 89 "No log file configured for the LogHandler!", 90 null, null); 91 FileWriter fw = new FileWriter (filename, true); 92 PrintWriter pw = new PrintWriter (fw); 93 pw.println("====================="); 94 pw.println("= " + Messages.getMessage("fault00")); 95 pw.println("====================="); 96 pw.close(); 97 } catch (Exception e) { 98 log.error(e); 99 } 100 } 101 } 102 | Popular Tags |