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-09 10:57:58 -0800 (Tue, 09 Jan 2001) $'
11
 * '$Revision: 647 $'
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
    
42
		String res = "return";
43
    
44
    System.out.println("host: " + host + " port: " + port + " filename: " +
45
                       filename + " cookie: " + cookie);
46
    try 
47
    {
48
      //while(DataFileServer.portIsAvailable(port)) 
49
      //{//loop until the port is there
50
      //}
51
      echoSocket = new Socket(host, port);
52
      out = echoSocket.getOutputStream();
53
      in = echoSocket.getInputStream();
54
    } 
55
    catch (UnknownHostException e) 
56
    {
57
      System.err.println("Don't know about host: " + host);
58
      System.out.println("error: " + e.getMessage());
59
      e.printStackTrace(System.out);
60
      System.exit(1);
61
    } 
62
    catch (IOException e) 
63
    {
64
      System.err.println("Couldn't get I/O for "
65
                         + "the connection to: "+host);
66
      System.out.println("error: " + e.getMessage());
67
      e.printStackTrace(System.out);
68
      System.exit(1);
69
    }
70
	  
71
    try
72
    {  
73
      File file = new File(filename);
74
      DataOutputStream dsout = new DataOutputStream(out);
75
      FileInputStream fs = new FileInputStream(file);
76
      // first convert the filename to a byte array
77
      String fn = file.getName();
78
      byte[] fname = fn.getBytes();
79
      // now write the string bytes followed by a '0'
80
      for (int i=0;i<fname.length;i++) 
81
      {
82
        dsout.write(fname[i]);    
83
      }
84
      dsout.write(0);  // marks end of name info
85
      
86
      //write the session id to the stream
87
      byte[] cook = cookie.getBytes();
88
      for(int i=0; i<cook.length; i++)
89
      {
90
        dsout.write(cook[i]);
91
      }
92
      dsout.write(0);
93
      
94
      // now read response from server
95
      InputStreamReader isr = new InputStreamReader(in);
96
      BufferedReader rin = new BufferedReader(isr);
97

    
98
      // now send the file data
99
      byte[] buf = new byte[1024];
100
      int cnt = 0;
101
      int i = 0;
102
      while (cnt!=-1) 
103
      {
104
        cnt = fs.read(buf);
105
        System.out.println("i = "+ i +" Bytes read = " + cnt);
106
        if (cnt!=-1) 
107
        {
108
          dsout.write(buf, 0, cnt);
109
        }
110
        i++;
111
      }
112
      fs.close();
113
      dsout.flush();
114
      dsout.close();
115
	  }
116
	  catch (Exception w) 
117
    {
118
      System.out.println("error in DataStreamTest: " + w.getMessage());
119
    }
120

    
121
	  return res;
122
	}
123
  
124
  public static void main(String[] args)
125
  {
126
    System.out.println("Starting DataStreamTest");
127
    try
128
    {
129
      String temp = null;
130
      String servlet = ":8080/berkley/servlet/metacat";
131
      String protocol = "http://";
132
      String host = "alpha.nceas.ucsb.edu";
133
      String server = protocol + host + servlet;
134
      String filename = "/home/berkley/xmltodb/test.dat";
135
      //login url
136
      String u1str = server + "?action=login&username=higgins&password" +
137
                      "=neetar&qformat=xml";
138
      System.out.println("u1: " + u1str);
139
      URL u1 = new URL(u1str);
140
      HttpMessage msg = new HttpMessage(u1);
141
      msg.sendPostMessage();
142
      String cookie = msg.getCookie();
143
      //get the port to send the data to
144
      System.out.println("u2: " + server + "?action=getdataport");
145
      URL u2 = new URL(server + "?action=getdataport");
146
      HttpMessage msg2 = new HttpMessage(u2);
147
      InputStream in = msg2.sendPostMessage();
148
      InputStreamReader isr = new InputStreamReader(in);
149
      char c;
150
      int i = isr.read();
151
      String temp2 = null;
152
      while(i != -1)
153
      { //get the server response to the getdataport request
154
        c = (char)i;
155
        temp2 += c;
156
        i = isr.read();
157
      }
158
      int temp3 = temp2.indexOf("<", 32);
159
      temp2 = temp2.substring(32, temp3); //parse out the port
160
      //the port number taken from the xml encoding
161
      temp2 = temp2.trim();
162
      int port = (new Integer(temp2)).intValue();
163
      int index = cookie.indexOf("JSESSIONID=");
164
      index = cookie.indexOf("=", index);
165
      int index2 = cookie.indexOf(";", index);
166
      //get just the session id information from the cookie
167
      cookie = cookie.substring(index+1, index2);
168
      
169
      //this for loop is a hack to give the servlet enough time to start
170
      //the socket thread.  Sometimes it doesn't work
171
      //for(int j=0; j<100000000; j++) { int x = 1; }
172
      
173
      SendFile(filename, host, port, cookie);
174
    }
175
    catch(Exception e)
176
    {
177
      System.out.println("error in main: " + e.getMessage());
178
      e.printStackTrace(System.out);
179
    }
180
    
181
    
182
  }
183
}
(22-22/42)