1 package org.apache.tomcat.maven.common.deployer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.codec.binary.Base64;
23 import org.apache.commons.io.IOUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.http.HttpHost;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.auth.AuthScope;
28 import org.apache.http.auth.Credentials;
29 import org.apache.http.auth.UsernamePasswordCredentials;
30 import org.apache.http.client.AuthCache;
31 import org.apache.http.client.methods.HttpGet;
32 import org.apache.http.client.methods.HttpPut;
33 import org.apache.http.client.methods.HttpRequestBase;
34 import org.apache.http.client.protocol.ClientContext;
35 import org.apache.http.entity.AbstractHttpEntity;
36 import org.apache.http.impl.auth.BasicScheme;
37 import org.apache.http.impl.client.BasicAuthCache;
38 import org.apache.http.impl.client.DefaultHttpClient;
39 import org.apache.http.impl.conn.BasicClientConnectionManager;
40 import org.apache.http.protocol.BasicHttpContext;
41 import org.apache.maven.plugin.logging.Log;
42
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.OutputStream;
46 import java.io.PrintStream;
47 import java.net.URL;
48 import java.net.URLEncoder;
49 import java.text.DecimalFormat;
50 import java.text.DecimalFormatSymbols;
51 import java.util.Locale;
52
53
54
55
56
57
58
59
60 public class TomcatManager
61 {
62
63
64
65
66
67
68
69 private static final String MANAGER_CHARSET = "UTF-8";
70
71
72
73
74
75
76
77
78 private URL url;
79
80
81
82
83 private String username;
84
85
86
87
88 private String password;
89
90
91
92
93 private String charset;
94
95
96
97
98 private String userAgent;
99
100
101
102
103 private DefaultHttpClient httpClient;
104
105
106
107
108 private BasicHttpContext localContext;
109
110
111
112
113
114
115
116
117
118
119
120 public TomcatManager( URL url )
121 {
122 this( url, "admin" );
123 }
124
125
126
127
128
129
130
131
132 public TomcatManager( URL url, String username )
133 {
134 this( url, username, "" );
135 }
136
137
138
139
140
141
142
143
144 public TomcatManager( URL url, String username, String password )
145 {
146 this( url, username, password, "ISO-8859-1" );
147 }
148
149
150
151
152
153
154
155
156
157
158 public TomcatManager( URL url, String username, String password, String charset )
159 {
160 this.url = url;
161 this.username = username;
162 this.password = password;
163 this.charset = charset;
164
165 this.httpClient = new DefaultHttpClient( new BasicClientConnectionManager() );
166 if ( StringUtils.isNotEmpty( username ) && StringUtils.isNotEmpty( password ) )
167 {
168 Credentials creds = new UsernamePasswordCredentials( username, password );
169
170 String host = url.getHost();
171 int port = url.getPort() > -1 ? url.getPort() : AuthScope.ANY_PORT;
172
173 httpClient.getCredentialsProvider().setCredentials( new AuthScope( host, port ), creds );
174
175 AuthCache authCache = new BasicAuthCache();
176 BasicScheme basicAuth = new BasicScheme();
177 HttpHost targetHost = new HttpHost( url.getHost(), url.getPort(), url.getProtocol() );
178 authCache.put( targetHost, basicAuth );
179
180 localContext = new BasicHttpContext();
181 localContext.setAttribute( ClientContext.AUTH_CACHE, authCache );
182 }
183 }
184
185
186
187
188
189
190
191
192
193
194 public URL getURL()
195 {
196 return url;
197 }
198
199
200
201
202
203
204 public String getUserName()
205 {
206 return username;
207 }
208
209
210
211
212
213
214 public String getPassword()
215 {
216 return password;
217 }
218
219
220
221
222
223
224 public String getCharset()
225 {
226 return charset;
227 }
228
229
230
231
232
233
234 public String getUserAgent()
235 {
236 return userAgent;
237 }
238
239
240
241
242
243
244 public void setUserAgent( String userAgent )
245 {
246 this.userAgent = userAgent;
247 }
248
249
250
251
252
253
254
255
256
257
258 public TomcatManagerResponse deploy( String path, URL war )
259 throws TomcatManagerException, IOException
260 {
261 return deploy( path, war, false );
262 }
263
264
265
266
267
268
269
270
271
272
273
274
275 public TomcatManagerResponse deploy( String path, URL war, boolean update )
276 throws TomcatManagerException, IOException
277 {
278 return deploy( path, war, update, null );
279 }
280
281
282
283
284
285
286
287
288
289
290
291
292
293 public TomcatManagerResponse deploy( String path, URL war, boolean update, String tag )
294 throws TomcatManagerException, IOException
295 {
296 return deployImpl( path, null, war, null, update, tag );
297 }
298
299
300
301
302
303
304
305
306
307
308 public TomcatManagerResponse deploy( String path, InputStream war )
309 throws TomcatManagerException, IOException
310 {
311 return deploy( path, war, false );
312 }
313
314
315
316
317
318
319
320
321
322
323
324
325 public TomcatManagerResponse deploy( String path, InputStream war, boolean update )
326 throws TomcatManagerException, IOException
327 {
328 return deploy( path, war, update, null );
329 }
330
331
332
333
334
335
336
337
338
339
340
341
342
343 public TomcatManagerResponse deploy( String path, InputStream war, boolean update, String tag )
344 throws TomcatManagerException, IOException
345 {
346 return deployImpl( path, null, null, war, update, tag );
347 }
348
349
350
351
352
353
354
355
356
357
358
359
360 public TomcatManagerResponse deploy( String path, InputStream war, boolean update, String tag, long length )
361 throws TomcatManagerException, IOException
362 {
363 return deployImpl( path, null, null, war, update, tag, length );
364 }
365
366
367
368
369
370
371
372
373
374
375 public TomcatManagerResponse deployContext( String path, URL config )
376 throws TomcatManagerException, IOException
377 {
378 return deployContext( path, config, false );
379 }
380
381
382
383
384
385
386
387
388
389
390
391
392 public TomcatManagerResponse deployContext( String path, URL config, boolean update )
393 throws TomcatManagerException, IOException
394 {
395 return deployContext( path, config, update, null );
396 }
397
398
399
400
401
402
403
404
405
406
407
408
409
410 public TomcatManagerResponse deployContext( String path, URL config, boolean update, String tag )
411 throws TomcatManagerException, IOException
412 {
413 return deployContext( path, config, null, update, tag );
414 }
415
416
417
418
419
420
421
422
423
424
425
426 public TomcatManagerResponse deployContext( String path, URL config, URL war )
427 throws TomcatManagerException, IOException
428 {
429 return deployContext( path, config, war, false );
430 }
431
432
433
434
435
436
437
438
439
440
441
442
443
444 public TomcatManagerResponse deployContext( String path, URL config, URL war, boolean update )
445 throws TomcatManagerException, IOException
446 {
447 return deployContext( path, config, war, update, null );
448 }
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463 public TomcatManagerResponse deployContext( String path, URL config, URL war, boolean update, String tag )
464 throws TomcatManagerException, IOException
465 {
466 return deployImpl( path, config, war, null, update, tag );
467 }
468
469
470
471
472
473
474
475
476
477 public TomcatManagerResponse undeploy( String path )
478 throws TomcatManagerException, IOException
479 {
480 return invoke( "/undeploy?path=" + URLEncoder.encode( path, charset ) );
481 }
482
483
484
485
486
487
488
489
490
491 public TomcatManagerResponse reload( String path )
492 throws TomcatManagerException, IOException
493 {
494 return invoke( "/reload?path=" + URLEncoder.encode( path, charset ) );
495 }
496
497
498
499
500
501
502
503
504
505 public TomcatManagerResponse start( String path )
506 throws TomcatManagerException, IOException
507 {
508 return invoke( "/start?path=" + URLEncoder.encode( path, charset ) );
509 }
510
511
512
513
514
515
516
517
518
519 public TomcatManagerResponse stop( String path )
520 throws TomcatManagerException, IOException
521 {
522 return invoke( "/stop?path=" + URLEncoder.encode( path, charset ) );
523 }
524
525
526
527
528
529
530
531
532 public TomcatManagerResponse list()
533 throws TomcatManagerException, IOException
534 {
535 return invoke( "/list" );
536 }
537
538
539
540
541
542
543
544
545 public TomcatManagerResponse getServerInfo()
546 throws TomcatManagerException, IOException
547 {
548 return invoke( "/serverinfo" );
549 }
550
551
552
553
554
555
556
557
558 public TomcatManagerResponse getResources()
559 throws TomcatManagerException, IOException
560 {
561 return getResources( null );
562 }
563
564
565
566
567
568
569
570
571
572 public TomcatManagerResponse getResources( String type )
573 throws TomcatManagerException, IOException
574 {
575 StringBuffer buffer = new StringBuffer();
576 buffer.append( "/resources" );
577
578 if ( type != null )
579 {
580 buffer.append( "?type=" + URLEncoder.encode( type, charset ) );
581 }
582 return invoke( buffer.toString() );
583 }
584
585
586
587
588
589
590
591
592 public TomcatManagerResponse getRoles()
593 throws TomcatManagerException, IOException
594 {
595 return invoke( "/roles" );
596 }
597
598
599
600
601
602
603
604
605
606 public TomcatManagerResponse getSessions( String path )
607 throws TomcatManagerException, IOException
608 {
609 return invoke( "/sessions?path=" + URLEncoder.encode( path, charset ) );
610 }
611
612
613
614
615
616
617
618
619
620
621
622
623
624 protected TomcatManagerResponse invoke( String path )
625 throws TomcatManagerException, IOException
626 {
627 return invoke( path, null, -1 );
628 }
629
630
631
632
633
634 private TomcatManagerResponse deployImpl( String path, URL config, URL war, InputStream data, boolean update,
635 String tag )
636 throws TomcatManagerException, IOException
637 {
638 return deployImpl( path, config, war, data, update, tag, -1 );
639 }
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654 private TomcatManagerResponse deployImpl( String path, URL config, URL war, InputStream data, boolean update,
655 String tag, long length )
656 throws TomcatManagerException, IOException
657 {
658 StringBuilder buffer = new StringBuilder( "/deploy" );
659 buffer.append( "?path=" ).append( URLEncoder.encode( path, charset ) );
660
661 if ( config != null )
662 {
663 buffer.append( "&config=" ).append( URLEncoder.encode( config.toString(), charset ) );
664 }
665
666 if ( war != null )
667 {
668 buffer.append( "&war=" ).append( URLEncoder.encode( war.toString(), charset ) );
669 }
670
671 if ( update )
672 {
673 buffer.append( "&update=true" );
674 }
675
676 if ( tag != null )
677 {
678 buffer.append( "&tag=" ).append( URLEncoder.encode( tag, charset ) );
679 }
680
681 return invoke( buffer.toString(), data, length );
682 }
683
684
685
686
687
688
689
690
691
692
693
694 protected TomcatManagerResponse invoke( String path, InputStream data, long length )
695 throws TomcatManagerException, IOException
696 {
697
698 HttpRequestBase httpRequestBase = null;
699 if ( data == null )
700 {
701 httpRequestBase = new HttpGet( url + path );
702 }
703 else
704 {
705 HttpPut httpPut = new HttpPut( url + path );
706
707 httpPut.setEntity( new RequestEntityImplementation( data, length, url + path ) );
708
709 httpRequestBase = httpPut;
710
711 }
712
713 if ( userAgent != null )
714 {
715 httpRequestBase.setHeader( "User-Agent", userAgent );
716 }
717
718 HttpResponse response = httpClient.execute( httpRequestBase, localContext );
719
720 return new TomcatManagerResponse().setStatusCode( response.getStatusLine().getStatusCode() ).setReasonPhrase(
721 response.getStatusLine().getReasonPhrase() ).setHttpResponseBody(
722 IOUtils.toString( response.getEntity().getContent() ) );
723
724 }
725
726
727
728
729
730
731
732
733
734 private String toAuthorization( String username, String password )
735 {
736 StringBuffer buffer = new StringBuffer();
737 buffer.append( username ).append( ':' );
738 if ( password != null )
739 {
740 buffer.append( password );
741 }
742 return "Basic " + new String( Base64.encodeBase64( buffer.toString().getBytes() ) );
743 }
744
745 private final class RequestEntityImplementation
746 extends AbstractHttpEntity
747 {
748
749 private final static int BUFFER_SIZE = 2048;
750
751 private InputStream stream;
752
753 PrintStream out = System.out;
754
755 private long length = -1;
756
757 private int lastLength;
758
759 private String url;
760
761 private long startTime;
762
763 private RequestEntityImplementation( final InputStream stream, long length, String url )
764 {
765 this.stream = stream;
766 this.length = length;
767 this.url = url;
768 }
769
770 public long getContentLength()
771 {
772 return length >= 0 ? length : -1;
773 }
774
775
776 public InputStream getContent()
777 throws IOException, IllegalStateException
778 {
779 return this.stream;
780 }
781
782 public boolean isRepeatable()
783 {
784 return false;
785 }
786
787
788 public void writeTo( final OutputStream outstream )
789 throws IOException
790 {
791 long completed = 0;
792 if ( outstream == null )
793 {
794 throw new IllegalArgumentException( "Output stream may not be null" );
795 }
796 transferInitiated( this.url );
797 this.startTime = System.currentTimeMillis();
798 try
799 {
800 byte[] buffer = new byte[BUFFER_SIZE];
801 int l;
802 if ( this.length < 0 )
803 {
804
805 while ( ( l = stream.read( buffer ) ) != -1 )
806 {
807 transferProgressed( completed += buffer.length, -1 );
808 outstream.write( buffer, 0, l );
809 }
810 }
811 else
812 {
813
814 long remaining = this.length;
815 while ( remaining > 0 )
816 {
817 int transferSize = (int) Math.min( BUFFER_SIZE, remaining );
818 completed += transferSize;
819 l = stream.read( buffer, 0, transferSize );
820 if ( l == -1 )
821 {
822 break;
823 }
824
825 outstream.write( buffer, 0, l );
826 remaining -= l;
827 transferProgressed( completed, this.length );
828 }
829 }
830 transferSucceeded( completed );
831 }
832 finally
833 {
834 stream.close();
835 out.println();
836 }
837
838 }
839
840 public boolean isStreaming()
841 {
842 return true;
843 }
844
845
846 public void transferInitiated( String url )
847 {
848 String message = "Uploading";
849
850 out.println( message + ": " + url );
851 }
852
853 public void transferProgressed( long completedSize, long totalSize )
854 {
855
856 StringBuilder buffer = new StringBuilder( 64 );
857
858 buffer.append( getStatus( completedSize, totalSize ) ).append( " " );
859 lastLength = buffer.length();
860 buffer.append( '\r' );
861
862 out.print( buffer );
863 }
864
865 public void transferSucceeded( long contentLength )
866 {
867
868 if ( contentLength >= 0 )
869 {
870 String type = "Uploaded";
871 String len = contentLength >= 1024 ? toKB( contentLength ) + " KB" : contentLength + " B";
872
873 String throughput = "";
874 long duration = System.currentTimeMillis() - startTime;
875 if ( duration > 0 )
876 {
877 DecimalFormat format = new DecimalFormat( "0.0", new DecimalFormatSymbols( Locale.ENGLISH ) );
878 double kbPerSec = ( contentLength / 1024.0 ) / ( duration / 1000.0 );
879 throughput = " at " + format.format( kbPerSec ) + " KB/sec";
880 }
881
882 out.println( type + ": " + url + " (" + len + throughput + ")" );
883 }
884 }
885
886 private String getStatus( long complete, long total )
887 {
888 if ( total >= 1024 )
889 {
890 return toKB( complete ) + "/" + toKB( total ) + " KB ";
891 }
892 else if ( total >= 0 )
893 {
894 return complete + "/" + total + " B ";
895 }
896 else if ( complete >= 1024 )
897 {
898 return toKB( complete ) + " KB ";
899 }
900 else
901 {
902 return complete + " B ";
903 }
904 }
905
906 private long toKB( long bytes )
907 {
908 return ( bytes + 1023 ) / 1024;
909 }
910
911 }
912 }