📄 http.java
字号:
package com.ibm.staf.service.http;
/*****************************************************************************/
/* Software Testing Automation Framework (STAF) */
/* (C) Copyright IBM Corp. 2002, 2004, 2005 */
/* */
/* This software is licensed under the Common Public License (CPL) V1.0. */
/*****************************************************************************/
/*****************************************************************************/
/* */
/* Class: HTTP */
/* Description: This class provides the STAFService Interface and implements */
/* the majority of the service function */
/* */
/*****************************************************************************/
import com.ibm.staf.*;
import com.ibm.staf.service.*;
import com.ibm.staf.service.utility.ServiceUtilities;
import com.ibm.staf.service.http.html.InvalidParameterValueException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.DataOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.net.UnknownHostException;
import java.net.ConnectException;
import java.net.MalformedURLException;
import com.ibm.staf.service.http.html.*;
import org.apache.commons.httpclient.HttpException;
public class HTTP implements STAFServiceInterfaceLevel30
{
private STAFHandle sHandle;
private String fServiceName;
private String fLocalMachineName = "";
private STAFCommandParser requestParser;
private STAFCommandParser infoParser;
private STAFCommandParser getParser;
private STAFCommandParser htmlActionParser;
private STAFCommandParser setParser;
private STAFCommandParser delCloseParser;
private STAFMapClassDefinition fListSessionMapClass;
private STAFMapClassDefinition fSessionMapClass;
private STAFMapClassDefinition fSessionOwnerMapClass;
private STAFMapClassDefinition fHttpResultMapClass;
private STAFMapClassDefinition fAuthHostMapClass;
private STAFMapClassDefinition fCookieMapClass;
private STAFMapClassDefinition fListFormMapClass;
private STAFMapClassDefinition fFormMapClass;
private STAFMapClassDefinition fFormControlMapClass;
private STAFMapClassDefinition fListLinkMapClass;
private STAFMapClassDefinition fLinkMapClass;
private SessionList sessionList;
private String fTempDir;
private int fTempFileCount = 0;
/* String Constants */
private static final String VERSIONINFO = "3.0.0 Beta 12";
// Version of STAF (or later) required for this service
// STAF Version 3.1.0 or later is required so that the privacy methods
// in STAFUtil are available.
private static final String REQUIREDSTAFVERSION = "3.1.0";
private static final String REQUEST = "REQUEST";
private static final String DOPOST = "DOPOST";
private static final String DOGET = "DOGET";
private static final String METHOD = "METHOD";
private static final String URL = "URL";
private static final String CONTENT = "CONTENT";
private static final String HEADER = "HEADER";
private static final String PARAMETER = "PARAMETER";
private static final String CONTENTFILE ="CONTENTFILE";
private static final String FILE = "FILE";
private static final String TOMACHINE = "TOMACHINE";
private static final String IGNOREERRORS = "IGNOREERRORS";
private static final String RETURNHEADERS = "RETURNHEADERS";
private static final String NOSTATUS = "NOSTATUS";
private static final String NOCONTENT = "RETURNNOCONTENT";
private static final String SESSION = "SESSION";
private static final String SESSIONS = "SESSIONS";
private static final String OPEN = "OPEN";
private static final String CLOSE = "CLOSE";
private static final String COOKIE = "COOKIE";
private static final String COOKIES = "COOKIES";
private static final String NAME = "NAME";
private static final String GET = "GET";
private static final String SET = "SET";
private static final String VALUE = "VALUE";
private static final String DELETE = "DELETE";
private static final String POLICY = "POLICY";
private static final String SHOW = "SHOW";
private static final String PAGE = "PAGE";
private static final String QUERY = "QUERY";
private static final String LIST = "LIST";
private static final String LINK = "LINK";
private static final String LINKS = "LINKS";
private static final String FORM = "FORM";
private static final String FORMS = "FORMS";
private static final String ID = "ID";
private static final String INDEX = "INDEX";
private static final String FOLLOW = "FOLLOW";
private static final String SUBMIT = "SUBMIT";
private static final String RESET = "RESET";
private static final String FORMCONTROLS = "CONTROLNAMES";
private static final String CONTROLNAME = "CONTROLNAME";
private static final String AUTOREDIRECT = "FOLLOWREDIRECT";
private static final String NOAUTOREDIRECT = "DONOTFOLLOWREDIRECT";
private static final String DEFAULTHEADER = "DEFAULTHEADER";
private static final String DEFAULTHEADERS = "DEFAULTHEADERS";
private static final String AUTHENTICATIONHOSTS = "AUTHENTICATIONHOSTS";
private static final String AUTHENTICATIONHOST = "AUTHENTICATIONHOST";
private static final String AUTHENTICATIONUSER = "AUTHENTICATIONUSER";
private static final String AUTHENTICATIONPWD = "AUTHENTICATIONPASSWORD";
private static final String AUTHENTICATIONDOMAIN = "AUTHENTICATIONDOMAIN";
private static final String HELP = "HELP";
private static final String VERSION = "VERSION";
private static final int BASEERROR = 4000;
public static final int UNKNOWNHOST = 4001;
private static final String UNKNOWNHOSTInfo =
"Unknown Host Error";
private static final String UNKNOWNHOSTDesc =
"The specified host could not be found in the naming service.";
public static final int CONNECTERROR = 4002;
private static final String CONNECTERRORInfo = "Connect Error";
private static final String CONNECTERRORDesc =
"The host was located, but a connection could not be made to " +
"the specified port.";
public static final int INVALIDMETHOD = 4003;
private static final String INVALIDMETHODInfo =
"Invalid Method";
private static final String INVALIDMETHODDesc =
"The specified method is not allowed. Allowed methods: POST, PUT, " +
"GET, HEAD, OPTIONS, DELETE, and TRACE.";
public static final int IOEXCEPTION = 4004;
private static final String IOEXCEPTIONInfo = "IO Exception";
private static final String IOEXCEPTIONDesc =
"The host was located, but an IO exception occurred opening a " +
"connection to the URL.";
public static final int CONTENTTOOLARGE = 4005;
private static final String CONTENTTOOLARGEInfo = "Out of Memory Error";
private static final String CONTENTTOOLARGEDesc =
"Trying to get content that is too large. Recommend " +
"redirecting the content to a file using the FILE option or, " +
"if the content is not needed, you may be able to specify the " +
"RETURNNOCONTENT option depending on the request.";
/*****************************************************************************/
/* */
/* Method: Constructor */
/* Description: Constructor method */
/* */
/*****************************************************************************/
public HTTP()
{
/* do nothing */
}
/*****************************************************************************/
/* */
/* Method: acceptRequest */
/* Description: required by interface STAFServiceInterfaceLevel30 */
/* performs first parse of request */
/* */
/*****************************************************************************/
public STAFResult acceptRequest(STAFServiceInterfaceLevel30.RequestInfo info)
{
try
{
STAFResult result;
String request = info.request.toUpperCase();
if (request.startsWith(OPEN))
{
result = handleOpen(info);
}
else if (request.startsWith(LIST))
{
result = handleList(info);
}
else if (request.startsWith(SET))
{
result = handleSet(info);
}
else if (request.startsWith(GET))
{
result = handleGetPage(info);
}
else if (request.startsWith(QUERY))
{
result = handleQuery(info);
}
else if (request.startsWith(CLOSE))
{
result = handleClose(info);
}
else if (request.startsWith(FOLLOW))
{
result = handleFollow(info);
}
else if (request.startsWith(SUBMIT))
{
result = handleFormAction(info);
}
else if (request.startsWith(DELETE))
{
result = handleDelete(info);
}
else if (request.startsWith(RESET))
{
result = handleFormAction(info);
}
else if ((request.startsWith(DOPOST)) ||
(request.startsWith(DOGET)) ||
(request.startsWith(REQUEST)))
{
result = handleMethod(info);
}
else if (request.startsWith(HELP))
{
result = help(info);
}
else if (request.startsWith(VERSION))
{
result = version(info);
}
else
{
result = new STAFResult(
STAFResult.InvalidRequestString, request);
}
return result;
}
catch (STAFException se)
{
return new STAFResult(se.rc, se.getMessage());
}
catch (Throwable t)
{
StringWriter sw = new StringWriter();
PrintWriter p = new PrintWriter(sw, true);
t.printStackTrace(p);
return new STAFResult(STAFResult.JavaError, sw.toString());
}
}
// enhanced functionality
/*****************************************************************************/
/* */
/* Method: handleOpen */
/* Description: Creates a new web session and goes to the specified url. */
/* Parameters: info - request info passed to acceptRequest */
/* Returns: session id of new session */
/* */
/*****************************************************************************/
private STAFResult handleOpen(STAFServiceInterfaceLevel30.RequestInfo info)
throws STAFException
{
// Verify the requester has at least trust level 3
STAFResult trustResult = STAFUtil.validateTrust(
3, fServiceName, "OPEN", fLocalMachineName, info);
if (trustResult.rc != STAFResult.Ok) return trustResult;
if (! info.request.equalsIgnoreCase("OPEN SESSION"))
{
return new STAFResult(STAFResult.InvalidRequestString,
info.request);
}
String result;
WebSession newSession = new WebSession(sessionList, info, this);
result = Integer.toString(newSession.getID());
return new STAFResult(STAFResult.Ok, result);
}
/*****************************************************************************/
/* */
/* Method: handleClose */
/* Description: ends a web session and removes it from the session list. */
/* Parameters: info - request info passed to acceptRequest */
/* Returns: STAFResult.OK */
/* */
/*****************************************************************************/
private STAFResult handleClose(STAFServiceInterfaceLevel30.RequestInfo info)
throws STAFException
{
// Verify the requester has at least trust level 3
STAFResult trustResult = STAFUtil.validateTrust(
3, fServiceName, "CLOSE", fLocalMachineName, info);
if (trustResult.rc != STAFResult.Ok) return trustResult;
// Parse the request
STAFCommandParseResult pResult = delCloseParser.parse(info.request);
if (pResult.rc != 0)
{
return new STAFResult(STAFResult.InvalidRequestString,
pResult.errorBuffer);
}
// Get resolved value for SESSION option and make sure it's an integer
STAFResult res = STAFUtil.resolveRequestVarAndCheckInt(
SESSION, pResult.optionValue(SESSION), sHandle, info.requestNumber);
if (res.rc != 0) return res;
int sessionId = Integer.parseInt(res.result);
// Check if the requester is the owner of the session
WebSession 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 5 if not the owner
// of the session
trustResult = STAFUtil.validateTrust(
5, fServiceName, "CLOSE", fLocalMachineName, info);
if (trustResult.rc != STAFResult.Ok) return trustResult;
}
// Release the resources held by the session and delete the session
try
{
session.releaseConnection();
sessionList.deleteSession(sessionId);
}catch (InvalidSessionIDException e)
{
return new STAFResult(STAFResult.DoesNotExist,e.getMessage());
}
return new STAFResult(STAFResult.Ok, "");
}
/*****************************************************************************/
/* */
/* Method: handleQuery */
/* Description: gets information about a specific entity. */
/* Parameters: info - request info passed to acceptRequest */
/* Returns: STAFResult containing detailed information on a specific entity */
/* */
/*****************************************************************************/
private STAFResult handleQuery(STAFServiceInterfaceLevel30.RequestInfo info)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -