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;
14  
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  import org.apache.ws.security.message.WSSecHeader;
19  import org.apache.ws.security.util.WSSecurityUtil;
20  import org.w3c.dom.Document;
21  import org.w3c.dom.Element;
22  
23  import com.eviware.soapui.SoapUI;
24  import com.eviware.soapui.config.OutgoingWssConfig;
25  import com.eviware.soapui.config.WSSEntryConfig;
26  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
27  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContainer;
28  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
29  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionsResult;
30  import com.eviware.soapui.support.StringUtils;
31  import com.eviware.soapui.support.resolver.ResolveContext;
32  
33  public class OutgoingWss implements PropertyExpansionContainer
34  {
35  	public static final String WSSENTRY_PROPERTY = OutgoingWss.class.getName() + "@wssEntry";
36  
37  	private OutgoingWssConfig config;
38  	private List<WssEntry> entries = new ArrayList<WssEntry>();
39  	private final DefaultWssContainer container;
40  
41  	public OutgoingWss( OutgoingWssConfig config, DefaultWssContainer container )
42  	{
43  		this.config = config;
44  		this.container = container;
45  
46  		for( WSSEntryConfig entryConfig : config.getEntryList() )
47  		{
48  			WssEntry entry = WssEntryRegistry.get().build( entryConfig, this );
49  			if( entry != null )
50  				entries.add( entry );
51  		}
52  	}
53  
54  	public WssContainer getWssContainer()
55  	{
56  		return container;
57  	}
58  
59  	public String getName()
60  	{
61  		return config.getName();
62  	}
63  
64  	public String getPassword()
65  	{
66  		return config.getPassword();
67  	}
68  
69  	public String getUsername()
70  	{
71  		return config.getUsername();
72  	}
73  
74  	public void setName( String arg0 )
75  	{
76  		config.setName( arg0 );
77  	}
78  
79  	public void setPassword( String arg0 )
80  	{
81  		config.setPassword( arg0 );
82  	}
83  
84  	public void setUsername( String arg0 )
85  	{
86  		config.setUsername( arg0 );
87  	}
88  
89  	public String getActor()
90  	{
91  		return config.getActor();
92  	}
93  
94  	public boolean getMustUnderstand()
95  	{
96  		return config.getMustUnderstand();
97  	}
98  
99  	public void setActor( String arg0 )
100 	{
101 		config.setActor( arg0 );
102 	}
103 
104 	public void setMustUnderstand( boolean arg0 )
105 	{
106 		config.setMustUnderstand( arg0 );
107 	}
108 
109 	public WssEntry addEntry( String type )
110 	{
111 		WssEntry newEntry = WssEntryRegistry.get().create( type, this );
112 		entries.add( newEntry );
113 
114 		container.fireWssEntryAdded( newEntry );
115 
116 		return newEntry;
117 	}
118 
119 	public void removeEntry( WssEntry entry )
120 	{
121 		int index = entries.indexOf( entry );
122 
123 		container.fireWssEntryRemoved( entries.remove( index ) );
124 		config.removeEntry( index );
125 		entry.release();
126 	}
127 
128 	public OutgoingWssConfig getConfig()
129 	{
130 		return config;
131 	}
132 
133 	public void processOutgoing( Document soapDocument, PropertyExpansionContext context )
134 	{
135 		Element header = WSSecurityUtil.findWsseSecurityHeaderBlock( soapDocument, soapDocument.getDocumentElement(),
136 				false );
137 
138 		while( header != null )
139 		{
140 			header.getParentNode().removeChild( header );
141 			header = WSSecurityUtil.findWsseSecurityHeaderBlock( soapDocument, soapDocument.getDocumentElement(), false );
142 		}
143 
144 		WSSecHeader secHeader = new WSSecHeader();
145 
146 		if( StringUtils.hasContent( getActor() ) )
147 			secHeader.setActor( getActor() );
148 
149 		secHeader.setMustUnderstand( getMustUnderstand() );
150 
151 		secHeader.insertSecurityHeader( soapDocument );
152 
153 		for( WssEntry entry : entries )
154 		{
155 			try
156 			{
157 				entry.process( secHeader, soapDocument, context );
158 			}
159 			catch( Throwable e )
160 			{
161 				SoapUI.logError( e );
162 			}
163 		}
164 	}
165 
166 	public List<WssEntry> getEntries()
167 	{
168 		return entries;
169 	}
170 
171 	public void updateConfig( OutgoingWssConfig config )
172 	{
173 		this.config = config;
174 
175 		for( int c = 0; c < entries.size(); c++ )
176 		{
177 			entries.get( c ).udpateConfig( this.config.getEntryArray( c ) );
178 		}
179 	}
180 
181 	public void release()
182 	{
183 		for( WssEntry entry : entries )
184 			entry.release();
185 	}
186 
187 	public PropertyExpansion[] getPropertyExpansions()
188 	{
189 		PropertyExpansionsResult result = new PropertyExpansionsResult( getWssContainer().getModelItem(), this );
190 
191 		result.extractAndAddAll( "username" );
192 		result.extractAndAddAll( "password" );
193 
194 		for( WssEntry entry : entries )
195 		{
196 			if( entry instanceof PropertyExpansionContainer )
197 				result.addAll( ( ( PropertyExpansionContainer )entry ).getPropertyExpansions() );
198 		}
199 
200 		return result.toArray();
201 	}
202 
203 	public void resolve( ResolveContext<?> context )
204 	{
205 	}
206 }