OneShopNotificationListener.jsp

From Developer's API

Revision as of 20:08, 16 January 2009 by Admin (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
<%@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>MCSSL.COM API Example</title>
    </head>
    <body>
        <h4>Welcome to the <u><b>MCSSL.COM</b> API Example</u>.</h4>
    </body>
</html>