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.support.editor.inspectors.attachments;
14  
15  import java.beans.PropertyChangeListener;
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import com.eviware.soapui.impl.support.AbstractHttpRequest;
20  import com.eviware.soapui.impl.wsdl.AttachmentContainer;
21  import com.eviware.soapui.impl.wsdl.HttpAttachmentPart;
22  import com.eviware.soapui.impl.wsdl.WsdlRequest;
23  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
24  import com.eviware.soapui.impl.wsdl.support.MessageExchangeModelItem;
25  import com.eviware.soapui.model.ModelItem;
26  import com.eviware.soapui.model.iface.Attachment;
27  import com.eviware.soapui.model.iface.MessagePart;
28  import com.eviware.soapui.support.editor.Editor;
29  import com.eviware.soapui.support.editor.EditorInspector;
30  import com.eviware.soapui.support.editor.registry.RequestInspectorFactory;
31  import com.eviware.soapui.support.editor.registry.ResponseInspectorFactory;
32  
33  public class AttachmentsInspectorFactory implements RequestInspectorFactory, ResponseInspectorFactory
34  {
35  	public static final String INSPECTOR_ID = "Attachments";
36  
37  	public String getInspectorId()
38  	{
39  		return INSPECTOR_ID;
40  	}
41  	
42  	public EditorInspector<?> createRequestInspector( Editor<?> editor, ModelItem modelItem )
43  	{
44  		if( modelItem instanceof AbstractHttpRequest<?> )
45  			return new AttachmentsInspector( ( AttachmentContainer ) modelItem );
46  		else if( modelItem instanceof WsdlMockResponse )
47  			return new AttachmentsInspector( new MockRequestAttachmentsContainer( (WsdlMockResponse)modelItem ) );
48   		else if( modelItem instanceof MessageExchangeModelItem )
49   			return new AttachmentsInspector( new WsdlMessageExchangeRequestAttachmentsContainer( (MessageExchangeModelItem)modelItem ));
50  		
51  		return null;
52  	}
53  
54  	public EditorInspector<?> createResponseInspector( Editor<?> editor, ModelItem modelItem )
55  	{
56  		if( modelItem instanceof AbstractHttpRequest<?> )
57  			return new AttachmentsInspector( new ResponseAttachmentsContainer( (AbstractHttpRequest<?>)modelItem ) );
58  		else if( modelItem instanceof WsdlMockResponse )
59  			return new AttachmentsInspector( ( WsdlMockResponse ) modelItem );
60  		else if( modelItem instanceof MessageExchangeModelItem )
61   			return new AttachmentsInspector( new WsdlMessageExchangeResponseAttachmentsContainer( (MessageExchangeModelItem)modelItem ));
62  		
63  		return null;
64  	}
65  	
66  	protected static class WsdlMessageExchangeRequestAttachmentsContainer implements AttachmentContainer
67  	{
68  		private final MessageExchangeModelItem request;
69  
70  		public WsdlMessageExchangeRequestAttachmentsContainer( MessageExchangeModelItem request )
71  		{
72  			this.request = request;
73  		}
74  
75  		public void addAttachmentsChangeListener( PropertyChangeListener listener )
76  		{
77  			request.addPropertyChangeListener( listener );
78  		}
79  
80  		public Attachment getAttachmentAt( int index )
81  		{
82  			return request.getMessageExchange() == null ? null : request.getMessageExchange().getRequestAttachments()[index];
83  		}
84  
85  		public int getAttachmentCount()
86  		{
87  			return request.getMessageExchange() == null ? 0 : request.getMessageExchange().getRequestAttachments().length;
88  		}
89  
90  		public HttpAttachmentPart getAttachmentPart( String partName )
91  		{
92  			return null;
93  		}
94  		
95  		public ModelItem getModelItem()
96  		{
97  			return request.getParent();
98  		}
99  
100 		public Attachment[] getAttachments()
101 		{
102 			return request.getMessageExchange() == null ? null : request.getMessageExchange().getRequestAttachments();
103 		}
104 
105 		public Attachment[] getAttachmentsForPart( String partName )
106 		{
107 			return request.getMessageExchange() == null ? null : request.getMessageExchange().getRequestAttachmentsForPart( partName );
108 		}
109 
110 		public HttpAttachmentPart[] getDefinedAttachmentParts()
111 		{
112 			if( request.getMessageExchange() == null || request.getMessageExchange().getOperation() == null )
113 				return new HttpAttachmentPart[0];
114 			
115 			MessagePart[] responseParts = request.getMessageExchange().getOperation().getDefaultRequestParts();
116 			
117 			List<HttpAttachmentPart> result = new ArrayList<HttpAttachmentPart>();
118 			
119 			for( MessagePart part : responseParts )
120 				if( part instanceof HttpAttachmentPart )
121 					result.add( ( HttpAttachmentPart ) part );
122 			
123 			return result.toArray( new HttpAttachmentPart[result.size()] );
124 		}
125 
126 		public boolean isMultipartEnabled()
127 		{
128 			return false;
129 		}
130 
131 		public void removeAttachmentsChangeListener( PropertyChangeListener listener )
132 		{
133 			request.removePropertyChangeListener( listener );
134 		}
135 	}
136 	
137 	protected static class WsdlMessageExchangeResponseAttachmentsContainer implements AttachmentContainer
138 	{
139 		private final MessageExchangeModelItem response;
140 
141 		public WsdlMessageExchangeResponseAttachmentsContainer( MessageExchangeModelItem response )
142 		{
143 			this.response = response;
144 		}
145 
146 		public void addAttachmentsChangeListener( PropertyChangeListener listener )
147 		{
148 			response.addPropertyChangeListener( listener );
149 		}
150 
151 		public Attachment getAttachmentAt( int index )
152 		{
153 			return response.getMessageExchange() == null ? null : response.getMessageExchange().getResponseAttachments()[index];
154 		}
155 
156 		public int getAttachmentCount()
157 		{
158 			return response.getMessageExchange() == null ? 0 : response.getMessageExchange().getResponseAttachments().length;
159 		}
160 
161 		public HttpAttachmentPart getAttachmentPart( String partName )
162 		{
163 			return null;
164 		}
165 		
166 		public ModelItem getModelItem()
167 		{
168 			return response.getParent();
169 		}
170 
171 		public Attachment[] getAttachments()
172 		{
173 			return response.getMessageExchange() == null ? null : response.getMessageExchange().getResponseAttachments();
174 		}
175 
176 		public Attachment[] getAttachmentsForPart( String partName )
177 		{
178 			return response.getMessageExchange() == null ? null : response.getMessageExchange().getResponseAttachmentsForPart( partName );
179 		}
180 
181 		public HttpAttachmentPart[] getDefinedAttachmentParts()
182 		{
183 			if( response.getMessageExchange() == null || response.getMessageExchange().getOperation() == null )
184 				return new HttpAttachmentPart[0];
185 			
186 			MessagePart[] responseParts = response.getMessageExchange().getOperation().getDefaultResponseParts();
187 			
188 			List<HttpAttachmentPart> result = new ArrayList<HttpAttachmentPart>();
189 			
190 			for( MessagePart part : responseParts )
191 				if( part instanceof HttpAttachmentPart )
192 					result.add( ( HttpAttachmentPart ) part );
193 			
194 			return result.toArray( new HttpAttachmentPart[result.size()] );
195 		}
196 
197 		public boolean isMultipartEnabled()
198 		{
199 			return false;
200 		}
201 
202 		public void removeAttachmentsChangeListener( PropertyChangeListener listener )
203 		{
204 			response.removePropertyChangeListener( listener );
205 		}
206 	}
207 	
208 	protected static class ResponseAttachmentsContainer implements AttachmentContainer
209 	{
210 		private final AbstractHttpRequest<?> request;
211 
212 		public ResponseAttachmentsContainer( AbstractHttpRequest<?> abstractHttpRequest )
213 		{
214 			this.request = abstractHttpRequest;
215 		}
216 
217 		public void addAttachmentsChangeListener( PropertyChangeListener listener )
218 		{
219 			request.addPropertyChangeListener( WsdlRequest.RESPONSE_PROPERTY, listener );
220 		}
221 
222 		public Attachment getAttachmentAt( int index )
223 		{
224 			return request.getResponse() == null ? null : request.getResponse().getAttachments()[index];
225 		}
226 
227 		public int getAttachmentCount()
228 		{
229 			return request.getResponse() == null ? 0 : request.getResponse().getAttachments().length;
230 		}
231 
232 		public HttpAttachmentPart getAttachmentPart( String partName )
233 		{
234 			return null;
235 		}
236 
237 		public ModelItem getModelItem()
238 		{
239 			return request;
240 		}
241 
242 		public Attachment[] getAttachments()
243 		{
244 			return request.getResponse() == null ? null : request.getResponse().getAttachments();
245 		}
246 
247 		public Attachment[] getAttachmentsForPart( String partName )
248 		{
249 			return request.getResponse() == null ? null : request.getResponse().getAttachmentsForPart( partName );
250 		}
251 
252 		public HttpAttachmentPart[] getDefinedAttachmentParts()
253 		{
254 			MessagePart[] responseParts = request.getResponseParts();
255 			
256 			List<HttpAttachmentPart> result = new ArrayList<HttpAttachmentPart>();
257 			
258 			for( MessagePart part : responseParts )
259 				if( part instanceof HttpAttachmentPart )
260 					result.add( ( HttpAttachmentPart ) part );
261 			
262 			return result.toArray( new HttpAttachmentPart[result.size()] );
263 		}
264 
265 		public boolean isMultipartEnabled()
266 		{
267 			return request.isMultipartEnabled();
268 		}
269 
270 		public void removeAttachmentsChangeListener( PropertyChangeListener listener )
271 		{
272 			request.removePropertyChangeListener( WsdlRequest.RESPONSE_PROPERTY, listener );
273 		}
274 	}
275 
276 	protected static class MockRequestAttachmentsContainer implements AttachmentContainer
277 	{
278 		private final WsdlMockResponse mockResponse;
279 
280 		public MockRequestAttachmentsContainer( WsdlMockResponse mockResponse )
281 		{
282 			this.mockResponse = mockResponse;
283 		}
284 
285 		public void addAttachmentsChangeListener( PropertyChangeListener listener )
286 		{
287 			mockResponse.addPropertyChangeListener( WsdlMockResponse.MOCKRESULT_PROPERTY, listener );
288 		}
289 
290 		public Attachment getAttachmentAt( int index )
291 		{
292 			return mockResponse.getMockResult() == null ? null : mockResponse.getMockResult().getMockRequest().getRequestAttachments()[index];
293 		}
294 
295 		public int getAttachmentCount()
296 		{
297 			return mockResponse.getMockResult() == null ? 0 : mockResponse.getMockResult().getMockRequest().getRequestAttachments().length;
298 		}
299 
300 		public HttpAttachmentPart getAttachmentPart( String partName )
301 		{
302 			return null;
303 		}
304 
305 		public Attachment[] getAttachments()
306 		{
307 			return mockResponse.getMockResult() == null ? null : mockResponse.getMockResult().getMockRequest().getRequestAttachments();
308 		}
309 
310 		public Attachment[] getAttachmentsForPart( String partName )
311 		{
312 			return null;
313 		}
314 		
315 		public ModelItem getModelItem()
316 		{
317 			return mockResponse;
318 		}
319 
320 		public HttpAttachmentPart[] getDefinedAttachmentParts()
321 		{
322 			MessagePart[] responseParts = mockResponse.getRequestParts();
323 			
324 			List<HttpAttachmentPart> result = new ArrayList<HttpAttachmentPart>();
325 			
326 			for( MessagePart part : responseParts )
327 				if( part instanceof HttpAttachmentPart )
328 					result.add( ( HttpAttachmentPart ) part );
329 			
330 			return result.toArray( new HttpAttachmentPart[result.size()] );
331 		}
332 
333 		public boolean isMultipartEnabled()
334 		{
335 			return mockResponse.isMultipartEnabled();
336 		}
337 
338 		public void removeAttachmentsChangeListener( PropertyChangeListener listener )
339 		{
340 			mockResponse.removePropertyChangeListener(  WsdlMockResponse.MOCKRESULT_PROPERTY, listener );
341 		}
342 	}
343 }