| 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 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.connector.http; |
| 34 | | | |
| 35 | | | import java.util.logging.Level; |
| 36 | | | import java.util.logging.Logger; |
| 37 | | | |
| 38 | | | import javax.resource.ResourceException; |
| 39 | | | |
| 40 | | | import org.jcoderz.commons.connector.ConnectionRequestFailedException; |
| 41 | | | import org.jcoderz.commons.connector.ConnectionResponseFailedException; |
| 42 | | | import org.jcoderz.commons.connector.ConnectionTimeoutErrorException; |
| 43 | | | import org.jcoderz.commons.connector.ConnectorConfiguration; |
| 44 | | | import org.jcoderz.commons.connector.ConnectorException; |
| 45 | | | import org.jcoderz.commons.connector.http.transport.ConnectorContext; |
| 46 | | | import org.jcoderz.commons.connector.http.transport |
| 47 | | | .HttpClientConnectionException; |
| 48 | | | import org.jcoderz.commons.connector.http.transport |
| 49 | | | .HttpConnectConnectionException; |
| 50 | | | import org.jcoderz.commons.connector.http.transport.HttpConnectionException; |
| 51 | | | import org.jcoderz.commons.connector.http.transport.HttpConnectorEventListener; |
| 52 | | | import org.jcoderz.commons.connector.http.transport.HttpEmptyResponseException; |
| 53 | | | import org.jcoderz.commons.connector.http.transport |
| 54 | | | .HttpInvalidResponseHeaderException; |
| 55 | | | import org.jcoderz.commons.connector.http.transport.HttpRequestResponseHeader; |
| 56 | | | import org.jcoderz.commons.connector.http.transport |
| 57 | | | .HttpServerConnectionException; |
| 58 | | | import org.jcoderz.commons.connector.http.transport |
| 59 | | | .HttpTimeoutConnectionException; |
| 60 | | | import org.jcoderz.commons.types.Url; |
| 61 | | | import org.jcoderz.commons.util.Assert; |
| 62 | | | |
| 63 | | | |
| 64 | | | |
| 65 | | | |
| 66 | | | |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | | |
| 71 | | | |
| 72 | | (1) | public final class HttpConnectionImpl |
| 73 | | | implements HttpConnectionExtended |
| 74 | | | { |
| 75 | | | |
| 76 | 0 | | private static final String CLASSNAME |
| 77 | | | = HttpConnectionImpl.class.getName(); |
| 78 | | | |
| 79 | 0 | | private static final Logger logger |
| 80 | | | = Logger.getLogger(CLASSNAME); |
| 81 | | | |
| 82 | | | private HttpManagedConnectionImpl mManagedConnection; |
| 83 | | | |
| 84 | | | |
| 85 | | | |
| 86 | 0 | | private boolean mClosedByManagedConnection = false; |
| 87 | | | |
| 88 | | | private int mRequiredDelay; |
| 89 | | | |
| 90 | 0 | | private boolean mIsRetryRequired = false; |
| 91 | | | private final ConnectorConfiguration mConfig; |
| 92 | | | |
| 93 | | | |
| 94 | | | |
| 95 | | | |
| 96 | | | |
| 97 | | | |
| 98 | | | @param |
| 99 | | | |
| 100 | | | public HttpConnectionImpl (HttpManagedConnectionImpl mc) |
| 101 | 0 | | { |
| 102 | 0 | | mManagedConnection = mc; |
| 103 | 0 | | mConfig = ConfigurationFactory.getConfiguration(); |
| 104 | 0 | | } |
| 105 | | | |
| 106 | | | {@inheritDoc} |
| 107 | | | public byte[] sendAndReceive (byte[] message) |
| 108 | | (2) | throws ResourceException, ConnectorException |
| 109 | | | { |
| 110 | 0 | | final String methodName = "sendAndReceive"; |
| 111 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 112 | | | { |
| 113 | 0 | | logger.entering(CLASSNAME, methodName); |
| 114 | | | } |
| 115 | 0 | | Assert.notNull(message, "message"); |
| 116 | | | |
| 117 | 0 | | mIsRetryRequired = false; |
| 118 | 0 | | byte[] result = null; |
| 119 | | | try |
| 120 | | | { |
| 121 | 0 | | result = getManagedConnection().sendAndReceive(message); |
| 122 | | | } |
| 123 | 0 | | catch (HttpClientConnectionException hce) |
| 124 | | | { |
| 125 | 0 | | handleClientConnectionException(hce); |
| 126 | | | } |
| 127 | 0 | | catch (HttpServerConnectionException hse) |
| 128 | | | { |
| 129 | 0 | | handleServerConnectionException(hse); |
| 130 | | | } |
| 131 | 0 | | catch (HttpConnectConnectionException hce) |
| 132 | | | { |
| 133 | 0 | | handleConnectConnectionException(hce); |
| 134 | | | } |
| 135 | 0 | | catch (HttpTimeoutConnectionException hte) |
| 136 | | | { |
| 137 | 0 | | handleTimeoutConnectionException(hte); |
| 138 | | | } |
| 139 | 0 | | catch (HttpInvalidResponseHeaderException hie) |
| 140 | | | { |
| 141 | 0 | | handleInvalidResponseHeaderException(hie); |
| 142 | | | } |
| 143 | 0 | | catch (HttpEmptyResponseException ere) |
| 144 | | | { |
| 145 | 0 | | handleEmptyResponseException(ere); |
| 146 | | | } |
| 147 | 0 | | catch (HttpConnectionException he) |
| 148 | | | { |
| 149 | 0 | | handleConnectionException(he); |
| 150 | 0 | | } |
| 151 | | | |
| 152 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 153 | | | { |
| 154 | 0 | | logger.exiting(CLASSNAME, methodName, result); |
| 155 | | | } |
| 156 | 0 | | return result; |
| 157 | | | } |
| 158 | | | |
| 159 | | | {@inheritDoc} |
| 160 | | | public void setRequestResponseHeader (HttpRequestResponseHeader header) |
| 161 | | | throws ResourceException |
| 162 | | | { |
| 163 | 0 | | getManagedConnection().setRequestResponseHeader(header); |
| 164 | 0 | | } |
| 165 | | | |
| 166 | | | {@inheritDoc} |
| 167 | | | public void setEventListener (HttpConnectorEventListener listener, |
| 168 | | | ConnectorContext context) |
| 169 | | | { |
| 170 | 0 | | getManagedConnection().setEventListener(listener, context); |
| 171 | 0 | | } |
| 172 | | | |
| 173 | | | private void handleClientConnectionException (HttpConnectionException ex) |
| 174 | | | throws ConnectionResponseFailedException |
| 175 | | | { |
| 176 | 0 | | logger.finer("Handling 'ClientConnectionException' " + ex.getMessage()); |
| 177 | 0 | | if (mConfig.getHttpClientErrorResendFlag()) |
| 178 | | | { |
| 179 | 0 | | mIsRetryRequired = true; |
| 180 | 0 | | mRequiredDelay = mConfig.getHttpClientErrorResendDelayInMilliSeconds(); |
| 181 | | | } |
| 182 | 0 | | final ConnectionResponseFailedException cre |
| 183 | | | = new ConnectionResponseFailedException(getUrl(), ex); |
| 184 | 0 | | invalidateManagedConnection(cre); |
| 185 | 0 | | throw cre; |
| 186 | | | } |
| 187 | | | |
| 188 | | | private void handleServerConnectionException (HttpConnectionException ex) |
| 189 | | | throws ConnectionResponseFailedException |
| 190 | | | { |
| 191 | 0 | | logger.finer("Handling 'ServerConnectionException' " + ex.getMessage()); |
| 192 | 0 | | if (mConfig.getHttpServerErrorResendFlag()) |
| 193 | | | { |
| 194 | 0 | | mIsRetryRequired = true; |
| 195 | 0 | | mRequiredDelay |
| 196 | | | = mConfig.getHttpServerErrorResendDelayInMilliSeconds(); |
| 197 | | | } |
| 198 | 0 | | final ConnectionResponseFailedException cre |
| 199 | | | = new ConnectionResponseFailedException(getUrl(), ex); |
| 200 | 0 | | invalidateManagedConnection(cre); |
| 201 | 0 | | throw cre; |
| 202 | | | } |
| 203 | | | |
| 204 | | | private void handleConnectConnectionException (HttpConnectionException ex) |
| 205 | | | throws ConnectionRequestFailedException |
| 206 | | | { |
| 207 | 0 | | logger.finer("Handling 'ConnectConnectionException' " + ex.getMessage()); |
| 208 | 0 | | final ConnectionRequestFailedException cre |
| 209 | | | = new ConnectionRequestFailedException(getUrl(), ex); |
| 210 | 0 | | invalidateManagedConnection(cre); |
| 211 | 0 | | throw cre; |
| 212 | | | } |
| 213 | | | |
| 214 | | | private void handleTimeoutConnectionException (HttpConnectionException ex) |
| 215 | | | throws ConnectionTimeoutErrorException |
| 216 | | | { |
| 217 | 0 | | logger.finer("Handling 'TimeoutConnectionException' " + ex.getMessage()); |
| 218 | 0 | | if (mConfig.getHttpReadTimeoutErrorResendFlag()) |
| 219 | | | { |
| 220 | 0 | | mIsRetryRequired = true; |
| 221 | 0 | | mRequiredDelay |
| 222 | | | = mConfig.getHttpReadTimeoutErrorResendDelayInMilliSeconds(); |
| 223 | | | } |
| 224 | 0 | | mIsRetryRequired = true; |
| 225 | 0 | | final StringBuffer previousFailure = new StringBuffer(); |
| 226 | 0 | | previousFailure.append(ex.toString()); |
| 227 | 0 | | final Throwable cause = ex.getCause(); |
| 228 | 0 | | if (cause != null) |
| 229 | | | { |
| 230 | 0 | | previousFailure.append(" caused by "); |
| 231 | 0 | | previousFailure.append(cause.toString()); |
| 232 | | | } |
| 233 | 0 | | final ConnectionTimeoutErrorException cte |
| 234 | | | = new ConnectionTimeoutErrorException( |
| 235 | | | getUrl(), previousFailure.toString(), ex); |
| 236 | 0 | | invalidateManagedConnection(cte); |
| 237 | 0 | | throw cte; |
| 238 | | | } |
| 239 | | | |
| 240 | | | private void handleInvalidResponseHeaderException ( |
| 241 | | | HttpConnectionException ex) |
| 242 | | | throws ConnectionResponseFailedException |
| 243 | | | { |
| 244 | 0 | | logger.finer("Handling 'InvalidResponseHeaderException' " |
| 245 | | | + ex.getMessage()); |
| 246 | 0 | | mIsRetryRequired = false; |
| 247 | 0 | | final ConnectionResponseFailedException cre |
| 248 | | | = new ConnectionResponseFailedException(getUrl(), ex); |
| 249 | 0 | | invalidateManagedConnection(cre); |
| 250 | 0 | | throw cre; |
| 251 | | | } |
| 252 | | | |
| 253 | | | private void handleEmptyResponseException ( |
| 254 | | | HttpConnectionException ex) |
| 255 | | | throws ConnectionResponseFailedException |
| 256 | | | { |
| 257 | 0 | | logger.finer("Handling 'HttpEmptyResponseException' " |
| 258 | | | + ex.getMessage()); |
| 259 | 0 | | mIsRetryRequired = false; |
| 260 | 0 | | final ConnectionResponseFailedException cre |
| 261 | | | = new ConnectionResponseFailedException(getUrl(), ex); |
| 262 | 0 | | invalidateManagedConnection(cre); |
| 263 | 0 | | throw cre; |
| 264 | | | } |
| 265 | | | |
| 266 | | | private void handleConnectionException (HttpConnectionException ex) |
| 267 | | | throws ConnectionRequestFailedException |
| 268 | | | { |
| 269 | | (3) | |
| 270 | | | |
| 271 | 0 | | logger.finer("Handling 'ConnectionException' " + ex.getMessage()); |
| 272 | 0 | | mIsRetryRequired = false; |
| 273 | 0 | | final ConnectionRequestFailedException cre |
| 274 | | | = new ConnectionRequestFailedException(getUrl(), ex); |
| 275 | 0 | | invalidateManagedConnection(cre); |
| 276 | 0 | | throw cre; |
| 277 | | | } |
| 278 | | | |
| 279 | | | {@inheritDoc} |
| 280 | | | public void close () |
| 281 | | | { |
| 282 | 0 | | final String methodName = "close"; |
| 283 | 0 | | logger.entering(CLASSNAME, methodName); |
| 284 | | | |
| 285 | | | |
| 286 | | | |
| 287 | | | |
| 288 | | | |
| 289 | | | |
| 290 | | | |
| 291 | | | |
| 292 | 0 | | if (mManagedConnection == null && mClosedByManagedConnection) |
| 293 | | | { |
| 294 | | | |
| 295 | | | |
| 296 | | | |
| 297 | 0 | | logger.finer("connection already closed by application server"); |
| 298 | | | |
| 299 | | | } |
| 300 | 0 | | else if (mManagedConnection == null && !mClosedByManagedConnection) |
| 301 | | | { |
| 302 | | | |
| 303 | 0 | | logger.finer("connection already closed"); |
| 304 | | | |
| 305 | | | } |
| 306 | | | else |
| 307 | | | { |
| 308 | | | |
| 309 | | | |
| 310 | 0 | | getManagedConnection().disassociateConnection(this); |
| 311 | | | |
| 312 | | | |
| 313 | 0 | | getManagedConnection().notifyAboutConnectionClosed(this); |
| 314 | | | |
| 315 | | | |
| 316 | | | |
| 317 | 0 | | disassociateManagedConnection(false); |
| 318 | | | } |
| 319 | 0 | | logger.exiting(CLASSNAME, methodName); |
| 320 | 0 | | } |
| 321 | | | |
| 322 | | | |
| 323 | | | |
| 324 | | | |
| 325 | | | @return |
| 326 | | | @throws |
| 327 | | | |
| 328 | | | |
| 329 | | | protected HttpManagedConnectionImpl getManagedConnection () |
| 330 | | | throws IllegalStateException |
| 331 | | | { |
| 332 | 0 | | if (mManagedConnection == null) |
| 333 | | | { |
| 334 | 0 | | final String errorText = "Connection handle is invalid."; |
| 335 | 0 | | final IllegalStateException ise = new IllegalStateException(errorText); |
| 336 | 0 | | throw ise; |
| 337 | | | } |
| 338 | 0 | | return mManagedConnection; |
| 339 | | | } |
| 340 | | | |
| 341 | | | |
| 342 | | | <p> |
| 343 | | | </p> |
| 344 | | | <p> |
| 345 | | | </p> |
| 346 | | | |
| 347 | | | @param |
| 348 | | | @throws |
| 349 | | | |
| 350 | | | protected void associateManagedConnection (HttpManagedConnectionImpl mc) |
| 351 | | | { |
| 352 | 0 | | mManagedConnection = mc; |
| 353 | 0 | | } |
| 354 | | | |
| 355 | | | |
| 356 | | | |
| 357 | | | |
| 358 | | | |
| 359 | | | |
| 360 | | | @param |
| 361 | | | |
| 362 | | | |
| 363 | | | public void disassociateManagedConnection (boolean closedByManagedConnection) |
| 364 | | | { |
| 365 | 0 | | if (closedByManagedConnection) |
| 366 | | | { |
| 367 | 0 | | logger.finest("disassociated by ManagedConnection"); |
| 368 | | | } |
| 369 | | | else |
| 370 | | | { |
| 371 | 0 | | logger.finest("disassociated by Connection"); |
| 372 | | | } |
| 373 | | | |
| 374 | 0 | | mClosedByManagedConnection = closedByManagedConnection; |
| 375 | 0 | | mManagedConnection = null; |
| 376 | 0 | | } |
| 377 | | | |
| 378 | | | |
| 379 | | | |
| 380 | | | |
| 381 | | | private void invalidateManagedConnection (final Exception cause) |
| 382 | | | { |
| 383 | 0 | | getManagedConnection().disassociateConnection(this); |
| 384 | 0 | | getManagedConnection().notifyAboutConnectionErrorOccurred( |
| 385 | | | cause, this); |
| 386 | 0 | | disassociateManagedConnection(false); |
| 387 | 0 | | } |
| 388 | | | |
| 389 | | | private Url getUrl () |
| 390 | | | { |
| 391 | 0 | | return getManagedConnection().getUrl(); |
| 392 | | | } |
| 393 | | | |
| 394 | | | |
| 395 | | | |
| 396 | | | |
| 397 | | | |
| 398 | | | @return |
| 399 | | | |
| 400 | | | |
| 401 | | | public int getRequiredDelayForRetries () |
| 402 | | | { |
| 403 | 0 | | return mRequiredDelay; |
| 404 | | | } |
| 405 | | | |
| 406 | | | |
| 407 | | | |
| 408 | | | |
| 409 | | | @return |
| 410 | | | |
| 411 | | | |
| 412 | | | public boolean isRetryRequired () |
| 413 | | | { |
| 414 | 0 | | return mIsRetryRequired; |
| 415 | | | } |
| 416 | | | } |