OneShopAPI.jsp

From Developer's API

(Difference between revisions)
m (changed return of empty strings from "" to (new String()))
 
Line 161: Line 161:
 
     <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>1ShoppingCart.COM API Example</title>
+
         <title>MCSSL.COM API Example</title>
 
     </head>
 
     </head>
 
     <body>
 
     <body>
         <h4>Welcome to the <u><b>1ShoppingCart.COM</b> API Example</u>.</h4>
+
         <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.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    class OneShopAPI
    {
        private String m_strMerchantID,m_strMerchantKey,m_strAPIURI;
        public HashMap m_hmParameters;
        
        public OneShopAPI(String p_strMerchantID,String p_strMerchantKey, String p_strAPIURI)
        {
            m_strMerchantID = p_strMerchantID;
            m_strMerchantKey = p_strMerchantKey;
            m_strAPIURI = p_strAPIURI;
            m_hmParameters = new HashMap();
            addParameter("Key",m_strMerchantKey);
        }
        
        public void addParameter(String p_strCurrentKey,String p_strCurrentValue)
        {            
            m_hmParameters.put(p_strCurrentKey,p_strCurrentValue);
        }
        
        public void deleteParameter(String p_strCurrentKey)
        {
            m_hmParameters.remove(p_strCurrentKey);
        }
        
        public void clearParameters()
        {
            m_hmParameters.clear();
            addParameter("Key",m_strMerchantKey);
        }
        
        public void buildParameters(String p_strCurrentResponseBody)
        {
            int iKeyValueCount = 2;
            clearParameters();
            Pattern patNextRecordSet = Pattern.compile("\\s*<NextRecordSet>(?:\\s*<\\w+>[^<>]*<\\/\\w+>\\s*)*<\\/NextRecordSet>");
            Matcher matchNextRecordSet = patNextRecordSet.matcher(p_strCurrentResponseBody);
            if(matchNextRecordSet.find())
            {
                Pattern patParameters = Pattern.compile("\\s*<(\\w+)>\\s*([^<>]*)<\\/\\1>\\s*");                
                Matcher matchParameters = patParameters.matcher(matchNextRecordSet.group());
                while(matchParameters.find())                                    
                    addParameter(matchParameters.group(1),matchParameters.group(2));
                    
            }
        }
        
        public String sendHTTPSRequest(String p_strCurrentURI,String p_strCurrentRequestBody)
        {
            try
            {
                URL urlCurrent = new URL(p_strCurrentURI);
                URLConnection ucCurrent = urlCurrent.openConnection();
                HttpsURLConnection httpsCurrentConnection = (HttpsURLConnection)ucCurrent;
                
                httpsCurrentConnection.setDoOutput(true);
                httpsCurrentConnection.setDoInput(true);
                httpsCurrentConnection.setRequestMethod("POST");
                
                OutputStream outRequest = httpsCurrentConnection.getOutputStream();
                Writer woutRequest = new OutputStreamWriter(outRequest);
                woutRequest.write(p_strCurrentRequestBody);
                woutRequest.flush();
                woutRequest.close();
                
                InputStream inResponse = httpsCurrentConnection.getInputStream();
                Reader rinResponse = new InputStreamReader(inResponse,"UTF-8");
                
                StringBuffer sbCurrent = new StringBuffer();
                int chCurrent = 0,iError = -1;
                while((chCurrent = rinResponse.read()) != iError)
                    sbCurrent.append((char)chCurrent);
                
                return(sbCurrent.toString());
            }
            catch(Exception eCurrent)
            {            
                return(new String());
            }
        }  
             
        public String apiRequest(String p_strCurrentMethod)
        {
            String strCurrentURI = m_strAPIURI + "/API/" + m_strMerchantID + p_strCurrentMethod;
            String strCurrentRequestBody = createRequestBody(m_hmParameters);
            String strCurrentResponseBody = sendHTTPSRequest(strCurrentURI,strCurrentRequestBody);
            return(strCurrentResponseBody);
        }
        
        public String createRequestBody(HashMap p_hmCurrentParameters)
        {
            String strCurrentRequestBody = new String();            
            if(p_hmCurrentParameters.size() > 0)
            {
                strCurrentRequestBody += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
                strCurrentRequestBody += "<Request>\n";
                for (Iterator iterCurrent = p_hmCurrentParameters.entrySet().iterator(); iterCurrent.hasNext();)
                {         
                    Map.Entry entCurrent = (Map.Entry)iterCurrent.next();
                    strCurrentRequestBody += "\t";
                    strCurrentRequestBody += "<" + (String)entCurrent.getKey() + ">";
                    strCurrentRequestBody += (String)entCurrent.getValue();
                    strCurrentRequestBody += "</" + (String)entCurrent.getKey() + ">";
                    strCurrentRequestBody += "\n";
                }
                strCurrentRequestBody += "</Request>";
                
            }
            
            return(strCurrentRequestBody);            
        }
        
        public String apiRequestXLink(String p_strCurrentXLink)
        {
            String strCurrentRequestBody = createRequestBody(m_hmParameters);
            String strCurrentResponseBody = sendHTTPSRequest(p_strCurrentXLink,strCurrentRequestBody);
            return(strCurrentResponseBody);
        }
        
        public String getOrdersList()
        {
            return(apiRequest("/ORDERS/LIST"));
        }
        
        public String getOrderById(String p_strCurrentOrderId)
        {
            return(apiRequest("/ORDERS/" + p_strCurrentOrderId + "/READ"));
        }
        
        public String getClientsList()
        {
            return(apiRequest("/CLIENTS/LIST"));
        }
        
        public String getClientById(String p_strCurrentClientId)
        {
            return(apiRequest("/CLIENTS/" + p_strCurrentClientId + "/READ"));
        }
        
        public String getProductsList()
        {
            return(apiRequest("/PRODUCTS/LIST"));
        }
        
        public String getProductById(String p_strCurrentProductId)
        {
            return(apiRequest("/PRODUCTS/" + p_strCurrentProductId + "/READ"));
        }
        
        public String getErrorsList()
        {
            return(apiRequest("/ERRORS/LIST"));
        }
    }
%>
<!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>