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.model.ModelItem;
29  import com.eviware.soapui.model.iface.Attachment;
30  import com.eviware.soapui.model.iface.MessagePart;
31  
32  public class AttachmentsInspectorFactory implements RequestInspectorFactory, ResponseInspectorFactory
33  {
34  	public static final String INSPECTOR_ID = "Attachments";
35  
36  	public String getInspectorId()
37  	{
38  		return INSPECTOR_ID;
39  	}
40  
41  	public XmlInspector createRequestInspector( XmlEditor editor, ModelItem modelItem )
42  	{
43  		if( modelItem instanceof WsdlRequest )
44  			return new AttachmentsInspector( ( AttachmentContainer ) modelItem );
45  		else if( modelItem instanceof WsdlMockResponse )
46  			return new AttachmentsInspector( new MockRequestAttachmentsContainer( (WsdlMockResponse)modelItem ) );
47  		
48  		return null;
49  	}
50  
51  	public XmlInspector createResponseInspector( XmlEditor editor, ModelItem modelItem )
52  	{
53  		if( modelItem instanceof WsdlRequest )
54  			return new AttachmentsInspector( new ResponseAttachmentsContainer( (WsdlRequest)modelItem ) );
55  		else if( modelItem instanceof WsdlMockResponse )
56  			return new AttachmentsInspector( ( WsdlMockResponse ) modelItem );
57  		
58  		return null;
59  	}
60  	
61  	private static class ResponseAttachmentsContainer implements AttachmentContainer
62  	{
63  		private final WsdlRequest request;
64  
65  		public ResponseAttachmentsContainer( WsdlRequest request )
66  		{
67  			this.request = request;
68  		}
69  
70  		public void addAttachmentsChangeListener( PropertyChangeListener listener )
71  		{
72  			request.addPropertyChangeListener( WsdlRequest.RESPONSE_PROPERTY, listener );
73  		}
74  
75  		public Attachment attachFile( File file, boolean cache )
76  		{
77  			return null;
78  		}
79  
80  		public Attachment getAttachmentAt( int index )
81  		{
82  			return request.getResponse() == null ? null : request.getResponse().getAttachments()[index];
83  		}
84  
85  		public int getAttachmentCount()
86  		{
87  			return request.getResponse() == null ? 0 : request.getResponse().getAttachments().length;
88  		}
89  
90  		public WsdlAttachmentPart getAttachmentPart( String partName )
91  		{
92  			return null;
93  		}
94  
95  		public Attachment[] getAttachments()
96  		{
97  			return request.getResponse() == null ? null : request.getResponse().getAttachments();
98  		}
99  
100 		public Attachment[] getAttachmentsForPart( String partName )
101 		{
102 			return request.getResponse() == null ? null : request.getResponse().getAttachmentsForPart( partName );
103 		}
104 
105 		public WsdlAttachmentPart[] getDefinedAttachmentParts()
106 		{
107 			MessagePart[] responseParts = request.getResponseParts();
108 			
109 			List<WsdlAttachmentPart> result = new ArrayList<WsdlAttachmentPart>();
110 			
111 			for( MessagePart part : responseParts )
112 				if( part instanceof WsdlAttachmentPart )
113 					result.add( ( WsdlAttachmentPart ) part );
114 			
115 			return result.toArray( new WsdlAttachmentPart[result.size()] );
116 		}
117 
118 		public boolean isMtomEnabled()
119 		{
120 			return request.isMtomEnabled();
121 		}
122 
123 		public boolean isMultipartEnabled()
124 		{
125 			return request.isMultipartEnabled();
126 		}
127 
128 		public boolean isReadOnly()
129 		{
130 			return true;
131 		}
132 
133 		public void removeAttachment( Attachment attachment )
134 		{
135 		}
136 
137 		public void removeAttachmentsChangeListener( PropertyChangeListener listener )
138 		{
139 			request.removePropertyChangeListener( WsdlRequest.RESPONSE_PROPERTY, listener );
140 		}
141 	}
142 
143 	private static class MockRequestAttachmentsContainer implements AttachmentContainer
144 	{
145 		private final WsdlMockResponse request;
146 
147 		public MockRequestAttachmentsContainer( WsdlMockResponse request )
148 		{
149 			this.request = request;
150 		}
151 
152 		public void addAttachmentsChangeListener( PropertyChangeListener listener )
153 		{
154 			request.addPropertyChangeListener( WsdlMockResponse.MOCKRESULT_PROPERTY, listener );
155 		}
156 
157 		public Attachment attachFile( File file, boolean cache )
158 		{
159 			return null;
160 		}
161 
162 		public Attachment getAttachmentAt( int index )
163 		{
164 			return request.getMockResult() == null ? null : request.getMockResult().getMockRequest().getRequestAttachments()[index];
165 		}
166 
167 		public int getAttachmentCount()
168 		{
169 			return request.getMockResult() == null ? 0 : request.getMockResult().getMockRequest().getRequestAttachments().length;
170 		}
171 
172 		public WsdlAttachmentPart getAttachmentPart( String partName )
173 		{
174 			return null;
175 		}
176 
177 		public Attachment[] getAttachments()
178 		{
179 			return request.getMockResult() == null ? null : request.getMockResult().getMockRequest().getRequestAttachments();
180 		}
181 
182 		public Attachment[] getAttachmentsForPart( String partName )
183 		{
184 			return null;
185 		}
186 
187 		public WsdlAttachmentPart[] getDefinedAttachmentParts()
188 		{
189 			return null;
190 		}
191 
192 		public boolean isMtomEnabled()
193 		{
194 			return request.isMtomEnabled();
195 		}
196 
197 		public boolean isMultipartEnabled()
198 		{
199 			return request.isMultipartEnabled();
200 		}
201 
202 		public boolean isReadOnly()
203 		{
204 			return true;
205 		}
206 
207 		public void removeAttachment( Attachment attachment )
208 		{
209 		}
210 
211 		public void removeAttachmentsChangeListener( PropertyChangeListener listener )
212 		{
213 			request.removePropertyChangeListener(  WsdlMockResponse.MOCKRESULT_PROPERTY, listener );
214 		}
215 	}
216 		
217 }