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 package org.metricshub.wbem.sblim.cimclient.internal.http;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 import java.io.IOException;
50 import java.io.InputStream;
51 import java.io.OutputStream;
52 import java.net.HttpURLConnection;
53 import java.net.SocketPermission;
54 import java.net.URI;
55 import java.security.Permission;
56 import org.metricshub.wbem.sblim.cimclient.internal.logging.LogAndTraceBroker;
57
58
59
60
61
62 public class HttpUrlConnection extends HttpURLConnection {
63 private boolean iConnected;
64
65 private HttpClient iHttpClient;
66
67 protected URI iUrl;
68
69 private HttpClientPool iHttpClientPool;
70
71 AuthorizationHandler iAuthHandler;
72
73
74
75
76
77
78
79
80
81
82
83 public HttpUrlConnection(URI pUri, HttpClientPool pHttpClientPool, AuthorizationHandler pAuthHandler) {
84 super(null);
85 this.iUrl = pUri;
86 this.iHttpClientPool = pHttpClientPool;
87 this.iAuthHandler = pAuthHandler;
88
89 }
90
91 @Override
92 public String toString() {
93 return (
94 "HttpUrlConnection=[url=" +
95 this.iUrl +
96 ",PoolSize=" +
97 this.iHttpClientPool.getNumberOfAvailableConnections() +
98 "," +
99 this.iAuthHandler +
100 "]"
101 );
102 }
103
104 @Override
105 public Permission getPermission() {
106 int port = this.iUrl.getPort();
107 port = port < 0 ? 80 : port;
108 String host = this.iUrl.getHost() + ":" + port;
109 Permission permission = new SocketPermission(host, "connect");
110 return permission;
111 }
112
113 @Override
114 public synchronized void connect() throws IOException {
115 if (!this.iConnected) getClient();
116 this.iHttpClient.connect();
117 }
118
119 private synchronized void getClient() {
120 if (this.iConnected) return;
121
122 this.iHttpClient = HttpClient.getClient(this.iUrl, this.iHttpClientPool, this.iAuthHandler);
123 this.iHttpClient.reset();
124 this.iConnected = true;
125 }
126
127 @Override
128 public synchronized void setRequestMethod(String pMethod) {
129 if (!this.iConnected) getClient();
130 this.iHttpClient.setRequestMethod(pMethod);
131 this.method = pMethod;
132 }
133
134 @Override
135 public synchronized void setRequestProperty(String key, String value) {
136 if (!this.iConnected) getClient();
137 this.iHttpClient.setRequestProperty(key, value);
138 }
139
140 @Override
141 public synchronized void disconnect() {
142 LogAndTraceBroker logger = LogAndTraceBroker.getBroker();
143 logger.entry();
144
145 if (this.iConnected) {
146 this.iConnected = false;
147 this.iHttpClient.disconnect();
148 if (this.iHttpClientPool != null) {
149 this.iHttpClientPool.removeConnectionFromPool(this.iHttpClient);
150 this.iHttpClient = null;
151 }
152 }
153 logger.exit();
154 }
155
156
157
158
159 public synchronized void close() {
160 LogAndTraceBroker logger = LogAndTraceBroker.getBroker();
161 logger.entry();
162
163 if (this.iHttpClientPool != null) {
164 synchronized (this.iHttpClientPool) {
165 this.iHttpClientPool.closePool();
166 this.iHttpClientPool = null;
167 }
168 }
169 logger.exit();
170 }
171
172 @Override
173 public synchronized InputStream getInputStream() throws IOException {
174 if (!this.iConnected) getClient();
175 try {
176 return this.iHttpClient.getInputStream();
177 } catch (RuntimeException e) {
178 disconnect();
179 throw e;
180 } catch (IOException e) {
181 disconnect();
182 throw e;
183 }
184 }
185
186 @Override
187 public synchronized OutputStream getOutputStream() throws RuntimeException {
188 if (!this.iConnected) getClient();
189 try {
190 return this.iHttpClient.getOutputStream();
191 } catch (RuntimeException e) {
192 disconnect();
193 throw e;
194 }
195 }
196
197 @Override
198 public boolean usingProxy() {
199 return false;
200 }
201
202
203
204
205
206
207 public synchronized HttpClient getHttpClient() {
208 if (!this.iConnected) getClient();
209 return this.iHttpClient;
210 }
211
212 @Override
213 public synchronized String getHeaderField(String name) {
214 if (!this.iConnected) getClient();
215 return this.iHttpClient.getHeaderField(name);
216 }
217
218 @Override
219 public synchronized String getHeaderFieldKey(int index) {
220 if (!this.iConnected) getClient();
221 return this.iHttpClient.getHeaderFieldName(index);
222 }
223
224 @Override
225 public synchronized String getHeaderField(int index) {
226 if (!this.iConnected) getClient();
227 return this.iHttpClient.getHeaderFieldValue(index);
228 }
229
230 @Override
231 public synchronized String getRequestProperty(String key) {
232 if (!this.iConnected) getClient();
233 return this.iHttpClient.getRequestProperty(key);
234 }
235
236 @Override
237 public synchronized String getRequestMethod() {
238 if (!this.iConnected) getClient();
239 return this.iHttpClient.getRequestMethod();
240 }
241
242
243
244
245 public synchronized void reset() {
246 if (!this.iConnected) getClient();
247 this.iHttpClient.reset();
248 }
249
250 @Override
251 public synchronized int getResponseCode() throws IOException {
252 if (!this.iConnected) getClient();
253 return this.iHttpClient.getResponseCode();
254 }
255
256 @Override
257 public synchronized String getResponseMessage() {
258 if (!this.iConnected) getClient();
259 return this.iHttpClient.getResponseMessage();
260 }
261
262
263
264
265
266
267
268 public synchronized void useHttp11(boolean pUse11) {
269 if (!this.iConnected) getClient();
270 this.iHttpClient.useHttp11(pUse11);
271 }
272 }