1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.rest.support;
14
15 import java.beans.PropertyChangeListener;
16 import java.beans.PropertyChangeSupport;
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Properties;
28 import java.util.Set;
29
30 import javax.xml.namespace.QName;
31
32 import org.apache.xmlbeans.XmlObject;
33 import org.apache.xmlbeans.XmlString;
34
35 import com.eviware.soapui.SoapUI;
36 import com.eviware.soapui.config.RestParameterConfig;
37 import com.eviware.soapui.config.RestParametersConfig;
38 import com.eviware.soapui.impl.wsdl.support.wsdl.UrlWsdlLoader;
39 import com.eviware.soapui.model.ModelItem;
40 import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
41 import com.eviware.soapui.model.testsuite.TestProperty;
42 import com.eviware.soapui.model.testsuite.TestPropertyListener;
43 import com.eviware.soapui.support.StringUtils;
44
45 public class XmlBeansRestParamsTestPropertyHolder implements RestParamsPropertyHolder
46 {
47 private RestParametersConfig config;
48 private List<RestParamProperty> properties = new ArrayList<RestParamProperty>();
49 private Map<String, RestParamProperty> propertyMap = new HashMap<String, RestParamProperty>();
50 private Set<TestPropertyListener> listeners = new HashSet<TestPropertyListener>();
51 private ModelItem modelItem;
52 private Properties overrideProperties;
53 private String propertiesLabel = "Test Properties";
54
55 public XmlBeansRestParamsTestPropertyHolder( ModelItem modelItem, RestParametersConfig config )
56 {
57 this.modelItem = modelItem;
58 this.config = config;
59
60 for( RestParameterConfig propertyConfig : config.getParameterList() )
61 {
62 addProperty( propertyConfig, false );
63 }
64 }
65
66 protected XmlBeansRestParamProperty addProperty( RestParameterConfig propertyConfig, boolean notify )
67 {
68 XmlBeansRestParamProperty propertiesStepProperty = new XmlBeansRestParamProperty( propertyConfig );
69 properties.add( propertiesStepProperty );
70 propertyMap.put( propertiesStepProperty.getName().toUpperCase(), propertiesStepProperty );
71
72 if( notify )
73 {
74 firePropertyAdded( propertiesStepProperty.getName() );
75 }
76
77 return propertiesStepProperty;
78 }
79
80 private void firePropertyAdded( String name )
81 {
82 TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
83 for( TestPropertyListener listener : listenersArray )
84 {
85 listener.propertyAdded( name );
86 }
87 }
88
89 private void firePropertyRemoved( String name )
90 {
91 TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
92 for( TestPropertyListener listener : listenersArray )
93 {
94 listener.propertyRemoved( name );
95 }
96 }
97
98 private void firePropertyMoved( String name, int oldIndex, int newIndex )
99 {
100 TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
101 for( TestPropertyListener listener : listenersArray )
102 {
103 listener.propertyMoved( name, oldIndex, newIndex );
104 }
105 }
106
107 private void firePropertyRenamed( String oldName, String newName )
108 {
109 TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
110 for( TestPropertyListener listener : listenersArray )
111 {
112 listener.propertyRenamed( oldName, newName );
113 }
114 }
115
116 private void firePropertyValueChanged( String name, String oldValue, String newValue )
117 {
118 TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
119 for( TestPropertyListener listener : listenersArray )
120 {
121 listener.propertyValueChanged( name, oldValue, newValue );
122 }
123 }
124
125 public RestParamProperty addProperty( String name )
126 {
127 if( hasProperty( name ) )
128 return getProperty( name );
129 RestParameterConfig propertyConfig = config.addNewParameter();
130 propertyConfig.setName( name );
131 return addProperty( propertyConfig, true );
132 }
133
134 public void addTestPropertyListener( TestPropertyListener listener )
135 {
136 listeners.add( listener );
137 }
138
139
140
141
142
143
144
145
146 public RestParamProperty getProperty( String name )
147 {
148 return propertyMap.get( name.toUpperCase() );
149 }
150
151 public String[] getPropertyNames()
152 {
153 String[] result = new String[properties.size()];
154 for( int c = 0; c < properties.size(); c++ )
155 result[c] = properties.get( c ).getName();
156
157 return result;
158 }
159
160 public String getPropertyValue( String name )
161 {
162 TestProperty property = getProperty( name );
163 return property == null ? null : property.getValue();
164 }
165
166 public RestParamProperty removeProperty( String propertyName )
167 {
168 RestParamProperty property = getProperty( propertyName );
169 if( property != null )
170 {
171 int ix = properties.indexOf( property );
172 propertyMap.remove( propertyName.toUpperCase() );
173 properties.remove( ix );
174
175 firePropertyRemoved( propertyName );
176
177 config.removeParameter( ix );
178 return property;
179 }
180
181 return null;
182 }
183
184 public void removeTestPropertyListener( TestPropertyListener listener )
185 {
186 listeners.remove( listener );
187 }
188
189 public void setPropertyValue( String name, String value )
190 {
191 RestParamProperty property = getProperty( name );
192 if( property != null )
193 property.setValue( value );
194 else
195 addProperty( name ).setValue( value );
196 }
197
198
199
200
201
202
203
204
205 public void resetValues()
206 {
207 for( RestParamProperty property : properties )
208 {
209 ( ( XmlBeansRestParamProperty )property ).reset();
210 }
211 }
212
213 public void resetPropertiesConfig( RestParametersConfig config )
214 {
215 this.config = config;
216
217 for( int c = 0; c < config.sizeOfParameterArray(); c++ )
218 {
219 ( ( XmlBeansRestParamProperty )properties.get( c ) ).setConfig( config.getParameterArray( c ) );
220 }
221 }
222
223 public boolean renameProperty( String name, String newName )
224 {
225 if( getProperty( newName ) != null )
226 return false;
227
228 RestParamProperty property = getProperty( name );
229 if( property == null )
230 return false;
231
232 property.setName( newName );
233
234 firePropertyRenamed( name, newName );
235 return true;
236 }
237
238
239
240
241
242
243
244
245 public int getPropertyIndex( String name )
246 {
247 for( int c = 0; c < properties.size(); c++ )
248 {
249 if( properties.get( c ).getName().equals( name ) )
250 {
251 return c;
252 }
253 }
254
255 return -1;
256 }
257
258 public class XmlBeansRestParamProperty implements RestParamProperty
259 {
260 private RestParameterConfig propertyConfig;
261 private PropertyChangeSupport propertySupport;
262
263 public XmlBeansRestParamProperty( RestParameterConfig propertyConfig )
264 {
265 this.propertyConfig = propertyConfig;
266
267 propertySupport = new PropertyChangeSupport( this );
268 }
269
270
271
272
273
274
275
276 public void addPropertyChangeListener( PropertyChangeListener listener )
277 {
278 propertySupport.addPropertyChangeListener( listener );
279 }
280
281
282
283
284
285
286
287
288 public void addPropertyChangeListener( String propertyName, PropertyChangeListener listener )
289 {
290 propertySupport.addPropertyChangeListener( propertyName, listener );
291 }
292
293
294
295
296
297
298
299 public void removePropertyChangeListener( PropertyChangeListener listener )
300 {
301 propertySupport.removePropertyChangeListener( listener );
302 }
303
304
305
306
307
308
309
310
311 public void removePropertyChangeListener( String propertyName, PropertyChangeListener listener )
312 {
313 propertySupport.removePropertyChangeListener( propertyName, listener );
314 }
315
316 public void setConfig( RestParameterConfig restParameterConfig )
317 {
318 this.propertyConfig = restParameterConfig;
319 }
320
321 public String getName()
322 {
323 return propertyConfig.getName();
324 }
325
326 public void setName( String name )
327 {
328 String oldName = getName();
329 propertyConfig.setName( name );
330
331 propertyMap.remove( oldName.toUpperCase() );
332 propertyMap.put( name.toUpperCase(), this );
333
334 firePropertyRenamed( oldName, name );
335 }
336
337 public String getDescription()
338 {
339 return propertyConfig.getDescription();
340 }
341
342 public void setDescription( String description )
343 {
344 String old = getDescription();
345 propertyConfig.setDescription( description );
346 propertySupport.firePropertyChange( "description", old, description );
347 }
348
349 public ParameterStyle getStyle()
350 {
351 if( propertyConfig.xgetStyle() == null )
352 propertyConfig.setStyle( RestParameterConfig.Style.QUERY );
353
354 return ParameterStyle.valueOf( propertyConfig.getStyle().toString() );
355 }
356
357 public void setStyle( ParameterStyle style )
358 {
359 ParameterStyle old = getStyle();
360
361 propertyConfig.setStyle( RestParameterConfig.Style.Enum.forString( style.name() ) );
362 propertySupport.firePropertyChange( "style", old, style );
363 }
364
365 public String getValue()
366 {
367 if( overrideProperties != null && overrideProperties.containsKey( getName() ) )
368 return overrideProperties.getProperty( getName() );
369
370 return propertyConfig.getValue() == null ? "" : propertyConfig.getValue();
371 }
372
373 public void setValue( String value )
374 {
375 String oldValue = getValue();
376 propertyConfig.setValue( value );
377
378 if( overrideProperties != null && overrideProperties.containsKey( getName() ) )
379 {
380 overrideProperties.remove( getName() );
381 if( overrideProperties.isEmpty() )
382 overrideProperties = null;
383 }
384
385 firePropertyValueChanged( getName(), oldValue, value );
386 }
387
388 public boolean isReadOnly()
389 {
390 return false;
391 }
392
393 public ModelItem getModelItem()
394 {
395 return modelItem;
396 }
397
398 public String getDefaultValue()
399 {
400 return propertyConfig.isSetDefault() ? propertyConfig.getDefault() : "";
401 }
402
403 public String[] getOptions()
404 {
405 return propertyConfig.getOptionList().toArray( new String[propertyConfig.sizeOfOptionArray()] );
406 }
407
408 public boolean getRequired()
409 {
410 return propertyConfig.getRequired();
411 }
412
413
414
415
416
417
418
419
420 public boolean isDisableUrlEncoding()
421 {
422 return propertyConfig.getDisableUrlEncoding();
423 }
424
425
426
427
428
429
430
431 public void setDisableUrlEncoding( boolean encode )
432 {
433 boolean old = isDisableUrlEncoding();
434 if( old == encode )
435 return;
436
437 propertyConfig.setDisableUrlEncoding( encode );
438 propertySupport.firePropertyChange( "disableUrlEncoding", old, encode );
439 }
440
441 public QName getType()
442 {
443 return propertyConfig.isSetType() ? propertyConfig.getType() : XmlString.type.getName();
444 }
445
446 public void setOptions( String[] arg0 )
447 {
448 String[] old = getOptions();
449 propertyConfig.setOptionArray( arg0 );
450 propertySupport.firePropertyChange( "options", old, arg0 );
451 }
452
453 public void setRequired( boolean arg0 )
454 {
455 boolean old = getRequired();
456 if( old == arg0 )
457 return;
458 propertyConfig.setRequired( arg0 );
459 propertySupport.firePropertyChange( "required", old, arg0 );
460 }
461
462 public void setType( QName arg0 )
463 {
464 propertyConfig.setType( arg0 );
465 }
466
467 public void setDefaultValue( String default1 )
468 {
469 String old = default1;
470 propertyConfig.setDefault( default1 );
471 propertySupport.firePropertyChange( "defaultValue", old, default1 );
472 }
473
474 public RestParameterConfig getConfig()
475 {
476 return propertyConfig;
477 }
478
479 @Override
480 public boolean equals( Object obj )
481 {
482 if( obj instanceof XmlBeansRestParamProperty )
483 {
484 return propertyConfig.toString().equals( ( ( XmlBeansRestParamProperty )obj ).propertyConfig.toString() );
485 }
486
487 return super.equals( obj );
488 }
489
490 public void reset()
491 {
492 setValue( getDefaultValue() );
493 }
494 }
495
496
497
498
499
500
501
502
503 public void saveTo( Properties props )
504 {
505 int cnt = 0;
506 for( RestParamProperty p : properties )
507 {
508 String name = p.getName();
509 String value = p.getValue();
510 if( value == null )
511 value = "";
512
513 props.setProperty( name, value );
514 cnt++ ;
515 }
516 }
517
518 public int getPropertyCount()
519 {
520 return properties.size();
521 }
522
523
524
525
526
527
528
529
530 public RestParamProperty getPropertyAt( int index )
531 {
532 return properties.get( index );
533 }
534
535 public List<TestProperty> getPropertyList()
536 {
537 List<TestProperty> result = new ArrayList<TestProperty>();
538
539 for( TestProperty property : properties )
540 result.add( property );
541
542 return result;
543 }
544
545 public Map<String, TestProperty> getProperties()
546 {
547 Map<String, TestProperty> result = new HashMap<String, TestProperty>();
548 for( RestParamProperty property : propertyMap.values() )
549 {
550 result.put( property.getName(), property );
551 }
552
553 return result;
554 }
555
556 public boolean hasProperty( String name )
557 {
558 return propertyMap.containsKey( name.toUpperCase() );
559 }
560
561 public int addPropertiesFromFile( String propFile )
562 {
563 if( !StringUtils.hasContent( propFile ) )
564 return 0;
565
566 try
567 {
568 InputStream input = null;
569
570 File file = new File( propFile );
571 if( file.exists() )
572 {
573 input = new FileInputStream( file );
574 }
575 else if( propFile.toLowerCase().startsWith( "http://" ) || propFile.toLowerCase().startsWith( "https://" ) )
576 {
577 UrlWsdlLoader loader = new UrlWsdlLoader( propFile, getModelItem() );
578 loader.setUseWorker( false );
579 input = loader.load();
580 }
581
582 if( input != null )
583 {
584 if( overrideProperties == null )
585 overrideProperties = new Properties();
586
587 int sz = overrideProperties.size();
588 overrideProperties.load( input );
589
590 for( Object key : overrideProperties.keySet() )
591 {
592 String name = key.toString();
593 if( !hasProperty( name ) )
594 addProperty( name );
595 }
596
597 return overrideProperties.size() - sz;
598 }
599 }
600 catch( Exception e )
601 {
602 SoapUI.logError( e );
603 }
604
605 return 0;
606 }
607
608 public ModelItem getModelItem()
609 {
610 return modelItem;
611 }
612
613
614
615
616
617
618
619 public PropertyExpansion[] getPropertyExpansions()
620 {
621 List<PropertyExpansion> result = new ArrayList<PropertyExpansion>();
622
623 return result.toArray( new PropertyExpansion[result.size()] );
624 }
625
626 public void moveProperty( String propertyName, int targetIndex )
627 {
628 RestParamProperty property = getProperty( propertyName );
629 int ix = properties.indexOf( property );
630
631 if( ix == targetIndex )
632 return;
633
634 if( targetIndex < 0 )
635 targetIndex = 0;
636
637 String value = property.getValue();
638 String defaultValue = property.getDefaultValue();
639 String style = property.getStyle().name();
640 String[] options = property.getOptions();
641 boolean required = property.getRequired();
642 QName type = property.getType();
643 String description = property.getDescription();
644 boolean disableUrlEncoding = property.isDisableUrlEncoding();
645
646 config.removeParameter( ix );
647
648 RestParameterConfig propertyConfig = null;
649
650 if( targetIndex < properties.size() )
651 {
652 properties.add( targetIndex, properties.remove( ix ) );
653 propertyConfig = config.insertNewParameter( targetIndex );
654 }
655 else
656 {
657 properties.add( properties.remove( ix ) );
658 propertyConfig = config.addNewParameter();
659 }
660
661 propertyConfig.setName( propertyName );
662 propertyConfig.setValue( value );
663 propertyConfig.setDefault( defaultValue );
664 propertyConfig.setStyle( RestParameterConfig.Style.Enum.forString( style ) );
665 propertyConfig.setOptionArray( options );
666 propertyConfig.setRequired( required );
667 propertyConfig.setType( type );
668 propertyConfig.setDescription( description );
669 propertyConfig.setDisableUrlEncoding( disableUrlEncoding );
670
671 resetPropertiesConfig( config );
672
673 if( targetIndex > properties.size() )
674 targetIndex = properties.size();
675
676 firePropertyMoved( propertyName, ix, targetIndex );
677 }
678
679 public void clear()
680 {
681 while( size() > 0 )
682 removeProperty( getPropertyAt( 0 ).getName() );
683 }
684
685 public boolean containsKey( Object key )
686 {
687 return hasProperty( ( String )key );
688 }
689
690 public boolean containsValue( Object value )
691 {
692 return propertyMap.containsValue( value );
693 }
694
695 public Set<java.util.Map.Entry<String, TestProperty>> entrySet()
696 {
697 HashSet<java.util.Map.Entry<String, TestProperty>> result = new HashSet<Entry<String, TestProperty>>();
698
699 for( TestProperty p : propertyMap.values() )
700 {
701
702
703
704 result.add( new HashMapEntry<String, TestProperty>( p.getName(), p ) );
705 }
706
707 return result;
708 }
709
710 private static class HashMapEntry<K, V> implements java.util.Map.Entry<K, V>
711 {
712 private K key;
713 private V value;
714
715 public HashMapEntry( K key, V value )
716 {
717 this.key = key;
718 this.value = value;
719 }
720
721 public K getKey()
722 {
723 return key;
724 }
725
726 public V getValue()
727 {
728 return value;
729 }
730
731 public V setValue( V value )
732 {
733 throw new UnsupportedOperationException();
734 }
735 }
736
737 public RestParamProperty get( Object key )
738 {
739 return getProperty( ( String )key );
740 }
741
742 public boolean isEmpty()
743 {
744 return propertyMap.isEmpty();
745 }
746
747 public Set<String> keySet()
748 {
749 return new HashSet<String>( Arrays.asList( getPropertyNames() ) );
750 }
751
752 public TestProperty put( String key, TestProperty value )
753 {
754 TestProperty result = addProperty( key );
755 result.setValue( value.getValue() );
756 return result;
757 }
758
759 public void putAll( Map<? extends String, ? extends TestProperty> m )
760 {
761 for( TestProperty p : m.values() )
762 {
763 addProperty( p.getName() ).setValue( p.getValue() );
764 }
765 }
766
767 public TestProperty remove( Object key )
768 {
769 return removeProperty( ( String )key );
770 }
771
772 public int size()
773 {
774 return propertyMap.size();
775 }
776
777 public Collection<TestProperty> values()
778 {
779 ArrayList<TestProperty> result = new ArrayList<TestProperty>();
780 result.addAll( propertyMap.values() );
781 return result;
782 }
783
784 public String getPropertiesLabel()
785 {
786 return propertiesLabel;
787 }
788
789 public void setPropertiesLabel( String propertiesLabel )
790 {
791 this.propertiesLabel = propertiesLabel;
792 }
793
794 public XmlObject getConfig()
795 {
796 return config;
797 }
798
799 public void addParameters( RestParamsPropertyHolder params )
800 {
801 for( int c = 0; c < params.getPropertyCount(); c++ )
802 {
803 RestParamProperty property = params.getPropertyAt( c );
804 if( !hasProperty( property.getName() ) )
805 {
806 addParameter( property );
807 }
808 }
809 }
810
811 public void addParameter( RestParamProperty property )
812 {
813 RestParamProperty prop = addProperty( property.getName() );
814 prop.setStyle( property.getStyle() );
815 prop.setValue( property.getValue() );
816 prop.setType( property.getType() );
817 prop.setDefaultValue( property.getDefaultValue() );
818 prop.setDescription( property.getDescription() );
819 prop.setOptions( property.getOptions() );
820 prop.setRequired( property.getRequired() );
821 }
822
823 public void release()
824 {
825 }
826
827 public RestParamProperty addProperty( XmlBeansRestParamProperty prop )
828 {
829 RestParameterConfig propertyConfig = ( RestParameterConfig )config.addNewParameter().set( prop.getConfig() );
830 return addProperty( propertyConfig, true );
831 }
832 }