1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.client;
15
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.net.UnknownHostException;
19 import java.util.Enumeration;
20 import java.util.LinkedList;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
24 import javax.net.ssl.SSLContext;
25
26 import org.eclipse.jetty.client.security.Authentication;
27 import org.eclipse.jetty.client.security.RealmResolver;
28 import org.eclipse.jetty.client.security.SecurityListener;
29 import org.eclipse.jetty.http.HttpBuffers;
30 import org.eclipse.jetty.http.HttpBuffersImpl;
31 import org.eclipse.jetty.http.HttpSchemes;
32 import org.eclipse.jetty.io.Buffers;
33 import org.eclipse.jetty.io.Buffers.Type;
34 import org.eclipse.jetty.util.Attributes;
35 import org.eclipse.jetty.util.AttributesMap;
36 import org.eclipse.jetty.util.component.AggregateLifeCycle;
37 import org.eclipse.jetty.util.component.Dumpable;
38 import org.eclipse.jetty.util.component.LifeCycle;
39 import org.eclipse.jetty.util.ssl.SslContextFactory;
40 import org.eclipse.jetty.util.thread.QueuedThreadPool;
41 import org.eclipse.jetty.util.thread.ThreadPool;
42 import org.eclipse.jetty.util.thread.Timeout;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public class HttpClient extends AggregateLifeCycle implements HttpBuffers, Attributes, Dumpable
70 {
71 public static final int CONNECTOR_SOCKET = 0;
72 public static final int CONNECTOR_SELECT_CHANNEL = 2;
73
74 private int _connectorType = CONNECTOR_SELECT_CHANNEL;
75 private boolean _useDirectBuffers = true;
76 private boolean _connectBlocking = true;
77 private int _maxConnectionsPerAddress = Integer.MAX_VALUE;
78 private int _maxQueueSizePerAddress = Integer.MAX_VALUE;
79 private ConcurrentMap<Address, HttpDestination> _destinations = new ConcurrentHashMap<Address, HttpDestination>();
80 ThreadPool _threadPool;
81 Connector _connector;
82 private long _idleTimeout = 20000;
83 private long _timeout = 320000;
84 private int _connectTimeout = 75000;
85 private Timeout _timeoutQ = new Timeout();
86 private Timeout _idleTimeoutQ = new Timeout();
87 private Address _proxy;
88 private Authentication _proxyAuthentication;
89 private Set<String> _noProxy;
90 private int _maxRetries = 3;
91 private int _maxRedirects = 20;
92 private LinkedList<String> _registeredListeners;
93
94 private final SslContextFactory _sslContextFactory;
95
96 private RealmResolver _realmResolver;
97
98 private AttributesMap _attributes=new AttributesMap();
99
100 private final HttpBuffersImpl _buffers= new HttpBuffersImpl();
101
102
103 private void setBufferTypes()
104 {
105 if (_connectorType==CONNECTOR_SOCKET)
106 {
107 _buffers.setRequestBufferType(Type.BYTE_ARRAY);
108 _buffers.setRequestHeaderType(Type.BYTE_ARRAY);
109 _buffers.setResponseBufferType(Type.BYTE_ARRAY);
110 _buffers.setResponseHeaderType(Type.BYTE_ARRAY);
111 }
112 else
113 {
114 _buffers.setRequestBufferType(Type.DIRECT);
115 _buffers.setRequestHeaderType(_useDirectBuffers?Type.DIRECT:Type.INDIRECT);
116 _buffers.setResponseBufferType(Type.DIRECT);
117 _buffers.setResponseHeaderType(_useDirectBuffers?Type.DIRECT:Type.INDIRECT);
118 }
119
120 }
121
122
123 public HttpClient()
124 {
125 this(new SslContextFactory());
126 }
127
128
129 public HttpClient(SslContextFactory sslContextFactory)
130 {
131 _sslContextFactory = sslContextFactory;
132 addBean(_sslContextFactory);
133 addBean(_buffers);
134 }
135
136
137
138
139
140 public boolean isConnectBlocking()
141 {
142 return _connectBlocking;
143 }
144
145
146
147
148
149 public void setConnectBlocking(boolean connectBlocking)
150 {
151 _connectBlocking = connectBlocking;
152 }
153
154
155 public void send(HttpExchange exchange) throws IOException
156 {
157 boolean ssl = HttpSchemes.HTTPS_BUFFER.equalsIgnoreCase(exchange.getScheme());
158 exchange.setStatus(HttpExchange.STATUS_WAITING_FOR_CONNECTION);
159 HttpDestination destination = getDestination(exchange.getAddress(), ssl);
160 destination.send(exchange);
161 }
162
163
164
165
166
167 public ThreadPool getThreadPool()
168 {
169 return _threadPool;
170 }
171
172
173
174
175
176
177
178 public void setThreadPool(ThreadPool threadPool)
179 {
180 removeBean(_threadPool);
181 _threadPool = threadPool;
182 addBean(_threadPool);
183 }
184
185
186
187
188
189
190
191 public Object getAttribute(String name)
192 {
193 return _attributes.getAttribute(name);
194 }
195
196
197
198
199
200 public Enumeration getAttributeNames()
201 {
202 return _attributes.getAttributeNames();
203 }
204
205
206
207
208
209 public void removeAttribute(String name)
210 {
211 _attributes.removeAttribute(name);
212 }
213
214
215
216
217
218
219
220
221
222 public void setAttribute(String name, Object attribute)
223 {
224 _attributes.setAttribute(name,attribute);
225 }
226
227
228 public void clearAttributes()
229 {
230 _attributes.clearAttributes();
231 }
232
233
234 public HttpDestination getDestination(Address remote, boolean ssl) throws IOException
235 {
236 if (remote == null)
237 throw new UnknownHostException("Remote socket address cannot be null.");
238
239 HttpDestination destination = _destinations.get(remote);
240 if (destination == null)
241 {
242 destination = new HttpDestination(this, remote, ssl);
243 if (_proxy != null && (_noProxy == null || !_noProxy.contains(remote.getHost())))
244 {
245 destination.setProxy(_proxy);
246 if (_proxyAuthentication != null)
247 destination.setProxyAuthentication(_proxyAuthentication);
248 }
249 HttpDestination other =_destinations.putIfAbsent(remote, destination);
250 if (other!=null)
251 destination=other;
252 }
253 return destination;
254 }
255
256
257 public void schedule(Timeout.Task task)
258 {
259 _timeoutQ.schedule(task);
260 }
261
262
263 public void schedule(Timeout.Task task, long timeout)
264 {
265 _timeoutQ.schedule(task, timeout - _timeoutQ.getDuration());
266 }
267
268
269 public void scheduleIdle(Timeout.Task task)
270 {
271 _idleTimeoutQ.schedule(task);
272 }
273
274
275 public void cancel(Timeout.Task task)
276 {
277 task.cancel();
278 }
279
280
281
282
283
284 public boolean getUseDirectBuffers()
285 {
286 return _useDirectBuffers;
287 }
288
289
290
291
292
293
294
295
296 public void setRealmResolver(RealmResolver resolver)
297 {
298 _realmResolver = resolver;
299 }
300
301
302
303
304
305
306
307 public RealmResolver getRealmResolver()
308 {
309 return _realmResolver;
310 }
311
312
313 public boolean hasRealms()
314 {
315 return _realmResolver == null ? false : true;
316 }
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331 public void registerListener(String listenerClass)
332 {
333 if (_registeredListeners == null)
334 {
335 _registeredListeners = new LinkedList<String>();
336 }
337 _registeredListeners.add(listenerClass);
338 }
339
340
341 public LinkedList<String> getRegisteredListeners()
342 {
343 return _registeredListeners;
344 }
345
346
347
348
349
350
351
352
353
354
355 public void setUseDirectBuffers(boolean direct)
356 {
357 _useDirectBuffers = direct;
358 setBufferTypes();
359 }
360
361
362
363
364
365 public int getConnectorType()
366 {
367 return _connectorType;
368 }
369
370
371 public void setConnectorType(int connectorType)
372 {
373 this._connectorType = connectorType;
374 setBufferTypes();
375 }
376
377
378 public int getMaxConnectionsPerAddress()
379 {
380 return _maxConnectionsPerAddress;
381 }
382
383
384 public void setMaxConnectionsPerAddress(int maxConnectionsPerAddress)
385 {
386 _maxConnectionsPerAddress = maxConnectionsPerAddress;
387 }
388
389 public int getMaxQueueSizePerAddress()
390 {
391 return _maxQueueSizePerAddress;
392 }
393
394 public void setMaxQueueSizePerAddress(int maxQueueSizePerAddress)
395 {
396 this._maxQueueSizePerAddress = maxQueueSizePerAddress;
397 }
398
399
400 @Override
401 protected void doStart() throws Exception
402 {
403 setBufferTypes();
404
405 _timeoutQ.setDuration(_timeout);
406 _timeoutQ.setNow();
407 _idleTimeoutQ.setDuration(_idleTimeout);
408 _idleTimeoutQ.setNow();
409
410 if (_threadPool==null)
411 {
412 QueuedThreadPool pool = new LocalQueuedThreadPool();
413 pool.setMaxThreads(16);
414 pool.setDaemon(true);
415 pool.setName("HttpClient");
416 _threadPool = pool;
417 addBean(_threadPool,true);
418 }
419
420 _connector=(_connectorType == CONNECTOR_SELECT_CHANNEL)?new SelectConnector(this):new SocketConnector(this);
421 addBean(_connector,true);
422
423 super.doStart();
424
425 _threadPool.dispatch(new Runnable()
426 {
427 public void run()
428 {
429 while (isRunning())
430 {
431 _timeoutQ.tick(System.currentTimeMillis());
432 _idleTimeoutQ.tick(_timeoutQ.getNow());
433 try
434 {
435 Thread.sleep(200);
436 }
437 catch (InterruptedException ignored)
438 {
439 }
440 }
441 }
442 });
443 }
444
445
446 @Override
447 protected void doStop() throws Exception
448 {
449 for (HttpDestination destination : _destinations.values())
450 destination.close();
451
452 _timeoutQ.cancelAll();
453 _idleTimeoutQ.cancelAll();
454
455 super.doStop();
456
457 if (_threadPool instanceof LocalQueuedThreadPool)
458 {
459 removeBean(_threadPool);
460 _threadPool = null;
461 }
462
463 removeBean(_connector);
464 }
465
466
467 interface Connector extends LifeCycle
468 {
469 public void startConnection(HttpDestination destination) throws IOException;
470 }
471
472
473
474
475
476
477
478
479 protected SSLContext getSSLContext()
480 {
481 return _sslContextFactory.getSslContext();
482 }
483
484
485
486
487
488 public SslContextFactory getSslContextFactory()
489 {
490 return _sslContextFactory;
491 }
492
493
494
495
496
497 public long getIdleTimeout()
498 {
499 return _idleTimeout;
500 }
501
502
503
504
505
506 public void setIdleTimeout(long ms)
507 {
508 _idleTimeout = ms;
509 }
510
511
512
513
514
515
516 @Deprecated
517 public int getSoTimeout()
518 {
519 return Long.valueOf(getTimeout()).intValue();
520 }
521
522
523
524
525
526
527 @Deprecated
528 public void setSoTimeout(int timeout)
529 {
530 setTimeout(timeout);
531 }
532
533
534
535
536
537 public long getTimeout()
538 {
539 return _timeout;
540 }
541
542
543
544
545
546 public void setTimeout(long timeout)
547 {
548 _timeout = timeout;
549 }
550
551
552
553
554
555 public int getConnectTimeout()
556 {
557 return _connectTimeout;
558 }
559
560
561
562
563
564 public void setConnectTimeout(int connectTimeout)
565 {
566 this._connectTimeout = connectTimeout;
567 }
568
569
570 public Address getProxy()
571 {
572 return _proxy;
573 }
574
575
576 public void setProxy(Address proxy)
577 {
578 this._proxy = proxy;
579 }
580
581
582 public Authentication getProxyAuthentication()
583 {
584 return _proxyAuthentication;
585 }
586
587
588 public void setProxyAuthentication(Authentication authentication)
589 {
590 _proxyAuthentication = authentication;
591 }
592
593
594 public boolean isProxied()
595 {
596 return this._proxy != null;
597 }
598
599
600 public Set<String> getNoProxy()
601 {
602 return _noProxy;
603 }
604
605
606 public void setNoProxy(Set<String> noProxyAddresses)
607 {
608 _noProxy = noProxyAddresses;
609 }
610
611
612 public int maxRetries()
613 {
614 return _maxRetries;
615 }
616
617
618 public void setMaxRetries(int retries)
619 {
620 _maxRetries = retries;
621 }
622
623
624 public int maxRedirects()
625 {
626 return _maxRedirects;
627 }
628
629
630 public void setMaxRedirects(int redirects)
631 {
632 _maxRedirects = redirects;
633 }
634
635 public int getRequestBufferSize()
636 {
637 return _buffers.getRequestBufferSize();
638 }
639
640 public void setRequestBufferSize(int requestBufferSize)
641 {
642 _buffers.setRequestBufferSize(requestBufferSize);
643 }
644
645 public int getRequestHeaderSize()
646 {
647 return _buffers.getRequestHeaderSize();
648 }
649
650 public void setRequestHeaderSize(int requestHeaderSize)
651 {
652 _buffers.setRequestHeaderSize(requestHeaderSize);
653 }
654
655 public int getResponseBufferSize()
656 {
657 return _buffers.getResponseBufferSize();
658 }
659
660 public void setResponseBufferSize(int responseBufferSize)
661 {
662 _buffers.setResponseBufferSize(responseBufferSize);
663 }
664
665 public int getResponseHeaderSize()
666 {
667 return _buffers.getResponseHeaderSize();
668 }
669
670 public void setResponseHeaderSize(int responseHeaderSize)
671 {
672 _buffers.setResponseHeaderSize(responseHeaderSize);
673 }
674
675 public Type getRequestBufferType()
676 {
677 return _buffers.getRequestBufferType();
678 }
679
680 public Type getRequestHeaderType()
681 {
682 return _buffers.getRequestHeaderType();
683 }
684
685 public Type getResponseBufferType()
686 {
687 return _buffers.getResponseBufferType();
688 }
689
690 public Type getResponseHeaderType()
691 {
692 return _buffers.getResponseHeaderType();
693 }
694
695 public void setRequestBuffers(Buffers requestBuffers)
696 {
697 _buffers.setRequestBuffers(requestBuffers);
698 }
699
700 public void setResponseBuffers(Buffers responseBuffers)
701 {
702 _buffers.setResponseBuffers(responseBuffers);
703 }
704
705 public Buffers getRequestBuffers()
706 {
707 return _buffers.getRequestBuffers();
708 }
709
710 public Buffers getResponseBuffers()
711 {
712 return _buffers.getResponseBuffers();
713 }
714
715 public void setMaxBuffers(int maxBuffers)
716 {
717 _buffers.setMaxBuffers(maxBuffers);
718 }
719
720 public int getMaxBuffers()
721 {
722 return _buffers.getMaxBuffers();
723 }
724
725
726 @Deprecated
727 public String getTrustStoreLocation()
728 {
729 return _sslContextFactory.getTrustStore();
730 }
731
732
733 @Deprecated
734 public void setTrustStoreLocation(String trustStoreLocation)
735 {
736 _sslContextFactory.setTrustStore(trustStoreLocation);
737 }
738
739
740 @Deprecated
741 public InputStream getTrustStoreInputStream()
742 {
743 return _sslContextFactory.getTrustStoreInputStream();
744 }
745
746
747 @Deprecated
748 public void setTrustStoreInputStream(InputStream trustStoreInputStream)
749 {
750 _sslContextFactory.setTrustStoreInputStream(trustStoreInputStream);
751 }
752
753
754 @Deprecated
755 public String getKeyStoreLocation()
756 {
757 return _sslContextFactory.getKeyStorePath();
758 }
759
760
761 @Deprecated
762 public void setKeyStoreLocation(String keyStoreLocation)
763 {
764 _sslContextFactory.setKeyStorePath(keyStoreLocation);
765 }
766
767 @Deprecated
768 public InputStream getKeyStoreInputStream()
769 {
770 return _sslContextFactory.getKeyStoreInputStream();
771 }
772
773 @Deprecated
774 public void setKeyStoreInputStream(InputStream keyStoreInputStream)
775 {
776 _sslContextFactory.setKeyStoreInputStream(keyStoreInputStream);
777 }
778
779
780 @Deprecated
781 public void setKeyStorePassword(String keyStorePassword)
782 {
783 _sslContextFactory.setKeyStorePassword(keyStorePassword);
784 }
785
786
787 @Deprecated
788 public void setKeyManagerPassword(String keyManagerPassword)
789 {
790 _sslContextFactory.setKeyManagerPassword(keyManagerPassword);
791 }
792
793
794 @Deprecated
795 public void setTrustStorePassword(String trustStorePassword)
796 {
797 _sslContextFactory.setTrustStorePassword(trustStorePassword);
798 }
799
800
801 @Deprecated
802 public String getKeyStoreType()
803 {
804 return _sslContextFactory.getKeyStoreType();
805 }
806
807
808 @Deprecated
809 public void setKeyStoreType(String keyStoreType)
810 {
811 _sslContextFactory.setKeyStoreType(keyStoreType);
812 }
813
814
815 @Deprecated
816 public String getTrustStoreType()
817 {
818 return _sslContextFactory.getTrustStoreType();
819 }
820
821
822 @Deprecated
823 public void setTrustStoreType(String trustStoreType)
824 {
825 _sslContextFactory.setTrustStoreType(trustStoreType);
826 }
827
828
829 @Deprecated
830 public String getKeyManagerAlgorithm()
831 {
832 return _sslContextFactory.getSslKeyManagerFactoryAlgorithm();
833 }
834
835
836 @Deprecated
837 public void setKeyManagerAlgorithm(String keyManagerAlgorithm)
838 {
839 _sslContextFactory.setSslKeyManagerFactoryAlgorithm(keyManagerAlgorithm);
840 }
841
842
843 @Deprecated
844 public String getTrustManagerAlgorithm()
845 {
846 return _sslContextFactory.getTrustManagerFactoryAlgorithm();
847 }
848
849
850 @Deprecated
851 public void setTrustManagerAlgorithm(String trustManagerAlgorithm)
852 {
853 _sslContextFactory.setTrustManagerFactoryAlgorithm(trustManagerAlgorithm);
854 }
855
856
857 @Deprecated
858 public String getProtocol()
859 {
860 return _sslContextFactory.getProtocol();
861 }
862
863
864 @Deprecated
865 public void setProtocol(String protocol)
866 {
867 _sslContextFactory.setProtocol(protocol);
868 }
869
870
871 @Deprecated
872 public String getProvider()
873 {
874 return _sslContextFactory.getProvider();
875 }
876
877
878 @Deprecated
879 public void setProvider(String provider)
880 {
881 setProvider(provider);
882 }
883
884
885 @Deprecated
886 public String getSecureRandomAlgorithm()
887 {
888 return _sslContextFactory.getSecureRandomAlgorithm();
889 }
890
891
892 @Deprecated
893 public void setSecureRandomAlgorithm(String secureRandomAlgorithm)
894 {
895 _sslContextFactory.setSecureRandomAlgorithm(secureRandomAlgorithm);
896 }
897
898 private static class LocalQueuedThreadPool extends QueuedThreadPool
899 {
900 }
901 }