| 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.ArrayList; |
| 36 | | | import java.util.Iterator; |
| 37 | | | import java.util.List; |
| 38 | | | import java.util.logging.Level; |
| 39 | | | import java.util.logging.Logger; |
| 40 | | | |
| 41 | | | import javax.resource.ResourceException; |
| 42 | | | |
| 43 | | | import org.jcoderz.commons.InternalErrorException; |
| 44 | | | import org.jcoderz.commons.connector.ConnectionTimeoutErrorException; |
| 45 | | | import org.jcoderz.commons.connector.ConnectorConfiguration; |
| 46 | | | import org.jcoderz.commons.connector.ConnectorException; |
| 47 | | | import org.jcoderz.commons.connector.CreatingConnectorFailedException; |
| 48 | | | import org.jcoderz.commons.connector.http.transport.ConnectorContext; |
| 49 | | | import org.jcoderz.commons.connector.http.transport.HttpConnectorEventListener; |
| 50 | | | import org.jcoderz.commons.connector.http.transport.HttpRequestResponseHeader; |
| 51 | | | import org.jcoderz.commons.util.Assert; |
| 52 | | | |
| 53 | | | |
| 54 | | | |
| 55 | | | |
| 56 | | | |
| 57 | | | |
| 58 | | | |
| 59 | | | |
| 60 | | (1) | public final class HttpConnectionHelper |
| 61 | | | implements HttpConnection |
| 62 | | | { |
| 63 | | | |
| 64 | 0 | | private static final String CLASSNAME |
| 65 | | | = HttpConnectionHelper.class.getName(); |
| 66 | | | |
| 67 | 0 | | private static final Logger logger |
| 68 | | | = Logger.getLogger(CLASSNAME); |
| 69 | | | |
| 70 | | | |
| 71 | | | private final HttpConnectionFactoryImpl mConnectionFactoryImpl; |
| 72 | | | |
| 73 | | | private final HttpConnectionSpec mConnectionSpec; |
| 74 | | | |
| 75 | | | private HttpConnectionExtended mConnection; |
| 76 | | | |
| 77 | | | private int mRequiredDelay; |
| 78 | | | |
| 79 | 0 | | private boolean mIsRetryRequired = false; |
| 80 | | | private final ConnectorConfiguration mConfig; |
| 81 | | | |
| 82 | | | private final int mAmountOfTries; |
| 83 | 0 | | private HttpRequestResponseHeader mRequestResponseHeader = null; |
| 84 | | | |
| 85 | | | private HttpConnectorEventListener mEventListener; |
| 86 | | | private ConnectorContext mListenerContext; |
| 87 | | | |
| 88 | | | |
| 89 | | | |
| 90 | | | |
| 91 | | | |
| 92 | | | |
| 93 | | | @param |
| 94 | | | @param |
| 95 | | | |
| 96 | | | public HttpConnectionHelper ( |
| 97 | | | HttpConnectionFactory cf, HttpConnectionSpec cs) |
| 98 | 0 | | { |
| 99 | 0 | | Assert.notNull(cs, "cs"); |
| 100 | 0 | | mConnectionFactoryImpl = (HttpConnectionFactoryImpl) cf; |
| 101 | 0 | | mConnectionSpec = cs; |
| 102 | | | |
| 103 | 0 | | mConfig = ConfigurationFactory.getConfiguration(); |
| 104 | 0 | | mAmountOfTries = mConfig.getAmountOfTriesForwardingRequest(); |
| 105 | 0 | | } |
| 106 | | | |
| 107 | | | {@inheritDoc} |
| 108 | | | public byte[] sendAndReceive (byte[] message) |
| 109 | | (2) | throws ResourceException, ConnectorException |
| 110 | | | { |
| 111 | 0 | | final String methodName = "sendAndReceive"; |
| 112 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 113 | | | { |
| 114 | 0 | | logger.entering(CLASSNAME, methodName); |
| 115 | | | } |
| 116 | 0 | | ConnectorException caughtException = null; |
| 117 | | | |
| 118 | 0 | | mIsRetryRequired = false; |
| 119 | 0 | | byte[] response = null; |
| 120 | 0 | | int tryNumber = 0; |
| 121 | 0 | | List collectedExceptions = null; |
| 122 | | | |
| 123 | 0 | | fireBeforeSend(); |
| 124 | | | |
| 125 | | | do |
| 126 | | | { |
| 127 | | | try |
| 128 | | | { |
| 129 | 0 | | tryNumber++; |
| 130 | 0 | | response = process(message); |
| 131 | | | } |
| 132 | 0 | | catch (ConnectorException ce) |
| 133 | | | { |
| 134 | 0 | | if (collectedExceptions == null) |
| 135 | | | { |
| 136 | 0 | | collectedExceptions = new ArrayList(); |
| 137 | | | } |
| 138 | 0 | | caughtException = ce; |
| 139 | 0 | | if (mIsRetryRequired && tryNumber < mAmountOfTries) |
| 140 | | | { |
| 141 | 0 | | collectedExceptions.add(caughtException); |
| 142 | 0 | | logForRetry(tryNumber, caughtException); |
| 143 | 0 | | caughtException = null; |
| 144 | 0 | | sleepForDelay(); |
| 145 | | | } |
| 146 | 0 | | } |
| 147 | | | } |
| 148 | | | |
| 149 | | | |
| 150 | | | while (response == null |
| 151 | | | && tryNumber < mAmountOfTries |
| 152 | 0 | | && caughtException == null); |
| 153 | | | |
| 154 | 0 | | fireAfterReceive(tryNumber, response); |
| 155 | | | |
| 156 | 0 | | assertRetries(tryNumber, caughtException, collectedExceptions); |
| 157 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 158 | | | { |
| 159 | 0 | | logger.exiting(CLASSNAME, methodName); |
| 160 | | | } |
| 161 | 0 | | return response; |
| 162 | | | } |
| 163 | | | |
| 164 | | | private void assertRetries ( |
| 165 | | | int tryNumber, |
| 166 | | | ConnectorException caughtException, |
| 167 | | | List collectedExceptions) |
| 168 | | | throws ConnectorException |
| 169 | | | { |
| 170 | 0 | | if (tryNumber >= mAmountOfTries) |
| 171 | | | { |
| 172 | 0 | | collectedExceptions.add(caughtException); |
| 173 | 0 | | final ConnectorException ex = createFinalTimeoutException( |
| 174 | | | collectedExceptions); |
| 175 | 0 | | throw ex; |
| 176 | | | } |
| 177 | 0 | | else if (caughtException != null) |
| 178 | | | { |
| 179 | | | |
| 180 | 0 | | throw caughtException; |
| 181 | | | } |
| 182 | 0 | | } |
| 183 | | | |
| 184 | | | |
| 185 | | | |
| 186 | | | |
| 187 | | | |
| 188 | | | @param |
| 189 | | | @return |
| 190 | | | @throws |
| 191 | | | |
| 192 | | | @throws |
| 193 | | | |
| 194 | | | private byte[] process (byte[] message) |
| 195 | | (3) | throws ResourceException, ConnectorException |
| 196 | | | { |
| 197 | 0 | | final String methodName = "process"; |
| 198 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 199 | | | { |
| 200 | 0 | | logger.entering(CLASSNAME, methodName); |
| 201 | | | } |
| 202 | | | byte[] response; |
| 203 | | | try |
| 204 | | | { |
| 205 | 0 | | response = getConnection().sendAndReceive(message); |
| 206 | | | } |
| 207 | 0 | | catch (ConnectorException ce) |
| 208 | | | { |
| 209 | 0 | | mIsRetryRequired = mConnection.isRetryRequired(); |
| 210 | 0 | | mRequiredDelay = mConnection.getRequiredDelayForRetries(); |
| 211 | 0 | | mConnection = null; |
| 212 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 213 | | | { |
| 214 | 0 | | logger.throwing(CLASSNAME, methodName, ce); |
| 215 | | | } |
| 216 | 0 | | throw ce; |
| 217 | | | } |
| 218 | 0 | | catch (ResourceException re) |
| 219 | | | { |
| 220 | 0 | | if (mConnection == null) |
| 221 | | | { |
| 222 | 0 | | mIsRetryRequired = true; |
| 223 | 0 | | mRequiredDelay |
| 224 | | | = mConfig.getConnectionErrorRetryDelayInMilliSeconds(); |
| 225 | 0 | | final CreatingConnectorFailedException rqe |
| 226 | | | = new CreatingConnectorFailedException( |
| 227 | | | mConnectionSpec.getUrl(), re); |
| 228 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 229 | | | { |
| 230 | 0 | | logger.throwing(CLASSNAME, methodName, rqe); |
| 231 | | | } |
| 232 | 0 | | throw rqe; |
| 233 | | | } |
| 234 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 235 | | | { |
| 236 | 0 | | logger.throwing(CLASSNAME, methodName, re); |
| 237 | | | } |
| 238 | 0 | | throw re; |
| 239 | 0 | | } |
| 240 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 241 | | | { |
| 242 | 0 | | logger.exiting(CLASSNAME, methodName); |
| 243 | | | } |
| 244 | 0 | | return response; |
| 245 | | | } |
| 246 | | | |
| 247 | | | {@inheritDoc} |
| 248 | | | public void setEventListener (HttpConnectorEventListener listener, |
| 249 | | | ConnectorContext context) |
| 250 | | | throws ResourceException |
| 251 | | | { |
| 252 | 0 | | mEventListener = listener; |
| 253 | 0 | | mListenerContext = context; |
| 254 | 0 | | getConnection().setEventListener(listener, context); |
| 255 | 0 | | } |
| 256 | | | |
| 257 | | | {@inheritDoc} |
| 258 | | | public void setRequestResponseHeader (HttpRequestResponseHeader header) |
| 259 | | | throws ResourceException |
| 260 | | | { |
| 261 | 0 | | mRequestResponseHeader = header; |
| 262 | 0 | | if (mConnection != null) |
| 263 | | | { |
| 264 | 0 | | mConnection.setRequestResponseHeader(mRequestResponseHeader); |
| 265 | | | } |
| 266 | 0 | | } |
| 267 | | | |
| 268 | | | {@inheritDoc} |
| 269 | | | public void close () |
| 270 | | | { |
| 271 | 0 | | if (mConnection != null) |
| 272 | | | { |
| 273 | 0 | | mConnection.close(); |
| 274 | | | } |
| 275 | 0 | | } |
| 276 | | | |
| 277 | | | private void fireBeforeSend () |
| 278 | | | { |
| 279 | 0 | | if (mEventListener != null) |
| 280 | | | { |
| 281 | 0 | | mEventListener.requestSendWithRetry(mListenerContext); |
| 282 | | | } |
| 283 | 0 | | } |
| 284 | | | |
| 285 | | | private void fireAfterReceive (int retries, byte[] response) |
| 286 | | | { |
| 287 | 0 | | if (mEventListener != null) |
| 288 | | | { |
| 289 | 0 | | mEventListener.responseReceivedAfterRetry( |
| 290 | | | retries, response, mListenerContext); |
| 291 | | | } |
| 292 | 0 | | } |
| 293 | | | |
| 294 | | | |
| 295 | | | |
| 296 | | | |
| 297 | | | @return |
| 298 | | | @throws |
| 299 | | | |
| 300 | | | |
| 301 | | | private HttpConnection getConnection () |
| 302 | | | throws ResourceException |
| 303 | | | { |
| 304 | 0 | | final String methodName = "getConnection"; |
| 305 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 306 | | | { |
| 307 | 0 | | logger.entering(CLASSNAME, methodName); |
| 308 | | | } |
| 309 | 0 | | if (mConnection == null) |
| 310 | | | { |
| 311 | 0 | | mConnection = (HttpConnectionExtended) mConnectionFactoryImpl. |
| 312 | | | getConnectionHandle(mConnectionSpec); |
| 313 | 0 | | if (mRequestResponseHeader != null) |
| 314 | | | { |
| 315 | 0 | | mConnection.setRequestResponseHeader(mRequestResponseHeader); |
| 316 | | | } |
| 317 | | | } |
| 318 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 319 | | | { |
| 320 | 0 | | logger.exiting(CLASSNAME, methodName, mConnection); |
| 321 | | | } |
| 322 | 0 | | return mConnection; |
| 323 | | | } |
| 324 | | | |
| 325 | | | |
| 326 | | | |
| 327 | | | @param |
| 328 | | | @param |
| 329 | | | |
| 330 | | | private void logForRetry (int tryNumber, ConnectorException ce) |
| 331 | | | { |
| 332 | 0 | | if (logger.isLoggable(Level.FINE)) |
| 333 | | | { |
| 334 | 0 | | final String messageText = "Will retry to send request " |
| 335 | | | + " after sleeping " + mRequiredDelay + " millis." |
| 336 | | | + "Resend caused by an '" + ce |
| 337 | | | + "'. This is try number " + tryNumber + "."; |
| 338 | 0 | | logger.fine(messageText); |
| 339 | | | } |
| 340 | 0 | | } |
| 341 | | | |
| 342 | | | |
| 343 | | | |
| 344 | | | @param |
| 345 | | | |
| 346 | | | |
| 347 | | | private void sleepForDelay () |
| 348 | | | { |
| 349 | | | try |
| 350 | | | { |
| 351 | 0 | | Thread.sleep(mRequiredDelay); |
| 352 | | | } |
| 353 | 0 | | catch (InterruptedException e) |
| 354 | | | { |
| 355 | 0 | | throw new InternalErrorException( |
| 356 | | | "Interrupt while sleeping for delay between connection retries", |
| 357 | | | e); |
| 358 | 0 | | } |
| 359 | 0 | | } |
| 360 | | | |
| 361 | | | |
| 362 | | | |
| 363 | | | |
| 364 | | | |
| 365 | | | private ConnectorException createFinalTimeoutException (List exceptions) |
| 366 | | | { |
| 367 | | | |
| 368 | | | |
| 369 | | | |
| 370 | | | ConnectionTimeoutErrorException result; |
| 371 | 0 | | if (exceptions != null) |
| 372 | | | { |
| 373 | 0 | | final Iterator i = exceptions.iterator(); |
| 374 | 0 | | int pos = 0; |
| 375 | 0 | | final StringBuffer failures = new StringBuffer(); |
| 376 | 0 | | while (i.hasNext()) |
| 377 | | | { |
| 378 | 0 | | pos++; |
| 379 | 0 | | final Exception e = (Exception) i.next(); |
| 380 | 0 | | failures.append("TRY_"); |
| 381 | 0 | | failures.append(pos); |
| 382 | 0 | | failures.append("_EXCEPTION_WAS "); |
| 383 | 0 | | failures.append(String.valueOf(e)); |
| 384 | 0 | (4) | failures.append('\n'); |
| 385 | 0 | | failures.append("stacktrace:"); |
| 386 | 0 | | failures.append(getStackTrace(e, null)); |
| 387 | 0 | | failures.append('\n'); |
| 388 | 0 | | } |
| 389 | 0 | | result = new ConnectionTimeoutErrorException( |
| 390 | | | mConnectionSpec.getUrl(), failures.toString()); |
| 391 | 0 | | } |
| 392 | | | else |
| 393 | | | { |
| 394 | 0 | | result = new ConnectionTimeoutErrorException( |
| 395 | | | mConnectionSpec.getUrl(), null); |
| 396 | | | } |
| 397 | 0 | | return result; |
| 398 | | | } |
| 399 | | | |
| 400 | | | private String getStackTrace (Throwable ex, StringBuffer result) |
| 401 | | | { |
| 402 | 0 | | final StackTraceElement[] stack = ex.getStackTrace(); |
| 403 | | | final StringBuffer buffer; |
| 404 | 0 | | if (result == null) |
| 405 | | | { |
| 406 | 0 | | buffer = new StringBuffer(); |
| 407 | | | } |
| 408 | | | else |
| 409 | | | { |
| 410 | 0 | | buffer = result; |
| 411 | 0 | (5) | buffer.append('\n'); |
| 412 | 0 | | buffer.append("Caused by:"); |
| 413 | 0 | | buffer.append(ex.toString()); |
| 414 | | | } |
| 415 | 0 | | buffer.append('\n'); |
| 416 | 0 | | int ix = 0; |
| 417 | 0 | | while (ix < stack.length) |
| 418 | | | { |
| 419 | 0 | | final StackTraceElement stackElement = stack[ix]; |
| 420 | 0 | | buffer.append(stackElement.toString()); |
| 421 | 0 | | buffer.append('\n'); |
| 422 | 0 | | ix++; |
| 423 | 0 | | } |
| 424 | 0 | | final Throwable cause = ex.getCause(); |
| 425 | 0 | | if (cause != null) |
| 426 | | | { |
| 427 | 0 | | getStackTrace(cause, buffer); |
| 428 | | | } |
| 429 | 0 | | return buffer.toString(); |
| 430 | | | } |
| 431 | | | |
| 432 | | | } |