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-18 15:15:21 -0800 (Thu, 18 Jan 2001) $'
|
11
|
* '$Revision: 675 $'
|
12
|
*
|
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
|
*/
|
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
|
DataOutputStream dsout = null;
|
56
|
InputStreamReader isr = null;
|
57
|
|
58
|
String retmsg = "";
|
59
|
|
60
|
System.out.println("host: " + host + " port: " + port + " filename: " +
|
61
|
filename + " cookie: " + cookie);
|
62
|
try
|
63
|
{
|
64
|
echoSocket = DataFileServer.getSocket(host, port); //get the socket
|
65
|
while(echoSocket == null)
|
66
|
{//loop until the port is there
|
67
|
echoSocket = DataFileServer.getSocket(host, port);
|
68
|
}
|
69
|
//echoSocket = new Socket(host, port);
|
70
|
out = echoSocket.getOutputStream(); //out to the server
|
71
|
in = echoSocket.getInputStream(); //in from server
|
72
|
isr = new InputStreamReader(in);
|
73
|
}
|
74
|
catch (UnknownHostException e)
|
75
|
{
|
76
|
System.err.println("Don't know about host: " + host +
|
77
|
" : error in DataStreamTest.sendFile: " +
|
78
|
e.getMessage());
|
79
|
e.printStackTrace(System.out);
|
80
|
System.exit(1);
|
81
|
}
|
82
|
catch (IOException e)
|
83
|
{
|
84
|
System.err.println("IO error in DataStreamTest.sendFile: "
|
85
|
+ "broken connection to: " + host);
|
86
|
System.out.println("error: " + e.getMessage());
|
87
|
e.printStackTrace(System.out);
|
88
|
System.exit(1);
|
89
|
}
|
90
|
|
91
|
try
|
92
|
{
|
93
|
File file = new File(filename); //get the file from the local system
|
94
|
dsout = new DataOutputStream(out);
|
95
|
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
|
dsout.write(0); //follow it with a 0
|
113
|
|
114
|
//write the length of the file followed by a 0
|
115
|
long flength = file.length();
|
116
|
System.out.println("file sized (B): " + flength);
|
117
|
String sflength = String.valueOf(flength); //note that this is changed
|
118
|
//to a string
|
119
|
|
120
|
byte[] fl = sflength.getBytes();
|
121
|
for(int i=0; i<fl.length; i++)
|
122
|
{
|
123
|
dsout.write(fl[i]);
|
124
|
}
|
125
|
dsout.write(0); //following 0
|
126
|
|
127
|
//dsout.write((int)flength);
|
128
|
//dsout.write(0);
|
129
|
dsout.flush();
|
130
|
|
131
|
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
|
cnt = fs.read(buf); //read the file
|
140
|
System.out.println("i = "+ i +" Bytes read = " + cnt);
|
141
|
if (cnt!=-1)
|
142
|
{
|
143
|
dsout.write(buf, 0, cnt); //write the byte to the server
|
144
|
}
|
145
|
i++;
|
146
|
}
|
147
|
fs.close();
|
148
|
dsout.flush();
|
149
|
}
|
150
|
catch (Exception w)
|
151
|
{
|
152
|
System.out.println("error in DataStreamTest.sendFile: " + w.getMessage());
|
153
|
}
|
154
|
|
155
|
try
|
156
|
{ //get the server's response
|
157
|
while(!isr.ready())
|
158
|
{
|
159
|
//System.out.println("not ready");
|
160
|
}
|
161
|
int msg = isr.read();
|
162
|
retmsg += (char)msg;
|
163
|
System.out.print("Message from server: ");
|
164
|
while(msg != -1)
|
165
|
{
|
166
|
System.out.print((char)msg);
|
167
|
System.out.flush();
|
168
|
msg = isr.read(); //get the message a character at a time
|
169
|
retmsg += (char)msg;
|
170
|
}
|
171
|
System.out.println();
|
172
|
dsout.flush();
|
173
|
dsout.close();
|
174
|
}
|
175
|
catch(Exception e)
|
176
|
{
|
177
|
System.out.println("error in DataStreamTest.sendFile: " + e.getMessage());
|
178
|
}
|
179
|
return retmsg;
|
180
|
}
|
181
|
|
182
|
public static void main(String[] args)
|
183
|
{
|
184
|
System.out.println("Starting DataStreamTest");
|
185
|
try
|
186
|
{
|
187
|
String temp = null;
|
188
|
String servlet = "/berkley/servlet/metacat";
|
189
|
String protocol = "http://";
|
190
|
String host = "dev.nceas.ucsb.edu";
|
191
|
String server = protocol + host + servlet;
|
192
|
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
|
//login url
|
202
|
String u1str = server + "?action=login&username=higgins&password" +
|
203
|
"=neetar&qformat=xml";
|
204
|
System.out.println("url 1: " + u1str);
|
205
|
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
|
System.out.println("url 2: " + server + "?action=getdataport");
|
211
|
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
|
//while(portIsAvailable(port)) {int x = 1;}
|
230
|
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
|
String servermsg = SendFile(filename, host, port, cookie);
|
237
|
System.out.println("server says (end): " + servermsg);
|
238
|
}
|
239
|
catch(Exception e)
|
240
|
{
|
241
|
System.out.println("error in main: " + e.getMessage());
|
242
|
e.printStackTrace(System.out);
|
243
|
}
|
244
|
}
|
245
|
}
|