1 package org.apache.commons.jcs.auxiliary.remote.http.client;
2
3 import java.io.IOException;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import org.apache.commons.httpclient.HttpClient;
25 import org.apache.commons.httpclient.HttpMethod;
26 import org.apache.commons.httpclient.HttpState;
27 import org.apache.commons.httpclient.HttpVersion;
28 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
29 import org.apache.commons.httpclient.cookie.CookiePolicy;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33
34
35
36
37
38 public abstract class AbstractHttpClient
39 {
40
41 private MultiThreadedHttpConnectionManager connectionManager;
42
43
44 private HttpClient httpClient;
45
46
47 private RemoteHttpCacheAttributes remoteHttpCacheAttributes;
48
49
50 private static final Log log = LogFactory.getLog( AbstractHttpClient.class );
51
52
53
54
55
56
57
58 public AbstractHttpClient( RemoteHttpCacheAttributes remoteHttpCacheAttributes )
59 {
60 this.remoteHttpCacheAttributes = remoteHttpCacheAttributes;
61 this.connectionManager = new MultiThreadedHttpConnectionManager();
62 this.httpClient = new HttpClient(this.connectionManager);
63
64 configureClient();
65 }
66
67
68
69
70 protected void configureClient()
71 {
72 if ( getRemoteHttpCacheAttributes().getMaxConnectionsPerHost() > 0 )
73 {
74 this.connectionManager.getParams()
75 .setMaxTotalConnections(getRemoteHttpCacheAttributes().getMaxConnectionsPerHost());
76 this.connectionManager.getParams()
77 .setDefaultMaxConnectionsPerHost(getRemoteHttpCacheAttributes().getMaxConnectionsPerHost());
78 }
79
80 this.connectionManager.getParams().setSoTimeout( getRemoteHttpCacheAttributes().getSocketTimeoutMillis() );
81
82 String httpVersion = getRemoteHttpCacheAttributes().getHttpVersion();
83 if ( httpVersion != null )
84 {
85 if ( "1.1".equals( httpVersion ) )
86 {
87 this.httpClient.getParams().setParameter( "http.protocol.version", HttpVersion.HTTP_1_1 );
88 }
89 else if ( "1.0".equals( httpVersion ) )
90 {
91 this.httpClient.getParams().setParameter( "http.protocol.version", HttpVersion.HTTP_1_0 );
92 }
93 else
94 {
95 log.warn( "Unrecognized value for 'httpVersion': [" + httpVersion + "]" );
96 }
97 }
98
99 this.connectionManager.getParams()
100 .setConnectionTimeout(getRemoteHttpCacheAttributes().getConnectionTimeoutMillis());
101
102
103 this.httpClient.getParams().setCookiePolicy( CookiePolicy.IGNORE_COOKIES );
104 }
105
106
107
108
109
110
111
112
113 protected final void doWebserviceCall( HttpMethod post )
114 throws IOException
115 {
116 HttpState httpState = preProcessWebserviceCall( post );
117 this.httpClient.executeMethod( null, post, httpState );
118 postProcessWebserviceCall( post, httpState );
119 }
120
121
122
123
124
125
126
127
128 protected abstract HttpState preProcessWebserviceCall( HttpMethod post )
129 throws IOException;
130
131
132
133
134
135
136
137
138 protected abstract void postProcessWebserviceCall( HttpMethod post, HttpState httpState )
139 throws IOException;
140
141
142
143
144 protected RemoteHttpCacheAttributes getRemoteHttpCacheAttributes()
145 {
146 return remoteHttpCacheAttributes;
147 }
148 }