home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / template.z / WebConnection.java < prev    next >
Text File  |  1996-12-09  |  12KB  |  346 lines

  1. /*
  2.  * WebConnection.java
  3.  *
  4.  *
  5.  * Copyright 1996 Sybase, Inc. All rights reserved.
  6.  */
  7.  
  8.  
  9. import java.io.*;
  10.  
  11. /**
  12.  * <P>The WebConnection class provides an interface to a World-Wide Web Server,
  13.  * specifically to the WebService class that is used by Optima++ as the basis
  14.  * for servicing a Web request.
  15.  *
  16.  * <P>Lifetime: created from main of application.
  17.  * Becomes garbage when the servlet exits.
  18.  *
  19.  * <P>See Optima++ documentation on creating a Web service DLL for a description
  20.  * of the methods, but be aware that, in Java, the first character
  21.  * of a method name is always in lower case.
  22.  */
  23.  
  24. public class WebConnection 
  25. //************************
  26. {
  27.  
  28.     //-------------------------------------------------------------
  29.     // Constructors
  30.     //-------------------------------------------------------------
  31.  
  32.     /**
  33.      * Constructs a new WebConnection object
  34.      */
  35.      
  36.     public WebConnection() 
  37.     //********************
  38.     {
  39.         _connected = false;
  40.         _dllload = false;
  41.         _serverHandle = -1;
  42.     }
  43.     
  44.     /**
  45.      * Constructs a new WebConnection object with the
  46.      * given arguments
  47.      * @param args argument list
  48.      */
  49.      
  50.     public WebConnection(String args[]) 
  51.     //*********************************
  52.     {
  53.         if (args.length >= 2) {
  54.             // Load connection DLL and keep a private handle to the
  55.             // calling WebService requestor
  56.             if (! _dllload) {
  57.                 System.load(args[1]);
  58.                 _dllload = true;
  59.             }
  60.             _serverHandle = -1;
  61.             _connected = initConnection(args);
  62.         }
  63.     }
  64.  
  65.     //-------------------------------------------------------------
  66.     // Properties
  67.     //-------------------------------------------------------------
  68.  
  69.     /**
  70.      * Returns a boolean indicating whether a connection to the
  71.      * WWW server has been established
  72.      * @return true if connected to the WWW server; false otherwise
  73.      */
  74.      
  75.     public boolean getConnected() 
  76.     //***************************
  77.     {
  78.         return _connected;
  79.     }
  80.     
  81.     /**
  82.      * Returns a specified value of the multi-valued named variable 
  83.      * POSTed from an HTML form
  84.      * @param     name  name of the variable whose value is to be returned
  85.      * @param     index index of the value to be returned 
  86.      * @exception WebServiceException If the handle to the WWW server is invalid
  87.      */
  88.      
  89.     public String getFormVariable(String name, int index) throws WebServiceException 
  90.     //******************************************************************************
  91.     {
  92.         return getVariable(FORM_VARIABLE, name, index);
  93.     }
  94.     
  95.     /**
  96.      * Returns the value of the named variable POSTed from an HTML form
  97.      * @param name name of the variable whose value is to be returned
  98.      * @exception WebServiceException If the handle to the WWW server is invalid
  99.      */
  100.      
  101.     public String getFormVariable(String name) throws WebServiceException 
  102.     //*******************************************************************
  103.     {
  104.         return getVariable(FORM_VARIABLE, name, 0);
  105.     }
  106.     
  107.     /** 
  108.      * Sets the value of the named variable POSTed from an HTML form
  109.      * to the value specified
  110.      * @param  name name of the variable whose value is to be set
  111.      * @param  value value to set the variable to
  112.      * @return true if the variable has been successfully set; false otherwise 
  113.      * @exception WebServiceException If the handle to the WWW server is invalid
  114.      */
  115.      
  116.     public boolean setFormVariable(String name, String value) throws WebServiceException 
  117.     //**********************************************************************************
  118.     {
  119.         return setVariable(FORM_VARIABLE, name, value);
  120.     }
  121.  
  122.     /**
  123.      * Returns a specified value of the multi-valued named variable 
  124.      * from an HTML form (from the URL?name=value)
  125.      * @param     name  name of the variable whose value is to be returned
  126.      * @param     index index of the value to be returned 
  127.      * @exception WebServiceException If the handle to the WWW server is invalid
  128.      */
  129.      
  130.     public String getQueryVariable(String name, int index) throws WebServiceException 
  131.     //*******************************************************************************
  132.     {
  133.         return getVariable(QUERY_VARIABLE, name, index);
  134.     }
  135.     
  136.     
  137.     /**
  138.      * Returns the value of the named variable from an HTML form (from the URL?name=value)
  139.      * @param name name of the variable whose value is to be returned
  140.      * @exception WebServiceException If the handle to the WWW server is invalid
  141.      */
  142.      
  143.     public String getQueryVariable(String name) throws WebServiceException 
  144.     //********************************************************************
  145.     {
  146.         return getVariable(QUERY_VARIABLE, name, 0);
  147.     }
  148.     
  149.     
  150.     /** 
  151.      * Sets the value of the named variable from an HTML form (from the URL?name=value)
  152.      * to the value specified
  153.      * @param  name name of the variable whose value is to be set
  154.      * @param  value value to set the variable to
  155.      * @return true if the variable has been successfully set; false otherwise 
  156.      * @exception WebServiceException If the handle to the WWW server is invalid
  157.      */
  158.      
  159.     public boolean setQueryVariable(String name, String value) throws WebServiceException 
  160.     //************************************************************************************
  161.     {
  162.         return setVariable(QUERY_VARIABLE, name, value);
  163.     }
  164.  
  165.     /**
  166.      * Returns the value of the HTTP response header specified 
  167.      * before any write()
  168.      * @param name name of the HTTP response header whose value is to be returned 
  169.      * @exception WebServiceException If the handle to the WWW server is invalid
  170.      */
  171.      
  172.     public String getResponseHeader(String name) throws WebServiceException 
  173.     //*********************************************************************
  174.     {
  175.         return getVariable(RESPONSE_VARIABLE, name, 0);
  176.     }
  177.     
  178.     /** 
  179.      * Sets the value of the named HTTP response header to the value specified
  180.      * @param  name name of the HTTP response header whose value is to be set
  181.      * @param  value value to set the variable to
  182.      * @return true if the response header has been successfully set; false otherwise 
  183.      * @exception WebServiceException If the handle to the WWW server is invalid
  184.      */
  185.     public boolean setResponseHeader(String name, String value) throws WebServiceException {
  186.         return setVariable(RESPONSE_VARIABLE, name, value);
  187.     }
  188.     
  189.     /**
  190.      * Removes the specified outgoing HTTP header
  191.      * @param name name of the HTTP header to be removed
  192.      * @return true if the response header has been successfully removed; false otherwise 
  193.      */
  194.  
  195.     public native boolean removeResponseHeader(String headername);
  196.     //***********************************************************
  197.  
  198.     /**
  199.      * Returns a boolean specifying whether this servlet was run from a form
  200.      * whose variables were submitted with the POST method
  201.      * @return true if the form POSTed its variables; false otherwise
  202.      * @exception WebServiceException If the handle to the WWW server is invalid
  203.      */
  204.      
  205.     public boolean getIsPostMethod() throws WebServiceException 
  206.     //*********************************************************
  207.     {
  208.         if (getInt(POST_METHOD) == 1)
  209.             return true;
  210.         return false;
  211.     }
  212.  
  213.     /**
  214.      * Returns the HTTP 1.0 result code that is sent to the browser
  215.      * (must be sent before any output)
  216.      * @exception WebServiceException If the handle to the WWW server is invalid
  217.      */
  218.       
  219.     public int getResultCode() throws WebServiceException
  220.     //***************************************************
  221.     {    
  222.         return getInt(RESULT_CODE);
  223.     }
  224.     
  225.     /**
  226.      * Sets the HTTP 1.0 result code that is sent to the browser
  227.      * (must be sent before any output) to the value specified.
  228.      * @param newcode value of the new HTTP 1.0 result code
  229.      * @return true if the result code has been successfully set; false otherwise 
  230.      * @exception WebServiceException If the handle to the WWW server is invalid
  231.      */
  232.      
  233.     public boolean setResultCode(int newcode) throws WebServiceException 
  234.     //******************************************************************
  235.     {
  236.         return setInt(RESULT_CODE, newcode);
  237.     }
  238.  
  239.     /**
  240.      * Constructs and returns an output stream connected to the WWW server
  241.      */
  242.      
  243.     PrintStream getStream() 
  244.     //*********************
  245.     {
  246.         return new PrintStream( new BaseServletStream( _serverHandle ) );
  247.     }
  248.  
  249.     /**
  250.      * Constructs and returns a Session object
  251.      */
  252.      
  253.     Session getSession() 
  254.     //******************
  255.     {
  256.         return new Session( _serverHandle );
  257.     }
  258.  
  259.  
  260.     //-------------------------------------------------------------
  261.     // Public Methods
  262.     //-------------------------------------------------------------
  263.     
  264.     /**
  265.      * Writes the specified String to the WWW server's client
  266.      * @param s String to be written
  267.      * @return true if the String was successfully written; false otherwise
  268.      */
  269.      
  270.     public boolean write(String s)
  271.     //****************************
  272.     {
  273.         return writeOutput(s, false);
  274.     }
  275.     
  276.     /**
  277.      * Writes the specified String, followed by a newline 
  278.      * to the WWW server's client
  279.      * @param s String to be written
  280.      * @return true if the String was successfully written; false otherwise
  281.      */
  282.      
  283.     public boolean writeln(String s) 
  284.     //******************************
  285.     {
  286.         return writeOutput(s, true);
  287.     }
  288.        
  289.     /**
  290.      * Writes the specified String, followed by a newline 
  291.      * to the WWW server's client
  292.      * @param s String to be written
  293.      * @return true if the String was successfully written; false otherwise
  294.      */
  295.     
  296.     public boolean writeLn(String s) 
  297.     //******************************
  298.     {
  299.         return writeOutput(s, true);
  300.     }
  301.  
  302.      
  303.     /**
  304.      * Sends a redirection message to the browser to redirect to a different URL
  305.      * @param newurl redirection URL
  306.      * @return true for success; false otherwise 
  307.      */
  308.      
  309.     public native boolean redirect(String newurl);
  310.     //*******************************************
  311.  
  312.  
  313.     //-------------------------------------------------------------
  314.     // Private Methods
  315.     //-------------------------------------------------------------
  316.  
  317.     private native boolean initConnection(String args[]);
  318.     private native boolean writeOutput(String str, boolean addNewline);
  319.     private native String getVariable(int code, String name, int index);
  320.     private native boolean setVariable(int code, String name, String value);
  321.     private native int getInt(int code);
  322.     private native boolean setInt(int code, int newvalue);
  323.  
  324.  
  325.     //-------------------------------------------------------------
  326.     // Data
  327.     //-------------------------------------------------------------
  328.     
  329.     // Class and Instance variables
  330.     // Do not shuffle or change instance variables without also updating
  331.     // isapiconnect/WebConnection.h
  332.     private int _serverHandle;
  333.     private boolean _connected;
  334.     private boolean _dllload;
  335.  
  336.     protected static final int FORM_VARIABLE = 1;
  337.     protected static final int QUERY_VARIABLE = 2;
  338.     protected static final int SERVER_VARIABLE = 3;
  339.     protected static final int RESPONSE_VARIABLE = 4;
  340.     protected static final int RESULT_CODE = 5;
  341.     protected static final int POST_METHOD = 6;
  342.  
  343.  
  344. }
  345.  
  346.