1 |
647
|
berkley
|
/**
|
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$'
|
10 |
|
|
* '$Date$'
|
11 |
|
|
* '$Revision$'
|
12 |
669
|
jones
|
*
|
13 |
|
|
* This program is free software; you can redistribute it and/or modify
|
14 |
|
|
* it under the terms of the GNU General Public License as published by
|
15 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
16 |
|
|
* (at your option) any later version.
|
17 |
|
|
*
|
18 |
|
|
* This program is distributed in the hope that it will be useful,
|
19 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21 |
|
|
* GNU General Public License for more details.
|
22 |
|
|
*
|
23 |
|
|
* You should have received a copy of the GNU General Public License
|
24 |
|
|
* along with this program; if not, write to the Free Software
|
25 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26 |
647
|
berkley
|
*/
|
27 |
|
|
|
28 |
|
|
package edu.ucsb.nceas.metacat;
|
29 |
|
|
|
30 |
|
|
import java.io.*;
|
31 |
|
|
import java.lang.*;
|
32 |
|
|
import java.util.*;
|
33 |
|
|
import java.sql.*;
|
34 |
|
|
import java.net.*;
|
35 |
|
|
import edu.ucsb.nceas.metacat.*;
|
36 |
|
|
import javax.servlet.http.*;
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
public class DataStreamTest
|
40 |
|
|
{
|
41 |
|
|
DataStreamTest()
|
42 |
|
|
{
|
43 |
|
|
|
44 |
|
|
}
|
45 |
|
|
|
46 |
|
|
/**
|
47 |
|
|
* sends a file to the data file server socket
|
48 |
|
|
*/
|
49 |
|
|
public static String SendFile(String filename, String host, int port,
|
50 |
|
|
String cookie)
|
51 |
|
|
{
|
52 |
|
|
Socket echoSocket = null;
|
53 |
|
|
OutputStream out = null;
|
54 |
|
|
InputStream in = null;
|
55 |
653
|
berkley
|
DataOutputStream dsout = null;
|
56 |
|
|
InputStreamReader isr = null;
|
57 |
647
|
berkley
|
|
58 |
656
|
berkley
|
String retmsg = "";
|
59 |
647
|
berkley
|
|
60 |
|
|
System.out.println("host: " + host + " port: " + port + " filename: " +
|
61 |
|
|
filename + " cookie: " + cookie);
|
62 |
|
|
try
|
63 |
|
|
{
|
64 |
658
|
berkley
|
echoSocket = DataFileServer.getSocket(host, port); //get the socket
|
65 |
651
|
berkley
|
while(echoSocket == null)
|
66 |
|
|
{//loop until the port is there
|
67 |
|
|
echoSocket = DataFileServer.getSocket(host, port);
|
68 |
|
|
}
|
69 |
|
|
//echoSocket = new Socket(host, port);
|
70 |
653
|
berkley
|
out = echoSocket.getOutputStream(); //out to the server
|
71 |
|
|
in = echoSocket.getInputStream(); //in from server
|
72 |
|
|
isr = new InputStreamReader(in);
|
73 |
647
|
berkley
|
}
|
74 |
|
|
catch (UnknownHostException e)
|
75 |
|
|
{
|
76 |
675
|
berkley
|
System.err.println("Don't know about host: " + host +
|
77 |
|
|
" : error in DataStreamTest.sendFile: " +
|
78 |
|
|
e.getMessage());
|
79 |
647
|
berkley
|
e.printStackTrace(System.out);
|
80 |
|
|
System.exit(1);
|
81 |
|
|
}
|
82 |
|
|
catch (IOException e)
|
83 |
|
|
{
|
84 |
675
|
berkley
|
System.err.println("IO error in DataStreamTest.sendFile: "
|
85 |
|
|
+ "broken connection to: " + host);
|
86 |
647
|
berkley
|
System.out.println("error: " + e.getMessage());
|
87 |
|
|
e.printStackTrace(System.out);
|
88 |
|
|
System.exit(1);
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
try
|
92 |
|
|
{
|
93 |
658
|
berkley
|
File file = new File(filename); //get the file from the local system
|
94 |
|
|
dsout = new DataOutputStream(out);
|
95 |
647
|
berkley
|
FileInputStream fs = new FileInputStream(file);
|
96 |
|
|
// first convert the filename to a byte array
|
97 |
|
|
String fn = file.getName();
|
98 |
|
|
byte[] fname = fn.getBytes();
|
99 |
|
|
// now write the string bytes followed by a '0'
|
100 |
|
|
for (int i=0;i<fname.length;i++)
|
101 |
|
|
{
|
102 |
|
|
dsout.write(fname[i]);
|
103 |
|
|
}
|
104 |
|
|
dsout.write(0); // marks end of name info
|
105 |
|
|
|
106 |
|
|
//write the session id to the stream
|
107 |
|
|
byte[] cook = cookie.getBytes();
|
108 |
|
|
for(int i=0; i<cook.length; i++)
|
109 |
|
|
{
|
110 |
|
|
dsout.write(cook[i]);
|
111 |
|
|
}
|
112 |
658
|
berkley
|
dsout.write(0); //follow it with a 0
|
113 |
647
|
berkley
|
|
114 |
653
|
berkley
|
//write the length of the file followed by a 0
|
115 |
|
|
long flength = file.length();
|
116 |
654
|
berkley
|
System.out.println("file sized (B): " + flength);
|
117 |
658
|
berkley
|
String sflength = String.valueOf(flength); //note that this is changed
|
118 |
|
|
//to a string
|
119 |
654
|
berkley
|
|
120 |
|
|
byte[] fl = sflength.getBytes();
|
121 |
|
|
for(int i=0; i<fl.length; i++)
|
122 |
|
|
{
|
123 |
|
|
dsout.write(fl[i]);
|
124 |
|
|
}
|
125 |
658
|
berkley
|
dsout.write(0); //following 0
|
126 |
654
|
berkley
|
|
127 |
|
|
//dsout.write((int)flength);
|
128 |
|
|
//dsout.write(0);
|
129 |
653
|
berkley
|
dsout.flush();
|
130 |
|
|
|
131 |
647
|
berkley
|
BufferedReader rin = new BufferedReader(isr);
|
132 |
|
|
|
133 |
|
|
// now send the file data
|
134 |
|
|
byte[] buf = new byte[1024];
|
135 |
|
|
int cnt = 0;
|
136 |
|
|
int i = 0;
|
137 |
|
|
while (cnt!=-1)
|
138 |
|
|
{
|
139 |
658
|
berkley
|
cnt = fs.read(buf); //read the file
|
140 |
654
|
berkley
|
System.out.println("i = "+ i +" Bytes read = " + cnt);
|
141 |
647
|
berkley
|
if (cnt!=-1)
|
142 |
|
|
{
|
143 |
658
|
berkley
|
dsout.write(buf, 0, cnt); //write the byte to the server
|
144 |
647
|
berkley
|
}
|
145 |
|
|
i++;
|
146 |
|
|
}
|
147 |
|
|
fs.close();
|
148 |
|
|
dsout.flush();
|
149 |
|
|
}
|
150 |
|
|
catch (Exception w)
|
151 |
|
|
{
|
152 |
675
|
berkley
|
System.out.println("error in DataStreamTest.sendFile: " + w.getMessage());
|
153 |
647
|
berkley
|
}
|
154 |
653
|
berkley
|
|
155 |
|
|
try
|
156 |
658
|
berkley
|
{ //get the server's response
|
157 |
653
|
berkley
|
while(!isr.ready())
|
158 |
|
|
{
|
159 |
|
|
//System.out.println("not ready");
|
160 |
|
|
}
|
161 |
|
|
int msg = isr.read();
|
162 |
656
|
berkley
|
retmsg += (char)msg;
|
163 |
653
|
berkley
|
System.out.print("Message from server: ");
|
164 |
|
|
while(msg != -1)
|
165 |
|
|
{
|
166 |
|
|
System.out.print((char)msg);
|
167 |
|
|
System.out.flush();
|
168 |
658
|
berkley
|
msg = isr.read(); //get the message a character at a time
|
169 |
656
|
berkley
|
retmsg += (char)msg;
|
170 |
653
|
berkley
|
}
|
171 |
|
|
System.out.println();
|
172 |
|
|
dsout.flush();
|
173 |
|
|
dsout.close();
|
174 |
|
|
}
|
175 |
|
|
catch(Exception e)
|
176 |
|
|
{
|
177 |
675
|
berkley
|
System.out.println("error in DataStreamTest.sendFile: " + e.getMessage());
|
178 |
653
|
berkley
|
}
|
179 |
656
|
berkley
|
return retmsg;
|
180 |
647
|
berkley
|
}
|
181 |
|
|
|
182 |
|
|
public static void main(String[] args)
|
183 |
|
|
{
|
184 |
|
|
System.out.println("Starting DataStreamTest");
|
185 |
|
|
try
|
186 |
|
|
{
|
187 |
|
|
String temp = null;
|
188 |
656
|
berkley
|
String servlet = "/berkley/servlet/metacat";
|
189 |
647
|
berkley
|
String protocol = "http://";
|
190 |
656
|
berkley
|
String host = "dev.nceas.ucsb.edu";
|
191 |
647
|
berkley
|
String server = protocol + host + servlet;
|
192 |
653
|
berkley
|
String filename = null;
|
193 |
|
|
if(args.length == 0)
|
194 |
|
|
{
|
195 |
|
|
filename = "/home/berkley/xmltodb/test.dat";
|
196 |
|
|
}
|
197 |
|
|
else
|
198 |
|
|
{
|
199 |
|
|
filename = args[0];
|
200 |
|
|
}
|
201 |
647
|
berkley
|
//login url
|
202 |
|
|
String u1str = server + "?action=login&username=higgins&password" +
|
203 |
|
|
"=neetar&qformat=xml";
|
204 |
653
|
berkley
|
System.out.println("url 1: " + u1str);
|
205 |
647
|
berkley
|
URL u1 = new URL(u1str);
|
206 |
|
|
HttpMessage msg = new HttpMessage(u1);
|
207 |
|
|
msg.sendPostMessage();
|
208 |
|
|
String cookie = msg.getCookie();
|
209 |
|
|
//get the port to send the data to
|
210 |
653
|
berkley
|
System.out.println("url 2: " + server + "?action=getdataport");
|
211 |
647
|
berkley
|
URL u2 = new URL(server + "?action=getdataport");
|
212 |
|
|
HttpMessage msg2 = new HttpMessage(u2);
|
213 |
|
|
InputStream in = msg2.sendPostMessage();
|
214 |
|
|
InputStreamReader isr = new InputStreamReader(in);
|
215 |
|
|
char c;
|
216 |
|
|
int i = isr.read();
|
217 |
|
|
String temp2 = null;
|
218 |
|
|
while(i != -1)
|
219 |
|
|
{ //get the server response to the getdataport request
|
220 |
|
|
c = (char)i;
|
221 |
|
|
temp2 += c;
|
222 |
|
|
i = isr.read();
|
223 |
|
|
}
|
224 |
|
|
int temp3 = temp2.indexOf("<", 32);
|
225 |
|
|
temp2 = temp2.substring(32, temp3); //parse out the port
|
226 |
|
|
//the port number taken from the xml encoding
|
227 |
|
|
temp2 = temp2.trim();
|
228 |
|
|
int port = (new Integer(temp2)).intValue();
|
229 |
651
|
berkley
|
//while(portIsAvailable(port)) {int x = 1;}
|
230 |
647
|
berkley
|
int index = cookie.indexOf("JSESSIONID=");
|
231 |
|
|
index = cookie.indexOf("=", index);
|
232 |
|
|
int index2 = cookie.indexOf(";", index);
|
233 |
|
|
//get just the session id information from the cookie
|
234 |
|
|
cookie = cookie.substring(index+1, index2);
|
235 |
|
|
|
236 |
656
|
berkley
|
String servermsg = SendFile(filename, host, port, cookie);
|
237 |
|
|
System.out.println("server says (end): " + servermsg);
|
238 |
647
|
berkley
|
}
|
239 |
|
|
catch(Exception e)
|
240 |
|
|
{
|
241 |
|
|
System.out.println("error in main: " + e.getMessage());
|
242 |
|
|
e.printStackTrace(System.out);
|
243 |
|
|
}
|
244 |
|
|
}
|
245 |
|
|
}
|