Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A test driver for the DataFileServer class
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Chad Berkley
7
 *    Release: @release@
8
 *
9
 *   '$Author: berkley $'
10
 *     '$Date: 2001-01-11 13:37:10 -0800 (Thu, 11 Jan 2001) $'
11
 * '$Revision: 656 $'
12
 */
13

    
14
package edu.ucsb.nceas.metacat;
15

    
16
import java.io.*;
17
import java.lang.*;
18
import java.util.*;
19
import java.sql.*;
20
import java.net.*;
21
import edu.ucsb.nceas.metacat.*;
22
import javax.servlet.http.*;
23

    
24

    
25
public class DataStreamTest
26
{
27
  DataStreamTest()
28
  {
29
  
30
  }
31
  
32
  /**
33
   * sends a file to the data file server socket
34
   */
35
  public static String SendFile(String filename, String host, int port, 
36
                                String cookie) 
37
  {
38
    Socket echoSocket = null;
39
    OutputStream out = null;
40
    InputStream in = null;
41
    DataOutputStream dsout = null;
42
    InputStreamReader isr = null;
43
    
44
		String retmsg = "";
45
    
46
    System.out.println("host: " + host + " port: " + port + " filename: " +
47
                       filename + " cookie: " + cookie);
48
    try 
49
    {
50
      echoSocket = DataFileServer.getSocket(host, port);
51
      while(echoSocket == null) 
52
      {//loop until the port is there
53
        echoSocket = DataFileServer.getSocket(host, port);
54
      }
55
      //echoSocket = new Socket(host, port);
56
      out = echoSocket.getOutputStream(); //out to the server
57
      in = echoSocket.getInputStream();   //in from server
58
      isr = new InputStreamReader(in);
59
    } 
60
    catch (UnknownHostException e) 
61
    {
62
      System.err.println("Don't know about host: " + host);
63
      System.out.println("error: " + e.getMessage());
64
      e.printStackTrace(System.out);
65
      System.exit(1);
66
    } 
67
    catch (IOException e) 
68
    {
69
      System.err.println("Couldn't get I/O for "
70
                         + "the connection to: "+host);
71
      System.out.println("error: " + e.getMessage());
72
      e.printStackTrace(System.out);
73
      System.exit(1);
74
    }
75
	  
76
    try
77
    {  
78
      File file = new File(filename);
79
      dsout = new DataOutputStream(out);
80
      FileInputStream fs = new FileInputStream(file);
81
      // first convert the filename to a byte array
82
      String fn = file.getName();
83
      byte[] fname = fn.getBytes();
84
      // now write the string bytes followed by a '0'
85
      for (int i=0;i<fname.length;i++) 
86
      {
87
        dsout.write(fname[i]);    
88
      }
89
      dsout.write(0);  // marks end of name info
90
      
91
      //write the session id to the stream
92
      byte[] cook = cookie.getBytes();
93
      for(int i=0; i<cook.length; i++)
94
      {
95
        dsout.write(cook[i]);
96
      }
97
      dsout.write(0);
98
      
99
      //write the length of the file followed by a 0
100
      long flength = file.length();
101
      System.out.println("file sized (B): " + flength);
102
      String sflength = String.valueOf(flength);
103
      
104
      byte[] fl = sflength.getBytes();
105
      for(int i=0; i<fl.length; i++)
106
      {
107
        dsout.write(fl[i]);
108
      }
109
      dsout.write(0);
110
      
111
      //dsout.write((int)flength);
112
      //dsout.write(0);
113
      dsout.flush();
114
      
115
      // now read response from server
116
      //InputStreamReader isr = new InputStreamReader(in);
117
      BufferedReader rin = new BufferedReader(isr);
118

    
119
      // now send the file data
120
      byte[] buf = new byte[1024];
121
      int cnt = 0;
122
      int i = 0;
123
      while (cnt!=-1) 
124
      {
125
        cnt = fs.read(buf);
126
        System.out.println("i = "+ i +" Bytes read = " + cnt);
127
        if (cnt!=-1) 
128
        {
129
          dsout.write(buf, 0, cnt);
130
        }
131
        i++;
132
      }
133
      fs.close();
134
      dsout.flush();
135
      //dsout.close();
136
	  }
137
	  catch (Exception w) 
138
    {
139
      System.out.println("error in DataStreamTest: " + w.getMessage());
140
    }
141
   
142
    try
143
    {
144
      while(!isr.ready()) 
145
      {
146
        //System.out.println("not ready");
147
      }
148
      int msg = isr.read();
149
      retmsg += (char)msg;
150
      System.out.print("Message from server: ");
151
      while(msg != -1)
152
      {
153
        System.out.print((char)msg);
154
        System.out.flush();
155
        msg = isr.read();
156
        retmsg += (char)msg;
157
      }
158
      System.out.println();
159
      dsout.flush();
160
      dsout.close();
161
    }
162
    catch(Exception e)
163
    {
164
      System.out.println("error: " + e.getMessage());
165
    }
166
	  return retmsg;
167
	}
168
  
169
  public static void main(String[] args)
170
  {
171
    System.out.println("Starting DataStreamTest");
172
    try
173
    {
174
      String temp = null;
175
      String servlet = "/berkley/servlet/metacat";
176
      String protocol = "http://";
177
      String host = "dev.nceas.ucsb.edu";
178
      String server = protocol + host + servlet;
179
      String filename = null;
180
      if(args.length == 0)
181
      {
182
        filename = "/home/berkley/xmltodb/test.dat";
183
      }
184
      else
185
      {
186
        filename = args[0];
187
      }
188
      //login url
189
      String u1str = server + "?action=login&username=higgins&password" +
190
                      "=neetar&qformat=xml";
191
      System.out.println("url 1: " + u1str);
192
      URL u1 = new URL(u1str);
193
      HttpMessage msg = new HttpMessage(u1);
194
      msg.sendPostMessage();
195
      String cookie = msg.getCookie();
196
      //get the port to send the data to
197
      System.out.println("url 2: " + server + "?action=getdataport");
198
      URL u2 = new URL(server + "?action=getdataport");
199
      HttpMessage msg2 = new HttpMessage(u2);
200
      InputStream in = msg2.sendPostMessage();
201
      InputStreamReader isr = new InputStreamReader(in);
202
      char c;
203
      int i = isr.read();
204
      String temp2 = null;
205
      while(i != -1)
206
      { //get the server response to the getdataport request
207
        c = (char)i;
208
        temp2 += c;
209
        i = isr.read();
210
      }
211
      int temp3 = temp2.indexOf("<", 32);
212
      temp2 = temp2.substring(32, temp3); //parse out the port
213
      //the port number taken from the xml encoding
214
      temp2 = temp2.trim();
215
      int port = (new Integer(temp2)).intValue();
216
      //while(portIsAvailable(port)) {int x = 1;}
217
      int index = cookie.indexOf("JSESSIONID=");
218
      index = cookie.indexOf("=", index);
219
      int index2 = cookie.indexOf(";", index);
220
      //get just the session id information from the cookie
221
      cookie = cookie.substring(index+1, index2);
222
      
223
      String servermsg = SendFile(filename, host, port, cookie);
224
      System.out.println("server says (end): " + servermsg);
225
    }
226
    catch(Exception e)
227
    {
228
      System.out.println("error in main: " + e.getMessage());
229
      e.printStackTrace(System.out);
230
    }
231
  }
232
}
(23-23/43)