Knowledge Center

Knowledge Center > RBM Solutions > Notifications > Sample Code for a Callout Receiver

Sample Code for a Callout Receiver

Handler Code Examples

The following examples show Java and PHP examples of handler code. 

 

Handler Java Code Example

packagecom.lsxy.tmsoft.service;

import java.io.*;
importjava.util.*;
importjava.text.*;
importjavax.servlet.*;
importjavax.servlet.http.*;

@SuppressWarnings("serial")
public class HandlerServletDemo extends HttpServlet {

        public long getLastModified(HttpServletRequestreq) {
                ServletContext context = getServletContext();
                Date timestamp = (Date) context.getAttribute("timestamp");
                if (timestamp == null)
                        return -1;
                else
                        returntimestamp.getTime();
        }

        public void init() {
                ServletContext context = getServletContext();
                context.setAttribute("timestamp", new Date());
                context.setAttribute("counter", new Integer(0));
        }

        private void handlerData(HttpServletRequestreq, HttpServletResponseres,StringmethodType)
                        throwsServletException, IOException {

                System.out.println("req.getContentLength() = " + req.getContentLength());
                String name = req.getParameter("name");
                System.out.println("name value = " + name);
               
                PrintWriter out = res.getWriter();

                if (req.getContentLength() > 0) {
                        InputStream in = req.getInputStream();
                        File f = new File("c:/temp", methodType+".txt");
                        FileOutputStream o = new FileOutputStream(f);
                        byte b[] = new byte[1024];
                        int n;
                        while ((n = in.read(b)) != -1) {
                                o.write(b, 0, n);
                        }
                        o.close();
                        in.close();
                        out.print("File upload success!");
                } else {
                        out.print("No file!");
                }

                res.setContentType("text/html");
                out.println("<HTML>");
                out.println("<HEAD><TITLE>Cache Test</TITLE></HEAD>");
                out.println("<BODY>");

                ServletContext context = getServletContext();
                String reload = req.getParameter("reload");
                // change the timestamp if paramater reload is true
                if (reload != null &&reload.equals("true")) {
                        context.setAttribute("timestamp", new Date());
                }
                // use counter and date to monitor if page updated
                Integer i = (Integer) context.getAttribute("counter");
                int x = i.intValue() + 1;
                context.setAttribute("counter", new Integer(x));
                out.println("<P><b>counter:" + x + "  </b>date:" + new Date() + "</b>");

                out.println("</BODY>");
                out.println("</HTML>");

        }

        public void doPost(HttpServletRequestreq, HttpServletResponse res)
                        throwsServletException, IOException {
                System.out.println("-------    doPostinit     -------");
                handlerData(req, res ,"doPost");
                System.out.println("-------    doPost end      -------");

        }

        public void doPut(HttpServletRequestreq, HttpServletResponse res) throws ServletException, IOException {
                System.out.println("-------    doPutinit     -------");
                handlerData(req, res ,"doPut");
                System.out.println("-------    doPut end      -------");
        }

        public void doGet(HttpServletRequestreq, HttpServletResponse res)
                        throwsServletException, IOException {
                System.out.println("-------    doGetinit     -------");
                handlerData(req, res , "doGet");
                System.out.println("-------    doGet end      -------");
        }
       
        public void doDelete(HttpServletRequestreq, HttpServletResponse res)
                        throwsServletException, IOException {
                System.out.println("-------    doDeleteinit     -------");
                handlerData(req, res , "doDelete");
                System.out.println("-------    doDelete end      -------");
        }
}

Handler PHP Code Example

<?php
_handle();
function _handle(){
    switch($_SERVER['REQUEST_<wbr/>METHOD'])
    {
        case 'GET': _handleGet($_GET); break;
        case 'POST': _handlePost($_POST); break;
    }
}
function _handleGet(){
    $msg = "_GET : ";
    # output
    foreach($_GET as $key=>$val){
        $msg .= "[".$key." = ".$val."]";
    }
    # do something
    doLog($msg);
}

function _handlePost(){
    # output
    # postdata is xml format string.
    $postdata = file_get_contents("php://<wbr/>input");

    # do something
    doLog($postdata);
}

function doLog($msg){
    $log_dir = "callout.log";
    $date = date('d.m.Y h:i:s');
    error_log("\n --- ", 3, $log_dir);
    error_log($date, 3, $log_dir);
    error_log("     ", 3, $log_dir);
    error_log($msg, 3, $log_dir);
}
?>

Web.xml File

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" version="2.5">
        <display-name>TestWeb</display-name>
        <welcome-file-list>
                <welcome-file>index.html</welcome-file>
                <welcome-file>index.htm</welcome-file>
                <welcome-file>index.jsp</welcome-file>
                <welcome-file>default.html</welcome-file>
                <welcome-file>default.htm</welcome-file>
                <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>


        <servlet>
                <servlet-name>HandlerDemo</servlet-name>
                <servlet-class>com.lsxy.tmsoft.service.HandlerServletDemo</servlet-class>
        </servlet>

        <servlet-mapping>
                <servlet-name>HandlerDemo</servlet-name>
                <url-pattern>/handlerDemo</url-pattern>
        </servlet-mapping>
</web-app>



 

Last modified
21:08, 18 Mar 2015

Tags

Classifications

(not set)