1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.teststeps;
14
15 import java.io.File;
16 import java.net.MalformedURLException;
17 import java.net.URL;
18
19 import com.eviware.soapui.SoapUI;
20 import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
21 import com.eviware.soapui.impl.wsdl.support.PathUtils;
22 import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
23 import com.eviware.soapui.model.testsuite.TestCaseRunContext;
24 import com.eviware.soapui.support.StringUtils;
25 import com.eviware.soapui.support.resolver.ResolveContext;
26
27 public abstract class AbstractPathPropertySupport
28 {
29 private final String propertyName;
30 private final AbstractWsdlModelItem<?> modelItem;
31
32 public AbstractPathPropertySupport( AbstractWsdlModelItem<?> modelItem, String propertyName )
33 {
34 this.modelItem = modelItem;
35 this.propertyName = propertyName;
36 }
37
38 public String set( String value, boolean notify )
39 {
40 String old = get();
41 value = PathUtils.relativizeResourcePath( value, modelItem );
42 try
43 {
44 setPropertyValue( PathUtils.normalizePath( value ) );
45 if( notify )
46 notifyUpdate( value, old );
47 }
48 catch( Exception e )
49 {
50 e.printStackTrace();
51 }
52
53 return old;
54 }
55
56 public String get()
57 {
58 try
59 {
60 return getPropertyValue();
61 }
62 catch( Exception e )
63 {
64 e.printStackTrace();
65 return null;
66 }
67 }
68
69 public String getPropertyName()
70 {
71 return propertyName;
72 }
73
74 public AbstractWsdlModelItem<?> getModelItem()
75 {
76 return modelItem;
77 }
78
79 public abstract void setPropertyValue( String value ) throws Exception;
80
81 protected void notifyUpdate( String value, String old )
82 {
83 modelItem.notifyPropertyChanged( modelItem.getClass().getName() + "@" + propertyName, old, value );
84 }
85
86 public String expand( TestCaseRunContext context )
87 {
88 try
89 {
90 return PathUtils.expandPath( getPropertyValue(), modelItem, context );
91 }
92 catch( Exception e )
93 {
94 e.printStackTrace();
95 return null;
96 }
97 }
98
99 public String expand()
100 {
101 try
102 {
103 return PathUtils.resolveResourcePath( getPropertyValue(), modelItem );
104 }
105 catch( Exception e )
106 {
107 e.printStackTrace();
108 return null;
109 }
110 }
111
112 public String expandUrl()
113 {
114 String result = expand();
115 if( StringUtils.hasContent( result ) )
116 {
117 try
118 {
119 if( PathUtils.isFilePath( result ) && !result.startsWith( "file:" ) )
120 {
121 result = new File( result ).toURI().toURL().toString();
122 }
123 else
124 {
125 result = new URL( result ).toString();
126 }
127 }
128 catch( MalformedURLException e )
129 {
130 SoapUI.logError( e );
131 }
132 }
133
134 return result;
135 }
136
137 public abstract String getPropertyValue() throws Exception;
138
139 public void resolveFile( ResolveContext<?> context, String errorDescription )
140 {
141 resolveFile( context, errorDescription, null, null, true );
142 }
143
144 public boolean containsPropertyExpansion()
145 {
146 try
147 {
148 return PropertyExpansionUtils.containsPropertyExpansion( getPropertyValue() );
149 }
150 catch( Exception e1 )
151 {
152 SoapUI.logError( e1 );
153 return false;
154 }
155 }
156
157 public void resolveFile( ResolveContext<?> context, String errorDescription, String extension, String fileType,
158 final boolean notify )
159 {
160 if( containsPropertyExpansion() )
161 return;
162
163 String source = expand();
164 if( StringUtils.hasContent( source ) )
165 {
166 try
167 {
168 new URL( source );
169 }
170 catch( Exception e )
171 {
172 File file = new File( source );
173 if( !file.exists() )
174 {
175 if( context.hasThisModelItem( modelItem, errorDescription, source ) )
176 return;
177 context.addPathToResolve( modelItem, errorDescription, source, new ResolveContext.FileResolver(
178 "Select File", extension, fileType, file.getParent() )
179 {
180
181 @Override
182 public boolean apply( File newFile )
183 {
184 set( newFile.getAbsolutePath(), notify );
185 return true;
186 }
187 } );
188 }
189 else
190 {
191 if( context.hasThisModelItem( modelItem, errorDescription, source ) )
192 context.getPath( modelItem, errorDescription, source ).setSolved( true );
193 }
194 }
195 }
196 }
197
198 public void resolveFolder( ResolveContext<?> context, String errorDescription, final boolean notify )
199 {
200 if( containsPropertyExpansion() )
201 return;
202
203 String source = expand();
204 if( StringUtils.hasContent( source ) )
205 {
206 try
207 {
208 new URL( source );
209 }
210 catch( Exception e )
211 {
212 File file = new File( source );
213 if( !file.exists() || !file.isDirectory() )
214 {
215 if( context.hasThisModelItem( modelItem, errorDescription, source ) )
216 return;
217 context.addPathToResolve( modelItem, errorDescription, source, new ResolveContext.DirectoryResolver(
218 "Select Directory", source )
219 {
220 @Override
221 public boolean apply( File newFile )
222 {
223 set( newFile.getAbsolutePath(), notify );
224 return true;
225 }
226 } );
227 }
228 else
229 {
230 if( context.hasThisModelItem( modelItem, errorDescription, source ) )
231 context.getPath( modelItem, errorDescription, source ).setSolved( true );
232 }
233 }
234 }
235 }
236 }