栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Exchange Web服务Java APi + RESTful推送通知侦听器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Exchange Web服务Java APi + RESTful推送通知侦听器

问题是防火墙阻止传入的EWS消息进入我的开发环境。

这是侦听器的最终版本。我希望有人能发现它有用

@Path("/emailnotification")public class ExchangeNotificationListener {private static final Log logger = LogFactory.getLog(ExchangeNotificationListener.class);private static final String OK = "OK";private static final String UNSUBSCRIBE = "Unsubscribe";@Path("/incomingevent")@POST()@Consumes(MediaType.TEXT_XML)public void onNotificationReceived(@Context HttpServletRequest request, @Context HttpServletResponse response)        throws Exception {    // Establish the start time so we can report total elapsed time to execute the call later.    long startTime = GregorianCalendar.getInstance().getTimeInMillis();    long endTime;    // Retrieve the EWS Notification message as an XML document    document notificationXML = loadXML(IOUtils.toString(request.getInputStream()));    // Deserialize the document    ExchangeNotification notif = new ExchangeNotification(notificationXML);    // We need the subscription id in order to proceed    String subscriptionId = notif.getSubscriptionId();    if (isBlank(subscriptionId)) {        logger.error("SOAP Envelope MUST contains the subscriptionId");        // If we did not successfully parse the XML document, tell Exchange that we got a bad request.        response.sendError(HttpServletResponse.SC_BAD_REQUEST);    }    if (!ExchangeSubscriptionMap.getInstance().getSubscriptionsMap().containsKey(subscriptionId)) {        logger.warn("SubscriptionId = " + subscriptionId     + " was not found in the subscriptions map. Unsubscribing...");        sendResponse(response, UNSUBSCRIBE);        return;    }    // Do some logic here depending on the current EventType    businessLogic(notif, response);    // Flush the buffer and report the total time taken to execute for this notification.    response.flushBuffer();    endTime = GregorianCalendar.getInstance().getTimeInMillis();    logger.debug(String.format("Total execution time: %1$s (ms)", (Long) (endTime - startTime)));}private void sendResponse(HttpServletResponse response, String msg) {    try {        // Build the HTTP response        String str = ExchangeUtils.getResponseXML(msg);        response.setCharacterEncoding("UTF-8");        response.setStatus(HttpServletResponse.SC_OK);        response.setContentType("text/xml; charset=UTF-8");        response.setContentLength(str.length());        // Send the response.        PrintWriter w = response.getWriter();        w.print(str);        w.flush();    } catch (IOException e) {        logger.error("Error getting the response writer");        Throwables.propagate(e);    }}@SuppressWarnings("unchecked")private void businessLogic(ExchangeNotification notification, HttpServletResponse response) {    try {        // Iterate the notification events        for (SubscriptionEvent event : notification.getEvents()) { // only take care of the Modified event switch (event.getEventType()) { case MODIFIED:     // logic here     // Get the ExchangeService instance this user use for Subscribing     MSExchangeServiceManager service = ExchangeSubscriptionMap.getInstance().getSubscriptionsMap().get(notification.getSubscriptionId()).getService();    //e.g: service.getUnreadMessages(WellKnownFolderName.Inbox));     break; default:     logger.debug("Skipping: " + event.getEventType());     break; }        }        // Finally, send the response.        sendResponse(response, OK);    } catch (Exception e) {        logger.error("EWS ==> Error processing request", e.getCause());        Throwables.propagate(e);    }}private document loadXML(String rawXML) {    document doc = null;    try {        logger.debug("Incoming request input stream : " + rawXML);        documentBuilderFactory domFactory = documentBuilderFactory.newInstance();        // turn on this flag in order to resolve manually the namespaces of the document        domFactory.setNamespaceAware(true);        documentBuilder builder = domFactory.newdocumentBuilder();        doc = builder.parse(new InputSource(new ByteArrayInputStream(rawXML.getBytes("UTF-8"))));    } catch (ParserConfigurationException e) {        logger.error("Unable to create a new documentBuilder");        Throwables.propagate(e);    } catch (UnsupportedEncodingException e) {        logger.error("Unsupported Encoding: UTF-8");        Throwables.propagate(e);    } catch (SAXException e) {        logger.error("Error parsing XML");        Throwables.propagate(e);    } catch (IOException e) {        logger.error("IOException");        Throwables.propagate(e);    }    return doc;  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/451493.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号