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.panels.request.components.editor.inspectors.attachments;
14  
15  import java.beans.PropertyChangeListener;
16  import java.io.File;
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import com.eviware.soapui.impl.wsdl.AttachmentContainer;
21  import com.eviware.soapui.impl.wsdl.WsdlAttachmentPart;
22  import com.eviware.soapui.impl.wsdl.WsdlRequest;
23  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
24  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.XmlEditor;
25  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.XmlInspector;
26  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.inspectors.registry.RequestInspectorFactory;
27  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.inspectors.registry.ResponseInspectorFactory;
28  import com.eviware.soapui.impl.wsdl.support.MessageExchangeModelItem;
29  import com.eviware.soapui.model.ModelItem;
30  import com.eviware.soapui.model.iface.Attachment;
31  import com.eviware.soapui.model.iface.MessagePart;
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 XmlInspector createRequestInspector( XmlEditor editor, ModelItem modelItem )
43  	{
44  		if( modelItem instanceof WsdlRequest )
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 XmlInspector createResponseInspector( XmlEditor editor, ModelItem modelItem )
55  	{
56  		if( modelItem instanceof WsdlRequest )
57  			return new AttachmentsInspector( new ResponseAttachmentsContainer( (WsdlRequest)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  	private 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 attachFile( File file, boolean cache )
81  		{
82  			return null;
83  		}
84  
85  		public Attachment getAttachmentAt( int index )
86  		{
87  			return request.getMessageExchange() == null ? null : request.getMessageExchange().getRequestAttachments()[index];
88  		}
89  
90  		public int getAttachmentCount()
91  		{
92  			return request.getMessageExchange() == null ? 0 : request.getMessageExchange().getRequestAttachments().length;
93  		}
94  
95  		public WsdlAttachmentPart getAttachmentPart( String partName )
96  		{
97  			return null;
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 WsdlAttachmentPart[] getDefinedAttachmentParts()
111 		{
112 			if( request.getMessageExchange() == null || request.getMessageExchange().getOperation() == null )
113 				return new WsdlAttachmentPart[0];
114 			
115 			MessagePart[] responseParts = request.getMessageExchange().getOperation().getDefaultRequestParts();
116 			
117 			List<WsdlAttachmentPart> result = new ArrayList<WsdlAttachmentPart>();
118 			
119 			for( MessagePart part : responseParts )
120 				if( part instanceof WsdlAttachmentPart )
121 					result.add( ( WsdlAttachmentPart ) part );
122 			
123 			return result.toArray( new WsdlAttachmentPart[result.size()] );
124 		}
125 
126 		public boolean isMtomEnabled()
127 		{
128 			return false;
129 		}
130 
131 		public boolean isMultipartEnabled()
132 		{
133 			return false;
134 		}
135 
136 		public boolean isReadOnly()
137 		{
138 			return true;
139 		}
140 
141 		public void removeAttachment( Attachment attachment )
142 		{
143 		}
144 
145 		public void removeAttachmentsChangeListener( PropertyChangeListener listener )
146 		{
147 			request.removePropertyChangeListener( listener );
148 		}
149 
150 		public boolean isInlineFilesEnabled()
151 		{
152 			return false;
153 		}
154 	}
155 	
156 	private static class WsdlMessageExchangeResponseAttachmentsContainer implements AttachmentContainer
157 	{
158 		private final MessageExchangeModelItem response;
159 
160 		public WsdlMessageExchangeResponseAttachmentsContainer( MessageExchangeModelItem response )
161 		{
162 			this.response = response;
163 		}
164 
165 		public void addAttachmentsChangeListener( PropertyChangeListener listener )
166 		{
167 			response.addPropertyChangeListener( listener );
168 		}
169 
170 		public Attachment attachFile( File file, boolean cache )
171 		{
172 			return null;
173 		}
174 
175 		public Attachment getAttachmentAt( int index )
176 		{
177 			return response.getMessageExchange() == null ? null : response.getMessageExchange().getResponseAttachments()[index];
178 		}
179 
180 		public int getAttachmentCount()
181 		{
182 			return response.getMessageExchange() == null ? 0 : response.getMessageExchange().getResponseAttachments().length;
183 		}
184 
185 		public WsdlAttachmentPart getAttachmentPart( String partName )
186 		{
187 			return null;
188 		}
189 
190 		public Attachment[] getAttachments()
191 		{
192 			return response.getMessageExchange() == null ? null : response.getMessageExchange().getResponseAttachments();
193 		}
194 
195 		public Attachment[] getAttachmentsForPart( String partName )
196 		{
197 			return response.getMessageExchange() == null ? null : response.getMessageExchange().getResponseAttachmentsForPart( partName );
198 		}
199 
200 		public WsdlAttachmentPart[] getDefinedAttachmentParts()
201 		{
202 			if( response.getMessageExchange() == null || response.getMessageExchange().getOperation() == null )
203 				return new WsdlAttachmentPart[0];
204 			
205 			MessagePart[] responseParts = response.getMessageExchange().getOperation().getDefaultResponseParts();
206 			
207 			List<WsdlAttachmentPart> result = new ArrayList<WsdlAttachmentPart>();
208 			
209 			for( MessagePart part : responseParts )
210 				if( part instanceof WsdlAttachmentPart )
211 					result.add( ( WsdlAttachmentPart ) part );
212 			
213 			return result.toArray( new WsdlAttachmentPart[result.size()] );
214 		}
215 
216 		public boolean isMtomEnabled()
217 		{
218 			return false;
219 		}
220 
221 		public boolean isMultipartEnabled()
222 		{
223 			return false;
224 		}
225 
226 		public boolean isReadOnly()
227 		{
228 			return true;
229 		}
230 
231 		public void removeAttachment( Attachment attachment )
232 		{
233 		}
234 
235 		public void removeAttachmentsChangeListener( PropertyChangeListener listener )
236 		{
237 			response.removePropertyChangeListener( listener );
238 		}
239 
240 		public boolean isInlineFilesEnabled()
241 		{
242 			return false;
243 		}
244 	}
245 	
246 	private static class ResponseAttachmentsContainer implements AttachmentContainer
247 	{
248 		private final WsdlRequest request;
249 
250 		public ResponseAttachmentsContainer( WsdlRequest request )
251 		{
252 			this.request = request;
253 		}
254 
255 		public void addAttachmentsChangeListener( PropertyChangeListener listener )
256 		{
257 			request.addPropertyChangeListener( WsdlRequest.RESPONSE_PROPERTY, listener );
258 		}
259 
260 		public Attachment attachFile( File file, boolean cache )
261 		{
262 			return null;
263 		}
264 
265 		public Attachment getAttachmentAt( int index )
266 		{
267 			return request.getResponse() == null ? null : request.getResponse().getAttachments()[index];
268 		}
269 
270 		public int getAttachmentCount()
271 		{
272 			return request.getResponse() == null ? 0 : request.getResponse().getAttachments().length;
273 		}
274 
275 		public WsdlAttachmentPart getAttachmentPart( String partName )
276 		{
277 			return null;
278 		}
279 
280 		public Attachment[] getAttachments()
281 		{
282 			return request.getResponse() == null ? null : request.getResponse().getAttachments();
283 		}
284 
285 		public Attachment[] getAttachmentsForPart( String partName )
286 		{
287 			return request.getResponse() == null ? null : request.getResponse().getAttachmentsForPart( partName );
288 		}
289 
290 		public WsdlAttachmentPart[] getDefinedAttachmentParts()
291 		{
292 			MessagePart[] responseParts = request.getResponseParts();
293 			
294 			List<WsdlAttachmentPart> result = new ArrayList<WsdlAttachmentPart>();
295 			
296 			for( MessagePart part : responseParts )
297 				if( part instanceof WsdlAttachmentPart )
298 					result.add( ( WsdlAttachmentPart ) part );
299 			
300 			return result.toArray( new WsdlAttachmentPart[result.size()] );
301 		}
302 
303 		public boolean isMtomEnabled()
304 		{
305 			return request.isMtomEnabled();
306 		}
307 
308 		public boolean isMultipartEnabled()
309 		{
310 			return request.isMultipartEnabled();
311 		}
312 
313 		public boolean isReadOnly()
314 		{
315 			return true;
316 		}
317 
318 		public void removeAttachment( Attachment attachment )
319 		{
320 		}
321 
322 		public void removeAttachmentsChangeListener( PropertyChangeListener listener )
323 		{
324 			request.removePropertyChangeListener( WsdlRequest.RESPONSE_PROPERTY, listener );
325 		}
326 
327 		public boolean isInlineFilesEnabled()
328 		{
329 			return false;
330 		}
331 	}
332 
333 	private static class MockRequestAttachmentsContainer implements AttachmentContainer
334 	{
335 		private final WsdlMockResponse request;
336 
337 		public MockRequestAttachmentsContainer( WsdlMockResponse request )
338 		{
339 			this.request = request;
340 		}
341 
342 		public void addAttachmentsChangeListener( PropertyChangeListener listener )
343 		{
344 			request.addPropertyChangeListener( WsdlMockResponse.MOCKRESULT_PROPERTY, listener );
345 		}
346 
347 		public Attachment attachFile( File file, boolean cache )
348 		{
349 			return null;
350 		}
351 
352 		public Attachment getAttachmentAt( int index )
353 		{
354 			return request.getMockResult() == null ? null : request.getMockResult().getMockRequest().getRequestAttachments()[index];
355 		}
356 
357 		public int getAttachmentCount()
358 		{
359 			return request.getMockResult() == null ? 0 : request.getMockResult().getMockRequest().getRequestAttachments().length;
360 		}
361 
362 		public WsdlAttachmentPart getAttachmentPart( String partName )
363 		{
364 			return null;
365 		}
366 
367 		public Attachment[] getAttachments()
368 		{
369 			return request.getMockResult() == null ? null : request.getMockResult().getMockRequest().getRequestAttachments();
370 		}
371 
372 		public Attachment[] getAttachmentsForPart( String partName )
373 		{
374 			return null;
375 		}
376 
377 		public WsdlAttachmentPart[] getDefinedAttachmentParts()
378 		{
379 			return null;
380 		}
381 
382 		public boolean isMtomEnabled()
383 		{
384 			return request.isMtomEnabled();
385 		}
386 
387 		public boolean isMultipartEnabled()
388 		{
389 			return request.isMultipartEnabled();
390 		}
391 
392 		public boolean isReadOnly()
393 		{
394 			return true;
395 		}
396 
397 		public void removeAttachment( Attachment attachment )
398 		{
399 		}
400 
401 		public void removeAttachmentsChangeListener( PropertyChangeListener listener )
402 		{
403 			request.removePropertyChangeListener(  WsdlMockResponse.MOCKRESULT_PROPERTY, listener );
404 		}
405 
406 		public boolean isInlineFilesEnabled()
407 		{
408 			// TODO Auto-generated method stub
409 			return false;
410 		}
411 	}
412 		
413 }