Package org.cafesip.reference.jiplet

Source Code of org.cafesip.reference.jiplet.SipRecorder

/*
* Created on Nov 22, 2004
*
*/
package org.cafesip.reference.jiplet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Iterator;

import javax.sip.header.CSeqHeader;
import javax.sip.header.CallIdHeader;
import javax.sip.header.FromHeader;
import javax.sip.header.ToHeader;
import javax.sip.message.Request;
import javax.sip.message.Response;

import org.cafesip.jiplet.Jiplet;
import org.cafesip.jiplet.JipletException;
import org.cafesip.jiplet.JipletRequest;
import org.cafesip.jiplet.JipletResponse;
import org.cafesip.jiplet.config.jip.InitParam;
import org.cafesip.jiplet.config.jip.InitParams;
import org.cafesip.jiplet.config.jip.JipletConfig;

/**
* The SipRecorder jiplet does not receive any messages from the container itself. But other
* jiplets forward requests, responses and timeout events to this jiplet. This jiplet records the
* events in a database log table.
*
* @author amit
*/
public class SipRecorder extends Jiplet
{
    private Connection connection = null;

    /**
     * 
     */
    public SipRecorder()
    {
    }

    /*
     * @see org.cafesip.jiplet.Jiplet#init(org.cafesip.jiplet.config.jip.JipletConfig)
     */
    public void init(JipletConfig config) throws JipletException
    {
        info(" being initialized");

        InitParams init = config.getInitParams();
        Iterator iter = init.getInitParam().iterator();
        String jdbc = null;
        String url = null;
        String user = null;
        String password = null;
        while (iter.hasNext() == true)
        {
            InitParam parm = (InitParam) iter.next();

            if (parm.getParamName().equals("jdbcDriver") == true)
            {
                jdbc = parm.getParamValue();
            }
            else if (parm.getParamName().equals("dbUrl") == true)
            {
                url = parm.getParamValue();
            }
            else if (parm.getParamName().equals("dbUser") == true)
            {
                user = parm.getParamValue();
            }
            else if (parm.getParamName().equals("dbPassword") == true)
            {
                password = parm.getParamValue();
            }

            if (isDebugEnabled() == true)
            {
                debug("Init param (" + parm.getParamName() + ", "
                        + parm.getParamValue() + ")");
            }
        }

        // print the application scope variables. The purpose of the code
        // segment below
        // is to demonstrate how to access application-scope variables. The
        // variables
        // printed below were set by the SipRegistrar jiplet. In order for this
        // to work,
        // the SipRegistrar jiplet must be started first. Take a look at the
        // startup-order
        // element in the jip,xml file to understand how to control the startup
        // order.
        Enumeration en = getJipletContext().getAttributeNames();
        while (en.hasMoreElements() == true)
        {
            String key = (String) en.nextElement();
            if (isDebugEnabled() == true)
            {
                debug("Application scope param (" + key + ", "
                        + getJipletContext().getAttribute(key) + ")");
            }
        }

        if (jdbc != null)
        {
            try
            {
                Class.forName(jdbc).newInstance();
                connection = DriverManager.getConnection(url, user, password);

                createLogTable();
            }
            catch (Exception e)
            {
                error("Could not initialized the database connection: " + e.getMessage()
                        + ". The call records will not be stored in the database");
            }
        }
    }

    private void createLogTable() throws SQLException
    {
        if (connection == null)
        {
            return;
        }

        String sql = "CREATE TABLE IF NOT EXISTS SipLogs  ("
                + " type VARCHAR(100) not null,"
                + " methodOrStatus VARCHAR(25) not null,"
                + " fromUser VARCHAR(255) not null,"
                + " toUser VARCHAR(255) not null,"         
                + " callId VARCHAR(255) not null,"
                + " cseq VARCHAR(255) not null,"
                + " time TIMESTAMP" + ")";

        Statement st = connection.createStatement();
        st.executeUpdate(sql);
    }

    private void printLog(String type, String methodOrStatus, String from,
            String to, String callId, String cseq) throws SQLException
    {
        if (connection == null)
        {
            return;
        }

        String sql = "INSERT INTO SipLogs (type, methodOrStatus, fromUser, toUser, callid, cseq, time) VALUES ("
                + "?, ?, ?, ?, ?, ?,  NULL" + ")";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1, type);
        ps.setString(2, methodOrStatus);
        ps.setString(3, from);
        ps.setString(4, to);
        ps.setString(5, callId);
        ps.setString(6, cseq);
        ps.executeUpdate();
    }

    /*
     * @see org.cafesip.jiplet.Jiplet#destroy()
     */
    public void destroy()
    {
        if (connection != null)
        {
            try
            {
                connection.close();
            }
            catch (SQLException e)
            {
                info("Exception while closing database connection: "
                        + e.getMessage());
            }
        }
    }

    /*
     * @see org.cafesip.jiplet.Jiplet#processRequest(org.cafesip.jiplet.JipletRequest)
     */
    public void processRequest(JipletRequest request)
    {
        Request req_msg = request.getRequestEvent().getRequest();

        try
        {
            printLog("REQ", req_msg.getMethod(), req_msg.getHeader(
                    FromHeader.NAME).toString(), req_msg.getHeader(
                    ToHeader.NAME).toString(), req_msg.getHeader(
                    CallIdHeader.NAME).toString(), req_msg.getHeader(
                    CSeqHeader.NAME).toString());
        }
        catch (SQLException e)
        {
            error("Error writing request record into the database: "
                    + e.getMessage());
        }
    }

    /*
     * @see org.cafesip.jiplet.Jiplet#processResponse(org.cafesip.jiplet.JipletResponse)
     */
    public void processResponse(JipletResponse response)
    {
        Response rsp_msg = response.getResponseEvent().getResponse();
        try
        {
            printLog("RSP", (new Integer(rsp_msg.getStatusCode())).toString(),
                    rsp_msg.getHeader(FromHeader.NAME).toString(), rsp_msg
                            .getHeader(ToHeader.NAME).toString(), rsp_msg
                            .getHeader(CallIdHeader.NAME).toString(), rsp_msg
                            .getHeader(CSeqHeader.NAME).toString());
        }
        catch (SQLException e)
        {
            error("Error writing response record into the database: "
                    + e.getMessage());
        }
    }
}
TOP

Related Classes of org.cafesip.reference.jiplet.SipRecorder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.