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-10 13:05:31 -0800 (Wed, 10 Jan 2001) $'
11
 * '$Revision: 653 $'
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 res = "return";
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
      dsout.write((int)flength);
103
      dsout.write(0);
104
      dsout.flush();
105
      
106
      // now read response from server
107
      //InputStreamReader isr = new InputStreamReader(in);
108
      BufferedReader rin = new BufferedReader(isr);
109

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