View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.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     ;
46  
47     public RestRepresentation( RestRequest restResource, RestResourceRepresentationConfig config )
48     {
49        this.restRequest = restResource;
50        this.config = config;
51  
52        if( config.getParams() == null )
53           config.addNewParams();
54  
55        params = new XmlBeansRestParamsTestPropertyHolder( restResource, config.getParams() );
56        propertyChangeSupport = new PropertyChangeSupport( this );
57     }
58  
59     public RestRequest getRestRequest()
60     {
61        return restRequest;
62     }
63  
64     public RestResourceRepresentationConfig getConfig()
65     {
66        return config;
67     }
68  
69     public XmlBeansRestParamsTestPropertyHolder getParams()
70     {
71        return params;
72     }
73  
74     public void setConfig( RestResourceRepresentationConfig config )
75     {
76        this.config = config;
77     }
78  
79     public String getId()
80     {
81        return config.getId();
82     }
83  
84     public Type getType()
85     {
86        if( !config.isSetType() )
87           return null;
88  
89        return Type.valueOf( config.getType().toString() );
90     }
91  
92     public String getMediaType()
93     {
94        return config.getMediaType();
95     }
96  
97     public void setId( String arg0 )
98     {
99        String old = getId();
100       config.setId( arg0 );
101       propertyChangeSupport.firePropertyChange( "id", old, arg0 );
102    }
103 
104    public void setType( Type type )
105    {
106       Type old = getType();
107       config.setType( RestResourceRepresentationTypeConfig.Enum.forString( type.toString() ) );
108       propertyChangeSupport.firePropertyChange( "type", old, type );
109    }
110 
111    public void setMediaType( String arg0 )
112    {
113       String old = getMediaType();
114       config.setMediaType( arg0 );
115       propertyChangeSupport.firePropertyChange( "mediaType", old, arg0 );
116    }
117 
118    public void setElement( QName name )
119    {
120       QName old = getElement();
121       config.setElement( name );
122       schemaType = null;
123       propertyChangeSupport.firePropertyChange( "element", old, name );
124    }
125 
126    public List getStatus()
127    {
128       return config.getStatus() == null ? new ArrayList() : config.getStatus();
129    }
130 
131    public void setStatus( List arg0 )
132    {
133       List old = getStatus();
134       config.setStatus( arg0 );
135       propertyChangeSupport.firePropertyChange( "status", old, arg0 );
136    }
137 
138    public SchemaType getSchemaType()
139    {
140       if( schemaType == null )
141       {
142          try
143          {
144             if( getElement() != null )
145             {
146                WadlDefinitionContext context = getRestRequest().getResource().getService().getWadlContext();
147                if( context.hasSchemaTypes() )
148                {
149                   SchemaGlobalElement element = context.getSchemaTypeSystem().findElement( getElement() );
150                   if( element != null )
151                      schemaType = element.getType();
152                }
153             }
154          }
155          catch( Exception e )
156          {
157             SoapUI.logError( e );
158          }
159       }
160 
161       return schemaType;
162    }
163 
164    public void release()
165    {
166    }
167 
168    public void setDescription( String description )
169    {
170       String old = getDescription();
171       config.setDescription( description );
172       propertyChangeSupport.firePropertyChange( "description", old, description );
173    }
174 
175    public String getDescription()
176    {
177       return config.getDescription();
178    }
179 
180    public QName getElement()
181    {
182       return config.getElement();
183    }
184 
185    public void addPropertyChangeListener( PropertyChangeListener listener )
186    {
187       propertyChangeSupport.addPropertyChangeListener( listener );
188    }
189 
190    public void addPropertyChangeListener( String propertyName, PropertyChangeListener listener )
191    {
192       propertyChangeSupport.addPropertyChangeListener( propertyName, listener );
193    }
194 
195    public void removePropertyChangeListener( PropertyChangeListener listener )
196    {
197       propertyChangeSupport.removePropertyChangeListener( listener );
198    }
199 
200    public void removePropertyChangeListener( String propertyName, PropertyChangeListener listener )
201    {
202       propertyChangeSupport.removePropertyChangeListener( propertyName, listener );
203    }
204 
205    public String getDefaultContent()
206    {
207       if( getElement() != null )
208       {
209          Document document = XmlUtils.createDocument( getElement() );
210          return XmlUtils.serialize( document );
211       }
212       else
213       {
214          return "";
215       }
216    }
217 }