| 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.transport; |
| 34 | | | |
| 35 | | | import java.io.IOException; |
| 36 | | | import java.io.InputStream; |
| 37 | | | import java.net.ConnectException; |
| 38 | | | import java.security.KeyStore; |
| 39 | | | import java.util.HashMap; |
| 40 | | | import java.util.Iterator; |
| 41 | | | import java.util.Map; |
| 42 | | | import java.util.logging.Level; |
| 43 | | | import java.util.logging.Logger; |
| 44 | | | |
| 45 | | | import org.apache.commons.httpclient.Header; |
| 46 | | | import org.apache.commons.httpclient.HostConfiguration; |
| 47 | | | import org.apache.commons.httpclient.HttpClient; |
| 48 | | | import org.apache.commons.httpclient.HttpConnection; |
| 49 | | | import org.apache.commons.httpclient.HttpConnectionManager; |
| 50 | | | import org.apache.commons.httpclient.HttpException; |
| 51 | | | import org.apache.commons.httpclient.SimpleHttpConnectionManager; |
| 52 | | | import org.apache.commons.httpclient.URI; |
| 53 | | | import org.apache.commons.httpclient.URIException; |
| 54 | | | import org.apache.commons.httpclient.methods.InputStreamRequestEntity; |
| 55 | | | import org.apache.commons.httpclient.methods.PostMethod; |
| 56 | | | import org.apache.commons.httpclient.params.HttpConnectionManagerParams; |
| 57 | | | import org.apache.commons.httpclient.protocol.Protocol; |
| 58 | | | import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; |
| 59 | | | import org.jcoderz.commons.ArgumentMalformedException; |
| 60 | | | import org.jcoderz.commons.util.Assert; |
| 61 | | | import org.jcoderz.commons.util.Constants; |
| 62 | | | import org.jcoderz.commons.util.HexUtil; |
| 63 | | | import org.jcoderz.commons.util.StringUtil; |
| 64 | | | |
| 65 | | | |
| 66 | | | |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | | |
| 71 | | | |
| 72 | 100 | (1) | public class HttpClientConnectionImpl |
| 73 | | | implements HttpClientConnection |
| 74 | | | { |
| 75 | | | |
| 76 | 75 | | private static final String CLASSNAME |
| 77 | | | = HttpClientConnectionImpl.class.getName(); |
| 78 | | | |
| 79 | 100 | | private static final Logger logger |
| 80 | | | = Logger.getLogger(CLASSNAME); |
| 81 | | | |
| 82 | | | private static final int DEFAULT_SSL_PORT = 443; |
| 83 | | | |
| 84 | | | private static final String LINE_FEED = "\n"; |
| 85 | | | |
| 86 | | | private static final int HTTP_RESULT_OK = 200; |
| 87 | | | |
| 88 | | | private static final int HTTP_REDIRECT = 300; |
| 89 | | | |
| 90 | | | private static final int HTTP_BAD_REQUEST = 400; |
| 91 | | | |
| 92 | | | private static final int HTTP_INTERNAL_SERVER_ERROR = 500; |
| 93 | | | |
| 94 | | | private HttpClient mHttpClient; |
| 95 | | | |
| 96 | | | private HostConfiguration mHostConfiguration; |
| 97 | | | |
| 98 | | | private PostMethod mPostMethod; |
| 99 | | | |
| 100 | | | |
| 101 | | | |
| 102 | 100 | | private InputStream mRequestBodyInputStream = null; |
| 103 | | | |
| 104 | 100 | | private int mRequestContentLength = 0; |
| 105 | | | |
| 106 | | | |
| 107 | 100 | | private boolean mIsRequestContentLengthSet = false; |
| 108 | | | |
| 109 | 100 | | private String mRequestContentType = null; |
| 110 | | | |
| 111 | | | |
| 112 | 100 | | private HttpConnectionState mState |
| 113 | | | = HttpConnectionState.CONNECTION_NOT_ESTABLISHED; |
| 114 | | | |
| 115 | | | |
| 116 | 100 | | private String mKeyStoreFilename = null; |
| 117 | | | |
| 118 | 100 | | private String mKeyStorePassword = null; |
| 119 | | | |
| 120 | | | |
| 121 | 100 | | private String mTrustStoreFilename = null; |
| 122 | | | |
| 123 | 100 | | private String mTrustStorePassword = null; |
| 124 | | | |
| 125 | 100 | | private KeyStore mKeyStore = null; |
| 126 | 100 | | private KeyStore mTrustStore = null; |
| 127 | | | |
| 128 | 100 | | private String mKeyAlias = null; |
| 129 | | | |
| 130 | 100 | | private String mKeyAliasPassword = null; |
| 131 | | | |
| 132 | | | |
| 133 | 100 | | private String mPath = null; |
| 134 | | | |
| 135 | 100 | | private HttpConnectorEventListener mHttpEventListener = null; |
| 136 | 100 | | private ConnectorContext mConnectorContext = null; |
| 137 | 100 | | private HttpRequestResponseHeader mRequestResponseHeader = null; |
| 138 | | | |
| 139 | | | {@inheritDoc} |
| 140 | | | public void establishConnection ( |
| 141 | | | String uriAsString, |
| 142 | | | int connectTimeout, |
| 143 | | | int readTimeout) |
| 144 | | | { |
| 145 | 100 | | Assert.notNull(uriAsString, "uriAsString"); |
| 146 | 100 | | mHostConfiguration = new HostConfiguration(); |
| 147 | 100 | | mPostMethod = new PostMethod(); |
| 148 | | | |
| 149 | | | URI uri; |
| 150 | | | try |
| 151 | | | { |
| 152 | 100 | | uri = new URI(uriAsString, false); |
| 153 | 100 | | mPath = uri.getPathQuery(); |
| 154 | 100 | | mPostMethod.setPath(mPath); |
| 155 | | | |
| 156 | 100 | | final String scheme = uri.getScheme(); |
| 157 | 100 | (2) | if (scheme != null && scheme.toLowerCase(Constants.SYSTEM_LOCALE). |
| 158 | | | equals("https")) |
| 159 | | | { |
| 160 | 0 | | SslSocketFactory sslFactory = null; |
| 161 | 0 | | if (mKeyStore != null && mTrustStore != null) |
| 162 | | | { |
| 163 | | | |
| 164 | 0 | | sslFactory = new SslSocketFactory( |
| 165 | | | mKeyStore, mTrustStore, mKeyAlias, mKeyAliasPassword); |
| 166 | | | } |
| 167 | | | else |
| 168 | | | { |
| 169 | | | |
| 170 | 0 | | sslFactory = new SslSocketFactory( |
| 171 | | | mKeyStoreFilename, mKeyStorePassword, |
| 172 | | | mTrustStoreFilename, mTrustStorePassword, |
| 173 | | | mKeyAlias, mKeyAliasPassword); |
| 174 | | | } |
| 175 | 0 | | final Protocol https |
| 176 | | | = new Protocol( |
| 177 | | | "https", |
| 178 | | | (ProtocolSocketFactory) sslFactory, |
| 179 | | | DEFAULT_SSL_PORT); |
| 180 | 0 | | int port = uri.getPort(); |
| 181 | 0 | | if (port == -1) |
| 182 | | | { |
| 183 | 0 | | port = DEFAULT_SSL_PORT; |
| 184 | | | } |
| 185 | 0 | | final String host = uri.getHost(); |
| 186 | 0 | | mHostConfiguration.setHost(host, port, https); |
| 187 | 0 | | } |
| 188 | | | else |
| 189 | | | { |
| 190 | 100 | | mHostConfiguration.setHost(uri); |
| 191 | | | } |
| 192 | | | } |
| 193 | 0 | | catch (URIException ue) |
| 194 | | | { |
| 195 | 0 | | final ArgumentMalformedException ame = new ArgumentMalformedException( |
| 196 | | | "uriAsString", uriAsString, ue.getMessage(), ue); |
| 197 | 0 | | throw ame; |
| 198 | | | } |
| 199 | 100 | | catch (IllegalArgumentException iae) |
| 200 | | | { |
| 201 | 100 | | final ArgumentMalformedException ame = new ArgumentMalformedException( |
| 202 | | | "uriAsString", uriAsString, iae.getMessage(), iae); |
| 203 | 100 | | throw ame; |
| 204 | 100 | | } |
| 205 | 100 | | mHttpClient = new HttpClient(); |
| 206 | | | |
| 207 | 100 | | final SimpleHttpConnectionManager connectionManager |
| 208 | | | = new SimpleHttpConnectionManager(); |
| 209 | 100 | | final HttpConnectionManagerParams httpParams |
| 210 | | | = new HttpConnectionManagerParams(); |
| 211 | 100 | | httpParams.setConnectionTimeout(connectTimeout); |
| 212 | 100 | | httpParams.setSoTimeout(readTimeout); |
| 213 | 100 | | connectionManager.setParams(httpParams); |
| 214 | 100 | | mHttpClient.setHttpConnectionManager(connectionManager); |
| 215 | 100 | | mState = HttpConnectionState.CONNECTION_ESTABLISHED; |
| 216 | 100 | | } |
| 217 | | | |
| 218 | | | {@inheritDoc} |
| 219 | | | public void releaseConnection () |
| 220 | | | { |
| 221 | 100 | | if (mState == HttpConnectionState.CONNECTION_RELEASED) |
| 222 | | | { |
| 223 | 100 | | return; |
| 224 | | | } |
| 225 | 100 | | if (mState != HttpConnectionState.CONNECTION_ESTABLISHED |
| 226 | | | && mState != HttpConnectionState.CONNECTION_EXECUTED) |
| 227 | | | { |
| 228 | 100 | | final IllegalStateException ile = new IllegalStateException( |
| 229 | | | "Connection must be established before"); |
| 230 | 100 | | throw ile; |
| 231 | | | } |
| 232 | 100 | | mPostMethod.releaseConnection(); |
| 233 | | | |
| 234 | | | |
| 235 | 100 | (3) | mPostMethod.recycle(); |
| 236 | 100 | | mState = HttpConnectionState.CONNECTION_RELEASED; |
| 237 | 100 | | } |
| 238 | | | |
| 239 | | | {@inheritDoc} |
| 240 | | | public void closeConnection () |
| 241 | | | { |
| 242 | 100 | | if (mState != HttpConnectionState.CONNECTION_ESTABLISHED |
| 243 | | | && mState != HttpConnectionState.CONNECTION_EXECUTED |
| 244 | | | && mState != HttpConnectionState.CONNECTION_RELEASED) |
| 245 | | | { |
| 246 | 100 | | final IllegalStateException ile = new IllegalStateException( |
| 247 | | | "Connection must be established before"); |
| 248 | 100 | | throw ile; |
| 249 | | | } |
| 250 | 100 | | final HttpConnectionManager connManager |
| 251 | | | = mHttpClient.getHttpConnectionManager(); |
| 252 | 100 | | final HttpConnection connection |
| 253 | | | = connManager.getConnection(mHostConfiguration); |
| 254 | 100 | | connection.close(); |
| 255 | 100 | | mState = HttpConnectionState.CONNECTION_CLOSED; |
| 256 | 100 | | } |
| 257 | | | |
| 258 | | | {@inheritDoc} |
| 259 | | | public void execute () |
| 260 | | | throws HttpConnectionException |
| 261 | | | { |
| 262 | 100 | | assertStateForExecute(); |
| 263 | 100 | | setRequestHeader(); |
| 264 | 100 | | mPostMethod.setRequestEntity(getRequestEntity()); |
| 265 | | | |
| 266 | | | |
| 267 | 100 | | if (mState == HttpConnectionState.CONNECTION_RELEASED) |
| 268 | | | { |
| 269 | 100 | | mPostMethod.setPath(mPath); |
| 270 | | | } |
| 271 | | | |
| 272 | 100 | | if (logger.isLoggable(Level.FINEST)) |
| 273 | | | { |
| 274 | 100 | | dumpRequestHeader(); |
| 275 | | | } |
| 276 | 100 | | int resultCode = 0; |
| 277 | | | try |
| 278 | | | { |
| 279 | 100 | | doCallbackRequestSend(); |
| 280 | 100 | | resultCode = mHttpClient.executeMethod( |
| 281 | | | mHostConfiguration, mPostMethod); |
| 282 | 100 | | doCallbackResponseReceived(resultCode, getResponseBody()); |
| 283 | | | } |
| 284 | 0 | | catch (HttpException he) |
| 285 | | | { |
| 286 | 0 | | final HttpConnectionException hce = new HttpConnectionException( |
| 287 | | | he.getMessage(), he); |
| 288 | 0 | | throw hce; |
| 289 | | | } |
| 290 | 100 | | catch (ConnectException ce) |
| 291 | | | { |
| 292 | 100 | | final HttpConnectConnectionException hce |
| 293 | | | = new HttpConnectConnectionException(ce.getMessage(), ce); |
| 294 | 100 | | throw hce; |
| 295 | | | } |
| 296 | 0 | | catch (IOException ioe) |
| 297 | | | { |
| 298 | 0 | | handleIOExceptionWhilstExecute(ioe); |
| 299 | 100 | | } |
| 300 | | | |
| 301 | 100 | | if (logger.isLoggable(Level.FINEST)) |
| 302 | | | { |
| 303 | 100 | | dumpResponse(); |
| 304 | | | } |
| 305 | | | |
| 306 | 100 | | assertResultCode(resultCode); |
| 307 | 100 | | assertResponseHeader(); |
| 308 | 100 | | mState = HttpConnectionState.CONNECTION_EXECUTED; |
| 309 | 100 | | } |
| 310 | | | |
| 311 | | | private void assertStateForExecute () |
| 312 | | | { |
| 313 | 100 | | if (mState != HttpConnectionState.CONNECTION_ESTABLISHED |
| 314 | | | && mState != HttpConnectionState.CONNECTION_RELEASED) |
| 315 | | | { |
| 316 | 100 | | final IllegalStateException ile = new IllegalStateException( |
| 317 | | | "Connection must be established before or released"); |
| 318 | 100 | | throw ile; |
| 319 | | | } |
| 320 | 100 | | } |
| 321 | | | |
| 322 | | | private void handleIOExceptionWhilstExecute ( |
| 323 | | | IOException ioe) |
| 324 | | | throws HttpConnectionException |
| 325 | | | { |
| 326 | 100 | | final String message = ioe.getMessage().toLowerCase( |
| 327 | | | Constants.SYSTEM_LOCALE); |
| 328 | | | HttpConnectionException hce; |
| 329 | 100 | (4)(5) | if (message.equals("read timed out")) |
| 330 | | | { |
| 331 | 100 | | hce = new HttpTimeoutConnectionException(ioe.getMessage(), ioe); |
| 332 | | | } |
| 333 | | | else |
| 334 | | | { |
| 335 | 100 | | hce = new HttpConnectionException(ioe.getMessage(), ioe); |
| 336 | | | } |
| 337 | 100 | | throw hce; |
| 338 | | | } |
| 339 | | | |
| 340 | | | {@inheritDoc} |
| 341 | | | public void setRequestBody (InputStream in) |
| 342 | | | { |
| 343 | 100 | | mRequestBodyInputStream = in; |
| 344 | 100 | | } |
| 345 | | | |
| 346 | | | {@inheritDoc} |
| 347 | | | public byte[] getResponseBody () |
| 348 | | | throws HttpEmptyResponseException |
| 349 | | | { |
| 350 | | | final byte[] result; |
| 351 | | | try |
| 352 | | | { |
| 353 | 100 | | result = mPostMethod.getResponseBody(); |
| 354 | | | } |
| 355 | 0 | | catch (IOException ioe) |
| 356 | | | { |
| 357 | 0 | | final HttpEmptyResponseException ere |
| 358 | | | = new HttpEmptyResponseException( |
| 359 | | | "IOException while obtaining response body", ioe); |
| 360 | 0 | | throw ere; |
| 361 | 100 | | } |
| 362 | | | |
| 363 | 100 | | if (result == null || result.length == 0) |
| 364 | | | { |
| 365 | 100 | | final HttpEmptyResponseException ere |
| 366 | | | = new HttpEmptyResponseException( |
| 367 | | | "Received empty http body in response"); |
| 368 | 100 | | throw ere; |
| 369 | | | } |
| 370 | 100 | | return result; |
| 371 | | | } |
| 372 | | | |
| 373 | | | {@inheritDoc} |
| 374 | | | public String getResponseHeader (String key) |
| 375 | | | { |
| 376 | 100 | | String result = null; |
| 377 | 100 | | final Header header = mPostMethod.getResponseHeader(key); |
| 378 | | | |
| 379 | 100 | | if (header != null) |
| 380 | | | { |
| 381 | 100 | (6) | result = (header.getElements())[0].getName(); |
| 382 | | | } |
| 383 | 100 | | return result; |
| 384 | | | } |
| 385 | | | |
| 386 | | | {@inheritDoc} |
| 387 | | | public void initSsl ( |
| 388 | | | String keyAlias, |
| 389 | | | String keyAliasPassword) |
| 390 | | | { |
| 391 | 100 | | Assert.notNull(keyAlias, "keyAlias"); |
| 392 | 100 | | Assert.notNull(keyAliasPassword, "keyAliasPassword"); |
| 393 | 100 | | mKeyStoreFilename = System.getProperty( |
| 394 | | | "javax.net.ssl.keyStore"); |
| 395 | 100 | | mKeyStorePassword = System.getProperty( |
| 396 | | | "javax.net.ssl.keyStorePassword"); |
| 397 | 100 | | mTrustStoreFilename = System.getProperty( |
| 398 | | | "javax.net.ssl.trustStore"); |
| 399 | 100 | | mTrustStorePassword = System.getProperty( |
| 400 | | | "javax.net.ssl.trustStorePassword"); |
| 401 | 100 | | mKeyStore = null; |
| 402 | 100 | | mTrustStore = null; |
| 403 | 100 | | mKeyAlias = keyAlias; |
| 404 | 100 | | mKeyAliasPassword = keyAliasPassword; |
| 405 | 100 | | } |
| 406 | | | |
| 407 | | | {@inheritDoc} |
| 408 | | | public void initSsl ( |
| 409 | | | KeyStore keyStore, |
| 410 | | | KeyStore trustStore, |
| 411 | | | String keyAlias, |
| 412 | | | String keyAliasPassword) |
| 413 | | | { |
| 414 | 0 | | Assert.notNull(keyStore, "keyStore"); |
| 415 | 0 | | Assert.notNull(trustStore, "trustStore"); |
| 416 | 0 | | Assert.notNull(keyAlias, "keyAlias"); |
| 417 | 0 | | Assert.notNull(keyAliasPassword, "keyAliasPassword"); |
| 418 | 0 | | mKeyStore = keyStore; |
| 419 | 0 | | mTrustStore = trustStore; |
| 420 | 0 | | mKeyAlias = keyAlias; |
| 421 | 0 | | mKeyAliasPassword = keyAliasPassword; |
| 422 | 0 | | } |
| 423 | | | |
| 424 | | | {@inheritDoc} |
| 425 | | | public void setEventListener (HttpConnectorEventListener listener, |
| 426 | | | ConnectorContext context) |
| 427 | | | { |
| 428 | 0 | | mHttpEventListener = listener; |
| 429 | 0 | | mConnectorContext = context; |
| 430 | 0 | | } |
| 431 | | | |
| 432 | | | {@inheritDoc} |
| 433 | | | public void setRequestResponseHeader (HttpRequestResponseHeader header) |
| 434 | | | { |
| 435 | 100 | | mRequestResponseHeader = header; |
| 436 | 100 | | } |
| 437 | | | |
| 438 | | | |
| 439 | | | |
| 440 | | | |
| 441 | | | |
| 442 | | | @return |
| 443 | | | |
| 444 | | | private InputStreamRequestEntity getRequestEntity () |
| 445 | | | { |
| 446 | 100 | | InputStreamRequestEntity entity = null; |
| 447 | 50 | | if (mIsRequestContentLengthSet && mRequestContentType != null) |
| 448 | | | { |
| 449 | 0 | | entity = new InputStreamRequestEntity(mRequestBodyInputStream, |
| 450 | | | mRequestContentLength, mRequestContentType); |
| 451 | | | } |
| 452 | 100 | | else if (mIsRequestContentLengthSet) |
| 453 | | | { |
| 454 | 0 | | entity = new InputStreamRequestEntity(mRequestBodyInputStream, |
| 455 | | | mRequestContentLength); |
| 456 | | | } |
| 457 | 100 | | else if (mRequestContentType != null) |
| 458 | | | { |
| 459 | 100 | | entity = new InputStreamRequestEntity(mRequestBodyInputStream, |
| 460 | | | mRequestContentType); |
| 461 | | | } |
| 462 | | | else |
| 463 | | | { |
| 464 | 0 | | entity = new InputStreamRequestEntity(mRequestBodyInputStream); |
| 465 | | | } |
| 466 | 100 | | return entity; |
| 467 | | | } |
| 468 | | | |
| 469 | | | |
| 470 | | | |
| 471 | | | |
| 472 | | | private void setRequestHeader () |
| 473 | | | { |
| 474 | 100 | | if (mRequestResponseHeader != null) |
| 475 | | | { |
| 476 | 100 | | final Map header |
| 477 | | | = new HashMap(mRequestResponseHeader.getRequestHeader()); |
| 478 | 100 | | if (!header.containsKey("Content-Type")) |
| 479 | | | { |
| 480 | | | |
| 481 | 0 | | header.put("Content-Type", "text/xml; charset=ISO-8859-1"); |
| 482 | | | } |
| 483 | 100 | | for (final Iterator it = header.keySet().iterator(); it.hasNext();) |
| 484 | | | { |
| 485 | 100 | | final String key = (String) it.next(); |
| 486 | 100 | (7) | final String value = (String) header.get(key); |
| 487 | 100 | | mPostMethod.setRequestHeader(key, value); |
| 488 | | | |
| 489 | 100 | (8)(9) | if (key.equals("Content-Length")) |
| 490 | | | { |
| 491 | 0 | | setRequestContentLength(Integer.parseInt(value)); |
| 492 | | | } |
| 493 | 100 | (10)(11) | else if (key.equals("Content-Type")) |
| 494 | | | { |
| 495 | 100 | | mRequestContentType = value; |
| 496 | | | } |
| 497 | 100 | | } |
| 498 | | | } |
| 499 | 100 | | } |
| 500 | | | |
| 501 | | | |
| 502 | | | |
| 503 | | | @param |
| 504 | | | |
| 505 | | | private void setRequestContentLength (int contentLength) |
| 506 | | | { |
| 507 | 0 | | mRequestContentLength = contentLength; |
| 508 | 0 | | mIsRequestContentLengthSet = true; |
| 509 | 0 | | } |
| 510 | | | |
| 511 | | | |
| 512 | | | |
| 513 | | | |
| 514 | | | |
| 515 | | | |
| 516 | | | @param |
| 517 | | | @throws |
| 518 | | | |
| 519 | | | private void assertResultCode (int resultCode) |
| 520 | | | throws HttpConnectionException |
| 521 | | | { |
| 522 | 100 | | if (resultCode != HTTP_RESULT_OK) |
| 523 | | | { |
| 524 | | | HttpConnectionException hce; |
| 525 | 0 | | if (resultCode >= HTTP_BAD_REQUEST |
| 526 | | | && resultCode < HTTP_INTERNAL_SERVER_ERROR) |
| 527 | | | { |
| 528 | 0 | | hce = new HttpClientConnectionException( |
| 529 | | | "HTTP Result Code of range 4xx in response", |
| 530 | | | null); |
| 531 | | | } |
| 532 | 0 | | else if (resultCode >= HTTP_INTERNAL_SERVER_ERROR) |
| 533 | | | { |
| 534 | 0 | | hce = new HttpServerConnectionException( |
| 535 | | | "HTTP Result Code of range 5xx in response", |
| 536 | | | null); |
| 537 | | | } |
| 538 | 0 | | else if (resultCode >= HTTP_REDIRECT |
| 539 | | | && resultCode < HTTP_BAD_REQUEST) |
| 540 | | | { |
| 541 | 0 | | hce = new HttpServerConnectionException( |
| 542 | | | "HTTP redirect not supported", |
| 543 | | | null); |
| 544 | | | } |
| 545 | | | else |
| 546 | | | { |
| 547 | 0 | | hce = new HttpServerConnectionException( |
| 548 | | | "Unsupported HTTP Result Code in response", |
| 549 | | | null); |
| 550 | | | } |
| 551 | 0 | | hce.setStatusCode(resultCode); |
| 552 | 0 | | hce.setHttpMessage(StringUtil.asciiToString(getResponseBody())); |
| 553 | | | |
| 554 | 0 | | throw hce; |
| 555 | | | } |
| 556 | 100 | | } |
| 557 | | | |
| 558 | | | |
| 559 | | | |
| 560 | | | |
| 561 | | | |
| 562 | | | @throws |
| 563 | | | |
| 564 | | | |
| 565 | | | private void assertResponseHeader () |
| 566 | | | throws HttpInvalidResponseHeaderException |
| 567 | | | { |
| 568 | 100 | | if (mRequestResponseHeader != null) |
| 569 | | | { |
| 570 | 100 | | logger.finest(mRequestResponseHeader.toString()); |
| 571 | 100 | | final Map expectedResponseHeader |
| 572 | | | = mRequestResponseHeader.getResponseHeader(); |
| 573 | 100 | | final StringBuffer message = new StringBuffer(); |
| 574 | 100 | | final Map invalidHeaders = new HashMap(); |
| 575 | 100 | | for (final Iterator it = expectedResponseHeader.keySet().iterator(); |
| 576 | 100 | | it.hasNext();) |
| 577 | | | { |
| 578 | 100 | | final String key = (String) it.next(); |
| 579 | 100 | (12) | final String value = (String) expectedResponseHeader.get(key); |
| 580 | 100 | | final String responseValue = getResponseHeader(key); |
| 581 | 100 | | if (responseValue == null) |
| 582 | | | { |
| 583 | 100 | | if (message.length() == 0) |
| 584 | | | { |
| 585 | 0 | (13) | message.append("One or more invalid header"); |
| 586 | 0 | | message.append(" values detected in response:\n"); |
| 587 | | | } |
| 588 | 100 | | message.append("Expected response header <"); |
| 589 | 100 | | message.append(key); |
| 590 | 100 | | message.append("> not received\n"); |
| 591 | 100 | | invalidHeaders.put(key, null); |
| 592 | | | } |
| 593 | 100 | | else if (!responseValue.equalsIgnoreCase(value)) |
| 594 | | | { |
| 595 | 100 | | if (message.length() == 0) |
| 596 | | | { |
| 597 | 100 | (14) | message.append("One or more invalid header"); |
| 598 | 100 | | message.append(" values detected in response:\n"); |
| 599 | | | } |
| 600 | 100 | | message.append("Value for <"); |
| 601 | 100 | | message.append(key); |
| 602 | 100 | | message.append("> is '"); |
| 603 | 100 | | message.append(responseValue); |
| 604 | 100 | | message.append("' expected was '"); |
| 605 | 100 | | message.append(value); |
| 606 | 100 | | message.append("'\n"); |
| 607 | 100 | | invalidHeaders.put(key, responseValue); |
| 608 | | | } |
| 609 | 100 | | } |
| 610 | | | |
| 611 | 100 | | if (message.length() != 0) |
| 612 | | | { |
| 613 | 100 | | final HttpInvalidResponseHeaderException ihe |
| 614 | | | = new HttpInvalidResponseHeaderException( |
| 615 | | | message.toString(), invalidHeaders); |
| 616 | | | |
| 617 | | | try |
| 618 | | | { |
| 619 | 100 | | ihe.setHttpMessage(HexUtil.dump(mPostMethod.getResponseBody())); |
| 620 | | | } |
| 621 | 0 | | catch (IOException ignore) |
| 622 | | | { |
| 623 | | | |
| 624 | 100 | | } |
| 625 | 100 | | throw ihe; |
| 626 | | | } |
| 627 | 100 | | } |
| 628 | | | else |
| 629 | | | { |
| 630 | 0 | | logger.finest("No response header for validating set"); |
| 631 | | | } |
| 632 | 100 | | } |
| 633 | | | |
| 634 | | | private void doCallbackRequestSend () |
| 635 | | | { |
| 636 | 100 | | if (mHttpEventListener != null) |
| 637 | | | { |
| 638 | 0 | | mHttpEventListener.requestSend(mConnectorContext); |
| 639 | | | } |
| 640 | 100 | | } |
| 641 | | | |
| 642 | | | private void doCallbackResponseReceived (int resultCode, byte[] responseData) |
| 643 | | | { |
| 644 | 100 | | if (mHttpEventListener != null) |
| 645 | | | { |
| 646 | 0 | | mHttpEventListener.responseReceived( |
| 647 | | | resultCode, responseData, mConnectorContext); |
| 648 | | | } |
| 649 | 100 | | } |
| 650 | | | |
| 651 | | | |
| 652 | | | |
| 653 | | | |
| 654 | | | private void dumpRequestHeader () |
| 655 | | | { |
| 656 | 100 | (15) | final StringBuffer dumpBuffer = new StringBuffer(); |
| 657 | 100 | (16) | dumpBuffer.append("\n-------------------------DUMP REQUEST HEADER---\n"); |
| 658 | 100 | | dumpBuffer.append("Host: "); |
| 659 | 100 | | dumpBuffer.append(mHostConfiguration.getHost()); |
| 660 | 100 | | dumpBuffer.append(LINE_FEED); |
| 661 | 100 | | dumpBuffer.append("Port: "); |
| 662 | 100 | | dumpBuffer.append(mHostConfiguration.getPort()); |
| 663 | 100 | | dumpBuffer.append(LINE_FEED); |
| 664 | 100 | | dumpBuffer.append("Path: "); |
| 665 | 100 | | dumpBuffer.append(mPostMethod.getPath()); |
| 666 | 100 | | dumpBuffer.append(LINE_FEED); |
| 667 | 100 | | dumpBuffer.append(LINE_FEED); |
| 668 | 100 | | dumpBuffer.append("Header:\n"); |
| 669 | 100 | | final Header[] headers = mPostMethod.getRequestHeaders(); |
| 670 | 100 | | for (int i = 0; i < headers.length; i++) |
| 671 | | | { |
| 672 | 100 | | final String header = headers[i].toString(); |
| 673 | 100 | | dumpBuffer.append(header); |
| 674 | | | } |
| 675 | 100 | | dumpBuffer.append(LINE_FEED); |
| 676 | 100 | | dumpBuffer.append("-----------------------------------------"); |
| 677 | 100 | | logger.finest(dumpBuffer.toString()); |
| 678 | 100 | | } |
| 679 | | | |
| 680 | | | |
| 681 | | | |
| 682 | | | |
| 683 | | | private void dumpResponse () |
| 684 | | | { |
| 685 | 100 | (17) | final StringBuffer dumpBuffer = new StringBuffer(); |
| 686 | 100 | (18) | dumpBuffer.append("\n-------------------------DUMP RESPONSE---\n"); |
| 687 | 100 | | dumpBuffer.append("StatusLine: "); |
| 688 | 100 | | dumpBuffer.append(mPostMethod.getStatusLine().toString()); |
| 689 | 100 | | dumpBuffer.append(LINE_FEED); |
| 690 | 100 | | dumpBuffer.append(LINE_FEED); |
| 691 | 100 | | dumpBuffer.append("Header:\n"); |
| 692 | 100 | | final Header[] headers = mPostMethod.getResponseHeaders(); |
| 693 | 100 | | for (int i = 0; i < headers.length; i++) |
| 694 | | | { |
| 695 | 100 | | final String header = headers[i].toString(); |
| 696 | 100 | | dumpBuffer.append(header); |
| 697 | | | } |
| 698 | 100 | | dumpBuffer.append(LINE_FEED); |
| 699 | 100 | | dumpBuffer.append("Body: \n"); |
| 700 | 100 | | byte[] response = null; |
| 701 | | | try |
| 702 | | | { |
| 703 | 100 | | response = mPostMethod.getResponseBody(); |
| 704 | | | } |
| 705 | 0 | | catch (IOException ignore) |
| 706 | | | { |
| 707 | | | |
| 708 | 100 | | } |
| 709 | 100 | | if (response != null) |
| 710 | | | { |
| 711 | 100 | | dumpBuffer.append(HexUtil.dump(response)); |
| 712 | | | } |
| 713 | | | else |
| 714 | | | { |
| 715 | 0 | | dumpBuffer.append("no response body available"); |
| 716 | | | } |
| 717 | 100 | | dumpBuffer.append(LINE_FEED); |
| 718 | 100 | | dumpBuffer.append("-----------------------------------------"); |
| 719 | 100 | | logger.finest(dumpBuffer.toString()); |
| 720 | 100 | | } |
| 721 | | | } |