📄 websession.java
字号:
package com.ibm.staf.service.http;/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2004 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************//*****************************************************************************//* *//* Class: WebSession *//* Description: This class provides the handle to maintain a http session. *//* *//*****************************************************************************/import com.ibm.staf.*;import java.util.Vector;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.net.URL;import java.net.MalformedURLException;import java.io.IOException;import java.io.FileNotFoundException;import java.io.StringReader;import java.io.File;import java.io.InputStream;import java.io.OutputStream;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.FileWriter;import org.w3c.dom.Document;import com.ibm.staf.service.STAFServiceInterfaceLevel30;import com.ibm.staf.STAFUtil;import com.ibm.staf.service.http.html.InvalidParameterValueException;import com.ibm.staf.service.http.html.WebLink;import com.ibm.staf.service.http.html.WebForm;import com.ibm.staf.service.http.html.HTMLParser;// HTTP Clientimport org.apache.commons.httpclient.*;import org.apache.commons.httpclient.util.EncodingUtil;import org.apache.commons.httpclient.methods.*;import org.apache.commons.httpclient.params.HttpMethodParams;import org.apache.commons.httpclient.auth.AuthScope;import org.apache.commons.httpclient.methods.multipart.*;// nekoHTMLimport org.cyberneko.html.parsers.DOMParser;import org.w3c.dom.html.HTMLDocument;// xercesimport org.xml.sax.SAXException;import org.xml.sax.InputSource;public class WebSession { // These constants define the type of identification of the page element to // be accessed. public static final int NAME_ID_TYPE = 0; public static final int ID_ID_TYPE = 1; public static final int INDEX_ID_TYPE = 2; // These constants define the parts of the summary public static final String ID = "ID"; public static final String URL = "URL"; public static final String TITLE = "TITLE"; public static final String POLICY = "POLICY"; public static final String OWNER_INSTANCE_UUID = "OWNER_INSTANCE_UUID"; public static final String OWNER_MACHINE = "OWNER_MACHINE"; public static final String OWNER_HANDLE_NAME = "OWNER_HANDLE_NAME"; public static final String OWNER_HANDLE = "OWNER_HANDLE"; // These constants are used to define the hash map of required components // of a requestMethod call public static final String REQUEST_PARAMETERS = "PARAMETERS"; public static final String REQUEST_FILES = "FILES"; public static final String REQUEST_HEADERS = "HEADERS"; public static final String REQUEST_METHOD = "METHOD"; public static final String REQUEST_URL = "URL"; public static final String REQUEST_CONTENT = "CONTENT"; public static final String REQUEST_REDIRECT = "AUTO REDIRECT"; public static final String RETURN_STATUS_CODE = "RETURN STATUS CODE"; public static final String RETURN_STATUS_MESSAGE = "RETURN STATUS MSG"; public static final String RETURN_HEADERS = "RETURN HEADERS"; protected int id; protected String ownerInstanceUUID; protected String ownerMachine; protected String ownerHandleName; protected int ownerHandle; protected HTTP httpService; protected HttpClient session; protected HttpMethodBase lastRequest; protected HTMLParser parser; protected HashMap defaultHeaders; protected HashMap authenticationSets; protected boolean followRedirect; protected String currentContents; protected String currentContentsFileName; private static final int BUFF_SIZE = 4096; private static final int MAX_STRING_SIZE = 51200; /*****************************************************************************//* *//* Method: Constructor *//* Description: Constructor method *//* Parameter: parentList - session list that this session is to be a part of *//* it is required to determine the id of this session*//* info - request info *//* *//*****************************************************************************/ public WebSession(SessionList parentList, STAFServiceInterfaceLevel30.RequestInfo info, HTTP httpService) { this.ownerInstanceUUID = info.stafInstanceUUID; this.ownerMachine = info.endpoint; this.ownerHandleName = info.handleName; this.ownerHandle = info.handle; this.httpService = httpService; id = parentList.addSession(this); session = new HttpClient(); lastRequest = null; parser = new HTMLParser(); defaultHeaders = new HashMap(); authenticationSets = new HashMap(); followRedirect = false; currentContents = ""; currentContentsFileName = ""; } public void releaseConnection() { currentContents = ""; if (currentContentsFileName.equals("")) { // Delete temporary file (new File(currentContentsFileName)).delete(); currentContentsFileName = ""; } if (lastRequest != null) { // Must call releaseConnection to release resources lastRequest.releaseConnection(); lastRequest = null; } try { parser.setContent(HTMLParser.EMPTY_DOC); } catch (Exception e) { // HTMLParser.EMPTY_DOC is safe and will not cause an exception } }/*****************************************************************************//* *//* Method: getID *//* Description: gets the session id of this WebSesson. *//* Returns: the ID which identifies the session in the list *//* *//*****************************************************************************/ public int getID() { return id; }/*****************************************************************************//* *//* Method: getOwnerMachine *//* Description: gets the owner machine for this WebSesson. *//* Returns: the machine for the owner of this session *//* *//*****************************************************************************/ public String getOwnerMachine() { return ownerMachine; }/*****************************************************************************//* *//* Method: getOwnerHandleName *//* Description: gets the owner handle name for this WebSesson. *//* Returns: the handle name for the owner of this session *//* *//*****************************************************************************/ public String getOwnerHandleName() { return ownerHandleName; }/*****************************************************************************//* *//* Method: getOwnerHandle *//* Description: gets the owner handle for this WebSesson. *//* Returns: the handle for the owner of this session *//* *//*****************************************************************************/ public int getOwnerHandle() { return ownerHandle; } /*****************************************************************************//* *//* Method: getCurrentContentsAsStream *//* Description: get the contents returned by the last interaction *//* Parameters: none *//* Returns: the contents of the last interaction in an InputStream. *//* This is the html code for the current page if the last action *//* was a goto or link. Otherwise it is the message generated by *//* the METHOD call. *//* *//*****************************************************************************/ public InputStream getCurrentContentsAsStream() throws IOException { try { return lastRequest.getResponseBodyAsStream(); } catch (NullPointerException e){ // no previous requests } // If an null if an error occurs return null; }/*****************************************************************************//* *//* Method: getCurrentContents *//* Description: get the contents returned by the last interaction *//* Parameters: none *//* Returns: the contents of the last interaction in a String. *//* This is the html code for the current page if the last action *//* was a goto or link. Otherwise it is the message generated by the *//* METHOD call. *//* *//*****************************************************************************/ public String getCurrentContents() throws STAFException, ContentTooLargeException { // Get content from where setCurrentContent stored it. // If content is small, the content is stored in a string. // If content is large, the content is stored in a temporary file. if (currentContentsFileName.equals("")) { // Current contents <= 50k are stored in a string return currentContents; } else { // Current contents > 50k are stored in a temporary file // Read from the file specified by currentContentsFileName StringBuffer output = new StringBuffer(); InputStream instream = null; final byte[] buffer = new byte[BUFF_SIZE]; try { instream = new DataInputStream(new FileInputStream( currentContentsFileName)); // Read InputStream while (true) { int amountRead = instream.read(buffer); if (amountRead == -1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -