001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package samples.security;
018:
019: import org.apache.axis.AxisFault;
020: import org.apache.axis.Handler;
021: import org.apache.axis.Message;
022: import org.apache.axis.MessageContext;
023: import org.apache.axis.components.logger.LogFactory;
024: import org.apache.axis.handlers.BasicHandler;
025: import org.apache.axis.utils.Messages;
026: import org.apache.commons.logging.Log;
027: import org.apache.xml.security.signature.XMLSignature;
028: import org.apache.xml.security.utils.Constants;
029: import org.apache.xpath.CachedXPathAPI;
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Element;
032:
033: import java.io.FileWriter;
034: import java.io.PrintWriter;
035:
036: public class LogHandler extends BasicHandler {
037: static Log log = LogFactory.getLog(LogHandler.class.getName());
038:
039: static {
040: org.apache.xml.security.Init.init();
041: }
042:
043: public void invoke(MessageContext msgContext) throws AxisFault {
044: try {
045: System.out.println("Starting Server verification");
046:
047: Message inMsg = msgContext.getRequestMessage();
048: Message outMsg = msgContext.getResponseMessage();
049:
050: // verify signed message
051:
052: Document doc = inMsg.getSOAPEnvelope().getAsDocument();
053: String BaseURI = "http://xml-security";
054: CachedXPathAPI xpathAPI = new CachedXPathAPI();
055:
056: Element nsctx = doc.createElement("nsctx");
057: nsctx.setAttribute("xmlns:ds", Constants.SignatureSpecNS);
058:
059: Element signatureElem = (Element) xpathAPI
060: .selectSingleNode(doc, "//ds:Signature", nsctx);
061:
062: // check to make sure that the document claims to have been signed
063: if (signatureElem == null) {
064: System.out.println("The document is not signed");
065: return;
066: }
067:
068: XMLSignature sig = new XMLSignature(signatureElem, BaseURI);
069:
070: boolean verify = sig.checkSignatureValue(sig.getKeyInfo()
071: .getPublicKey());
072: System.out.println("Server verification complete.");
073:
074: System.out.println("The signature is"
075: + (verify ? " " : " not ") + "valid");
076: } catch (Exception e) {
077: throw AxisFault.makeFault(e);
078: }
079:
080: }
081:
082: public void onFault(MessageContext msgContext) {
083: try {
084: Handler serviceHandler = msgContext.getService();
085: String filename = (String) getOption("filename");
086: if ((filename == null) || (filename.equals("")))
087: throw new AxisFault("Server.NoLogFile",
088: "No log file configured for the LogHandler!",
089: null, null);
090: FileWriter fw = new FileWriter(filename, true);
091: PrintWriter pw = new PrintWriter(fw);
092: pw.println("=====================");
093: pw.println("= " + Messages.getMessage("fault00"));
094: pw.println("=====================");
095: pw.close();
096: } catch (Exception e) {
097: log.error(e);
098: }
099: }
100: }
|