1 package ca.uhn.hl7v2.hoh.encoder;
2
3 import static ca.uhn.hl7v2.hoh.util.StringUtils.*;
4
5 import java.io.IOException;
6 import java.io.InputStream;
7
8 import ca.uhn.hl7v2.hoh.api.DecodeException;
9
10 public class Hl7OverHttpResponseDecoder extends AbstractHl7OverHttpDecoder {
11
12 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(Hl7OverHttpResponseDecoder.class);
13 private String myActionLine;
14
15 @Override
16 protected String readActionLineAndDecode(InputStream theInputStream) throws IOException, NoMessageReceivedException, DecodeException {
17 ourLog.trace("Entering readActionLineAndDecode(InputStream)");
18
19 if (myActionLine == null) {
20 String firstLine = readFirstLine(theInputStream);
21 if (firstLine == null || isBlank(firstLine)) {
22 throw new NoMessageReceivedException();
23 }
24
25 if (!firstLine.startsWith("HTTP/1.1 ")) {
26 throw new DecodeException("HTTP response begins with unknown HTTP version. Line is: " + firstLine);
27 }
28
29 String statusPart = firstLine.substring(9);
30 int spaceIdx = statusPart.indexOf(' ');
31 if (spaceIdx == -1) {
32 throw new DecodeException("Invalid response line, no space after status code. Line is: " + firstLine);
33 }
34
35 String statusCode = statusPart.substring(0, spaceIdx);
36 try {
37 setResponseStatus(Integer.parseInt(statusCode));
38 } catch (NumberFormatException e) {
39 throw new DecodeException("Invalid response line. Bad status code: " + statusCode);
40 }
41
42 setResponseName(statusPart.substring(spaceIdx).trim());
43 myActionLine = firstLine;
44 ourLog.trace("Action line is {}", myActionLine);
45
46 } else {
47 ourLog.trace("Already have an action line");
48 }
49
50 return myActionLine;
51 }
52
53 @Override
54 protected void authorize() throws AuthorizationFailureException {
55
56 }
57
58
59
60
61 @Override
62 public boolean isConnectionCloseHeaderPresent() {
63 return super.isConnectionCloseHeaderPresent();
64 }
65
66 }