View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2005, 2010
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Roberto Pineiro, IBM, roberto.pineiro@us.ibm.com
12   * @author : Chung-hao Tan, IBM, chungtan@us.ibm.com
13   * 
14   * 
15   * Change History
16   * Flag     Date        Prog         Description
17   *------------------------------------------------------------------------------- 
18   * 1353168  2005-11-24  fiuczy       Possible NullPointerExcection in HttpClient.streamFinished()
19   * 1488924  2006-05-15  lupusalex    Intermittent connection loss
20   * 1535756  2006-08-07  lupusalex    Make code warning free
21   * 1565892  2006-11-28  lupusalex    Make SBLIM client JSR48 compliant
22   * 2003590  2008-06-30  blaschke-oss Change licensing from CPL to EPL
23   * 2204488  2008-10-28  raman_arora  Fix code to remove compiler warnings
24   * 2524131  2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
25   * 3026360  2010-07-07  blaschke-oss Handle unwritten fields
26   */
27  package org.metricshub.wbem.sblim.cimclient.internal.http;
28  
29  /*-
30   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
31   * WBEM Java Client
32   * ჻჻჻჻჻჻
33   * Copyright 2023 - 2025 MetricsHub
34   * ჻჻჻჻჻჻
35   * Licensed under the Apache License, Version 2.0 (the "License");
36   * you may not use this file except in compliance with the License.
37   * You may obtain a copy of the License at
38   *
39   *      http://www.apache.org/licenses/LICENSE-2.0
40   *
41   * Unless required by applicable law or agreed to in writing, software
42   * distributed under the License is distributed on an "AS IS" BASIS,
43   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44   * See the License for the specific language governing permissions and
45   * limitations under the License.
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   * Class HttpUrlConnection encapsulates a http connection
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  	 * Ctor.
75  	 *
76  	 * @param pUri
77  	 *            The host URI
78  	 * @param pHttpClientPool
79  	 *            The client pool
80  	 * @param pAuthHandler
81  	 *            The authentication handler
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  		// iLogger = GlobalProperties.getLogger();
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 	 * Closes the client pool
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 	 * Returns the http client
204 	 *
205 	 * @return The http client
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 	 * Resets the http client
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 	 * Enables/Disables the use of http 1.1
264 	 *
265 	 * @param pUse11
266 	 *            If <code>true</code> http 1.1 is enabled.
267 	 */
268 	public synchronized void useHttp11(boolean pUse11) {
269 		if (!this.iConnected) getClient();
270 		this.iHttpClient.useHttp11(pUse11);
271 	}
272 }