| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.connector.http; |
|---|
| 34 | |
|---|
| 35 | import java.io.ByteArrayInputStream; |
|---|
| 36 | import java.io.PrintWriter; |
|---|
| 37 | import java.security.KeyStore; |
|---|
| 38 | import java.util.HashSet; |
|---|
| 39 | import java.util.Iterator; |
|---|
| 40 | import java.util.Set; |
|---|
| 41 | import java.util.logging.Level; |
|---|
| 42 | import java.util.logging.Logger; |
|---|
| 43 | |
|---|
| 44 | import javax.resource.NotSupportedException; |
|---|
| 45 | import javax.resource.ResourceException; |
|---|
| 46 | import javax.resource.spi.ConnectionEvent; |
|---|
| 47 | import javax.resource.spi.ConnectionEventListener; |
|---|
| 48 | import javax.resource.spi.ConnectionRequestInfo; |
|---|
| 49 | import javax.resource.spi.LocalTransaction; |
|---|
| 50 | import javax.resource.spi.ManagedConnection; |
|---|
| 51 | import javax.resource.spi.ManagedConnectionMetaData; |
|---|
| 52 | import javax.security.auth.Subject; |
|---|
| 53 | import javax.transaction.xa.XAResource; |
|---|
| 54 | |
|---|
| 55 | import org.jcoderz.commons.connector.ConnectorConfiguration; |
|---|
| 56 | import org.jcoderz.commons.connector.http.transport.ConnectorContext; |
|---|
| 57 | import org.jcoderz.commons.connector.http.transport.HttpClientConnection; |
|---|
| 58 | import org.jcoderz.commons.connector.http.transport.HttpClientConnectionImpl; |
|---|
| 59 | import org.jcoderz.commons.connector.http.transport.HttpConnectionException; |
|---|
| 60 | import org.jcoderz.commons.connector.http.transport.HttpConnectorEventListener; |
|---|
| 61 | import org.jcoderz.commons.connector.http.transport.HttpRequestResponseHeader; |
|---|
| 62 | import org.jcoderz.commons.types.Url; |
|---|
| 63 | import org.jcoderz.commons.util.Constants; |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * This class defines the managed connection maintained by the application |
|---|
| 70 | * server and triggered via the non-managed connection. |
|---|
| 71 | * |
|---|
| 72 | */ |
|---|
| 73 | public class HttpManagedConnectionImpl |
|---|
| 74 | implements ManagedConnection |
|---|
| 75 | { |
|---|
| 76 | /** Class name for use in logging. */ |
|---|
| 77 | private static final String CLASSNAME |
|---|
| 78 | = HttpManagedConnectionImpl.class.getName(); |
|---|
| 79 | /** Logger in use. */ |
|---|
| 80 | private static final Logger logger |
|---|
| 81 | = Logger.getLogger(CLASSNAME); |
|---|
| 82 | /** Indicated if the managed connection is established. */ |
|---|
| 83 | private boolean mConnectionAssociated = false; |
|---|
| 84 | /** Indicates if the managed connection is opened. */ |
|---|
| 85 | private boolean mConnectionOpened = false; |
|---|
| 86 | /** The logger used for the implementation of the interface |
|---|
| 87 | ManagedConnection.*/ |
|---|
| 88 | private PrintWriter mPrintWriter; |
|---|
| 89 | /** Object containing all app server references that must be notified |
|---|
| 90 | used for the implementation of the interface ManagedConnection. */ |
|---|
| 91 | private final Set mEventListeners = new HashSet(); |
|---|
| 92 | /** Connection handle associated with this ManagedConnection. */ |
|---|
| 93 | private HttpConnectionImpl mConnectionHandle; |
|---|
| 94 | /** The factory which have created this object. */ |
|---|
| 95 | private final HttpManagedConnectionFactoryImpl mManagedConnectionFactory; |
|---|
| 96 | /** The connection specification used to trigger the creation of |
|---|
| 97 | this physical connection. */ |
|---|
| 98 | private HttpConnectionRequestInfo mConnectionRequestInfo; |
|---|
| 99 | /** Physical Connection */ |
|---|
| 100 | private HttpClientConnection mConnection; |
|---|
| 101 | /** Flag indicates that the physical connection must be closed. */ |
|---|
| 102 | private boolean mPhysicalCloseRequired = false; |
|---|
| 103 | /** URL the physical Connection will connect to. */ |
|---|
| 104 | private Url mUrl; |
|---|
| 105 | private final ConnectorConfiguration mConfig; |
|---|
| 106 | |
|---|
| 107 | private int mConnectTimeout; |
|---|
| 108 | private int mReadTimeout; |
|---|
| 109 | private String mKeyAlias; |
|---|
| 110 | private String mKeyAliasPassword; |
|---|
| 111 | private KeyStore mKeyStore = null; |
|---|
| 112 | private KeyStore mTrustStore = null; |
|---|
| 113 | |
|---|
| 114 | private HttpConnectorEventListener mHttpEventListener; |
|---|
| 115 | private ConnectorContext mConnectorContext; |
|---|
| 116 | private HttpRequestResponseHeader mRequestResponseHeader; |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * Constructor. |
|---|
| 121 | * @param mcf the HttpManagedConnectionFactoryImpl associated with |
|---|
| 122 | * @param cri the ConnectionRequestInfo specifying the requested connection |
|---|
| 123 | */ |
|---|
| 124 | public HttpManagedConnectionImpl ( |
|---|
| 125 | HttpManagedConnectionFactoryImpl mcf, |
|---|
| 126 | ConnectionRequestInfo cri) |
|---|
| 127 | { |
|---|
| 128 | final String methodName = "Constructor"; |
|---|
| 129 | if (logger.isLoggable(Level.FINER)) |
|---|
| 130 | { |
|---|
| 131 | logger.entering(CLASSNAME, methodName, cri); |
|---|
| 132 | } |
|---|
| 133 | mConfig = ConfigurationFactory.getConfiguration(); |
|---|
| 134 | // set the ManagedConnectionFactory creating this object |
|---|
| 135 | mManagedConnectionFactory = mcf; |
|---|
| 136 | // set the obtained ConnectionRequestInfo |
|---|
| 137 | mConnectionRequestInfo = (HttpConnectionRequestInfo) cri; |
|---|
| 138 | mConnectionAssociated = false; |
|---|
| 139 | mConnectionOpened = false; |
|---|
| 140 | mConnectionHandle = null; |
|---|
| 141 | if (mConnectionRequestInfo != null) |
|---|
| 142 | { |
|---|
| 143 | configure(mConnectionRequestInfo); |
|---|
| 144 | } |
|---|
| 145 | if (logger.isLoggable(Level.FINER)) |
|---|
| 146 | { |
|---|
| 147 | logger.exiting(CLASSNAME, methodName); |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | /* |
|---|
| 152 | * Methods defined in interface |
|---|
| 153 | * javax.resource.spi.ManagedConnection |
|---|
| 154 | * |
|---|
| 155 | * void addConnectionEventListener |
|---|
| 156 | * (ConnectionEventListener listener) |
|---|
| 157 | * void associateConnection |
|---|
| 158 | * (java.lang.Object connection) |
|---|
| 159 | * void cleanup () |
|---|
| 160 | * void destroy () |
|---|
| 161 | * java.lang.Object getConnection (Subject subject, |
|---|
| 162 | * ConnectionRequestInfo info) |
|---|
| 163 | * LocalTransaction getLocalTransaction () |
|---|
| 164 | * java.io.PrintWriter getLogWriter () |
|---|
| 165 | * void setLogWriter (java.io.PrintWriter out) |
|---|
| 166 | * ManagedConnectionMetaData getMetaData () |
|---|
| 167 | * javax.transaction.xa.XAResource getXAResource () |
|---|
| 168 | * void removeConnectionEventListener |
|---|
| 169 | * (ConnectionEventListener listener |
|---|
| 170 | */ |
|---|
| 171 | |
|---|
| 172 | /** {@inheritDoc} */ |
|---|
| 173 | public void addConnectionEventListener (ConnectionEventListener listener) |
|---|
| 174 | { |
|---|
| 175 | mEventListeners.add(listener); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | /** {@inheritDoc} */ |
|---|
| 179 | public void associateConnection (Object connection) |
|---|
| 180 | { |
|---|
| 181 | final String methodName = "associateConnection"; |
|---|
| 182 | if (logger.isLoggable(Level.FINER)) |
|---|
| 183 | { |
|---|
| 184 | logger.entering(CLASSNAME, methodName); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | // the managed connection is already configured and the |
|---|
| 188 | // physical connection created anew if necessary |
|---|
| 189 | // - the check is obsolete |
|---|
| 190 | //checkIfDestroyed(); |
|---|
| 191 | if (mConnectionHandle != null) |
|---|
| 192 | { |
|---|
| 193 | if (logger.isLoggable(Level.WARNING)) |
|---|
| 194 | { |
|---|
| 195 | final String messageText = "connection still have a handle"; |
|---|
| 196 | logger.warning(messageText); |
|---|
| 197 | } |
|---|
| 198 | mConnectionHandle.disassociateManagedConnection(true); |
|---|
| 199 | } |
|---|
| 200 | final HttpConnectionImpl usercon = (HttpConnectionImpl) connection; |
|---|
| 201 | usercon.associateManagedConnection(this); |
|---|
| 202 | mConnectionHandle = usercon; |
|---|
| 203 | if (logger.isLoggable(Level.FINER)) |
|---|
| 204 | { |
|---|
| 205 | logger.exiting(CLASSNAME, methodName); |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | /** {@inheritDoc} */ |
|---|
| 210 | public void cleanup () |
|---|
| 211 | { |
|---|
| 212 | final String methodName = "cleanup"; |
|---|
| 213 | if (logger.isLoggable(Level.FINER)) |
|---|
| 214 | { |
|---|
| 215 | logger.entering(CLASSNAME, methodName); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | // if makes no difference if the physical connection is closed |
|---|
| 219 | // or not cause reusing the managed connection creates a new |
|---|
| 220 | // physical connection inside "configure" if necessary |
|---|
| 221 | // - the check is obsolete |
|---|
| 222 | //checkIfDestroyed(); |
|---|
| 223 | // Invalidate the connection handle and |
|---|
| 224 | // release the reference. |
|---|
| 225 | if (mConnectionAssociated) |
|---|
| 226 | { |
|---|
| 227 | mConnectionHandle.disassociateManagedConnection(true); |
|---|
| 228 | mConnectionAssociated = false; |
|---|
| 229 | mConnectionHandle = null; |
|---|
| 230 | } |
|---|
| 231 | if (logger.isLoggable(Level.FINER)) |
|---|
| 232 | { |
|---|
| 233 | logger.exiting(CLASSNAME, methodName); |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | /** {@inheritDoc} */ |
|---|
| 238 | public void destroy () |
|---|
| 239 | { |
|---|
| 240 | final String methodName = "destroy"; |
|---|
| 241 | if (logger.isLoggable(Level.FINER)) |
|---|
| 242 | { |
|---|
| 243 | logger.entering(CLASSNAME, methodName); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | // the container is not the only one allowed to close the physical |
|---|
| 247 | // connection - the managed connection close it itself in |
|---|
| 248 | // case of an "Connection : Close" attribute received in the |
|---|
| 249 | // response message |
|---|
| 250 | // - the check is obsolete |
|---|
| 251 | //checkIfDestroyed(); |
|---|
| 252 | |
|---|
| 253 | // trace message |
|---|
| 254 | if (logger.isLoggable(Level.FINE)) |
|---|
| 255 | { |
|---|
| 256 | final String messageText |
|---|
| 257 | = "destroy PHYSICAL CONNECTION (" + this + ") to " |
|---|
| 258 | + mConnectionRequestInfo; |
|---|
| 259 | logger.log(Level.FINE, messageText); |
|---|
| 260 | } |
|---|
| 261 | // |
|---|
| 262 | disconnect(); |
|---|
| 263 | |
|---|
| 264 | if (logger.isLoggable(Level.FINER)) |
|---|
| 265 | { |
|---|
| 266 | logger.exiting(CLASSNAME, methodName); |
|---|
| 267 | } |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | /** {@inheritDoc} */ |
|---|
| 271 | public Object getConnection (Subject subject, ConnectionRequestInfo cri) |
|---|
| 272 | { |
|---|
| 273 | final String methodName = "getConnection"; |
|---|
| 274 | if (logger.isLoggable(Level.FINER)) |
|---|
| 275 | { |
|---|
| 276 | logger.entering(CLASSNAME, methodName); |
|---|
| 277 | } |
|---|
| 278 | mPhysicalCloseRequired = false; |
|---|
| 279 | mConnectionRequestInfo = (HttpConnectionRequestInfo) cri; |
|---|
| 280 | |
|---|
| 281 | // There must not an association to a connection handle when this |
|---|
| 282 | // method is called. |
|---|
| 283 | if (mConnectionAssociated) |
|---|
| 284 | { |
|---|
| 285 | final String errorText |
|---|
| 286 | = "managed connection already associated to a connection handle"; |
|---|
| 287 | throw new IllegalStateException(errorText); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | // if not connected then configure and connect |
|---|
| 291 | if (!mConnectionOpened) |
|---|
| 292 | { |
|---|
| 293 | // trace message |
|---|
| 294 | if (logger.isLoggable(Level.FINE)) |
|---|
| 295 | { |
|---|
| 296 | final String messageText |
|---|
| 297 | = "establishing NEW PHYSICAL CONNECTION (" + this + ") to " |
|---|
| 298 | + mConnectionRequestInfo; |
|---|
| 299 | logger.fine(messageText); |
|---|
| 300 | } |
|---|
| 301 | // |
|---|
| 302 | if (mConnectionRequestInfo != null) |
|---|
| 303 | { |
|---|
| 304 | configure(mConnectionRequestInfo); |
|---|
| 305 | } |
|---|
| 306 | // we connect while sending.... |
|---|
| 307 | } |
|---|
| 308 | else |
|---|
| 309 | { |
|---|
| 310 | // trace message |
|---|
| 311 | if (logger.isLoggable(Level.FINE)) |
|---|
| 312 | { |
|---|
| 313 | final String messageText |
|---|
| 314 | = "reusing PHYSICAL CONNECTION (" + this + ") to " |
|---|
| 315 | + mConnectionRequestInfo; |
|---|
| 316 | logger.fine(messageText); |
|---|
| 317 | } |
|---|
| 318 | // |
|---|
| 319 | } |
|---|
| 320 | mConnectionAssociated = true; |
|---|
| 321 | mConnectionHandle = new HttpConnectionImpl(this); |
|---|
| 322 | return mConnectionHandle; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | /** {@inheritDoc} */ |
|---|
| 326 | public LocalTransaction getLocalTransaction () |
|---|
| 327 | throws ResourceException |
|---|
| 328 | { |
|---|
| 329 | final String messageText = "Local transaction not supported"; |
|---|
| 330 | throw new NotSupportedException(messageText); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | /** {@inheritDoc} */ |
|---|
| 334 | public PrintWriter getLogWriter () |
|---|
| 335 | { |
|---|
| 336 | return mPrintWriter; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | /** {@inheritDoc} */ |
|---|
| 340 | public void setLogWriter (PrintWriter out) |
|---|
| 341 | { |
|---|
| 342 | mPrintWriter = out; |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | /** {@inheritDoc} */ |
|---|
| 346 | public ManagedConnectionMetaData getMetaData () |
|---|
| 347 | { |
|---|
| 348 | // it is not specified that the physical connection have to be |
|---|
| 349 | // open whilst that call |
|---|
| 350 | final HttpManagedConnectionMetaData managedConnectionMetaData |
|---|
| 351 | = new HttpManagedConnectionMetaData(this); |
|---|
| 352 | return managedConnectionMetaData; |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | /** {@inheritDoc} */ |
|---|
| 356 | public XAResource getXAResource () |
|---|
| 357 | throws ResourceException |
|---|
| 358 | { |
|---|
| 359 | final String messageText = "XA transaction not supported"; |
|---|
| 360 | throw new NotSupportedException(messageText); |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | /** {@inheritDoc} */ |
|---|
| 364 | public void removeConnectionEventListener (ConnectionEventListener listener) |
|---|
| 365 | { |
|---|
| 366 | mEventListeners.remove(listener); |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | /* |
|---|
| 371 | * Implementation of interface |
|---|
| 372 | * javax.resource.spi.HttpManagedConnection |
|---|
| 373 | * |
|---|
| 374 | * finished. |
|---|
| 375 | */ |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | /** |
|---|
| 379 | * Performs a physical connection close. |
|---|
| 380 | */ |
|---|
| 381 | private void disconnect () |
|---|
| 382 | { |
|---|
| 383 | final String methodName = "disconnect"; |
|---|
| 384 | if (logger.isLoggable(Level.FINER)) |
|---|
| 385 | { |
|---|
| 386 | logger.entering(CLASSNAME, methodName); |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | // if "disconnect" is triggered by container side |
|---|
| 390 | // and the response contained a "Connection:Close" |
|---|
| 391 | // HTTP header the connection has been already |
|---|
| 392 | // closed by the Managed Connection |
|---|
| 393 | if (!mConnectionOpened && mPhysicalCloseRequired) |
|---|
| 394 | { |
|---|
| 395 | if (logger.isLoggable(Level.FINE)) |
|---|
| 396 | { |
|---|
| 397 | final String messageText = "already disconnected (" + this + ")" |
|---|
| 398 | + " due to \'Connection:Close\' in response"; |
|---|
| 399 | logger.fine(messageText); |
|---|
| 400 | } |
|---|
| 401 | } |
|---|
| 402 | else |
|---|
| 403 | { |
|---|
| 404 | // close the physical socket connection |
|---|
| 405 | mConnection.closeConnection(); |
|---|
| 406 | mConnectionOpened = false; |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | if (logger.isLoggable(Level.FINER)) |
|---|
| 410 | { |
|---|
| 411 | logger.exiting(CLASSNAME, methodName); |
|---|
| 412 | } |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | /** |
|---|
| 416 | * Configures the physical connection. |
|---|
| 417 | * Uses configuration parameter from the |
|---|
| 418 | * {@link org.jcoderz.commons.connector.http.HttpConnectionSpec |
|---|
| 419 | * HttpConnectionSpec}. |
|---|
| 420 | * This method is called inside the |
|---|
| 421 | * {@link #HttpManagedConnection(HttpManagedConnectionFactoryImpl, |
|---|
| 422 | * ConnectionRequestInfo) constructor} |
|---|
| 423 | * and |
|---|
| 424 | * {@link #getConnection(Subject, ConnectionRequestInfo ) getConnection(..)} |
|---|
| 425 | * method. |
|---|
| 426 | * |
|---|
| 427 | * @param cri the Connection Request Info |
|---|
| 428 | */ |
|---|
| 429 | private final void configure (HttpConnectionRequestInfo cri) |
|---|
| 430 | { |
|---|
| 431 | final String methodName = "configure"; |
|---|
| 432 | if (logger.isLoggable(Level.FINER)) |
|---|
| 433 | { |
|---|
| 434 | logger.entering(CLASSNAME, methodName); |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | String messageText; |
|---|
| 438 | String errorText; |
|---|
| 439 | |
|---|
| 440 | if (mConnectionOpened) |
|---|
| 441 | { |
|---|
| 442 | messageText = "Unable to configure an already opened connection"; |
|---|
| 443 | // trace message |
|---|
| 444 | if (logger.isLoggable(Level.WARNING)) |
|---|
| 445 | { |
|---|
| 446 | logger.warning(messageText); |
|---|
| 447 | } |
|---|
| 448 | // |
|---|
| 449 | } |
|---|
| 450 | else |
|---|
| 451 | { |
|---|
| 452 | if (cri.getConnectionSpec() != null) |
|---|
| 453 | { |
|---|
| 454 | // set configuration parameters obtained in the cri |
|---|
| 455 | // for the connection |
|---|
| 456 | mUrl = cri.getConnectionSpec().getUrl(); |
|---|
| 457 | |
|---|
| 458 | mConnectTimeout = mConfig.getConnectTimeoutInMilliSeconds(); |
|---|
| 459 | mReadTimeout = mConfig.getReadTimeoutInMilliSeconds(); |
|---|
| 460 | |
|---|
| 461 | mKeyAlias = mConfig.getSslKeyAlias(); |
|---|
| 462 | mKeyAliasPassword = mConfig.getSslKeyAliasPassword(); |
|---|
| 463 | //FIXME: mKeyStore = KeyStoreLocator.getKeyStore(); |
|---|
| 464 | //FIXME: mTrustStore = KeyStoreLocator.getTrustStore(); |
|---|
| 465 | |
|---|
| 466 | if (mConnection == null) |
|---|
| 467 | { |
|---|
| 468 | // trace message |
|---|
| 469 | if (logger.isLoggable(Level.FINER)) |
|---|
| 470 | { |
|---|
| 471 | messageText = "creating new HTTPConnection"; |
|---|
| 472 | logger.log(Level.FINER, messageText); |
|---|
| 473 | } |
|---|
| 474 | // |
|---|
| 475 | mConnection = new HttpClientConnectionImpl(); |
|---|
| 476 | } |
|---|
| 477 | |
|---|
| 478 | mConnection.initSsl( |
|---|
| 479 | mKeyStore, mTrustStore, mKeyAlias, mKeyAliasPassword); |
|---|
| 480 | mConnection.establishConnection( |
|---|
| 481 | mUrl.toString(), mConnectTimeout, mReadTimeout); |
|---|
| 482 | } |
|---|
| 483 | else |
|---|
| 484 | { |
|---|
| 485 | errorText = "connection specification is not set"; |
|---|
| 486 | throw new IllegalStateException(errorText); |
|---|
| 487 | } |
|---|
| 488 | } |
|---|
| 489 | if (logger.isLoggable(Level.FINER)) |
|---|
| 490 | { |
|---|
| 491 | logger.exiting(CLASSNAME, methodName); |
|---|
| 492 | } |
|---|
| 493 | } |
|---|
| 494 | |
|---|
| 495 | /** |
|---|
| 496 | * This method is used by the |
|---|
| 497 | * {@link org.jcoderz.commons.connector.http.HttpConnectionImpl |
|---|
| 498 | * HttpConnection Implementation} class to send and receive an HTTP |
|---|
| 499 | * request/response using the commons-httpclient library. |
|---|
| 500 | * |
|---|
| 501 | * @param message the message body to send via HTTP. |
|---|
| 502 | * @return the response received. |
|---|
| 503 | * @throws HttpConnectionException for all types of connection failures |
|---|
| 504 | */ |
|---|
| 505 | public byte[] sendAndReceive (byte[] message) |
|---|
| 506 | throws HttpConnectionException |
|---|
| 507 | { |
|---|
| 508 | final String methodName = "sendAndReceive(byte[])"; |
|---|
| 509 | if (logger.isLoggable(Level.FINER)) |
|---|
| 510 | { |
|---|
| 511 | logger.entering(CLASSNAME, methodName); |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | // set the event listener for "requestSend" / "responseReceived" |
|---|
| 515 | mConnection.setEventListener(mHttpEventListener, mConnectorContext); |
|---|
| 516 | |
|---|
| 517 | // add Content-Length and Connection to the request header |
|---|
| 518 | // will not be overwritten if already set |
|---|
| 519 | getRequestResponseHeader().addRequestHeader("Content-Length", |
|---|
| 520 | String.valueOf(message.length)); |
|---|
| 521 | getRequestResponseHeader().addRequestHeader("Connection", |
|---|
| 522 | "Keep-Alive"); |
|---|
| 523 | |
|---|
| 524 | // set all given request header pairs |
|---|
| 525 | mConnection.setRequestResponseHeader(getRequestResponseHeader()); |
|---|
| 526 | |
|---|
| 527 | // set the given message body |
|---|
| 528 | mConnection.setRequestBody(new ByteArrayInputStream(message)); |
|---|
| 529 | |
|---|
| 530 | mConnectionOpened = true; |
|---|
| 531 | |
|---|
| 532 | //execute the httpclient connection |
|---|
| 533 | mConnection.execute(); |
|---|
| 534 | |
|---|
| 535 | // obtain the response |
|---|
| 536 | final byte[] responseBytes = mConnection.getResponseBody(); |
|---|
| 537 | |
|---|
| 538 | if (isCloseConnectionRequired()) |
|---|
| 539 | { |
|---|
| 540 | mPhysicalCloseRequired = true; |
|---|
| 541 | } |
|---|
| 542 | |
|---|
| 543 | mConnection.releaseConnection(); |
|---|
| 544 | if (logger.isLoggable(Level.FINER)) |
|---|
| 545 | { |
|---|
| 546 | logger.exiting(CLASSNAME, methodName); |
|---|
| 547 | } |
|---|
| 548 | return responseBytes; |
|---|
| 549 | } |
|---|
| 550 | |
|---|
| 551 | /** |
|---|
| 552 | * Sets the header set for sending and validate whilst receiving. |
|---|
| 553 | * |
|---|
| 554 | * @param header header to set and to validate |
|---|
| 555 | */ |
|---|
| 556 | public void setRequestResponseHeader (HttpRequestResponseHeader header) |
|---|
| 557 | { |
|---|
| 558 | mRequestResponseHeader = header; |
|---|
| 559 | } |
|---|
| 560 | |
|---|
| 561 | /** |
|---|
| 562 | * Sets the event listener used for SLA logs. |
|---|
| 563 | * This listener will be set at the |
|---|
| 564 | * |
|---|
| 565 | * @param listener the listener to set |
|---|
| 566 | * @param context the context to set |
|---|
| 567 | */ |
|---|
| 568 | public void setEventListener (HttpConnectorEventListener listener, |
|---|
| 569 | ConnectorContext context) |
|---|
| 570 | { |
|---|
| 571 | mHttpEventListener = listener; |
|---|
| 572 | mConnectorContext = context; |
|---|
| 573 | } |
|---|
| 574 | |
|---|
| 575 | /** |
|---|
| 576 | * This method is used by the |
|---|
| 577 | * {@link HttpConnectionHelper HttpConnectionHelper} |
|---|
| 578 | * class to disassociate the connection handle |
|---|
| 579 | * from this HttpManagedConnection instance. |
|---|
| 580 | * |
|---|
| 581 | * @param conn the connection handle to be disassociated |
|---|
| 582 | * @throws javax.resource.spi.IllegalStateException if the connection handle |
|---|
| 583 | * to disassociate is not associated |
|---|
| 584 | */ |
|---|
| 585 | void disassociateConnection (HttpConnectionImpl conn) |
|---|
| 586 | throws IllegalStateException |
|---|
| 587 | { |
|---|
| 588 | final String methodName = "disassociateConnection"; |
|---|
| 589 | if (logger.isLoggable(Level.FINER)) |
|---|
| 590 | { |
|---|
| 591 | logger.entering(CLASSNAME, methodName); |
|---|
| 592 | } |
|---|
| 593 | |
|---|
| 594 | if (conn != mConnectionHandle) |
|---|
| 595 | { |
|---|
| 596 | final String errorText = "Connection handle was not created by " |
|---|
| 597 | + "THIS HttpManagedConnection " + conn; |
|---|
| 598 | final IllegalStateException ise = new IllegalStateException(errorText); |
|---|
| 599 | // trace message |
|---|
| 600 | if (logger.isLoggable(Level.FINE)) |
|---|
| 601 | { |
|---|
| 602 | logger.log(Level.FINE, errorText); |
|---|
| 603 | } |
|---|
| 604 | if (logger.isLoggable(Level.FINER)) |
|---|
| 605 | { |
|---|
| 606 | logger.throwing(CLASSNAME, methodName, ise); |
|---|
| 607 | } |
|---|
| 608 | // |
|---|
| 609 | throw ise; |
|---|
| 610 | } |
|---|
| 611 | mConnectionAssociated = false; |
|---|
| 612 | mConnectionHandle = null; |
|---|
| 613 | if (mPhysicalCloseRequired) |
|---|
| 614 | { |
|---|
| 615 | disconnect(); |
|---|
| 616 | } |
|---|
| 617 | else |
|---|
| 618 | { |
|---|
| 619 | mConnection.releaseConnection(); |
|---|
| 620 | } |
|---|
| 621 | |
|---|
| 622 | if (logger.isLoggable(Level.FINER)) |
|---|
| 623 | { |
|---|
| 624 | logger.exiting(CLASSNAME, methodName); |
|---|
| 625 | } |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | /** |
|---|
| 629 | * Gets the connection specification used to trigger the creation |
|---|
| 630 | * of this physical connection. |
|---|
| 631 | * |
|---|
| 632 | * @return HttpConnectionRequestInfo - the requested object specifying |
|---|
| 633 | * the requested connection |
|---|
| 634 | */ |
|---|
| 635 | ConnectionRequestInfo getConnectionRequestInfo () |
|---|
| 636 | { |
|---|
| 637 | return mConnectionRequestInfo; |
|---|
| 638 | } |
|---|
| 639 | |
|---|
| 640 | /** |
|---|
| 641 | * Notify the registered set of listeners when an application component |
|---|
| 642 | * closes a connection handle. The application server uses this connection |
|---|
| 643 | * close event to make a decision on whether or not to put this |
|---|
| 644 | * HttpManagedConnection instance back into the connection pool. |
|---|
| 645 | * |
|---|
| 646 | * @param connectionHandle the connection handle performing a close |
|---|
| 647 | */ |
|---|
| 648 | void notifyAboutConnectionClosed ( |
|---|
| 649 | HttpConnectionImpl connectionHandle) |
|---|
| 650 | { |
|---|
| 651 | final String methodName = "notifyAboutConnectionClosed"; |
|---|
| 652 | if (logger.isLoggable(Level.FINER)) |
|---|
| 653 | { |
|---|
| 654 | logger.entering(CLASSNAME, methodName); |
|---|
| 655 | } |
|---|
| 656 | |
|---|
| 657 | if (mConnectionAssociated) |
|---|
| 658 | { |
|---|
| 659 | if (logger.isLoggable(Level.WARNING)) |
|---|
| 660 | { |
|---|
| 661 | final String messageText |
|---|
| 662 | = "HttpManagedConnection SHOULD have disassociated before"; |
|---|
| 663 | logger.warning(messageText); |
|---|
| 664 | } |
|---|
| 665 | |
|---|
| 666 | mConnectionAssociated = false; |
|---|
| 667 | mConnectionHandle = null; |
|---|
| 668 | } |
|---|
| 669 | final ConnectionEvent ce = new ConnectionEvent( |
|---|
| 670 | this, ConnectionEvent.CONNECTION_CLOSED); |
|---|
| 671 | ce.setConnectionHandle(connectionHandle); |
|---|
| 672 | final Iterator it = mEventListeners.iterator(); |
|---|
| 673 | while (it.hasNext()) |
|---|
| 674 | { |
|---|
| 675 | final ConnectionEventListener listener |
|---|
| 676 | = (ConnectionEventListener) it.next(); |
|---|
| 677 | listener.connectionClosed(ce); |
|---|
| 678 | } |
|---|
| 679 | if (logger.isLoggable(Level.FINER)) |
|---|
| 680 | { |
|---|
| 681 | logger.exiting(CLASSNAME, methodName); |
|---|
| 682 | } |
|---|
| 683 | } |
|---|
| 684 | |
|---|
| 685 | /** |
|---|
| 686 | * Notify the registered listeners of the occurrence of a physical |
|---|
| 687 | * connection-related error. The event notification happens just before |
|---|
| 688 | * a resource adapter throws an exception to the application component |
|---|
| 689 | * using the connection handle. |
|---|
| 690 | * The connectionErrorOccurred method indicates that the associated |
|---|
| 691 | * HttpManagedConnection instance is now invalid and unusable. |
|---|
| 692 | * The application server handles the connection error event |
|---|
| 693 | * notification by initiating application server-specific cleanup |
|---|
| 694 | * (for example, removing HttpManagedConnection instance from the |
|---|
| 695 | * connection pool) and then calling HttpManagedConnection.destroy |
|---|
| 696 | * method to destroy the physical connection. |
|---|
| 697 | * |
|---|
| 698 | * @param e the exception indication a connection error |
|---|
| 699 | * @param connectionHandle the connection handle notifying the |
|---|
| 700 | * connection error |
|---|
| 701 | */ |
|---|
| 702 | void notifyAboutConnectionErrorOccurred ( |
|---|
| 703 | Exception e, |
|---|
| 704 | HttpConnectionImpl connectionHandle) |
|---|
| 705 | { |
|---|
| 706 | final String methodName = "notifyAboutConnectionErrorOccurred"; |
|---|
| 707 | if (logger.isLoggable(Level.FINER)) |
|---|
| 708 | { |
|---|
| 709 | logger.entering(CLASSNAME, methodName); |
|---|
| 710 | } |
|---|
| 711 | |
|---|
| 712 | if (mConnectionAssociated) |
|---|
| 713 | { |
|---|
| 714 | if (logger.isLoggable(Level.WARNING)) |
|---|
| 715 | { |
|---|
| 716 | final String messageText |
|---|
| 717 | = "HttpManagedConnection SHOULD have disassociated before"; |
|---|
| 718 | logger.warning(messageText); |
|---|
| 719 | } |
|---|
| 720 | } |
|---|
| 721 | final ConnectionEvent ce = new ConnectionEvent( |
|---|
| 722 | this, ConnectionEvent.CONNECTION_ERROR_OCCURRED, e); |
|---|
| 723 | if (connectionHandle != null) |
|---|
| 724 | { |
|---|
| 725 | ce.setConnectionHandle(connectionHandle); |
|---|
| 726 | } |
|---|
| 727 | |
|---|
| 728 | final Iterator it = mEventListeners.iterator(); |
|---|
| 729 | while (it.hasNext()) |
|---|
| 730 | { |
|---|
| 731 | final ConnectionEventListener listener |
|---|
| 732 | = (ConnectionEventListener) it.next(); |
|---|
| 733 | listener.connectionErrorOccurred(ce); |
|---|
| 734 | } |
|---|
| 735 | if (logger.isLoggable(Level.FINER)) |
|---|
| 736 | { |
|---|
| 737 | logger.exiting(CLASSNAME, methodName); |
|---|
| 738 | } |
|---|
| 739 | } |
|---|
| 740 | |
|---|
| 741 | /** |
|---|
| 742 | * Returns true if the underlying physical connection is currently open. |
|---|
| 743 | * |
|---|
| 744 | * @return boolean - true if connection is open, else false |
|---|
| 745 | */ |
|---|
| 746 | boolean isOpen () |
|---|
| 747 | { |
|---|
| 748 | return mConnectionOpened; |
|---|
| 749 | } |
|---|
| 750 | |
|---|
| 751 | /** |
|---|
| 752 | * Checks the response header if a physical connection close is required. |
|---|
| 753 | */ |
|---|
| 754 | private boolean isCloseConnectionRequired () |
|---|
| 755 | { |
|---|
| 756 | boolean result = false; |
|---|
| 757 | final String connectionValue |
|---|
| 758 | = mConnection.getResponseHeader("Connection"); |
|---|
| 759 | if (connectionValue != null |
|---|
| 760 | && connectionValue.toLowerCase( |
|---|
| 761 | Constants.SYSTEM_LOCALE).equals("close")) |
|---|
| 762 | { |
|---|
| 763 | logger.fine("\'Connection\' attribute in response header is \'" |
|---|
| 764 | + connectionValue |
|---|
| 765 | + "\' physical connection will be closed"); |
|---|
| 766 | result = true; |
|---|
| 767 | } |
|---|
| 768 | return result; |
|---|
| 769 | } |
|---|
| 770 | |
|---|
| 771 | protected Url getUrl () |
|---|
| 772 | { |
|---|
| 773 | return mUrl; |
|---|
| 774 | } |
|---|
| 775 | |
|---|
| 776 | /** |
|---|
| 777 | * Gets the managed connection factory of this managed connection. |
|---|
| 778 | * |
|---|
| 779 | * @return ManagedPaymentProtocolConnectionFactory the factory in use |
|---|
| 780 | */ |
|---|
| 781 | public HttpManagedConnectionFactoryImpl getManagedConnectionFactory () |
|---|
| 782 | { |
|---|
| 783 | return mManagedConnectionFactory; |
|---|
| 784 | } |
|---|
| 785 | |
|---|
| 786 | private HttpRequestResponseHeader getRequestResponseHeader () |
|---|
| 787 | { |
|---|
| 788 | if (mRequestResponseHeader == null) |
|---|
| 789 | { |
|---|
| 790 | mRequestResponseHeader = new HttpRequestResponseHeader(); |
|---|
| 791 | } |
|---|
| 792 | return mRequestResponseHeader; |
|---|
| 793 | } |
|---|
| 794 | |
|---|
| 795 | /** |
|---|
| 796 | * Gets the string representation of the HttpManagedConnection. |
|---|
| 797 | * |
|---|
| 798 | * @return the string representation |
|---|
| 799 | */ |
|---|
| 800 | public String toString () |
|---|
| 801 | { |
|---|
| 802 | final String result |
|---|
| 803 | = "HttpManagedConnectionImpl hashCode()=" + hashCode(); |
|---|
| 804 | return result; |
|---|
| 805 | } |
|---|
| 806 | } |
|---|