Guest User

Untitled

a guest
Sep 14th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. In the controller, something bad happens -
  2. So we call the utils class !
  3.  
  4. if (StringUtils.isBlank(patientId) && StringUtils.isBlank(encounterUuid))
  5.     return ExceptionUtil.generateMessage(request, response, ErrorDetailsEnum.ERROR_1);
  6.  
  7. =================================================
  8. This is what the exception utils class looks like-
  9. Note that this is where we assign the content types to the responses. (im putting it here to prevent myself repeating in on the *controller class)
  10.  
  11. public class ExceptionUtil {
  12.     public static Object generateMessage(HttpServletRequest request, HttpServletResponse response, ErrorDetailsEnum error){
  13.         boolean isPipeDelimited = false;
  14.        
  15.         String acceptHeader = request.getHeader("Accept");
  16.         if (acceptHeader == null || !acceptHeader.contains("text/xml"))
  17.                 isPipeDelimited = true;
  18.        
  19.         if(isPipeDelimited){
  20.             response.setContentType("application/json");
  21.             return WriteJsonMessage(error);    
  22.             }else{
  23.                 response.setContentType("text/xml");
  24.             return WriteXmlMessage(error); 
  25.             }
  26.     }
  27.  
  28.     private static Object WriteJsonMessage(ErrorDetailsEnum error) {
  29.         ObjectMapper mapper = new ObjectMapper();
  30.         ErrorMessageTemplate errorMessageTemplate = new ErrorMessageTemplate();
  31.         errorMessageTemplate.setErrorMessage(error);
  32.         errorMessageTemplate.setErrorCode(Integer.toString(error.getCode()));
  33.         //errorMessageTemplate.setErrorDetails(errorDetails);
  34.  
  35.         Writer strWriter = new StringWriter();
  36.         try {
  37.             mapper.writeValue(strWriter, errorMessageTemplate);
  38.         } catch (JsonGenerationException e) {
  39.             // TODO Auto-generated catch block
  40.             e.printStackTrace();
  41.         } catch (JsonMappingException e) {
  42.             // TODO Auto-generated catch block
  43.             e.printStackTrace();
  44.         } catch (IOException e) {
  45.             // TODO Auto-generated catch block
  46.             e.printStackTrace();
  47.         }
  48.         String userDataJSON = strWriter.toString();
  49.         System.out.println(userDataJSON);
  50.  
  51.         return userDataJSON;
  52.     }
  53.    
  54.     private static Object WriteXmlMessage(ErrorDetailsEnum error) {
  55.         String xmlString = null;
  56.          try {
  57.                 DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
  58.                 DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
  59.                 Document doc = docBuilder.newDocument();
  60.                
  61.                 //create the root element and add it to the document
  62.                 Element root = doc.createElement("error");
  63.                 doc.appendChild(root);
  64.  
  65.                 //create child element, add an attribute, and add to root
  66.                 Element errorMsg = doc.createElement("errorMessage");
  67.                 root.appendChild(errorMsg);
  68.  
  69.                 //add a text element to the child
  70.                 Text errorText = doc.createTextNode(error.toString());
  71.                 errorMsg.appendChild(errorText);
  72.                
  73.                 //create child element, add an attribute, and add to root
  74.                 Element errorCode = doc.createElement("errorCode");
  75.                 root.appendChild(errorCode);
  76.                
  77.                 //add a text element to the child
  78.                 Text errorCodeText = doc.createTextNode(Integer.toString(error.getCode()));
  79.                 errorCode.appendChild(errorCodeText);
  80.  
  81.                 //set up a transformer
  82.                 TransformerFactory transfac = TransformerFactory.newInstance();
  83.                 Transformer trans = transfac.newTransformer();
  84.                 trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  85.                 trans.setOutputProperty(OutputKeys.INDENT, "yes");
  86.  
  87.                 //create string from xml tree
  88.                 StringWriter sw = new StringWriter();
  89.                 StreamResult result = new StreamResult(sw);
  90.                 DOMSource source = new DOMSource(doc);
  91.                 trans.transform(source, result);
  92.                 xmlString = sw.toString();
  93.  
  94.                 System.out.println("Here's the xml:\n\n" + xmlString);
  95.                
  96.             } catch (Exception e) {
  97.                 System.out.println(e);
  98.             }
  99.          return xmlString;
  100.     }
  101.  
  102. }
  103.  
  104. ======================================
  105.  
  106. And now, the ErrorDetailsEnum, which contains the error message and the code for every potential issue-
  107.  
  108. package org.openmrs.module.hl7query.util;
  109.  
  110. public enum ErrorDetailsEnum {
  111.      ERROR_1(400), ERROR_2(401), ERROR_3(402);
  112.      
  113.      private int code;
  114.      
  115.      private ErrorDetailsEnum(int c) {
  116.        code = c;
  117.      }
  118.      
  119.      public int getCode() {
  120.        return code;
  121.      }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment