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