1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package net.codesmarts.log4j;
32
33 import org.apache.commons.httpclient.Credentials;
34 import org.apache.commons.httpclient.HttpClient;
35 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
36 import org.apache.commons.httpclient.UsernamePasswordCredentials;
37
38 /***
39 * Base class for creating BugReport appenders that file bugs to a web based bug tracking system
40 * @author Fred McCann
41 */
42 public abstract class AbstractHttpBugReportAppender extends AbstractBugReportAppender {
43
44 /***
45 * Connection manager for http clients
46 */
47 private static MultiThreadedHttpConnectionManager connectionManager =
48 new MultiThreadedHttpConnectionManager();
49
50 /***
51 * username for HTTP authentication
52 */
53 private String authenticationUsername = null;
54
55 /***
56 * password for HTTP authentication
57 */
58 private String authenticationPassword = null;
59
60 /***
61 * realm for HTTP authentication
62 */
63 private String authenticationRealm = null;
64
65 /***
66 * host for HTTP authentication
67 */
68 private String authenticationHost = null;
69
70 /***
71 * host for HTTP authentication
72 */
73 private boolean useAuthentication = false;
74
75 /***
76 * initialize appender
77 */
78 public void init() {
79
80 if (authenticationUsername!=null || authenticationPassword!=null ||
81 authenticationRealm!=null || authenticationHost!=null)
82 useAuthentication=true;
83
84 if (useAuthentication) {
85 boolean haveAllOpts=true;
86
87 if (authenticationUsername==null) {
88 haveAllOpts=false;
89 getErrorHandler().error("Authentication Username not provided, disabling HTTP authentication.");
90 }
91 if (authenticationPassword==null) {
92 haveAllOpts=false;
93 getErrorHandler().error("Authentication Password not provided, disabling HTTP authentication.");
94 }
95 if (authenticationHost==null) {
96 haveAllOpts=false;
97 getErrorHandler().error("Authentication Host not provided, disabling HTTP authentication.");
98 }
99
100 if (useAuthentication && !haveAllOpts)
101 useAuthentication=false;
102 }
103
104 }
105
106 /***
107 * Get an HttpClient
108 * @return Returns the client.
109 */
110 public HttpClient getHttpClient() {
111 HttpClient client = new HttpClient(connectionManager);
112
113 if (useAuthentication) {
114 Credentials credentials =
115 new UsernamePasswordCredentials(authenticationUsername,authenticationPassword);
116 client.getState().setCredentials(authenticationRealm, authenticationHost, credentials);
117 }
118
119 return client;
120 }
121
122 /***
123 * Enable checking of stale connections
124 * @param enabled
125 */
126 public void setConnectionStaleCheckingEnabled(boolean enabled) {
127 connectionManager.setConnectionStaleCheckingEnabled(enabled);
128 }
129
130 /***
131 * set Maximum number of connections allowed per host
132 * @param connections
133 */
134 public void setMaxConnectionsPerHost(int connections) {
135 connectionManager.setMaxConnectionsPerHost(connections);
136 }
137
138 /***
139 * set maximum number of allowed connections
140 * @param total
141 */
142 public void setMaxTotalConnections(int max) {
143 connectionManager.setMaxTotalConnections(max);
144 }
145
146 /***
147 * Get the HTTP authentication password
148 * @return Returns the authenticationPassword.
149 */
150 public String getAuthenticationPassword() {
151 return authenticationPassword;
152 }
153
154 /***
155 * Set the HTTP authentication password
156 * @param authenticationPassword The authenticationPassword to set.
157 */
158 public void setAuthenticationPassword(String authenticationPassword) {
159 this.authenticationPassword = authenticationPassword;
160 }
161
162 /***
163 * Get the HTTP authentication username
164 * @return Returns the authenticationUsername.
165 */
166 public String getAuthenticationUsername() {
167 return authenticationUsername;
168 }
169
170 /***
171 * Set the HTTP authentication username
172 * @param authenticationUsername The authenticationUsername to set.
173 */
174 public void setAuthenticationUsername(String authenticationUsername) {
175 this.authenticationUsername = authenticationUsername;
176 }
177
178
179 /***
180 * Get the host for HTTP authentication
181 * @return Returns the authenticationHost.
182 */
183 public String getAuthenticationHost() {
184 return authenticationHost;
185 }
186
187 /***
188 * Set the host for HTTP authentication
189 * @param authenticationHost The authenticationHost to set.
190 */
191 public void setAuthenticationHost(String authenticationHost) {
192 this.authenticationHost = authenticationHost;
193 }
194
195 /***
196 * Get the realm for HTTP authentication
197 * @return Returns the authenticationRealm.
198 */
199 public String getAuthenticationRealm() {
200 return authenticationRealm;
201 }
202
203 /***
204 * Set the realm for HTTP authentication
205 * @param authenticationRealm The authenticationRealm to set.
206 */
207 public void setAuthenticationRealm(String authenticationRealm) {
208 this.authenticationRealm = authenticationRealm;
209 }
210 }