You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ha...@apache.org on 2004/09/03 17:43:27 UTC

cvs commit: ws-axis/c/tests/utils/monitor/org/apache/test RequestForwarder.java RequestHandler.java TCPMonitor.java

hawkeye     2004/09/03 08:43:27

  Added:       c/tests/utils/monitor/org/apache/test RequestForwarder.java
                        RequestHandler.java TCPMonitor.java
  Log:
  Provision of utility to allow automated testing of "on-the-wire" SOAP messages
  
  Submitted by: Andrew Perry
  Reviewed by: Adrian Dick
  
  Revision  Changes    Path
  1.1                  ws-axis/c/tests/utils/monitor/org/apache/test/RequestForwarder.java
  
  Index: RequestForwarder.java
  ===================================================================
  package org.apache.test;
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.IOException;
  import java.net.SocketException;
  
  /*
   * Created on 31-Aug-2004
   *
   * To change the template for this generated file go to
   * Window>Preferences>Java>Code Generation>Code and Comments
   */
  
  /**
   * @author Andrew Perry
   *
   * Read data from an input socket, pass the data to a handler to do what it
   * wants to its copy of the data, then write the data to an output socket.
   * 
   * Data on the input socket is either the request from the client or the 
   * response from the server.
   * 
   */
  public class RequestForwarder extends Thread {
  	private BufferedReader reader;
  	private BufferedWriter writer;
  	private RequestHandler requestHandler;
  
  	/**
  	 * @param bufferedReader
  	 * @param urlOutputWriter
  	 * @param requestHandler
  	 */
  	public RequestForwarder(
  		BufferedReader bufferedReader,
  		BufferedWriter urlOutputWriter,
  		RequestHandler requestHandler) {
  		this.reader = bufferedReader;
  		this.writer = urlOutputWriter;
  		this.requestHandler = requestHandler;
  	}
  
  	public void run() {
  		char[] buffer = new char[1];
  		try {
  			int ret = 0;
  			while ((ret = reader.read(buffer)) != -1) {
  				String line = new String(buffer);
  				// give the incoming line to the handler
  				requestHandler.incomingRequestLine(line);
  
  				// Output the line to the real URL
  				writer.write(line);
  				writer.flush();
  			}
  			writer.close();
  			reader.close();
  		} catch (SocketException socketException) {
  			//System.out.println("socketException: " + socketException);
  		} catch (IOException exception) {
  			requestHandler.handleWritingException(exception);
  		} catch (RuntimeException runtimeException) {
  			System.out.println("runtimeException: " + runtimeException);
  		} catch (Throwable exception) {
  			requestHandler.handleReadingException(exception);
  		}
  		requestHandler.close();
  	}
  }
  
  
  1.1                  ws-axis/c/tests/utils/monitor/org/apache/test/RequestHandler.java
  
  Index: RequestHandler.java
  ===================================================================
  package org.apache.test;
  import java.io.IOException;
  
  /**
   * @author hawkeye
   * This class is given an incoming request. It can do with it what it will. the idea is that
   * this class could potentially output the message to a log and then forward the message
   * to another URI.
   *
   */
  
  public abstract class RequestHandler {
  	/**
  	 * handle any exceptions that happen while we are writing out the output to the forwarding uri
  	 * @param exception
  	 */
  	public abstract void handleWritingException(IOException exception);
  
  	/**
  	 * 
  	 * Handle exceptions that happen when reading in the request. 
  	 * 
  	 * @param exception
  	 */
  	public abstract void handleReadingException(Throwable exception);
  
  	/**
  	 * As the lines come in from the initial request this method is called. 
  	 * The handler can choose to print them out to a file etc.
  	 * This will not change what is sent to the server.
  	 *  
  	 * @param line a line coming in from the requestor
  	 */
  	public abstract void incomingRequestLine(String line) throws IOException;
  
  	/**
  	 * Call close on ant open files
  	 *
  	 */
  	public abstract void close();
  }
  
  
  
  1.1                  ws-axis/c/tests/utils/monitor/org/apache/test/TCPMonitor.java
  
  Index: TCPMonitor.java
  ===================================================================
  package org.apache.test;
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  //import javax.net.ServerSocketFactory;
  //import javax.net.SocketFactory;
  
  /**
   * @author hawkeye
   * This class is designed to listen on a given port and send the request received on that
   * port to the given RequestHandler. 
   * This class is meant to be used in a test suite scenario. where an instance of this class
   * is created per call to a test.
   * 
   */
  public class TCPMonitor {
  	private ServerSocket serverSocket;
  	private String forwardingURL;
  	private String responseFile;
  	private int forwardingPort;
  
  	/**
  	 * Creates a new TCPMonitor listening on the given port for incoming requests (this is always on localhost of course!)
  	 * 
  	 * @param listenerPort the port to listen for incoming requests
  	 * @throws IOException if any issues occur listening for connections or supporting them. 
  	 */
  	public TCPMonitor(
  		int listenerPort,
  		String urlToForwardTo,
  		int forwardingPort,
  		String responseFile)
  		throws IOException {
  		serverSocket = new ServerSocket(listenerPort);
  		this.forwardingURL = urlToForwardTo;
  		this.forwardingPort = forwardingPort;
  		this.responseFile = responseFile;
  	}
  
  	/**
  	 * Listen for incoming connections and give them to the handler.
  	 * 
  	 * Only one listen is called on the server socket. If more than one incoming connection is
  	 * expected then this will not be handled.
  	 *   
  	 * @param handler
  	 */
  	public void listen(RequestHandler handler) throws IOException {
  		Socket clientSocket = serverSocket.accept();
  
  		// Create a reader to read the request sent from the client
  		BufferedReader bufferedReader =
  			new BufferedReader(
  				new InputStreamReader(clientSocket.getInputStream()));
  
  		// Open the socket to the server to forward the request
  		Socket outputSocket = new Socket(forwardingURL, forwardingPort);
  
  		// Create a writer to write the incoming client request to
  		BufferedWriter urlOutputWriter =
  			new BufferedWriter(
  				new OutputStreamWriter(outputSocket.getOutputStream()));
  
  		// We have to read in parallel thread.
  		RequestForwarder requestReader =
  			new RequestForwarder(bufferedReader, urlOutputWriter, handler);
  		
  		// spin off the thread to listen for a client request
  		requestReader.start();
  
  		// Now handle the reply coming back from the forwarded URL
  		
  		// Create a reader to read the response from the server
  		BufferedReader uriBufferedReader =
  			new BufferedReader(
  				new InputStreamReader(outputSocket.getInputStream()));
  
  		// Create the writer to send it back to the client
  		BufferedWriter bufferedWriter =
  			new BufferedWriter(
  				new OutputStreamWriter(clientSocket.getOutputStream()));
  
  		RequestForwarder responseReader =
  			new RequestForwarder(
  				uriBufferedReader,
  				bufferedWriter,
  				new MyRequestHandler(responseFile));
  		
  		// spin off the thread to listen for a client request
  		responseReader.start();
  	}
  
  	public static void main(String[] args) {
  
  		try {
  			int listener_port = 0;
  			int forward_port = 0;
  			String forward_host = "";
  			String output_file = "";
  			String response_file = "";
  			for (int i = 0; i < args.length; i++) {
  				if (args[i].equals("-l")) {
  					listener_port = Integer.parseInt(args[++i]);
  					continue;
  				}
  				if (args[i].equals("-p")) {
  					forward_port = Integer.parseInt(args[++i]);
  					continue;
  				}
  				if (args[i].equals("-h")) {
  					forward_host = new String(args[++i]);
  					continue;
  				}
  				if (args[i].equals("-o")) {
  					output_file = new String(args[++i]);
  					continue;
  				}
  				if (args[i].equals("-r")) {
  					response_file = new String(args[++i]);
  					continue;
  				}
  			}
  			if (listener_port == 0
  				|| forward_port == 0
  				|| forward_host.equals("")
  				|| output_file.equals("")) {
  				System.out.println(
  					"usage: TCPMonitor <-l listen port> <-p forward port> <-h forward host> <-o request output file> [-r response output file]");
  				return;
  			}
  			TCPMonitor monitor =
  				new TCPMonitor(listener_port, forward_host, forward_port, response_file);
  			monitor.listen(new MyRequestHandler(output_file));
  		} catch (IOException exception) {
  			exception.printStackTrace();
  		}
  	}
  }
  
  class MyRequestHandler extends RequestHandler {
  	private FileWriter output;
  	private boolean output_set = false;
  
  	public MyRequestHandler() {
  		output_set = false;
  	}
  
  	public MyRequestHandler(String output_file) throws IOException {
  		if(output_file.equals("")) {
  			output_set = false;
  		} else {
  			output = new FileWriter(output_file);
  			output_set = true;
  		}
  	}
  	/* (non-Javadoc)
  	 * @see RequestHandler#handleWritingException(java.io.IOException)
  	 */
  	public void handleWritingException(IOException exception) {
  		exception.printStackTrace();
  	}
  	/* (non-Javadoc)
  	 * @see RequestHandler#handleReadingException(java.io.IOException)
  	 */
  	public void handleReadingException(Throwable exception) {
  		exception.printStackTrace();
  	}
  
  	/* (non-Javadoc)
  	 * @see RequestHandler#incomingRequestLine(java.lang.String)
  	 */
  	public void incomingRequestLine(String line) throws IOException {
  		if (output_set) {
  			output.write(line);
  		}
  	}
  
  	public void close() {
  		if (output_set) {
  			try {
  				output.close();
  			} catch (IOException e) {
  				// do nothing
  			}
  		}
  	}
  }