OneShopNotificationListener.jsp
From Developer's API
(Difference between revisions)
Revision as of 20:32, 29 May 2008
<%@page import="java.io.*, java.util.*, java.util.regex.*, java.net.*, java.security.*,javax.net.ssl.*" %> <%@page contentType="text/plain" pageEncoding="UTF-8"%> <% class OneShopNotification { public String m_Type = new String(); public String m_Token = new String(); } class OneShopNotificationListener { public String readRawPost(HttpServletRequest req) { try { Reader isrCurrent = new InputStreamReader(req.getInputStream(),"UTF-8"); StringBuffer sbCurrent = new StringBuffer(); int chCurrent = 0,iError = -1; while((chCurrent = isrCurrent.read()) != iError) sbCurrent.append((char)chCurrent); return(sbCurrent.toString()); } catch(Exception e) { return(new String()); } } public OneShopNotification lexResponse(String p_strCurrentRequestBody) { OneShopNotification osn = new OneShopNotification(); // <(\w+)>\s*<Token>\s*(\d+)\s*<\/Token>\s*<\/\1> Pattern patNotification = Pattern.compile("<(\\w+)>\\s*<Token>\\s*(\\d+)\\s*<\\/Token>\\s*<\\/\\1>"); Matcher matchNotification = patNotification.matcher(p_strCurrentRequestBody); if(matchNotification.find()) { osn.m_Type = matchNotification.group(1); osn.m_Token = matchNotification.group(2); } return(osn); } } OneShopNotificationListener apiListener = new OneShopNotificationListener(); String strCurrentRequestBody = apiListener.readRawPost(request); OneShopNotification osnCurrent = apiListener.lexResponse(strCurrentRequestBody); if(osnCurrent.m_Type.equals("NewOrder")) { // perform action with new order notification } else if(osnCurrent.m_Type.equals("OtherNotification")) { // perform action with other notification } else if(osnCurrent.m_Type.equals("")) { // listener received invalid post information } %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>1ShoppingCart.COM API Example</title> </head> <body> <h4>Welcome to the <u><b>1ShoppingCart.COM</b> API Example</u>.</h4> </body> </html>