View Javadoc

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