1
10package org.jstk.wss4axis;
11
12import javax.xml.rpc.handler.Handler;
13import javax.xml.rpc.handler.MessageContext;
14import javax.xml.rpc.handler.HandlerInfo;
15import javax.xml.rpc.handler.soap.SOAPMessageContext;
16import javax.xml.soap.SOAPMessage;
17import org.w3c.dom.Document;
18import java.util.Map;
19
20public class WSSServiceHandler implements Handler {
21 private String keyStoreFile = null;
22 private String keyStoreType = "JCEKS";
23 private String keyStorePassword = "changeit";
24 private String keyEntryAlias = "mykey";
25 private String keyEntryPassword = null;
26
27 private String trustStoreFile = null;
28 private String trustStoreType = "JCEKS";
29 private String trustStorePassword = "changeit";
30 private String certEntryAlias = "mykey";
31 private boolean verbose = true;
32
33 public WSSServiceHandler() {
34 }
35
36 public boolean handleRequest(MessageContext context) {
37 if (verbose)
38 System.out.println("WSSServiceHandler: handleRequest ...");
39 if (context instanceof SOAPMessageContext){
40 try {
41 SOAPMessageContext soapCtx = (SOAPMessageContext)context;
42 SOAPMessage soapMsg = soapCtx.getMessage();
43 Document doc = SOAPUtility.toDocument(soapMsg);
44
45 WSSUtility.decrypt(doc, keyStoreFile, keyStoreType,
46 keyStorePassword, keyEntryAlias, keyEntryPassword);
47 WSSUtility.verify(doc, trustStoreFile, trustStoreType, trustStorePassword);
48 WSSUtility.cleanup(doc);
49
50 soapMsg = SOAPUtility.toSOAPMessage(doc);
51 soapCtx.setMessage(soapMsg);
52 } catch (Exception e){
53 System.err.println("WSServiceHandler::handleRequest -- Exception: " + e);
54 }
55 } else {
56 System.out.println("WSSServiceHandler: context.getClass() = " + context.getClass());
57 }
58 if (verbose)
59 System.out.println("WSSServiceHandler: ... handleRequest");
60 return true;
61 }
62
63 public boolean handleResponse(MessageContext context) {
64 if (verbose)
65 System.out.println("WSSServiceHandler: handleResponse ...");
66 if (context instanceof SOAPMessageContext){
67 try {
68 SOAPMessageContext soapCtx = (SOAPMessageContext)context;
69 SOAPMessage soapMsg = soapCtx.getMessage();
70 Document doc = SOAPUtility.toDocument(soapMsg);
71
72 WSSUtility.sign(doc, keyStoreFile, keyStoreType,
73 keyStorePassword, keyEntryAlias, keyEntryPassword);
74 WSSUtility.encrypt(doc, trustStoreFile, trustStoreType,
75 trustStorePassword, certEntryAlias);
76
77 soapMsg = SOAPUtility.toSOAPMessage(doc);
78 soapCtx.setMessage(soapMsg);
79 } catch (Exception e){
80 System.err.println("WSServiceHandler::handleResponse -- Exception: " + e);
81 }
82 }
83 if (verbose)
84 System.out.println("... WSSServiceHandler: handleResponse");
85 return true;
86 }
87
88 public boolean handleFault(MessageContext context) {
89 System.out.println("WSSServiceHandler: In handleFault");
90 return true;
91 }
92
93 public void init(HandlerInfo config) {
94 System.out.println("WSSServiceHandler: init ...");
95 Object o = null;
96 Map configProps = config.getHandlerConfig();
97 String verboseStr = (String)configProps.get("verbose");
98 if (verboseStr != null && verboseStr.equalsIgnoreCase("false"))
99 verbose = false;
00 keyStoreFile = (String)configProps.get("keyStoreFile");
01 if ((o = configProps.get("keyStoreType")) != null)
02 keyStoreType = (String)o;
03 if ((o = configProps.get("keyStorePassword")) != null)
04 keyStorePassword = (String)o;
05 if ((o = configProps.get("keyEntryAlias")) != null)
06 keyEntryAlias = (String)o;
07 if ((o = configProps.get("keyEntryPassword")) != null)
08 keyEntryPassword = (String)o;
09 else
10 keyEntryPassword = keyStorePassword;
11
12 if (verbose){
13 System.out.println("WSSServiceHandler:: keyStoreFile = " + keyStoreFile);
14 System.out.println("WSSServiceHandler:: keyStoreType = " + keyStoreType);
15 System.out.println("WSSServiceHandler:: keyStorePassword = " + keyStorePassword);
16 System.out.println("WSSServiceHandler:: keyEntryAlias = " + keyEntryAlias);
17 System.out.println("WSSServiceHandler:: keyEntryPassword = " + keyEntryPassword);
18 }
19
20 trustStoreFile = (String)configProps.get("trustStoreFile");
21 if ((o = configProps.get("trustStoreType")) != null)
22 trustStoreType = (String)o;
23 if ((o = configProps.get("trustStorePassword")) != null)
24 trustStorePassword = (String)o;
25 if ((o = configProps.get("certEntryAlias")) != null)
26 certEntryAlias = (String)o;
27
28 if (verbose){
29 System.out.println("WSSServiceHandler:: trustStoreFile = " + trustStoreFile);
30 System.out.println("WSSServiceHandler:: trustStoreType = " + trustStoreType);
31 System.out.println("WSSServiceHandler:: trustStorePassword = " + trustStorePassword);
32 System.out.println("WSSServiceHandler:: certEntryAlias = " + certEntryAlias);
33
34 System.out.println("WSSServiceHandler: ... init");
35 }
36 }
37
38 public void destroy() {
39 }
40
41 public javax.xml.namespace.QName[] getHeaders() {
42 return null;
43 }
44}
45