View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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  
31  public class OutgoingWss implements PropertyExpansionContainer
32  {
33  	public static final String WSSENTRY_PROPERTY = OutgoingWss.class.getName() + "@wssEntry";
34  	
35  	private OutgoingWssConfig config;
36  	private List<WssEntry> entries = new ArrayList<WssEntry>();
37  	private final DefaultWssContainer container;
38  
39  	public OutgoingWss( OutgoingWssConfig config, DefaultWssContainer container )
40  	{
41  		this.config = config;
42  		this.container = container;
43  
44  		for( WSSEntryConfig entryConfig : config.getEntryList() )
45  		{
46  			WssEntry entry = WssEntryRegistry.get().build( entryConfig, this );
47  			if( entry != null )
48  				entries.add( entry );
49  		}
50  	}
51  	
52  	public WssContainer getWssContainer()
53  	{
54  		return container;
55  	}
56  
57  	public String getName()
58  	{
59  		return config.getName();
60  	}
61  
62  	public String getPassword()
63  	{
64  		return config.getPassword();
65  	}
66  
67  	public String getUsername()
68  	{
69  		return config.getUsername();
70  	}
71  
72  	public void setName( String arg0 )
73  	{
74  		config.setName( arg0 );
75  	}
76  
77  	public void setPassword( String arg0 )
78  	{
79  		config.setPassword( arg0 );
80  	}
81  
82  	public void setUsername( String arg0 )
83  	{
84  		config.setUsername( arg0 );
85  	}
86  
87  	public WssEntry addEntry( String type )
88  	{
89  		WssEntry newEntry = WssEntryRegistry.get().create( type, this );
90  		entries.add( newEntry );
91  		
92  		container.fireWssEntryAdded( newEntry );
93  		
94  		return newEntry;
95  	}
96  
97  	public void removeEntry( WssEntry entry )
98  	{
99  		int index = entries.indexOf( entry );
100 
101 		container.fireWssEntryRemoved( entries.remove( index ) );
102 		config.removeEntry( index );
103 		entry.release();
104 	}
105 
106 	public OutgoingWssConfig getConfig()
107 	{
108 		return config;
109 	}
110 
111 	public void processOutgoing( Document soapDocument, PropertyExpansionContext context )
112 	{
113 		Element header = WSSecurityUtil.findWsseSecurityHeaderBlock( soapDocument, soapDocument.getDocumentElement(),
114 					false );
115 
116 		while( header != null )
117 		{
118 			header.getParentNode().removeChild( header );
119 			header = WSSecurityUtil.findWsseSecurityHeaderBlock( soapDocument, soapDocument.getDocumentElement(), false );
120 		}
121 
122 		WSSecHeader secHeader = new WSSecHeader();
123 		secHeader.insertSecurityHeader( soapDocument );
124 
125 		for( WssEntry entry : entries )
126 		{
127 			try
128 			{
129 				entry.process( secHeader, soapDocument, context );
130 			}
131 			catch( Throwable e )
132 			{
133 				SoapUI.logError( e );
134 			}
135 		}
136 	}
137 
138 	public List<WssEntry> getEntries()
139 	{
140 		return entries;
141 	}
142 
143 	public void updateConfig( OutgoingWssConfig config )
144 	{
145 		this.config = config;
146 		
147 		for( int c = 0; c < entries.size(); c++ )
148 		{
149 			entries.get( c ).udpateConfig( this.config.getEntryArray( c ) );
150 		}
151 	}
152 	
153 	public void release()
154 	{
155 		for( WssEntry entry : entries )
156 			entry.release();
157 	}
158 
159 	public PropertyExpansion[] getPropertyExpansions()
160 	{
161 		PropertyExpansionsResult result = new PropertyExpansionsResult( getWssContainer().getModelItem(), this );
162 		
163 		result.extractAndAddAll( "username" );
164 		result.extractAndAddAll( "password" );
165 		
166 		for( WssEntry entry : entries )
167 		{
168 			if( entry instanceof PropertyExpansionContainer )
169 				result.addAll( ((PropertyExpansionContainer)entry).getPropertyExpansions() );
170 		}
171 		
172 		return result.toArray();
173 	}
174 }