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