OneShopNotificationListener.jsp
From Developer's API
(Difference between revisions)
(Created OneShopNotificationListener.jsp) |
|||
(One intermediate revision by one user not shown) | |||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | <%@page import="java.io.*, java.util.*, java.util.regex.*, java.net.*, java.security.*,javax.net.ssl.*" %> | + | <%@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"%> | <%@page contentType="text/plain" pageEncoding="UTF-8"%> | ||
<% | <% | ||
Line 30: | Line 30: | ||
public OneShopNotification lexResponse(String p_strCurrentRequestBody) | public OneShopNotification lexResponse(String p_strCurrentRequestBody) | ||
{ | { | ||
− | OneShopNotification osn = new OneShopNotification(); | + | OneShopNotification osn = new OneShopNotification(); |
− | + | Pattern patNotification = Pattern.compile("<(\\w+)>\\s*<Token>\\s*([^<>\\s]+)\\s*<\\/Token>\\s*<\\/\\1>"); | |
− | Pattern patNotification = Pattern.compile("<(\\w+)>\\s*<Token>\\s*(\\ | + | |
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); | ||
} | } | ||
%> | %> | ||
Line 66: | Line 89: | ||
<head> | <head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | ||
− | <title> | + | <title>MCSSL.COM API Example</title> |
</head> | </head> | ||
<body> | <body> | ||
− | <h4>Welcome to the <u><b> | + | <h4>Welcome to the <u><b>MCSSL.COM</b> API Example</u>.</h4> |
</body> | </body> | ||
</html> | </html> | ||
</pre> | </pre> |
Latest revision as of 20:08, 16 January 2009
<%@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>