OneShopNotificationListener.jsp

From Developer's API

(Difference between revisions)
(Created OneShopNotificationListener.jsp)
 
(Added Logging Functions)
Line 1: Line 1:
 
<pre>
 
<pre>
&lt;%@page import="java.io.*, java.util.*, java.util.regex.*, java.net.*, java.security.*,javax.net.ssl.*" %&gt;
+
&lt;%@page import="java.io.*, java.util.*, java.util.regex.*, java.net.*, java.security.*,javax.net.ssl.*,java.text.SimpleDateFormat" %&gt;
 
&lt;%@page contentType="text/plain" pageEncoding="UTF-8"%&gt;
 
&lt;%@page contentType="text/plain" pageEncoding="UTF-8"%&gt;
 
&lt;%
 
&lt;%
Line 30: Line 30:
 
         public OneShopNotification lexResponse(String p_strCurrentRequestBody)
 
         public OneShopNotification lexResponse(String p_strCurrentRequestBody)
 
         {             
 
         {             
             OneShopNotification osn = new OneShopNotification();
+
             OneShopNotification osn = new OneShopNotification();          
            // &lt;(\w+)&gt;\s*&lt;Token&gt;\s*(\d+)\s*&lt;\/Token&gt;\s*&lt;\/\1&gt;
+
             Pattern patNotification = Pattern.compile("&lt;(\\w+)&gt;\\s*&lt;Token&gt;\\s*([^&lt;&gt;\\s]+)\\s*&lt;\\/Token&gt;\\s*&lt;\\/\\1&gt;");
             Pattern patNotification = Pattern.compile("&lt;(\\w+)&gt;\\s*&lt;Token&gt;\\s*(\\d+)\\s*&lt;\\/Token&gt;\\s*&lt;\\/\\1&gt;");
+
 
             Matcher matchNotification = patNotification.matcher(p_strCurrentRequestBody);
 
             Matcher matchNotification = patNotification.matcher(p_strCurrentRequestBody);
 
             if(matchNotification.find())
 
             if(matchNotification.find())
Line 41: Line 40:
 
              
 
              
 
             return(osn);
 
             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);
 +
            }
 
         }
 
         }
 
     }
 
     }
Line 49: Line 69:
 
     if(osnCurrent.m_Type.equals("NewOrder"))
 
     if(osnCurrent.m_Type.equals("NewOrder"))
 
     {
 
     {
         // perform action with new order notification
+
         // perform action with new order notification      
 +
        apiListener.appendLog(osnCurrent.m_Type + " notification received with a token value of " + osnCurrent.m_Token);
 
     }
 
     }
 
     else
 
     else
Line 55: Line 76:
 
     {
 
     {
 
         // perform action with other notification
 
         // perform action with other notification
 +
        apiListener.appendLog(osnCurrent.m_Type + " notification received with a token value of " + osnCurrent.m_Token);
 
     }
 
     }
 
     else
 
     else
Line 60: Line 82:
 
     {
 
     {
 
         // listener received invalid post information
 
         // listener received invalid post information
 +
        apiListener.appendLog("invalid notification received : \n" + strCurrentRequestBody);
 
     }
 
     }
 
%&gt;
 
%&gt;

Revision as of 14:18, 30 May 2008

<%@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>