| 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 | |
package ca.uhn.hl7v2.llp; |
| 28 | |
|
| 29 | |
import java.io.IOException; |
| 30 | |
import java.io.InputStream; |
| 31 | |
import java.io.OutputStream; |
| 32 | |
import java.net.SocketException; |
| 33 | |
import java.net.SocketTimeoutException; |
| 34 | |
|
| 35 | |
import org.slf4j.Logger; |
| 36 | |
import org.slf4j.LoggerFactory; |
| 37 | |
|
| 38 | |
import static ca.uhn.hl7v2.llp.MllpConstants.END_BYTE1; |
| 39 | |
import static ca.uhn.hl7v2.llp.MllpConstants.END_BYTE2; |
| 40 | |
import static ca.uhn.hl7v2.llp.MllpConstants.START_BYTE; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 301 | enum MllpDecoderState { |
| 50 | |
|
| 51 | 5 | START(START_BYTE, true) { |
| 52 | |
@Override |
| 53 | |
protected MllpDecoderState proceed() { |
| 54 | 1416 | return BODY; |
| 55 | |
} |
| 56 | |
|
| 57 | |
@Override |
| 58 | |
protected void handleEndOfStream() throws IOException { |
| 59 | 276 | LOG.info("End of input stream reached."); |
| 60 | 276 | throw new SocketException("End of input stream reached before message starts"); |
| 61 | |
} |
| 62 | |
|
| 63 | |
}, |
| 64 | 5 | BODY(END_BYTE1, false) { |
| 65 | |
@Override |
| 66 | |
protected MllpDecoderState proceed() { |
| 67 | 1401 | return PREPARE_END; |
| 68 | |
} |
| 69 | |
|
| 70 | |
@Override |
| 71 | |
protected void handleEndOfStream() throws IOException, LLPException { |
| 72 | 5 | throw new LLPException("MLLP protocol violation - Stream ends in the message body"); |
| 73 | |
} |
| 74 | |
|
| 75 | |
}, |
| 76 | 5 | PREPARE_END(END_BYTE2, true) { |
| 77 | |
@Override |
| 78 | |
protected MllpDecoderState proceed() { |
| 79 | 1396 | return END; |
| 80 | |
} |
| 81 | |
|
| 82 | |
@Override |
| 83 | |
protected void handleEndOfStream() throws IOException, LLPException { |
| 84 | 5 | throw new LLPException("MLLP protocol violation - Stream ends before LLP end byte"); |
| 85 | |
} |
| 86 | |
}, |
| 87 | 5 | END(0, false) { |
| 88 | |
@Override |
| 89 | |
protected MllpDecoderState proceed() { |
| 90 | 0 | return END; |
| 91 | |
} |
| 92 | |
|
| 93 | |
@Override |
| 94 | |
protected void handleEndOfStream() throws IOException, LLPException { |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
@Override |
| 98 | |
MllpDecoderState read(InputStream in, OutputStream out) throws IOException, LLPException { |
| 99 | 0 | throw new LLPException("Internal error - reading after end of message"); |
| 100 | |
} |
| 101 | |
}; |
| 102 | |
|
| 103 | |
private int nextStateByte; |
| 104 | |
private boolean mustChangeState; |
| 105 | |
|
| 106 | 5 | private static final Logger LOG = LoggerFactory.getLogger(MllpDecoderState.class); |
| 107 | |
|
| 108 | |
|
| 109 | 20 | MllpDecoderState(int nextStateByte, boolean mustChangeState) { |
| 110 | 20 | this.nextStateByte = nextStateByte; |
| 111 | 20 | this.mustChangeState = mustChangeState; |
| 112 | 20 | } |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
MllpDecoderState read(InputStream in, OutputStream out) throws SocketTimeoutException, IOException, LLPException { |
| 126 | |
int c; |
| 127 | |
try { |
| 128 | 302699 | if ((c = in.read()) == -1) { |
| 129 | 286 | handleEndOfStream(); |
| 130 | |
} else { |
| 131 | 301936 | LowerLayerProtocol.logCharacterReceived(c); |
| 132 | |
} |
| 133 | 301936 | if (c == nextStateByte) { |
| 134 | 4213 | return proceed(); |
| 135 | |
} |
| 136 | 297723 | if (mustChangeState) { |
| 137 | 40 | throw new LLPException("MLLP protocol violation - Expected byte '" + nextStateByte + |
| 138 | |
"' in state " + this + " but was '" + c + "'"); |
| 139 | |
} |
| 140 | 297683 | out.write(c); |
| 141 | 214 | } catch (SocketTimeoutException e) { |
| 142 | |
|
| 143 | 214 | throw e; |
| 144 | 524 | } catch (SocketException e) { |
| 145 | 524 | LOG.info("SocketException on read() attempt. Socket appears to have been closed: " + e.getMessage()); |
| 146 | 524 | throw e; |
| 147 | 297683 | } |
| 148 | 297683 | return this; |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
protected abstract MllpDecoderState proceed(); |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
protected abstract void handleEndOfStream() throws IOException, LLPException; |
| 165 | |
|
| 166 | |
} |