Debian Codegraph @

samples/security/LogHandler.java

raw
1 /*
2 * Copyright 2001-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
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 // verify signed message
52
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 // check to make sure that the document claims to have been signed
64 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 }