Imported from dev1.link2tek.net CommEngine.git
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

214 linhas
7.4 KiB

  1. package altk.comm.engine;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.zip.ZipInputStream;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.xml.parsers.ParserConfigurationException;
  9. import javax.xml.parsers.SAXParser;
  10. import javax.xml.parsers.SAXParserFactory;
  11. import org.xml.sax.Attributes;
  12. import org.xml.sax.InputSource;
  13. import org.xml.sax.SAXException;
  14. import org.xml.sax.helpers.DefaultHandler;
  15. import altk.comm.engine.exception.BroadcastMsgException;
  16. import altk.comm.engine.exception.EngineException;
  17. import altk.common.engine.util.LoggingInputStreamReader;
  18. import altk.common.engine.util.UTF8BenevolentDecoder;
  19. public abstract class XMLSAXBroadcast extends Broadcast
  20. {
  21. protected class BaseParserHandler extends DefaultHandler
  22. {
  23. /**
  24. * Text collector used during SAX parsing
  25. */
  26. StringBuffer characters;
  27. private String contact_id;
  28. private String activity_record_id;
  29. private Map<String, String> recipientProp;
  30. private boolean inRecipient = false;
  31. private boolean inRecipientProp;
  32. /**
  33. * This method is called by the SAX parser when a text node
  34. * is encountered in the XML tree during parsing.
  35. */
  36. @Override
  37. public void characters(char[] ch, int start, int length)
  38. {
  39. characters.append(ch, start, length);
  40. }
  41. @Override
  42. public void startElement(String uri, String localName, String qName,
  43. Attributes attributes)
  44. {
  45. // Initialize characters to start collecting
  46. characters = new StringBuffer();
  47. if (qName.equals("recipient"))
  48. {
  49. // get attributes: contact_id, call_record_id
  50. contact_id = attributes.getValue("", "contact_id");
  51. activity_record_id = attributes.getValue("", activityRecordIdParamName);
  52. // Prepare contact properties
  53. recipientProp = new HashMap<String, String>();
  54. inRecipient = true;
  55. inRecipientProp = false;
  56. }
  57. else if (qName.equals(broadcastType))
  58. {
  59. // broadcast_id
  60. myLogger.debug("broadcast_id: " + attributes.getValue("", "broadcast_id"));
  61. setBroadcastId(attributes.getValue("", "broadcast_id"));
  62. // launch_record_id
  63. setLaunchRecordId(attributes.getValue("", "launch_record_id"));
  64. }
  65. else if (inRecipient)
  66. {
  67. inRecipientProp = true;
  68. }
  69. }
  70. @Override
  71. public void endElement(String uri, String localName, String qName)
  72. {
  73. if (qName.equals("recipient"))
  74. {
  75. Recipient recipient;
  76. try
  77. {
  78. recipient = new Recipient(contact_id, activity_record_id, recipientProp);
  79. recipientList.add(recipient);
  80. }
  81. catch (IllegalArgumentException e)
  82. {
  83. CommonLogger.alarm.warn("This contact not added (" + e.getMessage()
  84. + "): "
  85. + "' contact_id='" + contact_id
  86. + "' " + activityRecordIdParamName + "='" + activity_record_id + "'");
  87. }
  88. }
  89. else if (qName.equals("async_status_post_back"))
  90. {
  91. postBackURL = getTrimmedText();
  92. }
  93. else if (qName.equals("expire_time"))
  94. {
  95. expireTime = Long.parseLong(getTrimmedText());
  96. // defaults to 20 minutes
  97. myLogger.debug("expire_time decoded to be " + expireTime);
  98. if (expireTime == 0)
  99. {
  100. expireTime = System.currentTimeMillis() + 20 * 60 * 1000;
  101. myLogger.debug("expire time adjusted to be " + expireTime);
  102. }
  103. }
  104. else if (inRecipientProp)
  105. {
  106. recipientProp.put(qName, getText());
  107. inRecipientProp = false;
  108. }
  109. }
  110. /**
  111. *
  112. * @return char string collected in characters, but not trimmed
  113. */
  114. protected String getText()
  115. {
  116. return characters.toString();
  117. }
  118. /**
  119. *
  120. * @return char string collected in characters, and trimmed
  121. */
  122. protected String getTrimmedText()
  123. {
  124. return characters.toString().trim();
  125. }
  126. }
  127. public XMLSAXBroadcast(String broadcastType,
  128. String activityRecordIdParamName, String jobReportRootNodeName)
  129. {
  130. super(broadcastType, activityRecordIdParamName, jobReportRootNodeName);
  131. }
  132. @Override
  133. protected final void decode(HttpServletRequest request, boolean notInService)
  134. throws EngineException
  135. {
  136. try
  137. {
  138. // Check if content is zipped
  139. InputStream inb;
  140. String contentType = request.getContentType();
  141. if (contentType != null && contentType.split("/")[1].equalsIgnoreCase("zip"))
  142. {
  143. ZipInputStream zip = new ZipInputStream(request.getInputStream());
  144. zip.getNextEntry();
  145. inb = zip;
  146. }
  147. else
  148. {
  149. inb = request.getInputStream();
  150. }
  151. // Get a XML parser
  152. myLogger.info("Begin parsing");
  153. SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
  154. long parseStart = System.currentTimeMillis();
  155. int contentLength = request.getContentLength();
  156. CommonLogger.activity.info("Receiving " + contentLength + " bytes of data");
  157. // Convert InputStream inb into InputSource, using the UTF-8 decoder that
  158. // cleans content off bad UTF-8 characters.
  159. InputSource is = new InputSource(new LoggingInputStreamReader(inb,
  160. UTF8BenevolentDecoder.getDecoder(), CommonLogger.activity));
  161. DefaultHandler parserHandler = getParserHandler();
  162. parser.parse(is, parserHandler);
  163. long parseEnd = System.currentTimeMillis();
  164. myLogger.info("Parsing took " + (parseEnd - parseStart)/1000 + " seconds");
  165. }
  166. catch (ParserConfigurationException e)
  167. {
  168. String errorText = "While parsing: " + e.getMessage();
  169. myLogger.warn(errorText, e);
  170. throw new BroadcastMsgException(e.getMessage(), e);
  171. }
  172. catch (SAXException e)
  173. {
  174. String errorText = "While parsing: " + e.getMessage();
  175. myLogger.warn(errorText, e);
  176. throw new BroadcastMsgException(e.getMessage(), e);
  177. }
  178. catch (IOException e)
  179. {
  180. String errorText = "While parsing: " + e.getMessage();
  181. myLogger.warn(errorText, e);
  182. throw new BroadcastMsgException(e.getMessage(), e);
  183. }
  184. catch (Exception e)
  185. {
  186. // catch any problem we may have missed
  187. String errorText = "While parsing: " + e.getMessage();
  188. myLogger.warn(errorText, e);
  189. throw new BroadcastMsgException(e.getMessage(), e);
  190. }
  191. }
  192. protected abstract DefaultHandler getParserHandler();
  193. }