1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.support;
14
15 import java.io.BufferedInputStream;
16 import java.io.BufferedOutputStream;
17 import java.io.BufferedReader;
18 import java.io.ByteArrayOutputStream;
19 import java.io.DataInputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.OutputStream;
27 import java.lang.reflect.Method;
28 import java.net.URL;
29 import java.net.URLDecoder;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.Properties;
34
35 import com.eviware.soapui.support.editor.inspectors.attachments.ContentTypeHandler;
36 import com.eviware.soapui.support.types.StringToStringMap;
37
38 public class Tools
39 {
40 public static final int COPY_BUFFER_SIZE = 1000;
41
42 public static String[] tokenizeArgs( String args )
43 {
44 if( args == null || args.trim().length() == 0 )
45 return null;
46
47 List<String> l = Arrays.asList( args.split( " " ) );
48 List<String> result = new ArrayList<String>();
49
50 for( int c = 0; c < l.size(); c++ )
51 {
52 String s = l.get( c );
53 if( s.startsWith( "\"" ) )
54 {
55 c++ ;
56 s += " " + l.get( c );
57 while( !( s.endsWith( "\"" ) && !s.endsWith( "//\"" ) ) && c < l.size() )
58 {
59 c++ ;
60 }
61
62
63 s = c == l.size() ? s.substring( 1 ) : s.substring( 1, s.length() - 1 );
64
65
66 s = s.replace( "//\"", "\"" );
67 }
68
69 result.add( s );
70 }
71
72 return result.toArray( new String[result.size()] );
73 }
74
75 public static String convertToHtml( String str )
76 {
77 StringBuffer result = new StringBuffer( "<html><body>" );
78
79 for( int c = 0; c < str.length(); c++ )
80 {
81 char ch = str.charAt( c );
82 if( ch == '\n' )
83 result.append( "<br>" );
84 else
85 result.append( ch );
86 }
87
88 result.append( "</body></html>" );
89 return result.toString();
90 }
91
92 public static String getFilename( String filePath )
93 {
94 if( filePath == null || filePath.length() == 0 )
95 return filePath;
96
97 int ix = filePath.lastIndexOf( File.separatorChar );
98 if( ix <= 0 )
99 return filePath;
100
101 return filePath.substring( ix + 1, filePath.length() );
102 }
103
104 public static String getDir( String filePath )
105 {
106 if( filePath == null || filePath.length() == 0 )
107 return filePath;
108
109 int ix = filePath.lastIndexOf( File.separatorChar );
110 if( ix <= 0 )
111 return filePath;
112
113 return ensureDir( filePath.substring( 0, ix ), "" );
114 }
115
116 public static String ensureDir( String dir, String basedir )
117 {
118 if( dir == null || dir.length() == 0 )
119 return "";
120
121 File dirFile = new File( dir );
122 if( !dirFile.isAbsolute() )
123 {
124 if( basedir.length() == 0 )
125 basedir = new File( "" ).getAbsolutePath();
126
127 dirFile = new File( basedir, dir );
128 }
129
130 dirFile.mkdirs();
131 return dirFile.getAbsolutePath();
132 }
133
134 public static String ensureFileDir( String file, String basedir )
135 {
136 if( file == null || file.length() == 0 )
137 return "";
138
139 File dirFile = new File( basedir, file );
140 if( !dirFile.isAbsolute() )
141 {
142 if( basedir.length() == 0 )
143 basedir = new File( "" ).getAbsolutePath();
144
145 dirFile = new File( basedir, file );
146 }
147
148 String absolutePath = dirFile.getAbsolutePath();
149 if( !dirFile.exists() )
150 {
151 int ix = absolutePath.lastIndexOf( File.separatorChar );
152 File fileDir = new File( absolutePath.substring( 0, ix ) );
153 fileDir.mkdirs();
154 }
155
156 return absolutePath;
157 }
158
159 public static String ensureDir( String outputDir )
160 {
161 if( outputDir == null )
162 outputDir = "";
163
164 File output = new File( outputDir );
165 output.mkdirs();
166 return outputDir;
167 }
168
169
170 private static final byte[] copyBuffer = new byte[8192];
171 public static final long READ_ALL = 0;
172
173 public static void openURL( String url )
174 {
175 String osName = System.getProperty( "os.name" );
176
177 try
178 {
179 if( osName.startsWith( "Mac OS" ) )
180 {
181 Class<?> fileMgr = Class.forName( "com.apple.eio.FileManager" );
182 Method openURL = fileMgr.getDeclaredMethod( "openURL", new Class[] { String.class } );
183 openURL.invoke( null, new Object[] { url } );
184 }
185 else if( osName.startsWith( "Windows" ) )
186 {
187 if( url.startsWith( "file:" ) )
188 {
189 url = URLDecoder.decode( url.substring( 5 ), "utf-8" );
190 while( url.startsWith( "/" ) )
191 url = url.substring( 1 );
192
193 url = url.replace( '/', '//' );
194
195 if( !new File( url ).exists() )
196 {
197 UISupport.showErrorMessage( "File [" + url + "] not found" );
198 return;
199 }
200 }
201
202 Runtime.getRuntime().exec( "rundll32 url.dll,FileProtocolHandler " + url );
203 }
204 else
205 {
206 String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
207 String browser = null;
208 for( int count = 0; count < browsers.length && browser == null; count++ )
209 if( Runtime.getRuntime().exec( new String[] { "which", browsers[count] } ).waitFor() == 0 )
210 browser = browsers[count];
211 if( browser == null )
212 throw new Exception( "Could not find web browser" );
213 else
214 Runtime.getRuntime().exec( new String[] { browser, url } );
215 }
216 }
217 catch( Exception e )
218 {
219 UISupport.showErrorMessage( e );
220 }
221 }
222
223 public static ByteArrayOutputStream readAll( InputStream instream, long maxSize ) throws IOException
224 {
225 ByteArrayOutputStream outstream = new ByteArrayOutputStream( 4096 );
226
227 readAndWrite( instream, maxSize, outstream );
228
229 outstream.close();
230 return outstream;
231 }
232
233 public static void readAndWrite( InputStream instream, long maxSize, OutputStream outstream ) throws IOException
234 {
235 byte[] buffer = new byte[4096];
236 int len;
237 int read = 0;
238 int toRead = 4096;
239
240 if( maxSize > 0 )
241 {
242 if( read + toRead > maxSize )
243 toRead = ( int )( maxSize - read );
244 }
245
246 while( ( len = instream.read( buffer, 0, toRead ) ) > 0 )
247 {
248 outstream.write( buffer, 0, len );
249 read += toRead;
250
251 if( maxSize > 0 )
252 {
253 if( read + toRead > maxSize )
254 toRead = ( int )( maxSize - read );
255 }
256 }
257 }
258
259 public static int copyFile( File source, File target, boolean overwrite ) throws IOException
260 {
261 int bytes = 0;
262
263 if( target.exists() )
264 {
265 if( overwrite )
266 target.delete();
267 else
268 return -1;
269 }
270 else
271 {
272 String path = target.getAbsolutePath();
273 int ix = path.lastIndexOf( File.separatorChar );
274 if( ix != -1 )
275 {
276 path = path.substring( 0, ix );
277 File pathFile = new File( path );
278 if( !pathFile.exists() )
279 pathFile.mkdirs();
280 }
281 }
282
283 BufferedInputStream in = new BufferedInputStream( new FileInputStream( source ) );
284 BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream( target ) );
285
286 int read = in.read( copyBuffer );
287 while( read != -1 )
288 {
289 if( read > 0 )
290 {
291 out.write( copyBuffer, 0, read );
292 bytes += read;
293 }
294 read = in.read( copyBuffer );
295 }
296
297 in.close();
298 out.close();
299
300 return bytes;
301 }
302
303 /***
304 * Joins a relative url to a base url.. needs improvements..
305 */
306
307 public static String joinRelativeUrl( String baseUrl, String url )
308 {
309 if( baseUrl.indexOf( '?' ) > 0 )
310 baseUrl = baseUrl.substring( 0, baseUrl.indexOf( '?' ) );
311
312 boolean isWindowsUrl = baseUrl.indexOf( '//' ) >= 0;
313 boolean isUsedInUnix = File.separatorChar == '/';
314
315 if( isUsedInUnix && isWindowsUrl )
316 {
317 baseUrl = baseUrl.replace( '//', '/' );
318 url = url.replace( '//', '/' );
319 }
320
321 boolean isFile = baseUrl.startsWith( "file:" );
322
323 int ix = baseUrl.lastIndexOf( '//' );
324 if( ix == -1 )
325 ix = baseUrl.lastIndexOf( '/' );
326
327
328 if( url.startsWith( "/" ) && !isFile )
329 {
330 ix = baseUrl.indexOf( "/", baseUrl.indexOf( "//" ) + 2 );
331 return baseUrl.substring( 0, ix ) + url;
332 }
333
334
335 while( url.startsWith( ".//" ) || url.startsWith( "./" ) )
336 url = url.substring( 2 );
337
338
339 while( url.startsWith( "../" ) || url.startsWith( "..//" ) )
340 {
341 int ix2 = baseUrl.lastIndexOf( '//', ix - 1 );
342 if( ix2 == -1 )
343 ix2 = baseUrl.lastIndexOf( '/', ix - 1 );
344 if( ix2 == -1 )
345 break;
346
347 baseUrl = baseUrl.substring( 0, ix2 + 1 );
348 ix = ix2;
349
350 url = url.substring( 3 );
351 }
352
353
354 while( url.indexOf( "/./" ) != -1 || url.indexOf( "//.//" ) != -1 )
355 {
356 int ix2 = url.indexOf( "/./" );
357 if( ix2 == -1 )
358 ix2 = url.indexOf( "//.//" );
359
360 url = url.substring( 0, ix2 ) + url.substring( ix2 + 2 );
361 }
362
363
364 while( url.indexOf( "/../" ) != -1 || url.indexOf( "//..//" ) != -1 )
365 {
366 int ix2 = -1;
367
368 int ix3 = url.indexOf( "/../" );
369 if( ix3 == -1 )
370 {
371 ix3 = url.indexOf( "//..//" );
372 ix2 = url.lastIndexOf( '//', ix3 - 1 );
373 }
374 else
375 {
376 ix2 = url.lastIndexOf( '/', ix3 - 1 );
377 }
378
379 if( ix2 == -1 )
380 break;
381
382 url = url.substring( 0, ix2 ) + url.substring( ix3 + 3 );
383 }
384
385 String result = baseUrl.substring( 0, ix + 1 ) + url;
386 if( isFile )
387 result = result.replace( '/', File.separatorChar );
388
389 return result;
390 }
391
392 public static boolean isEmpty( String str )
393 {
394 return str == null || str.trim().length() == 0;
395 }
396
397 public static long writeAll( OutputStream out, InputStream in ) throws IOException
398 {
399 byte[] buffer = new byte[COPY_BUFFER_SIZE];
400 long total = 0;
401
402 int sz = in.read( buffer );
403 while( sz != -1 )
404 {
405 out.write( buffer, 0, sz );
406 total += sz;
407 sz = in.read( buffer );
408 }
409
410 return total;
411 }
412
413 public static String expandProperties( StringToStringMap values, String content, boolean leaveMissing )
414 {
415 int ix = content.indexOf( "${" );
416 if( ix == -1 )
417 return content;
418
419 StringBuffer buf = new StringBuffer();
420 int lastIx = 0;
421 while( ix != -1 )
422 {
423 buf.append( content.substring( lastIx, ix ) );
424
425 int ix2 = content.indexOf( '}', ix + 2 );
426 if( ix2 == -1 )
427 break;
428
429 int ix3 = content.lastIndexOf( "${", ix2 );
430 if( ix3 != ix )
431 {
432 buf.append( content.substring( ix, ix3 ) );
433 ix = ix3;
434 }
435
436 String propertyName = content.substring( ix + 2, ix2 );
437 Object property = values.get( propertyName );
438 if( property != null )
439 {
440 buf.append( property.toString() );
441 }
442 else if( leaveMissing )
443 {
444 buf.append( "${" ).append( propertyName ).append( '}' );
445 }
446
447 lastIx = ix2 + 1;
448 ix = content.indexOf( "${", lastIx );
449 }
450
451 if( lastIx < content.length() )
452 buf.append( content.substring( lastIx ) );
453
454 return buf.toString();
455 }
456
457 /***
458 * Replaces the host part of the specified endpoint with the specified host
459 *
460 * @param endpoint
461 * the endpoint to modify
462 * @param host
463 * the host to set
464 * @return the modified endpoint
465 */
466
467 public static String replaceHost( String endpoint, String host )
468 {
469 int ix1 = endpoint.indexOf( "://" );
470 if( ix1 < 0 )
471 return endpoint;
472
473 int ix2 = endpoint.indexOf( ":", ix1 + 3 );
474 if( ix2 == -1 || host.indexOf( ":" ) > 0 )
475 {
476 ix2 = endpoint.indexOf( "/", ix1 + 3 );
477 if( ix2 == ix1 + 3 )
478 ix2 = -1;
479 }
480
481 return endpoint.substring( 0, ix1 ) + "://" + host + ( ix2 == -1 ? "" : endpoint.substring( ix2 ) );
482 }
483
484 public static String getEndpointFromUrl( URL baseUrl )
485 {
486 StringBuffer result = new StringBuffer();
487 result.append( baseUrl.getProtocol() ).append( "://" );
488 result.append( baseUrl.getHost() );
489 if( baseUrl.getPort() > 0 )
490 result.append( ':' ).append( baseUrl.getPort() );
491
492 return result.toString();
493 }
494
495 public static String getContentTypeFromFilename( String fileName )
496 {
497 return ContentTypeHandler.getContentTypeFromFilename( fileName );
498 }
499
500 public static String getExtensionForContentType( String contentType )
501 {
502 return ContentTypeHandler.getExtensionForContentType( contentType );
503 }
504
505 public static String getFileContent( String path )
506 {
507 String content = "";
508 try
509 {
510 FileInputStream fstream = new FileInputStream( path );
511 DataInputStream in = new DataInputStream( fstream );
512 BufferedReader br = new BufferedReader( new InputStreamReader( in ) );
513 content = br.readLine();
514 br.close();
515 in.close();
516 }
517 catch( Exception e )
518 {
519 System.err.println( "Error: " + e.getMessage() );
520 }
521 return content;
522 }
523 public static String getTitleProperty( String path )
524 {
525 String content = "";
526 try
527 {
528 FileInputStream fstream = new FileInputStream( path );
529 Properties props = new Properties();
530 props.load( fstream );
531 fstream.close();
532 content = props.getProperty( "soapui.app.title" );
533 }
534 catch( Exception e )
535 {
536 System.err.println( "Error: " + e.getMessage() );
537 }
538 return content;
539 }
540 }