001/*
002 *  $Rev: 1357 $:     Revision of last commit
003 *  $Author: tgutwin $:  Author of last commit
004 *  $Date: 2020-06-20 14:19:31 -0700 (Sat, 20 Jun 2020) $:    Date of last commit
005 *  $URL: svn://fred.webarts.bc.ca/open/trunk/projects/WebARTS/ca/bc/webarts/tools/sockets/ClientThread.java $
006 */
007/*
008 *
009 *  Written by Tom Gutwin - WebARTS Design.
010 *  Copyright (C) 2020 WebARTS Design, North Vancouver Canada
011 *  http://www.webarts.bc.ca
012 *
013 *  This program is free software; you can redistribute it and/or modify
014 *  it under the terms of the GNU General Public License as published by
015 *  the Free Software Foundation; either version 2 of the License, or
016 *  (at your option) any later version.
017 *
018 *  This program is distributed in the hope that it will be useful,
019 *  but WITHOUT ANY WARRANTY; without_ even the implied warranty of
020 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
021 *  GNU General Public License for more details.
022 *
023 *  You should have received a copy of the GNU General Public License
024 *  along with this program; if not, write to the Free Software
025 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
026 */
027
028package ca.bc.webarts.tools.sockets;
029
030import java.io.DataInputStream;
031import java.io.PrintStream;
032import java.io.IOException;
033import java.net.Socket;
034
035/**
036 * The  client thread (part of the TCPSocketServer)  that gets created when a connectiuon comes in.
037 * This client thread opens the input and the output streams for a particular client,
038 * gets the client request and <b>executes it verbatum</b> on the commandline  then terminates.
039 *
040 * @see TCPSocketServer
041 */
042public class ClientThread extends Thread
043{
044  protected int debug_=2;
045  protected String clientRequest = null;
046  protected DataInputStream is = null;
047  protected PrintStream os = null;
048  protected Socket clientSocket = null;
049
050
051  public ClientThread()
052  {
053  }
054
055
056  public ClientThread(Socket clientSocket)
057  {
058    this.clientSocket = clientSocket;
059  }
060
061
062  /** Chunks the output from the passed inputStream into individual lines/Strings.  **/
063  protected java.util.List<String> readOutput(java.io.InputStream i)
064  {
065    java.util.List<String>  retVal = new java.util.ArrayList<String>();
066    String line = "";
067    java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(i));
068
069    try{while ((line = in.readLine()) != null) retVal.add(line);}catch(IOException ioex){}
070    return retVal;
071  }
072
073
074  /**
075    * Set Method for class field 'clientSocket'.
076    *
077    * @param clientSocket is the value to set this class field to.
078    *
079    **/
080  public  void setClientSocket(Socket clientSocket)
081  {
082    this.clientSocket = clientSocket;
083  }  // setClientSocket Method
084
085
086  /**
087    * Get Method for class field 'clientSocket'.
088    *
089    * @return Socket - The value the class field 'clientSocket'.
090    *
091    **/
092  public Socket getClientSocket()
093  {
094    return clientSocket;
095  }  // getClientSocket Method
096
097
098  /** The thread execution method.  **/
099  public void run()
100  {
101    String TEMP_DIR = System.getProperty("java.io.tmpdir");
102    try
103    {
104      /*
105       * Create input and output streams for this client.
106       */
107      if (debug_>1) System.out.println("   >Starting ClientThread: "+ Thread.currentThread().getId());
108
109      is = new DataInputStream(clientSocket.getInputStream());
110      os = new PrintStream(clientSocket.getOutputStream());
111      String clientRequest = "";
112      java.util.List<String> results = null;
113      int exitCode = 0;
114
115      //os.println("?");
116      clientRequest = is.readLine().trim();
117      if (debug_>1) System.out.println("   > ClientRequest: "+clientRequest);
118      String[] cmdArgs = clientRequest.split("\\s");
119      java.util.List <String> cmdList = java.util.Arrays.asList(cmdArgs);
120
121      /* Execute the request. */
122      synchronized (this)
123      {
124        // send a native command
125        if (debug_>2) System.out.println("Doing something with the socket request");
126        java.lang.Process procRet = null;
127        try
128        {
129          java.lang.ProcessBuilder pb = new java.lang.ProcessBuilder(cmdList);
130          pb.directory(new java.io.File(TEMP_DIR));
131
132          procRet = pb.start();
133          os.println("...");
134          results = readOutput(procRet.getInputStream());
135          exitCode = procRet.waitFor();
136        }
137        catch (Exception ex)
138        {
139          procRet = null;
140        }
141        if(procRet!=null && procRet.exitValue()==0)
142        {
143          os.println(TCPSocketServer.SUCCESS);
144          for(String s : results) os.println(s);
145        }
146        else
147          os.println(TCPSocketServer.ERROR);
148
149        os.println(TCPSocketServer.END);
150      }
151     if (debug_>1) System.out.println("   >Closing ClientThread: "+ Thread.currentThread().getId());
152
153      /*
154       * Close the output stream, close the input stream, close the socket.
155       */
156      is.close();
157      os.close();
158      clientSocket.close();
159    }
160    catch (IOException e)
161    {
162    }
163  }
164}