⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 http.java

📁 Software Testing Automation Framework (STAF)的开发代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

            }
            catch (InvalidCookieIDException e)
            {
                session.addCookie(name, value);
            }
        }
    }
    else if (pResult.optionTimes(FORM) > 0)
    {
        String id = "";
        int idType = -1;

        if (pResult.optionTimes(ID) > 0)
        {
            res = STAFUtil.resolveRequestVar(
                pResult.optionValue(ID), sHandle, info.requestNumber);

            if (res.rc != 0) return res;

            id = res.result;
            idType = WebSession.ID_ID_TYPE;
        }
        else if (pResult.optionTimes(NAME) > 0)
        {
            res = STAFUtil.resolveRequestVar(
                pResult.optionValue(NAME), sHandle, info.requestNumber);

            if (res.rc != 0) return res;

            id = res.result;
            idType = WebSession.NAME_ID_TYPE;
        }
        else if (pResult.optionTimes(INDEX) > 0)
        {
            res = STAFUtil.resolveRequestVar(
                pResult.optionValue(INDEX), sHandle, info.requestNumber);

            if (res.rc != 0) return res;

            id = res.result;
            idType = WebSession.INDEX_ID_TYPE;
        }
        else
        {
            // default to the 1st form on the page
            id = "1";
            idType = WebSession.INDEX_ID_TYPE;
        }
        
        if (pResult.optionTimes(CONTROLNAME) > 0)
        {
            res = STAFUtil.resolveRequestVar(
                pResult.optionValue(CONTROLNAME), sHandle, info.requestNumber);

            if (res.rc != 0) return res;

            String control = res.result;

            res = STAFUtil.resolveRequestVar(
                pResult.optionValue(VALUE), sHandle, info.requestNumber);

            if (res.rc != 0) return res;

            String value = res.result;

            try
            {
                session.setFormElement(id, idType, control, value);
            }
            catch (InvalidElementIDException e)
            {
                return new STAFResult(STAFResult.DoesNotExist, e.getMessage());
            }
            catch(InvalidParameterValueException e)
            {
                return new STAFResult(STAFResult.InvalidValue, e.getMessage());
            }
        }
    }

    return new STAFResult(STAFResult.Ok, result);
}

/*****************************************************************************/
/*                                                                           */
/* Method: handleMethod                                                      */
/* Description: creates the specified web request submits it, and return the */
/*              output.                                                      */
/* Parameters: info - request info passed to acceptRequest                   */
/* Returns: STAFResult with the result of the requested method               */
/*                                                                           */
/*****************************************************************************/

private STAFResult handleMethod(
    STAFServiceInterfaceLevel30.RequestInfo info) throws STAFException
{
    STAFCommandParseResult pResult = requestParser.parse(info.request);

    if (pResult.rc != 0)
    {
        return new STAFResult(STAFResult.InvalidRequestString,
                              pResult.errorBuffer);
    }

    if (pResult.optionTimes(SESSION) == 0)
        return handleNonSessionMethod(info);

    return handleSessionMethod(info);

}

/*****************************************************************************/
/*                                                                           */
/* Method: handleNonSessionMethod                                            */
/* Description: creates the specified web request submits it, and return the */
/*              output.                                                      */
/* Parameters: info - request info passed to acceptRequest                   */
/* Returns: STAFResult with the result of the requested method               */
/*                                                                           */
/*****************************************************************************/

private STAFResult handleNonSessionMethod(
    STAFServiceInterfaceLevel30.RequestInfo info) throws STAFException
{
    // create new session
    WebSession newSession = new WebSession(sessionList, info, this);

    int sessionId = newSession.getID();

    // add SESSION <sessionId> to info
    info.request += " SESSION " + sessionId;

    STAFResult res;

    try
    {
        // execute request
        res = handleSessionMethod(info);
    }
    catch (STAFException e)
    {
        // delete session if exception occurs during method request
        try{
            sessionList.deleteSession(sessionId);

        }catch (InvalidSessionIDException ide)
        {}
        throw e;
    }

    // delete session
    try{
        sessionList.deleteSession(sessionId);

    }catch (InvalidSessionIDException e)
    {}

    return res;
}

/*****************************************************************************/
/*                                                                           */
/* Method: handleSessionMethod                                               */
/* Description: creates the specified web request submits it, and return the */
/*              output for an existing session.                              */
/* Parameters: info - request info passed to acceptRequest                   */
/* Returns: STAFResult with the result of the requested method               */
/*                                                                           */
/*****************************************************************************/

