1 | |
package ca.uhn.hl7v2.hoh.encoder; |
2 | |
|
3 | |
import java.util.HashMap; |
4 | |
import java.util.Map; |
5 | |
import java.util.StringTokenizer; |
6 | |
import java.util.regex.Matcher; |
7 | |
import java.util.regex.Pattern; |
8 | |
|
9 | |
import ca.uhn.hl7v2.AcknowledgmentCode; |
10 | |
|
11 | |
import static ca.uhn.hl7v2.AcknowledgmentCode.*; |
12 | |
|
13 | 5 | public enum ResponseCode { |
14 | |
|
15 | 5 | HTTP_200_OK(200, "OK"), |
16 | |
|
17 | 5 | HTTP_400_BAD_REQUEST(400, "Bad Request"), |
18 | |
|
19 | 5 | HTTP_500_INTERNAL_SERVER_ERROR(500, "Internal Server Error"), ; |
20 | |
|
21 | |
private int myCode; |
22 | |
private String myMessage; |
23 | 5 | private static final Map<AcknowledgmentCode, ResponseCode> ourAckCodesToResponseCodes = new HashMap<AcknowledgmentCode, ResponseCode>(); |
24 | 5 | private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResponseCode.class); |
25 | |
|
26 | |
static { |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 5 | ourAckCodesToResponseCodes.put(AA, HTTP_200_OK); |
32 | 5 | ourAckCodesToResponseCodes.put(CA, HTTP_200_OK); |
33 | 5 | ourAckCodesToResponseCodes.put(AR, HTTP_200_OK); |
34 | 5 | ourAckCodesToResponseCodes.put(CR, HTTP_200_OK); |
35 | 5 | ourAckCodesToResponseCodes.put(AE, HTTP_200_OK); |
36 | 5 | ourAckCodesToResponseCodes.put(CE, HTTP_200_OK); |
37 | 5 | } |
38 | |
|
39 | 15 | ResponseCode(int theCode, String theMessage) { |
40 | 15 | myCode = theCode; |
41 | 15 | myMessage = theMessage; |
42 | 15 | } |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public static ResponseCode forAcknowledgementCode(String theAcknowledgementCode) { |
53 | 240 | ResponseCode retVal = null; |
54 | 240 | if (theAcknowledgementCode != null) { |
55 | 240 | retVal = ourAckCodesToResponseCodes.get(AcknowledgmentCode.valueOf(theAcknowledgementCode)); |
56 | |
} else { |
57 | 0 | ourLog.warn("No HTTP response code defined for acknowledgement code: " + theAcknowledgementCode); |
58 | 0 | retVal = ResponseCode.HTTP_500_INTERNAL_SERVER_ERROR; |
59 | |
} |
60 | 240 | if (retVal == null) { |
61 | 0 | ourLog.warn("No HTTP response code defined for acknowledgement code: " + theAcknowledgementCode); |
62 | 0 | retVal = HTTP_500_INTERNAL_SERVER_ERROR; |
63 | |
} |
64 | 240 | return retVal; |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public static ResponseCode detect(String theRawMessage) { |
72 | 240 | switch (EncodingStyle.detect(theRawMessage)) { |
73 | |
case ER7: |
74 | |
default: |
75 | 200 | StringTokenizer tok = new StringTokenizer(theRawMessage, "\r"); |
76 | 400 | while (tok.hasMoreTokens()) { |
77 | 400 | String nextSegment = tok.nextToken(); |
78 | 400 | if (nextSegment.startsWith("MSA")) { |
79 | 200 | if (nextSegment.length() >= 6) { |
80 | 200 | String code = nextSegment.substring(4, 6); |
81 | 200 | return forAcknowledgementCode(code); |
82 | |
} |
83 | |
} |
84 | 200 | } |
85 | 0 | ourLog.warn("Could not detect MSA.1 value in ER7 message"); |
86 | 0 | return HTTP_500_INTERNAL_SERVER_ERROR; |
87 | |
|
88 | |
case XML: |
89 | |
|
90 | 40 | Pattern p = Pattern.compile("<MSA\\.1>(.*?)</MSA\\.1>"); |
91 | 40 | Matcher m = p.matcher(theRawMessage); |
92 | 40 | if (m.find()) { |
93 | 40 | String code = m.group(1); |
94 | 40 | return forAcknowledgementCode(code); |
95 | |
} else { |
96 | 0 | ourLog.warn("Could not detect MSA.1 value in XML message"); |
97 | 0 | return ResponseCode.HTTP_500_INTERNAL_SERVER_ERROR; |
98 | |
} |
99 | |
} |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public int getCode() { |
106 | 210 | return myCode; |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
public void setCode(int theCode) { |
114 | 0 | myCode = theCode; |
115 | 0 | } |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public String getMessage() { |
121 | 160 | return myMessage; |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public void setMessage(String theMessage) { |
129 | 0 | myMessage = theMessage; |
130 | 0 | } |
131 | |
|
132 | |
} |