View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
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.support.wss.entries;
14  
15  import javax.swing.JComponent;
16  
17  import org.apache.ws.security.WSSConfig;
18  import org.apache.ws.security.message.WSSecHeader;
19  import org.apache.ws.security.message.WSSecTimestamp;
20  import org.w3c.dom.Document;
21  
22  import com.eviware.soapui.config.WSSEntryConfig;
23  import com.eviware.soapui.impl.wsdl.support.wss.OutgoingWss;
24  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
25  import com.eviware.soapui.support.components.SimpleBindingForm;
26  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
27  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
28  import com.jgoodies.binding.PresentationModel;
29  
30  public class AddTimestampEntry extends WssEntryBase
31  {
32  	public static final String TYPE = "Timestamp";
33  
34  	private int timeToLive;
35  	private boolean strictTimestamp;
36  
37  	public void init( WSSEntryConfig config, OutgoingWss container )
38  	{
39  		super.init( config, container, TYPE );
40  	}
41  
42  	@Override
43  	protected JComponent buildUI()
44  	{
45  		SimpleBindingForm form = new SimpleBindingForm( new PresentationModel<AddTimestampEntry>( this ) );
46  		form.addSpace( 5 );
47  		form.appendTextField( "timeToLive", "Time To Live", "Sets the TimeToLive value for the Timestamp Token" );
48  		form.appendCheckBox( "strictTimestamp", "Millisecond Precision", "Sets precision of timestamp to milliseconds" );
49  
50  		return form.getPanel();
51  	}
52  
53  	@Override
54  	protected void load( XmlObjectConfigurationReader reader )
55  	{
56  		timeToLive = reader.readInt( "timeToLive", 0 );
57  		strictTimestamp = reader.readBoolean( "strictTimestamp", true );
58  	}
59  
60  	@Override
61  	protected void save( XmlObjectConfigurationBuilder builder )
62  	{
63  		builder.add( "timeToLive", timeToLive );
64  		builder.add( "strictTimestamp", strictTimestamp );
65  	}
66  
67  	public void process( WSSecHeader secHeader, Document doc, PropertyExpansionContext context )
68  	{
69  		if( timeToLive <= 0 )
70  			return;
71  
72  		WSSecTimestamp timestamp = new WSSecTimestamp();
73  		timestamp.setTimeToLive( timeToLive );
74  
75  		if( !strictTimestamp )
76  		{
77  			WSSConfig wsc = WSSConfig.getNewInstance();
78  			wsc.setPrecisionInMilliSeconds( false );
79  			wsc.setTimeStampStrict( false );
80  			timestamp.setWsConfig( wsc );
81  		}
82  
83  		timestamp.build( doc, secHeader );
84  	}
85  
86  	public String getTimeToLive()
87  	{
88  		return String.valueOf( timeToLive );
89  	}
90  
91  	public boolean isStrictTimestamp()
92  	{
93  		return strictTimestamp;
94  	}
95  
96  	public void setStrictTimestamp( boolean strictTimestamp )
97  	{
98  		this.strictTimestamp = strictTimestamp;
99  		saveConfig();
100 	}
101 
102 	public void setTimeToLive( String timeToLive )
103 	{
104 		try
105 		{
106 			this.timeToLive = Integer.valueOf( timeToLive );
107 			saveConfig();
108 		}
109 		catch( Exception e )
110 		{
111 		}
112 	}
113 }