private STAFResult handleSessionMethod(
    STAFServiceInterfaceLevel30.RequestInfo info) throws STAFException
{
    // Parse the request

    STAFCommandParseResult pResult = requestParser.parse(info.request);

    if (pResult.rc != 0)
    {
        return new STAFResult(STAFResult.InvalidRequestString,
                              pResult.errorBuffer);
    }
    
    // Get method type

    String method = "";
    String action = "";
    STAFResult res = new STAFResult();

    if (pResult.optionTimes(METHOD) > 0)
    {
        res = STAFUtil.resolveRequestVar(
            pResult.optionValue(METHOD), sHandle, info.requestNumber);

        if (res.rc != 0) return res;

        method = res.result;
        action = "REQUEST METHOD";
    }
    else if (pResult.optionTimes(DOPOST) > 0)
    {
        method = "POST";
        action = "DOPOST";
    }
    else if (pResult.optionTimes(DOGET) > 0)
    {
        method = "GET";
        action = "DOGET";
    }

    // Verify the requester has at least trust level 3

    STAFResult trustResult = STAFUtil.validateTrust(
        3, fServiceName, action, fLocalMachineName, info);

    if (trustResult.rc != STAFResult.Ok) return trustResult;

    // Get resolved value for SESSION option and make sure it's an integer

    res = STAFUtil.resolveRequestVarAndCheckInt(
        SESSION, pResult.optionValue(SESSION), sHandle, info.requestNumber);

    if (res.rc != 0) return res;

    int sessionId = Integer.parseInt(res.result);

    // Resolve the URL option value

    res = STAFUtil.resolveRequestVar(
        pResult.optionValue(URL), sHandle, info.requestNumber);

    if (res.rc != 0) return res;

    String url = res.result;

    WebSession session;

    // Get the session
    try
    {
        session = sessionList.getSession(sessionId);
    }
    catch (InvalidSessionIDException e)
    {
        return new STAFResult(STAFResult.DoesNotExist, e.getMessage());
    }

    if (!session.isOwner(info.stafInstanceUUID))
    {
        // Verify the requester has at least trust level 4 since not the
        // session owner

        trustResult = STAFUtil.validateTrust(
            4, fServiceName, action, fLocalMachineName, info);

        if (trustResult.rc != STAFResult.Ok) return trustResult;
    }

    // Get headers

    int numHeaders = pResult.optionTimes(HEADER);
    HashMap headers = null;

    if (numHeaders > 0)
    {
        headers = new HashMap();

        for (int i = 1; i <= numHeaders; i++)
        {
            res = STAFUtil.resolveRequestVar(
                pResult.optionValue(HEADER, i), sHandle, info.requestNumber);

            if (res.rc != 0) return res;

            String h = res.result;
            int equalPos = h.indexOf("=");

            if (equalPos == -1)
            {
                return new STAFResult(STAFResult.InvalidValue,
                                      "Invalid HEADER: " + h);
            }

            String key = h.substring(0, equalPos);
            String value = h.substring(equalPos + 1);
            headers.put(key, value);
        }
    }

    // Get POST parameters

    Vector params = new Vector();
    int numParams = pResult.optionTimes(PARAMETER);

    if (numParams > 0)
    {
        for (int i = 1; i <= numParams; i++)
        {
            res = STAFUtil.resolveRequestVar(
                pResult.optionValue(PARAMETER, i), sHandle, info.requestNumber);

            if (res.rc != 0) return res;

            String h = res.result;
            int equalPos = h.indexOf("=");

            if (equalPos == -1)
            {
                return new STAFResult(STAFResult.InvalidValue,
                                      "Invalid PARAMETER: " + h);
            }

            String key = h.substring(0, equalPos);
            String value = h.substring(equalPos + 1);

            Vector pair = new Vector(2);
            pair.addElement(key);
            pair.addElement(value);

            params.addElement(pair);
        }
    }
    else
        params = null;

    // Files to be added to the request
    Vector files = new Vector();
    int numFiles = pResult.optionTimes(CONTENTFILE);

    if (numFiles > 0)
    {
        for (int i = 1; i <= numFiles; i++)
        {
            res = STAFUtil.resolveRequestVar(
                pResult.optionValue(CONTENTFILE, i), sHandle,
                info.requestNumber);

            if (res.rc != 0) return res;

            String h = res.result;
            int equalPos = h.indexOf("=");

            if (equalPos == -1)
            {
                return new STAFResult(STAFResult.InvalidValue,
                                      "Invalid CONTENTFILE: " + h);
            }

            String key = h.substring(0, equalPos);
            String value = h.substring(equalPos + 1);

            // if CONTENTFILEMACHINE option enabled
            //   get ContentFileMachine with same key / parameter name
            //   copy file from contentFileMachine to
            //    service temp dir
            //   change value to temp file path

            Vector pair = new Vector(2);
            pair.addElement(key);
            pair.addElement(value);

            files.addElement(pair);
       }
    }
    else
        files = null;

    // Get content

    String content = null;

    if (pResult.optionTimes(CONTENT) > 0)
    {
        res = STAFUtil.resolveRequestVar(
            pResult.optionValue(CONTENT), sHandle, info.requestNumber);

        if (res.rc != 0) return res;

        content = res.result;
    }

    // Resolve auto redirection on 3XX returns

    Boolean redirect = null;
    if (pResult.optionTimes(AUTOREDIRECT) > 0)
        redirect = new Boolean(true);
    if (pResult.optionTimes(NOAUTOREDIRECT) > 0)
        redirect = new Boolean(false);

    // Check if the toFile option was specified

    String toFile = null;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -