View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  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.message.WSSecHeader;
24  import org.apache.ws.security.message.WSSecTimestamp;
25  import org.w3c.dom.Document;
26  import org.xml.sax.InputSource;
27  
28  import com.eviware.soapui.impl.wsdl.WsdlRequest;
29  import com.eviware.soapui.support.UISupport;
30  import com.eviware.soapui.support.xml.XmlUtils;
31  
32  /***
33   * Creates an empty WsdlRequest containing a SOAP Envelope and empty Body
34   * 
35   * @author Ole.Matzura
36   */
37  
38  public class AddWSTimestampAction extends AbstractAction
39  {
40     private final WsdlRequest request;
41  
42  	public AddWSTimestampAction( WsdlRequest request )
43     {
44        super( "Add WS-Timestamp" );
45  		this.request = request;
46     }
47     
48     public void actionPerformed(ActionEvent e)
49     {
50     	String req = request.getRequestContent();
51     	
52     	try
53  		{
54     		String ttlString = UISupport.prompt( "Add WS-Timestamp", "Specify Time-To-Live value", "60" );
55     		if( ttlString == null )
56     			return;
57     		
58     		int ttl = 0;
59  			try
60  			{
61  				ttl = Integer.parseInt(ttlString);
62  			}
63  			catch (Exception ex)
64  			{
65  			}
66  			
67  			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
68  			dbf.setNamespaceAware( true );
69  			DocumentBuilder db = dbf.newDocumentBuilder();
70  			Document doc = db.parse( new InputSource( new StringReader( req )));
71  			WSSecTimestamp addTimestamp = new WSSecTimestamp();
72  			addTimestamp.setTimeToLive( ttl );
73  			
74  			StringWriter writer = new StringWriter();
75  			WSSecHeader secHeader = new WSSecHeader();
76  			secHeader.insertSecurityHeader( doc );
77  			XmlUtils.serializePretty( addTimestamp.build( doc, secHeader ), writer );
78  			request.setRequestContent( writer.toString() );
79  		}
80  		catch ( Exception e1)
81  		{
82  			UISupport.showErrorMessage( e1 );
83  		}
84     }
85  }