1
\$\begingroup\$

I have written an imperfect utility for parameter verification and error checking of simple parameters passed to a Java Servlet via GET/POST requests. As a junior programmer, I got tired of having to check for things like null/empty values and testing whether certain parameters such as password, email, phone number etc. are of the right length and subsequently building response elements to notify the user such input is incorrect. The implementation of such checks is below as well as the self contained code for the utility. I am curious how I can make this better or whether it is even necessary at all.

protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    ParameterVerificationUtility veri = new ParameterVerificationUtility();

    String email = request.getParameter("email");
    veri.addPairs(veri.new ParameterPair("email", email, ParameterPair.V_NOT_EMPTY | ParameterPair.V_NOT_NULL | ParameterPair.V_LENGTH,10,100));
    String password = request.getParameter("password");
    veri.addPairs(veri.new ParameterPair("password", password, ParameterPair.V_NOT_EMPTY | ParameterPair.V_NOT_NULL | ParameterPair.V_LENGTH,10,100));
    String first_name = request.getParameter("first_name");
    veri.addPairs(veri.new ParameterPair("first name", first_name, ParameterPair.V_NOT_EMPTY | ParameterPair.V_NOT_NULL | ParameterPair.V_LENGTH,10,100));
    String last_name = request.getParameter("last_name");
    veri.addPairs(veri.new ParameterPair("last name", last_name, ParameterPair.V_NOT_EMPTY | ParameterPair.V_NOT_NULL | ParameterPair.V_LENGTH,10,100));
    String zipcode = request.getParameter("zipcode");
    veri.addPairs(veri.new ParameterPair("zipcode", zipcode, ParameterPair.V_NOT_EMPTY | ParameterPair.V_NOT_NULL | ParameterPair.V_LENGTH,10,100));

    if(veri.checkPairs() != 0)
    {
        JSONObject server_response = new JSONObject();
        server_response.put("request_status", "failure");

        JSONArray errorList = new JSONArray();

        System.out.println("Error List size = "+veri.getErrorLists().size());

        for(List<TypeErrors> listErrors: veri.getErrorLists())
        {
            for(TypeErrors e: listErrors)
            {
                JSONObject error = new JSONObject();
                error.put("parameter", e._key);
                error.put("error_code",e._code);
                error.put("details",e._error);
                errorList.add(error);
            }

        }

        server_response.put("errors", errorList);
        sendResponse(server_response.toString(),response);
        return;
    }
//Otherwise execute the correct action below...etc...
}

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.yourapphere.utils.ParameterVerificationUtility.ParameterPair.TypeErrors;

public class ParameterVerificationUtility {

List<ParameterPair> pairs;
List<List<TypeErrors>> errors;

public ParameterVerificationUtility()
{
    pairs = new ArrayList<>();
    errors = new ArrayList<>();
}


public void addPairs(String key, String val, int bitMask )
{
    pairs.add(new ParameterPair(key, val, bitMask));
}

public void addPairs(ParameterPair pair)
{
    pairs.add(pair);
}

public int checkPairs()
{
    int incorrect = 0;

    for(ParameterPair pp: pairs)
    {
        if(!pp.verifyConstraints())
        {
            errors.add(pp.getErrors());
            incorrect++;
        }
    }

    return incorrect;
}

public List<List<TypeErrors>> getErrorLists()
{
    return errors;
}


public class ParameterPair{

    String _key;
    Object _val;

    public static final int V_NOT_NULL = 1;
    public static final int V_NOT_EMPTY = (1 << 1);
    public static final int V_IS_NUMERIC = (1 << 2);
    public static final int V_INTEGER = (1 << 3);
    public static final int V_DOUBLE = (1 << 4);
    public static final int V_LENGTH = (1 << 5);

    List<TypeErrors> _typeErrors = new ArrayList<>();
    int _typeBitMask = 0x0000;
    int _minLength = -1;
    int _maxLength = -1;

    public boolean _verified = true;

    public ParameterPair(String key, Object val)
    {
        _key = key;
        _val = val;
    }

    public ParameterPair(String key, Object val, int bitMask)
    {
        this(key,val);
        _typeBitMask = bitMask;
    }
    public ParameterPair(String key, Object val, int bitMask, int lengthStart, int lengthEnd)
    {
        this(key,val,bitMask);
        _minLength = lengthStart;
        _maxLength = lengthEnd;
    }

    public boolean verifyConstraints()
    {
        return verify();
    }

    public boolean verifyConstraints(int bitMask)
    {
        _typeBitMask = bitMask;
        return verify();
    }

    public List<TypeErrors> getErrors()
    {
        return _typeErrors;
    }

    private boolean verify()
    {
        boolean verified = true;

        if((V_NOT_NULL & _typeBitMask) == 1 )
        {
            if(_val == null)
            {
                verified = false;
                _typeErrors.add(new TypeErrors(_key,"Input value is Null or missing",V_NOT_NULL));
            }
        }           
        else if((V_NOT_EMPTY & _typeBitMask) == 1)
        {
            String temp = (String)_val;
            if(temp.isEmpty())
            {   verified = false;
                _typeErrors.add(new TypeErrors(_key,"Input value is empty",V_NOT_EMPTY));
            }
        }

        if((V_IS_NUMERIC & _typeBitMask) == 1)
        {
            String temp = (String)_val;
            if(!StringUtils.isNumericSpace(temp))
            {
                verified = false;
                _typeErrors.add(new TypeErrors(_key,"Input value is not a number",V_IS_NUMERIC));
            }
        }

        if((V_LENGTH & _typeBitMask) == 1)
        {
            String temp = (String)_val;
            if(temp != null)
            {
                if(temp.length() < _minLength)
                    _typeErrors.add(new TypeErrors(_key,"Input value Length must be at least "+_minLength+" characters",V_LENGTH));
                if(temp.length() > _maxLength)
                    _typeErrors.add(new TypeErrors(_key,"Input value Length must be at most "+_maxLength,V_LENGTH));
            }
        }



        return verified;

    }

    public class TypeErrors{

        public String _key;
        public String _error;
        public int _code;

        public TypeErrors(String key, String error, int code)
        {
            _key = key;
            _error = error;
            _code = code;
        }
    }
}
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.