View Javadoc
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  public enum ResponseCode {
14  
15  	HTTP_200_OK(200, "OK"),
16  
17  	HTTP_400_BAD_REQUEST(400, "Bad Request"),
18  
19  	HTTP_500_INTERNAL_SERVER_ERROR(500, "Internal Server Error"), ;
20  
21  	private int myCode;
22  	private String myMessage;
23  	private static final Map<AcknowledgmentCode, ResponseCode> ourAckCodesToResponseCodes = new HashMap<>();
24  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResponseCode.class);
25  
26  	static {
27  		/*
28  		 * As of a decision by the HL7 ITS working group, all HL7
29  		 * response codes now map to HTTP 200.
30  		 */
31  		ourAckCodesToResponseCodes.put(AA, HTTP_200_OK);
32  		ourAckCodesToResponseCodes.put(CA, HTTP_200_OK);
33  		ourAckCodesToResponseCodes.put(AR, HTTP_200_OK);
34  		ourAckCodesToResponseCodes.put(CR, HTTP_200_OK);
35  		ourAckCodesToResponseCodes.put(AE, HTTP_200_OK);
36  		ourAckCodesToResponseCodes.put(CE, HTTP_200_OK);
37  	}
38  
39  	ResponseCode(int theCode, String theMessage) {
40  		myCode = theCode;
41  		myMessage = theMessage;
42  	}
43  
44  	/**
45  	 * Returns the appropriate response code for a given ACK code. If none can
46  	 * be detected, returns {@link #HTTP_500_INTERNAL_SERVER_ERROR}
47  	 * 
48  	 * @param theAcknowledgementCode
49  	 *            The acknowledgement code, e.g. "AA", or "CE"
50  	 * @return The appropriate code
51  	 */
52  	public static ResponseCode forAcknowledgementCode(String theAcknowledgementCode) {
53  		ResponseCode retVal;
54  		if (theAcknowledgementCode != null) {
55  			retVal = ourAckCodesToResponseCodes.get(AcknowledgmentCode.valueOf(theAcknowledgementCode));
56  		} else {
57  			ourLog.warn("No HTTP response code defined for acknowledgement code: " + theAcknowledgementCode);
58  			retVal = ResponseCode.HTTP_500_INTERNAL_SERVER_ERROR;
59  		}
60  		if (retVal == null) {
61  			ourLog.warn("No HTTP response code defined for acknowledgement code: " + theAcknowledgementCode);
62  			retVal = HTTP_500_INTERNAL_SERVER_ERROR;
63  		}
64  		return retVal;
65  	}
66  
67  	/**
68  	 * Detects the appropriate HTTP response code for a given message
69  	 * 
70  	 */
71  	public static ResponseCode detect(String theRawMessage) {
72  		switch (EncodingStyle.detect(theRawMessage)) {
73  		case ER7:
74  		default:
75  			StringTokenizer tok = new StringTokenizer(theRawMessage, "\r");
76  			while (tok.hasMoreTokens()) {
77  				String nextSegment = tok.nextToken();
78  				if (nextSegment.startsWith("MSA")) {
79  					if (nextSegment.length() >= 6) {
80  						String code = nextSegment.substring(4, 6);
81  						return forAcknowledgementCode(code);
82  					}
83  				}
84  			}
85  			ourLog.warn("Could not detect MSA.1 value in ER7 message");
86  			return HTTP_500_INTERNAL_SERVER_ERROR;
87  
88  		case XML:
89  
90  			Pattern p = Pattern.compile("<MSA\\.1>(.*?)</MSA\\.1>");
91  			Matcher m = p.matcher(theRawMessage);
92  			if (m.find()) {
93  				String code = m.group(1);
94  				return forAcknowledgementCode(code);
95  			} else {
96  				ourLog.warn("Could not detect MSA.1 value in XML message");
97  				return ResponseCode.HTTP_500_INTERNAL_SERVER_ERROR;
98  			}
99  		}
100 	}
101 
102 	/**
103 	 * @return the code
104 	 */
105 	public int getCode() {
106 		return myCode;
107 	}
108 
109 	/**
110 	 * @param theCode
111 	 *            the code to set
112 	 */
113 	public void setCode(int theCode) {
114 		myCode = theCode;
115 	}
116 
117 	/**
118 	 * @return the message
119 	 */
120 	public String getMessage() {
121 		return myMessage;
122 	}
123 
124 	/**
125 	 * @param theMessage
126 	 *            the message to set
127 	 */
128 	public void setMessage(String theMessage) {
129 		myMessage = theMessage;
130 	}
131 
132 }