1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.rest;
14
15 import java.beans.PropertyChangeEvent;
16 import java.beans.PropertyChangeListener;
17 import java.beans.PropertyChangeSupport;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.xml.namespace.QName;
22
23 import org.apache.xmlbeans.SchemaGlobalElement;
24 import org.apache.xmlbeans.SchemaType;
25
26 import com.eviware.soapui.SoapUI;
27 import com.eviware.soapui.config.RestResourceRepresentationConfig;
28 import com.eviware.soapui.config.RestResourceRepresentationTypeConfig;
29 import com.eviware.soapui.impl.rest.panels.request.inspectors.schema.InferredSchemaManager;
30 import com.eviware.soapui.impl.rest.support.RestParamsPropertyHolder;
31 import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder;
32 import com.eviware.soapui.impl.wadl.WadlDefinitionContext;
33 import com.eviware.soapui.impl.wsdl.support.xsd.SampleXmlUtil;
34 import com.eviware.soapui.support.PropertyChangeNotifier;
35
36 public class RestRepresentation implements PropertyChangeNotifier, PropertyChangeListener
37 {
38
39 private final RestMethod restMethod;
40 private RestResourceRepresentationConfig config;
41 private RestParamsPropertyHolder params;
42 private PropertyChangeSupport propertyChangeSupport;
43 private SchemaType schemaType;
44
45 public enum Type
46 {
47 REQUEST, RESPONSE, FAULT
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public RestRepresentation( RestMethod restMethod, RestResourceRepresentationConfig config )
69 {
70 this.restMethod = restMethod;
71 this.config = config;
72
73 if( config.getParams() == null )
74 config.addNewParams();
75
76 params = new XmlBeansRestParamsTestPropertyHolder( restMethod, config.getParams() );
77 propertyChangeSupport = new PropertyChangeSupport( this );
78
79 InferredSchemaManager.addPropertyChangeListener( this.restMethod.getResource().getService(), this );
80 }
81
82 public RestMethod getRestMethod()
83 {
84 return restMethod;
85 }
86
87 public RestResourceRepresentationConfig getConfig()
88 {
89 return config;
90 }
91
92 public RestParamsPropertyHolder getParams()
93 {
94 return params;
95 }
96
97 public void setConfig( RestResourceRepresentationConfig config )
98 {
99 this.config = config;
100 }
101
102 public String getId()
103 {
104 return config.getId();
105 }
106
107 public Type getType()
108 {
109 if( !config.isSetType() )
110 return null;
111
112 return Type.valueOf( config.getType().toString() );
113 }
114
115 public String getMediaType()
116 {
117 return config.getMediaType();
118 }
119
120 public void setId( String arg0 )
121 {
122 String old = getId();
123 config.setId( arg0 );
124 propertyChangeSupport.firePropertyChange( "id", old, arg0 );
125 }
126
127 public void setType( Type type )
128 {
129 Type old = getType();
130 config.setType( RestResourceRepresentationTypeConfig.Enum.forString( type.toString() ) );
131 propertyChangeSupport.firePropertyChange( "type", old, type );
132 }
133
134 public void setMediaType( String arg0 )
135 {
136 String old = getMediaType();
137 config.setMediaType( arg0 );
138 propertyChangeSupport.firePropertyChange( "mediaType", old, arg0 );
139 }
140
141 public void setElement( QName name )
142 {
143 QName old = getElement();
144 config.setElement( name );
145 schemaType = null;
146 propertyChangeSupport.firePropertyChange( "element", old, name );
147 }
148
149 public List<?> getStatus()
150 {
151 return config.getStatus() == null ? new ArrayList<Object>() : config.getStatus();
152 }
153
154 public void setStatus( List<?> arg0 )
155 {
156 List<?> old = getStatus();
157 config.setStatus( arg0 );
158 propertyChangeSupport.firePropertyChange( "status", old, arg0 );
159 }
160
161 public SchemaType getSchemaType()
162 {
163 if( schemaType == null )
164 {
165 try
166 {
167 if( getElement() != null )
168 {
169 WadlDefinitionContext context = getRestMethod().getResource().getService().getWadlContext();
170 if( context.hasSchemaTypes() )
171 {
172 schemaType = context.getSchemaTypeSystem().findDocumentType( getElement() );
173 if( schemaType == null )
174 {
175 SchemaGlobalElement element = context.getSchemaTypeSystem().findElement( getElement() );
176 if( element != null )
177 {
178 schemaType = element.getType();
179 }
180 }
181 }
182 }
183 }
184 catch( Exception e )
185 {
186 SoapUI.logError( e );
187 }
188 }
189
190 return schemaType;
191 }
192
193 public void release()
194 {
195 InferredSchemaManager.removePropertyChangeListener( getRestMethod().getResource().getService(), this );
196 }
197
198 public void setDescription( String description )
199 {
200 String old = getDescription();
201 config.setDescription( description );
202 propertyChangeSupport.firePropertyChange( "description", old, description );
203 }
204
205 public String getDescription()
206 {
207 return config.getDescription();
208 }
209
210 public QName getElement()
211 {
212 return config.getElement();
213 }
214
215 public void addPropertyChangeListener( PropertyChangeListener listener )
216 {
217 propertyChangeSupport.addPropertyChangeListener( listener );
218 }
219
220 public void addPropertyChangeListener( String propertyName, PropertyChangeListener listener )
221 {
222 propertyChangeSupport.addPropertyChangeListener( propertyName, listener );
223 }
224
225 public void removePropertyChangeListener( PropertyChangeListener listener )
226 {
227 propertyChangeSupport.removePropertyChangeListener( listener );
228 }
229
230 public void removePropertyChangeListener( String propertyName, PropertyChangeListener listener )
231 {
232 propertyChangeSupport.removePropertyChangeListener( propertyName, listener );
233 }
234
235 public String getDefaultContent()
236 {
237 if( getSchemaType() != null )
238 {
239
240 SampleXmlUtil generator = new SampleXmlUtil( false );
241 generator.setIgnoreOptional( false );
242 return generator.createSample( getSchemaType() );
243
244 }
245 else
246 {
247 return "";
248 }
249 }
250
251 public void propertyChange( PropertyChangeEvent evt )
252 {
253 schemaType = null;
254 }
255 }