View Javadoc

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