com.m2a.bean.M2ACountryIPBlockerBean.java Source code

Java tutorial

Introduction

Here is the source code for com.m2a.bean.M2ACountryIPBlockerBean.java

Source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.m2a.bean;

import com.m2a.biz.M2AProcessBeanBase;
import com.m2a.resources.M2ATokens;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.scaffold.util.ProcessResultBase;
import org.apache.commons.scaffold.util.ResultList;
import org.apache.commons.scaffold.util.ResultListBase;
import com.m2a.sql.M2AAccess;
import org.apache.log4j.Category;

import com.m2a.dto.M2ACustomerRegistrationDTO;
import com.maxmind.www.maxmind_soap.minfraud_soap.MinfraudWebServiceStub;
import com.maxmind.www.maxmind_soap.minfraud_soap.MinfraudWebServiceStub.Minfraud_soap8;
import com.maxmind.www.maxmind_soap.minfraud_soap.MinfraudWebServiceStub.Minfraud_soap8Response;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author srini.vasan
 */
public class M2ACountryIPBlockerBean extends M2AProcessBeanBase {

    private Category log = Category.getInstance(M2ACountryIPBlockerBean.class.getName());

    protected void prepareDto() {
        if (log.isDebugEnabled()) {
            log.debug("*** Executing prepareDto()");
        }
        setDtoByName("com.m2a.dto.M2ACustomerRegistrationDTO");
    }

    public Object execute() throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("*** Executing execute()");
        }
        /*
         * Specific command string for this activity
         * Generally this will a Stored Procedure name with ? delimited by comma for arguments
         */
        /*
         * Parameters are to sent to Persistence access layer in the form of Object array
         */
        Object[] parameters = new Object[5];
        /*
         * Data from the form is copied to the DTO set earlier via prepareDto()
         * getDto() returns Object, hence it has to type casted to specific dto class
         */
        M2ACustomerRegistrationDTO bizDto;
        bizDto = (M2ACustomerRegistrationDTO) getDto();

        /*
         * Populate the parameter array by reading data from DTO
         */
        String countryCode = (String) getFormMap().get("customerCountryCode");
        String ipAddress = (String) getFormMap().get("remoteAddr");

        //get licensekey & requesttype from properties
        String sep = pathSeparator();
        String propsPath = System.getProperty("catalina.base") + sep + "deploy" + sep + "M2A.war" + sep + "WEB-INF"
                + sep + "classes" + sep + "com" + sep + "m2a" + sep + "props" + sep + "ipblocker.props";
        File config = new File(propsPath);
        Properties prop = new Properties();
        prop.load(new FileInputStream(config));
        String licenseKey = prop.getProperty("ipblocker.licensekey");
        String url = prop.getProperty("ipblocker.url");
        //String requestType = prop.getProperty("ipblocker.requesttype");        

        /*MinfraudWebServiceStub minfraudWebServiceStub = new MinfraudWebServiceStub();
        Minfraud_soap8 minfraud_soap80 = new Minfraud_soap8();
        minfraud_soap80.setCountry(countryCode);
        minfraud_soap80.setI(ipAddress);
        minfraud_soap80.setLicense_key(licenseKey);
        minfraud_soap80.setRequested_type(requestType);
            
        Minfraud_soap8Response minfraud_soap8Response = minfraudWebServiceStub.minfraud_soap8(minfraud_soap80); 
        String matchStatus = minfraud_soap8Response.getMinfraud_output().getCountryMatch();
        //String matchStatus = "Yes";
        System.out.println("****Match Status : "+matchStatus);            */
        String command = "p_getCountryBlockedIP(?,?,?,?,?)";
        if (log.isDebugEnabled()) {
            log.debug("*** command string value is " + command);
        }
        parameters[0] = ipAddress;
        parameters[1] = countryCode;
        parameters[2] = new Integer(1);
        parameters[3] = "";
        parameters[4] = "";

        ResultList list = new ResultListBase(
                M2AAccess.findCollection(M2ATokens.APPNAME, bizDto, command, parameters));
        String ipCntryCode = "";
        if ((list != null) && (list.get(0) != null)) {
            M2ACustomerRegistrationDTO resultDTO = (M2ACustomerRegistrationDTO) list.get(0);
            ipCntryCode = resultDTO.getCustomerAddressCountryCode();
        }
        String matchStatus = "";
        if (!ipCntryCode.equals("") && countryCode.equals("UK")) {
            throw new Exception("Please register from the country selected on the registration page");
        } else {
            HttpClient httpClient = new HttpClient();
            GetMethod method = null;
            try {
                //String url = "http://geoip3.maxmind.com/a";
                String queryString = "?l=" + licenseKey + "&i=" + ipAddress;
                // Create a method instance.
                method = new GetMethod(url + queryString);
                // Provide custom retry handler is necessary
                method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                        new DefaultHttpMethodRetryHandler(3, false));
                // Execute the method.
                int statusCode = httpClient.executeMethod(method);
                if (statusCode != HttpStatus.SC_OK) {
                    log.fatal("\nResponse || Method failed: " + method.getStatusLine());
                }
                // Read the response body.
                byte[] responseBody = method.getResponseBody();
                matchStatus = new String(responseBody);
                System.out.println(new String(responseBody));
                //matchStatus = "IN";
            } finally {
                // Release the connection.
                if (method != null) {
                    method.releaseConnection();
                }
            }
            parameters[0] = ipAddress;
            parameters[1] = countryCode;
            parameters[2] = new Integer(2);
            parameters[3] = matchStatus;
            parameters[4] = "";

            if (!matchStatus.equals(countryCode) && countryCode.equals("UK")) {
                list = new ResultListBase(M2AAccess.findCollection(M2ATokens.APPNAME, bizDto, command, parameters));
                throw new Exception("Please register from the country selected on the registration page");
            }
        }
        // if(!matchStatus.equals("Yes") && countryCode.equals("UK")){

        /**
         * Persistence layer class M2AAccess.findCollection returns collection of dto objects.
         * This execute() method must return ProcessResult and ProcessResult holds collection of
         * data in ResultList class, hence the returned collection has to be wrapped in
         * ResultList class.
         */
        /**
         * Got a collection of DTO from access layer
         * Login result will be only one row ie. one DTO
         * We need to refer the first DTO only in the processResult
         * To refer to the first DTO in processAction we need setSingleForm to true
         */
        ProcessResultBase result = new ProcessResultBase(list);

        result.setSingleForm(true);
        /**
         * set as the Client scope
         */
        if (log.isDebugEnabled()) {
            log.debug("*** Set scope for process ProcessResult to " + M2ATokens.EMPTY);
        }
        result.setScope(M2ATokens.EMPTY);
        return result;
    }

    public String pathSeparator() {
        String seperator = System.getProperties().getProperty("file.separator");
        if (seperator.equals("\\")) {
            seperator = "\\\\";
        }
        return seperator;
    }
}