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