01: /*
02: * soapUI, copyright (C) 2004-2007 eviware.com
03: *
04: * soapUI is free software; you can redistribute it and/or modify it under the
05: * terms of version 2.1 of the GNU Lesser General Public License as published by
06: * the Free Software Foundation.
07: *
08: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
09: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: * See the GNU Lesser General Public License for more details at gnu.org.
11: */
12:
13: package com.eviware.soapui.impl.wsdl.actions.request;
14:
15: import java.awt.event.ActionEvent;
16: import java.io.StringReader;
17: import java.io.StringWriter;
18:
19: import javax.swing.AbstractAction;
20: import javax.xml.parsers.DocumentBuilder;
21: import javax.xml.parsers.DocumentBuilderFactory;
22:
23: import org.apache.ws.security.WSConstants;
24: import org.apache.ws.security.message.WSSecHeader;
25: import org.apache.ws.security.message.WSSecUsernameToken;
26: import org.w3c.dom.Document;
27: import org.xml.sax.InputSource;
28:
29: import com.eviware.soapui.impl.wsdl.WsdlRequest;
30: import com.eviware.soapui.support.UISupport;
31: import com.eviware.soapui.support.xml.XmlUtils;
32:
33: /**
34: * Prompts to add a WSS Username Token to the specified WsdlRequests requestContent
35: *
36: * @author Ole.Matzura
37: */
38:
39: public class AddWSSUsernameTokenAction extends AbstractAction {
40: private final WsdlRequest request;
41:
42: public AddWSSUsernameTokenAction(WsdlRequest request) {
43: super ("Add WSS Username Token");
44: this .request = request;
45: }
46:
47: public void actionPerformed(ActionEvent e) {
48: if ((request.getUsername() == null || request.getUsername()
49: .length() == 0)
50: && (request.getPassword() == null || request
51: .getPassword().length() == 0)) {
52: UISupport
53: .showErrorMessage("Request is missing username and password");
54: return;
55: }
56:
57: String req = request.getRequestContent();
58:
59: try {
60: String passwordType = (String) UISupport.prompt(
61: "Add WSS Username Token", "Specify Password Type",
62: new String[] { WsdlRequest.PW_TYPE_TEXT,
63: WsdlRequest.PW_TYPE_DIGEST });
64:
65: if (passwordType == null)
66: return;
67:
68: DocumentBuilderFactory dbf = DocumentBuilderFactory
69: .newInstance();
70: dbf.setNamespaceAware(true);
71: DocumentBuilder db = dbf.newDocumentBuilder();
72: Document doc = db.parse(new InputSource(new StringReader(
73: req)));
74: WSSecUsernameToken addUsernameToken = new WSSecUsernameToken();
75:
76: if (WsdlRequest.PW_TYPE_DIGEST.equals(passwordType)) {
77: addUsernameToken
78: .setPasswordType(WSConstants.PASSWORD_DIGEST);
79: } else {
80: addUsernameToken
81: .setPasswordType(WSConstants.PASSWORD_TEXT);
82: }
83:
84: addUsernameToken.setUserInfo(request.getUsername(), request
85: .getPassword());
86:
87: StringWriter writer = new StringWriter();
88:
89: WSSecHeader secHeader = new WSSecHeader();
90: secHeader.insertSecurityHeader(doc);
91: XmlUtils.serializePretty(addUsernameToken.build(doc,
92: secHeader), writer);
93: request.setRequestContent(writer.toString());
94: } catch (Exception e1) {
95: UISupport.showErrorMessage(e1);
96: }
97: }
98: }
|