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: jones $'
|
10
|
* '$Date: 2001-01-18 11:52:00 -0800 (Thu, 18 Jan 2001) $'
|
11
|
* '$Revision: 669 $'
|
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
|
System.out.println("error: " + e.getMessage());
|
78
|
e.printStackTrace(System.out);
|
79
|
System.exit(1);
|
80
|
}
|
81
|
catch (IOException e)
|
82
|
{
|
83
|
System.err.println("Couldn't get I/O for "
|
84
|
+ "the connection to: " + host);
|
85
|
System.out.println("error: " + e.getMessage());
|
86
|
e.printStackTrace(System.out);
|
87
|
System.exit(1);
|
88
|
}
|
89
|
|
90
|
try
|
91
|
{
|
92
|
File file = new File(filename); //get the file from the local system
|
93
|
dsout = new DataOutputStream(out);
|
94
|
FileInputStream fs = new FileInputStream(file);
|
95
|
// first convert the filename to a byte array
|
96
|
String fn = file.getName();
|
97
|
byte[] fname = fn.getBytes();
|
98
|
// now write the string bytes followed by a '0'
|
99
|
for (int i=0;i<fname.length;i++)
|
100
|
{
|
101
|
dsout.write(fname[i]);
|
102
|
}
|
103
|
dsout.write(0); // marks end of name info
|
104
|
|
105
|
//write the session id to the stream
|
106
|
byte[] cook = cookie.getBytes();
|
107
|
for(int i=0; i<cook.length; i++)
|
108
|
{
|
109
|
dsout.write(cook[i]);
|
110
|
}
|
111
|
dsout.write(0); //follow it with a 0
|
112
|
|
113
|
//write the length of the file followed by a 0
|
114
|
long flength = file.length();
|
115
|
System.out.println("file sized (B): " + flength);
|
116
|
String sflength = String.valueOf(flength); //note that this is changed
|
117
|
//to a string
|
118
|
|
119
|
byte[] fl = sflength.getBytes();
|
120
|
for(int i=0; i<fl.length; i++)
|
121
|
{
|
122
|
dsout.write(fl[i]);
|
123
|
}
|
124
|
dsout.write(0); //following 0
|
125
|
|
126
|
//dsout.write((int)flength);
|
127
|
//dsout.write(0);
|
128
|
dsout.flush();
|
129
|
|
130
|
BufferedReader rin = new BufferedReader(isr);
|
131
|
|
132
|
// now send the file data
|
133
|
byte[] buf = new byte[1024];
|
134
|
int cnt = 0;
|
135
|
int i = 0;
|
136
|
while (cnt!=-1)
|
137
|
{
|
138
|
cnt = fs.read(buf); //read the file
|
139
|
System.out.println("i = "+ i +" Bytes read = " + cnt);
|
140
|
if (cnt!=-1)
|
141
|
{
|
142
|
dsout.write(buf, 0, cnt); //write the byte to the server
|
143
|
}
|
144
|
i++;
|
145
|
}
|
146
|
fs.close();
|
147
|
dsout.flush();
|
148
|
}
|
149
|
catch (Exception w)
|
150
|
{
|
151
|
System.out.println("error in DataStreamTest: " + w.getMessage());
|
152
|
}
|
153
|
|
154
|
try
|
155
|
{ //get the server's response
|
156
|
while(!isr.ready())
|
157
|
{
|
158
|
//System.out.println("not ready");
|
159
|
}
|
160
|
int msg = isr.read();
|
161
|
retmsg += (char)msg;
|
162
|
System.out.print("Message from server: ");
|
163
|
while(msg != -1)
|
164
|
{
|
165
|
System.out.print((char)msg);
|
166
|
System.out.flush();
|
167
|
msg = isr.read(); //get the message a character at a time
|
168
|
retmsg += (char)msg;
|
169
|
}
|
170
|
System.out.println();
|
171
|
dsout.flush();
|
172
|
dsout.close();
|
173
|
}
|
174
|
catch(Exception e)
|
175
|
{
|
176
|
System.out.println("error: " + e.getMessage());
|
177
|
}
|
178
|
return retmsg;
|
179
|
}
|
180
|
|
181
|
public static void main(String[] args)
|
182
|
{
|
183
|
System.out.println("Starting DataStreamTest");
|
184
|
try
|
185
|
{
|
186
|
String temp = null;
|
187
|
String servlet = "/berkley/servlet/metacat";
|
188
|
String protocol = "http://";
|
189
|
String host = "dev.nceas.ucsb.edu";
|
190
|
String server = protocol + host + servlet;
|
191
|
String filename = null;
|
192
|
if(args.length == 0)
|
193
|
{
|
194
|
filename = "/home/berkley/xmltodb/test.dat";
|
195
|
}
|
196
|
else
|
197
|
{
|
198
|
filename = args[0];
|
199
|
}
|
200
|
//login url
|
201
|
String u1str = server + "?action=login&username=higgins&password" +
|
202
|
"=neetar&qformat=xml";
|
203
|
System.out.println("url 1: " + u1str);
|
204
|
URL u1 = new URL(u1str);
|
205
|
HttpMessage msg = new HttpMessage(u1);
|
206
|
msg.sendPostMessage();
|
207
|
String cookie = msg.getCookie();
|
208
|
//get the port to send the data to
|
209
|
System.out.println("url 2: " + server + "?action=getdataport");
|
210
|
URL u2 = new URL(server + "?action=getdataport");
|
211
|
HttpMessage msg2 = new HttpMessage(u2);
|
212
|
InputStream in = msg2.sendPostMessage();
|
213
|
InputStreamReader isr = new InputStreamReader(in);
|
214
|
char c;
|
215
|
int i = isr.read();
|
216
|
String temp2 = null;
|
217
|
while(i != -1)
|
218
|
{ //get the server response to the getdataport request
|
219
|
c = (char)i;
|
220
|
temp2 += c;
|
221
|
i = isr.read();
|
222
|
}
|
223
|
int temp3 = temp2.indexOf("<", 32);
|
224
|
temp2 = temp2.substring(32, temp3); //parse out the port
|
225
|
//the port number taken from the xml encoding
|
226
|
temp2 = temp2.trim();
|
227
|
int port = (new Integer(temp2)).intValue();
|
228
|
//while(portIsAvailable(port)) {int x = 1;}
|
229
|
int index = cookie.indexOf("JSESSIONID=");
|
230
|
index = cookie.indexOf("=", index);
|
231
|
int index2 = cookie.indexOf(";", index);
|
232
|
//get just the session id information from the cookie
|
233
|
cookie = cookie.substring(index+1, index2);
|
234
|
|
235
|
String servermsg = SendFile(filename, host, port, cookie);
|
236
|
System.out.println("server says (end): " + servermsg);
|
237
|
}
|
238
|
catch(Exception e)
|
239
|
{
|
240
|
System.out.println("error in main: " + e.getMessage());
|
241
|
e.printStackTrace(System.out);
|
242
|
}
|
243
|
}
|
244
|
}
|