home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1997 May
/
Pcwk0597.iso
/
sybase
/
starbuck
/
template.z
/
WebConnection.java
< prev
next >
Wrap
Text File
|
1996-12-09
|
12KB
|
346 lines
/*
* WebConnection.java
*
*
* Copyright 1996 Sybase, Inc. All rights reserved.
*/
import java.io.*;
/**
* <P>The WebConnection class provides an interface to a World-Wide Web Server,
* specifically to the WebService class that is used by Optima++ as the basis
* for servicing a Web request.
*
* <P>Lifetime: created from main of application.
* Becomes garbage when the servlet exits.
*
* <P>See Optima++ documentation on creating a Web service DLL for a description
* of the methods, but be aware that, in Java, the first character
* of a method name is always in lower case.
*/
public class WebConnection
//************************
{
//-------------------------------------------------------------
// Constructors
//-------------------------------------------------------------
/**
* Constructs a new WebConnection object
*/
public WebConnection()
//********************
{
_connected = false;
_dllload = false;
_serverHandle = -1;
}
/**
* Constructs a new WebConnection object with the
* given arguments
* @param args argument list
*/
public WebConnection(String args[])
//*********************************
{
if (args.length >= 2) {
// Load connection DLL and keep a private handle to the
// calling WebService requestor
if (! _dllload) {
System.load(args[1]);
_dllload = true;
}
_serverHandle = -1;
_connected = initConnection(args);
}
}
//-------------------------------------------------------------
// Properties
//-------------------------------------------------------------
/**
* Returns a boolean indicating whether a connection to the
* WWW server has been established
* @return true if connected to the WWW server; false otherwise
*/
public boolean getConnected()
//***************************
{
return _connected;
}
/**
* Returns a specified value of the multi-valued named variable
* POSTed from an HTML form
* @param name name of the variable whose value is to be returned
* @param index index of the value to be returned
* @exception WebServiceException If the handle to the WWW server is invalid
*/
public String getFormVariable(String name, int index) throws WebServiceException
//******************************************************************************
{
return getVariable(FORM_VARIABLE, name, index);
}
/**
* Returns the value of the named variable POSTed from an HTML form
* @param name name of the variable whose value is to be returned
* @exception WebServiceException If the handle to the WWW server is invalid
*/
public String getFormVariable(String name) throws WebServiceException
//*******************************************************************
{
return getVariable(FORM_VARIABLE, name, 0);
}
/**
* Sets the value of the named variable POSTed from an HTML form
* to the value specified
* @param name name of the variable whose value is to be set
* @param value value to set the variable to
* @return true if the variable has been successfully set; false otherwise
* @exception WebServiceException If the handle to the WWW server is invalid
*/
public boolean setFormVariable(String name, String value) throws WebServiceException
//**********************************************************************************
{
return setVariable(FORM_VARIABLE, name, value);
}
/**
* Returns a specified value of the multi-valued named variable
* from an HTML form (from the URL?name=value)
* @param name name of the variable whose value is to be returned
* @param index index of the value to be returned
* @exception WebServiceException If the handle to the WWW server is invalid
*/
public String getQueryVariable(String name, int index) throws WebServiceException
//*******************************************************************************
{
return getVariable(QUERY_VARIABLE, name, index);
}
/**
* Returns the value of the named variable from an HTML form (from the URL?name=value)
* @param name name of the variable whose value is to be returned
* @exception WebServiceException If the handle to the WWW server is invalid
*/
public String getQueryVariable(String name) throws WebServiceException
//********************************************************************
{
return getVariable(QUERY_VARIABLE, name, 0);
}
/**
* Sets the value of the named variable from an HTML form (from the URL?name=value)
* to the value specified
* @param name name of the variable whose value is to be set
* @param value value to set the variable to
* @return true if the variable has been successfully set; false otherwise
* @exception WebServiceException If the handle to the WWW server is invalid
*/
public boolean setQueryVariable(String name, String value) throws WebServiceException
//************************************************************************************
{
return setVariable(QUERY_VARIABLE, name, value);
}
/**
* Returns the value of the HTTP response header specified
* before any write()
* @param name name of the HTTP response header whose value is to be returned
* @exception WebServiceException If the handle to the WWW server is invalid
*/
public String getResponseHeader(String name) throws WebServiceException
//*********************************************************************
{
return getVariable(RESPONSE_VARIABLE, name, 0);
}
/**
* Sets the value of the named HTTP response header to the value specified
* @param name name of the HTTP response header whose value is to be set
* @param value value to set the variable to
* @return true if the response header has been successfully set; false otherwise
* @exception WebServiceException If the handle to the WWW server is invalid
*/
public boolean setResponseHeader(String name, String value) throws WebServiceException {
return setVariable(RESPONSE_VARIABLE, name, value);
}
/**
* Removes the specified outgoing HTTP header
* @param name name of the HTTP header to be removed
* @return true if the response header has been successfully removed; false otherwise
*/
public native boolean removeResponseHeader(String headername);
//***********************************************************
/**
* Returns a boolean specifying whether this servlet was run from a form
* whose variables were submitted with the POST method
* @return true if the form POSTed its variables; false otherwise
* @exception WebServiceException If the handle to the WWW server is invalid
*/
public boolean getIsPostMethod() throws WebServiceException
//*********************************************************
{
if (getInt(POST_METHOD) == 1)
return true;
return false;
}
/**
* Returns the HTTP 1.0 result code that is sent to the browser
* (must be sent before any output)
* @exception WebServiceException If the handle to the WWW server is invalid
*/
public int getResultCode() throws WebServiceException
//***************************************************
{
return getInt(RESULT_CODE);
}
/**
* Sets the HTTP 1.0 result code that is sent to the browser
* (must be sent before any output) to the value specified.
* @param newcode value of the new HTTP 1.0 result code
* @return true if the result code has been successfully set; false otherwise
* @exception WebServiceException If the handle to the WWW server is invalid
*/
public boolean setResultCode(int newcode) throws WebServiceException
//******************************************************************
{
return setInt(RESULT_CODE, newcode);
}
/**
* Constructs and returns an output stream connected to the WWW server
*/
PrintStream getStream()
//*********************
{
return new PrintStream( new BaseServletStream( _serverHandle ) );
}
/**
* Constructs and returns a Session object
*/
Session getSession()
//******************
{
return new Session( _serverHandle );
}
//-------------------------------------------------------------
// Public Methods
//-------------------------------------------------------------
/**
* Writes the specified String to the WWW server's client
* @param s String to be written
* @return true if the String was successfully written; false otherwise
*/
public boolean write(String s)
//****************************
{
return writeOutput(s, false);
}
/**
* Writes the specified String, followed by a newline
* to the WWW server's client
* @param s String to be written
* @return true if the String was successfully written; false otherwise
*/
public boolean writeln(String s)
//******************************
{
return writeOutput(s, true);
}
/**
* Writes the specified String, followed by a newline
* to the WWW server's client
* @param s String to be written
* @return true if the String was successfully written; false otherwise
*/
public boolean writeLn(String s)
//******************************
{
return writeOutput(s, true);
}
/**
* Sends a redirection message to the browser to redirect to a different URL
* @param newurl redirection URL
* @return true for success; false otherwise
*/
public native boolean redirect(String newurl);
//*******************************************
//-------------------------------------------------------------
// Private Methods
//-------------------------------------------------------------
private native boolean initConnection(String args[]);
private native boolean writeOutput(String str, boolean addNewline);
private native String getVariable(int code, String name, int index);
private native boolean setVariable(int code, String name, String value);
private native int getInt(int code);
private native boolean setInt(int code, int newvalue);
//-------------------------------------------------------------
// Data
//-------------------------------------------------------------
// Class and Instance variables
// Do not shuffle or change instance variables without also updating
// isapiconnect/WebConnection.h
private int _serverHandle;
private boolean _connected;
private boolean _dllload;
protected static final int FORM_VARIABLE = 1;
protected static final int QUERY_VARIABLE = 2;
protected static final int SERVER_VARIABLE = 3;
protected static final int RESPONSE_VARIABLE = 4;
protected static final int RESULT_CODE = 5;
protected static final int POST_METHOD = 6;
}