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

📄 basetunnelclient.java

📁 大量java源程序
💻 JAVA
字号:

/*
 * @(#)BaseTunnelClient
 *
 * Copyright (c) 1998 Karl Moss. All Rights Reserved.
 *
 * You may study, use, modify, and distribute this software for any
 * purpose provided that this copyright notice appears in all copies.
 *
 * This software is provided WITHOUT WARRANTY either expressed or
 * implied.
 *
 * @author  Karl Moss
 * @version 1.0
 * @date    17Apr98
 *
 */

package javaservlets.tunnel.client;

import java.io.*;

/**
  * <p>This class provides base functionality common to all
  * types of tunnel clients
  */

public abstract class BaseTunnelClient
{
  // The connection url
  java.net.URL m_url;

  // The server object handle
  int m_objectHandle;

  String m_cookie = null;
  String cookie;
  java.net.URLConnection con;
  /**
    * <p>Sets the URL of the connection
    *
    * @param url Connection URL
    */
  public void _setURL(java.net.URL url)
    {
      m_url = url;
    }

  /**
    * <p>Gets the URL of the connection
    *
    * @return The connection URL
    */
  public java.net.URL _getURL()
    {
      return m_url;
    }

  /**
    * <p>Gets an input stream to be used for reading data
    * from the connection. Must be implemented by the client.
    *
    * @param in Input stream from the connection URL
    * @return Input stream to read data from the connection
    */
  public abstract DataInput _getInputStream(InputStream in)
    throws IOException;

  /**
    * <p>Gets an output stream to be used for writing data to
    * an internal buffer. The buffer will be written to the
    * connection. Must be implemented by the client.
    *
    * @param buffer Buffer to hold the output data
    * @return Output stream to write data to the buffer
    */
  public abstract DataOutput _getOutputStream(
        ByteArrayOutputStream buffer)
    throws IOException;

  /**
    * <p>Flushes the any buffered data to the output stream. The
    * default implementation does nothing
    *
    * @param out Output stream to flush
    */
  public void _flush(DataOutput out) throws IOException
    {
    }

  /**
    * <p>Closes the input stream. The default implementation does
    * nothing
    *
    * @param in Input stream to close
    */
  public void _close(DataInput in) throws IOException
    {
    }

  /**
    * <p>Initializes the client. Also makes a server request
    * to initialize the server as well.
    */
  public void _initialize() throws TunnelException
    {
      try {
        // Create a new buffer that will hold our data
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        // Create a method header. An ordinal value of -1 is
        // reserved for initializing the server
        _createHeader(buffer, -1);

        // Invoke the method. This will send the initialization
        // header to the server
        DataInput in = _invokeMethod(buffer.toByteArray());

        // Get the handle to the object that was just
        // created
        m_objectHandle = in.readInt();

        // Close properly
        _close(in);



      }
      catch (IOException ex) {
        // Re-throw as a tunnel exception
        ex.printStackTrace();
        throw new TunnelException(ex.getMessage());
      }
    }

  /**
    * Parses the given cookie and returns the cookie key
    * and value. For simplicity the key/ value is assumed
    * to be before the first ';', as in:
    *
    * jrunsessionid=3509823408122; path=/
    *
    * @param rawCookie The raw cookie data
    * @return The key/value of the cookie
    */
  public String parseCookie(String raw)
    {
      String c = raw;

      if (raw != null) {

        // Find the first ';'
        int endIndex = raw.indexOf(";");

        // Found a ';', assume the key/value is prior
        if (endIndex >= 0) {
          c = raw.substring(0, endIndex);
        }
      }

      return c;
    }
  /**
    * <p>Starts a method by creating the method header.
    * The header consists of the method ordinal to invoke.
    *
    * @param buffer Buffer to hold the header data
    * @param ordinal Method ordinal to invoke on the server
    * @return Output stream to be used to send parameters
    */
  public DataOutput _createHeader(ByteArrayOutputStream buffer,
                                  int ordinal)
    throws TunnelException
    {
      try {
        // Get an output stream use to write data to the buffer
        DataOutput out = _getOutputStream(buffer);

        // Write the method ordinal
        out.writeInt(ordinal);

        // If we are not initializing the object we need to send
        // the object handle along with the header
        if (ordinal != -1) {
          out.writeInt(m_objectHandle);
        }

        _flush(out);
        return out;
      }
      catch (IOException ex) {
        // Re-throw as a tunnel exception
        ex.printStackTrace();
        throw new TunnelException(ex.getMessage());
      }
    }

  /**
    * <p>Sends the given buffer that will cause a remote
    * method to be invoked.
    *
    * @param buffer Buffer containing data to send to the server
    * @return Input stream to be used to read the response from
    * the server
    */
  public DataInput _invokeMethod(byte buf[])
    throws TunnelException
    {
      DataInput in = null;

      try {
        // Get the server URL
        java.net.URL url = _getURL();
        if (url == null) {
          throw new IOException("Server URL has not been set");
        }

        // Attempt to connect to the host
        java.net.URLConnection con = url.openConnection();

        // Set the session ID if necessary
      if (m_cookie != null) {
        con.setRequestProperty("cookie", m_cookie);
      }

        // Initialize the connection
        con.setUseCaches(false);
        con.setDoOutput(true);
        con.setDoInput(true);

        // Set the content that we are sending
        con.setRequestProperty("Content-type",
                               "application/octet-stream");

        // Set the length of the data buffer we are sending
        con.setRequestProperty("Content-length",
                               "" + buf.length);

        // Get the output stream to the server and send our
        // data buffer
        DataOutputStream out =
          new DataOutputStream(con.getOutputStream());
        out.write(buf);

        // Flush the output stream and close it
        out.flush();
        out.close();

        // Get the input stream we can use to read the response
        in = _getInputStream(con.getInputStream());

        // The server will always respond with an int value
        // that will either be the method ordinal that was
        // invoked, or a -2 indicating an exception was thrown
        // from the server
        int ordinal = in.readInt();

         // Get the session cookie if we haven't already
        if (m_cookie == null) {
         if(con==null){
        System.out.println("con=null");
        }
        else{
        String cookie = con.getHeaderField("set-cookie");
        if (cookie != null) {
          m_cookie = parseCookie(cookie);
          System.out.println("Setting session ID=" + m_cookie);
        }
        if(cookie==null){
        System.out.println("cookie=null");
       //PP5ovtQoRYokJvMsuTnad5L1PC7b5bJjAW3f0eM2iiL3Gm9mFV3z!-6599725297148783633!-1072559611!7001!7002
       //PP5o8tX7QoiIScZHFNte71CPaE8PJXB7o3HNC85rqFDdOcdD3X4X!-6599725297148783633!-1072559611!7001!7002

        }
        }
      }

        // Check for an exception on the server.
        if (ordinal == -2) {
          // Read the exception message and throw it
          String msg = in.readUTF();
          throw new TunnelException(msg);
        }

      }
      catch (IOException ex) {
        // Re-throw as a tunnel exception
        ex.printStackTrace();
        throw new TunnelException(ex.getMessage());
      }

      // Return the input stream to be used to read the rest
      // of the response from the server
      return in;
    }

}

⌨️ 快捷键说明

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