1 /*
2  * @(#) $Id: WSSClientHandler.java,v 1.2 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
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 javax.xml.soap.SOAPPart;
18import javax.xml.soap.SOAPEnvelope;
19import org.w3c.dom.Document;
20import java.util.Map;
21
22public class WSSClientHandler implements Handler {
23    private String keyStoreFile = null;
24    private String keyStoreType = "JCEKS";
25    private String keyStorePassword = "changeit";
26    private String keyEntryAlias = "mykey";
27    private String keyEntryPassword = null;
28
29    private String trustStoreFile = null;
30    private String trustStoreType = "JCEKS";
31    private String trustStorePassword = "changeit";
32    private String certEntryAlias = "mykey";
33
34    private boolean verbose = true;
35
36    public WSSClientHandler() {
37    }
38
39    public boolean handleRequest(MessageContext context) {
40        if (verbose)
41            System.out.println("WSSClientHandler: handleRequest ...");
42        if (context instanceof SOAPMessageContext){
43            try {
44                SOAPMessageContext soapCtx = (SOAPMessageContext)context;
45                SOAPMessage soapMsg = soapCtx.getMessage();
46                Document doc = SOAPUtility.toDocument(soapMsg);
47
48                WSSUtility.sign(doc, keyStoreFile, keyStoreType,
49                        keyStorePassword, keyEntryAlias, keyEntryPassword);
50                WSSUtility.encrypt(doc, trustStoreFile, trustStoreType,
51                        trustStorePassword, certEntryAlias);
52
53                soapMsg = SOAPUtility.toSOAPMessage(doc);
54                soapCtx.setMessage(soapMsg);
55            } catch (Exception e){
56                System.err.println("WSClientHandler::handleRequest -- Exception: " + e);
57            }
58        }
59        if (verbose)
60            System.out.println("WSSClientHandler: ... handleRequest");
61        return true;
62    }
63
64    public boolean handleResponse(MessageContext context) {
65        if (verbose)
66            System.out.println("WSSClientHandler: handleResponse ...");
67        if (context instanceof SOAPMessageContext){
68            try {
69                SOAPMessageContext soapCtx = (SOAPMessageContext)context;
70                SOAPMessage soapMsg = soapCtx.getMessage();
71                Document doc = SOAPUtility.toDocument(soapMsg);
72
73                WSSUtility.decrypt(doc, keyStoreFile, keyStoreType,
74                        keyStorePassword, keyEntryAlias, keyEntryPassword);
75                WSSUtility.verify(doc, trustStoreFile, trustStoreType, trustStorePassword);
76                WSSUtility.cleanup(doc);
77
78                soapMsg = SOAPUtility.toSOAPMessage(doc);
79                soapCtx.setMessage(soapMsg);
80            } catch (Exception e){
81                System.err.println("WSClientHandler::handleResponse -- Exception: " + e);
82            }
83        }
84        if (verbose)
85            System.out.println("... WSSClientHandler: handleResponse");
86        return true;
87    }
88
89    public boolean handleFault(MessageContext context) {
90        System.out.println("WSSClientHandler: In handleFault");
91        return true;
92    }
93
94    public void init(HandlerInfo config) {
95        System.out.println("WSSClientHandler: init ...");
96        Object o = null;
97        Map configProps = config.getHandlerConfig();
98        String verboseStr = (String)configProps.get("verbose");
99        if (verboseStr != null && verboseStr.equalsIgnoreCase("false"))
00            verbose = false;
01
02        keyStoreFile = (String)configProps.get("keyStoreFile");
03        if ((o = configProps.get("keyStoreType")) != null)
04            keyStoreType = (String)o;
05        if ((o = configProps.get("keyStorePassword")) != null)
06            keyStorePassword = (String)o;
07        if ((o = configProps.get("keyEntryAlias")) != null)
08            keyEntryAlias = (String)o;
09        if ((o = configProps.get("keyEntryPassword")) != null)
10            keyEntryPassword = (String)o;
11        else
12            keyEntryPassword = keyStorePassword;
13
14        if (verbose){
15            System.out.println("WSSClientHandler:: keyStoreFile = " + keyStoreFile);
16            System.out.println("WSSClientHandler:: keyStoreType = " + keyStoreType);
17            System.out.println("WSSClientHandler:: keyStorePassword = " + keyStorePassword);
18            System.out.println("WSSClientHandler:: keyEntryAlias = " + keyEntryAlias);
19            System.out.println("WSSClientHandler:: keyEntryPassword = " + keyEntryPassword);
20        }
21
22        trustStoreFile = (String)configProps.get("trustStoreFile");
23        if ((o = configProps.get("trustStoreType")) != null)
24            trustStoreType = (String)o;
25        if ((o = configProps.get("trustStorePassword")) != null)
26            trustStorePassword = (String)o;
27        if ((o = configProps.get("certEntryAlias")) != null)
28            certEntryAlias = (String)o;
29
30        if (verbose){
31            System.out.println("WSSClientHandler:: trustStoreFile = " + trustStoreFile);
32            System.out.println("WSSClientHandler:: trustStoreType = " + trustStoreType);
33            System.out.println("WSSClientHandler:: trustStorePassword = " + trustStorePassword);
34            System.out.println("WSSClientHandler:: certEntryAlias = " + certEntryAlias);
35
36            System.out.println("WSSClientHandler: ... init");
37        }
38    }
39
40    public void destroy() {
41    }
42
43    public javax.xml.namespace.QName[] getHeaders() {
44        return null;
45    }
46}
47