| 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.rmi.RemoteException; |
| 36 | | | import javax.resource.ResourceException; |
| 37 | | | import org.jcoderz.commons.connector.ConnectorException; |
| 38 | | | import org.jcoderz.commons.connector.http.transport.ConnectorContext; |
| 39 | | | import org.jcoderz.commons.connector.http.transport.HttpConnectorEventListener; |
| 40 | | | import org.jcoderz.commons.connector.http.transport.HttpRequestResponseHeader; |
| 41 | | | import org.jcoderz.commons.types.Url; |
| 42 | | | |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | 0 | (1) | public class HttpConnectorSessionImpl |
| 50 | | | implements HttpConnectorSessionInterface, |
| 51 | | | HttpConnectorEventListener |
| 52 | | | { |
| 53 | 0 | | private long mSendTime = 0; |
| 54 | 0 | | private long mReceivedTime = 0; |
| 55 | 0 | | private int mTryCounter = 1; |
| 56 | | | |
| 57 | | | {@inheritDoc} |
| 58 | | | public ConnectorResponse sendAndReceive ( |
| 59 | | | byte[] request, Url target, |
| 60 | | | HttpRequestResponseHeader header) |
| 61 | | (2) | throws RemoteException, ResourceException, ConnectorException |
| 62 | | | { |
| 63 | 0 | | reset(); |
| 64 | 0 | | final HttpConnectionSpec spec = new HttpConnectionSpec(this, target); |
| 65 | 0 | | final HttpConnection connection |
| 66 | | | = HttpConnectionUtil.getHttpConnection(spec); |
| 67 | 0 | | connection.setEventListener( |
| 68 | 0 | (3)(4) | ((HttpConnectorEventListener) this), new ConnectorContext() { }); |
| 69 | 0 | | connection.setRequestResponseHeader(header); |
| 70 | | | final byte[] response; |
| 71 | | | try |
| 72 | | | { |
| 73 | 0 | | response = connection.sendAndReceive( |
| 74 | | | "this is a message".getBytes()); |
| 75 | | | } |
| 76 | 0 | | catch (ConnectorException cex) |
| 77 | | | { |
| 78 | 0 | | cex.log(); |
| 79 | 0 | | throw cex; |
| 80 | 0 | | } |
| 81 | 0 | | connection.close(); |
| 82 | 0 | | return new ConnectorResponse( |
| 83 | | | calculatedRequestTime(), mTryCounter, response); |
| 84 | | | } |
| 85 | | | |
| 86 | | | {@inheritDoc} |
| 87 | | | public void requestSend (ConnectorContext context) |
| 88 | | | { |
| 89 | 0 | | if (mSendTime > 0) |
| 90 | | | { |
| 91 | 0 | | mTryCounter++; |
| 92 | | | } |
| 93 | 0 | | mSendTime = System.currentTimeMillis(); |
| 94 | 0 | | } |
| 95 | | | |
| 96 | | | {@inheritDoc} |
| 97 | | | public void responseReceived ( |
| 98 | | | int resultCode, byte[] response, ConnectorContext context) |
| 99 | | | { |
| 100 | 0 | | mReceivedTime = System.currentTimeMillis(); |
| 101 | 0 | | } |
| 102 | | | |
| 103 | | | private long calculatedRequestTime () |
| 104 | | | { |
| 105 | | | long result; |
| 106 | 0 | | if (mReceivedTime == 0) |
| 107 | | | { |
| 108 | 0 | | result = -1; |
| 109 | | | } |
| 110 | 0 | | else if (mReceivedTime <= mSendTime) |
| 111 | | | { |
| 112 | 0 | | result = 0; |
| 113 | | | } |
| 114 | | | else |
| 115 | | | { |
| 116 | 0 | | result = mReceivedTime - mSendTime; |
| 117 | | | } |
| 118 | 0 | | return result; |
| 119 | | | } |
| 120 | | | |
| 121 | | | private void reset () |
| 122 | | | { |
| 123 | 0 | | mSendTime = 0; |
| 124 | 0 | | mReceivedTime = 0; |
| 125 | 0 | | mTryCounter = 1; |
| 126 | 0 | | } |
| 127 | | | |
| 128 | | | {@inheritDoc} |
| 129 | | | public void requestSendWithRetry (ConnectorContext context) |
| 130 | | | { |
| 131 | | | |
| 132 | 0 | | } |
| 133 | | | |
| 134 | | | {@inheritDoc} |
| 135 | | | public void responseReceivedAfterRetry (int numberOfRetries, |
| 136 | | | byte[] responseData, ConnectorContext context) |
| 137 | | | { |
| 138 | | | |
| 139 | 0 | | } |
| 140 | | | } |