OneShopNotificationListener.jsp

From Developer's API

Revision as of 14:18, 30 May 2008 by Jasonm (Talk | contribs)
<%@page import="java.io.*, java.util.*, java.util.regex.*, java.net.*, java.security.*,javax.net.ssl.*,java.text.SimpleDateFormat" %>
<%@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();            
            Pattern patNotification = Pattern.compile("<(\\w+)>\\s*<Token>\\s*([^<>\\s]+)\\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);
        }
        
        public boolean appendLog(String p_strCurrentItem) 
        {
            BufferedWriter bwCurrent;
            String strOutput = new String();
            try 
            {
                Calendar calCurrent = Calendar.getInstance();
                SimpleDateFormat sdfCurrent = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");                
                strOutput = sdfCurrent.format(calCurrent.getTime()) + " ; " + p_strCurrentItem + "\n";
                bwCurrent = new BufferedWriter(new FileWriter("oneshop_notifications.log",true));                
                bwCurrent.write(strOutput,0,strOutput.length());                
                bwCurrent.flush();
                bwCurrent.close();
                return(true);
            }
            catch (IOException e) 
            {
              return(false);
            }
        }
    }
    
    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        
        apiListener.appendLog(osnCurrent.m_Type + " notification received with a token value of " + osnCurrent.m_Token);
    }
    else
    if(osnCurrent.m_Type.equals("OtherNotification"))
    {
        // perform action with other notification
        apiListener.appendLog(osnCurrent.m_Type + " notification received with a token value of " + osnCurrent.m_Token);
    }
    else
    if(osnCurrent.m_Type.equals(""))
    {
        // listener received invalid post information
        apiListener.appendLog("invalid notification received : \n" + strCurrentRequestBody);
    }
%>
<!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>