Revision 653
Added by berkley almost 24 years ago
src/edu/ucsb/nceas/metacat/DataFileServer.java | ||
---|---|---|
137 | 137 |
{ |
138 | 138 |
ServerSocket server = null; |
139 | 139 |
Socket client = null; |
140 |
OutputStreamWriter osw = null; |
|
140 | 141 |
try |
141 | 142 |
{ |
142 | 143 |
//System.out.println("Starting on port " + port); |
... | ... | |
147 | 148 |
client = server.accept(); |
148 | 149 |
//System.out.println("Accepted from " + client.getInetAddress()); |
149 | 150 |
|
150 |
InputStream in = client.getInputStream(); |
|
151 |
OutputStreamWriter osw = new OutputStreamWriter(client.getOutputStream()); |
|
151 |
InputStream in = client.getInputStream(); //in from the client |
|
152 |
OutputStream sout = client.getOutputStream(); //out to the client |
|
153 |
osw = new OutputStreamWriter(sout); |
|
152 | 154 |
//System.out.println("output stream received"); |
153 | 155 |
// first read to get the file name |
154 | 156 |
byte[] str = new byte[1024]; |
155 | 157 |
int val = -1; |
156 | 158 |
int i = 0; |
157 | 159 |
//get the filename |
158 |
while (val!=0)
|
|
160 |
while (val != 0)
|
|
159 | 161 |
{ |
160 | 162 |
//System.out.println("i: " + i); |
161 | 163 |
val = in.read(); |
... | ... | |
169 | 171 |
i = 0; |
170 | 172 |
str = new byte[1024]; |
171 | 173 |
//get the session id |
172 |
while (val!=0)
|
|
174 |
while (val != 0)
|
|
173 | 175 |
{ |
174 | 176 |
val = in.read(); |
175 | 177 |
str[i] = (byte)val; |
176 | 178 |
i++; |
177 | 179 |
} |
178 | 180 |
String session_id = (new String(str,0, i-1)).trim(); |
179 |
//System.out.println("session_id: " + session_id); |
|
180 | 181 |
|
182 |
//get the length of the file in bytes |
|
183 |
int val2 = -1; |
|
184 |
long fsize = 0; |
|
185 |
while(val2 != 0) |
|
186 |
{ |
|
187 |
val2 = in.read(); |
|
188 |
if(val2 != 0) |
|
189 |
{ |
|
190 |
fsize = val2; |
|
191 |
} |
|
192 |
} |
|
193 |
//System.out.println("file size: " + fsize); |
|
194 |
|
|
181 | 195 |
if(!sess_id.equals(session_id)) |
182 | 196 |
{//this is an unauthorized connection to this port |
183 | 197 |
System.out.println("unauthorized request"); |
... | ... | |
208 | 222 |
String accnum = anum.generate(null, "INSERT"); |
209 | 223 |
//osw.write(restext,0,restext.length()); |
210 | 224 |
//osw.flush(); |
225 |
|
|
211 | 226 |
FileOutputStream out = new FileOutputStream(outfile); |
212 | 227 |
byte[] buf = new byte[1024]; |
213 | 228 |
int cnt = 0; |
214 |
while (cnt!=-1) |
|
229 |
int j = 0; |
|
230 |
while (j < fsize) |
|
215 | 231 |
{ |
232 |
//System.out.println("1 j:" + j); |
|
216 | 233 |
cnt = in.read(buf, 0, 1024); |
217 | 234 |
if (cnt!=-1) |
218 | 235 |
{ |
219 | 236 |
out.write(buf, 0, cnt); |
220 | 237 |
} |
238 |
j += cnt; |
|
221 | 239 |
} |
240 |
|
|
222 | 241 |
out.flush(); |
223 | 242 |
out.close(); |
224 | 243 |
//put the new document info into the DB |
225 | 244 |
updateDB(accnum, filename, user); |
245 |
//System.out.println("sending new docid: " + accnum); |
|
246 |
osw.write("<?xml version=\"1.0\"?><docid>" + accnum + "</docid>"); |
|
247 |
osw.flush(); |
|
248 |
osw.close(); |
|
249 |
//System.out.println("docid sent"); |
|
226 | 250 |
} |
227 | 251 |
catch(Exception e) |
228 | 252 |
{ |
229 | 253 |
System.out.println("error in DataFileServer.run(): " + e.getMessage()); |
254 |
try |
|
255 |
{ |
|
256 |
osw.write("<?xml version=\"1.0\"?><error>" + e.getMessage() + |
|
257 |
"</error>"); |
|
258 |
osw.flush(); |
|
259 |
osw.close(); |
|
260 |
} |
|
261 |
catch(Exception ee) |
|
262 |
{ |
|
263 |
System.out.println("error: " + ee.getMessage()); |
|
264 |
} |
|
230 | 265 |
e.printStackTrace(System.out); |
231 | 266 |
} |
232 | 267 |
} |
... | ... | |
238 | 273 |
catch (IOException ex) |
239 | 274 |
{ |
240 | 275 |
ex.printStackTrace(); |
276 |
try |
|
277 |
{ |
|
278 |
osw.write("<?xml version=\"1.0\"?><error>" + ex.getMessage() + |
|
279 |
"</error>"); |
|
280 |
osw.flush(); |
|
281 |
osw.close(); |
|
282 |
} |
|
283 |
catch(Exception ee) |
|
284 |
{ |
|
285 |
System.out.println("error: " + ee.getMessage()); |
|
286 |
} |
|
241 | 287 |
} |
242 | 288 |
finally |
243 | 289 |
{ |
src/edu/ucsb/nceas/metacat/DataStreamTest.java | ||
---|---|---|
38 | 38 |
Socket echoSocket = null; |
39 | 39 |
OutputStream out = null; |
40 | 40 |
InputStream in = null; |
41 |
DataOutputStream dsout = null; |
|
42 |
InputStreamReader isr = null; |
|
41 | 43 |
|
42 | 44 |
String res = "return"; |
43 | 45 |
|
... | ... | |
51 | 53 |
echoSocket = DataFileServer.getSocket(host, port); |
52 | 54 |
} |
53 | 55 |
//echoSocket = new Socket(host, port); |
54 |
out = echoSocket.getOutputStream(); |
|
55 |
in = echoSocket.getInputStream(); |
|
56 |
out = echoSocket.getOutputStream(); //out to the server |
|
57 |
in = echoSocket.getInputStream(); //in from server |
|
58 |
isr = new InputStreamReader(in); |
|
56 | 59 |
} |
57 | 60 |
catch (UnknownHostException e) |
58 | 61 |
{ |
... | ... | |
73 | 76 |
try |
74 | 77 |
{ |
75 | 78 |
File file = new File(filename); |
76 |
DataOutputStream dsout = new DataOutputStream(out);
|
|
79 |
dsout = new DataOutputStream(out); |
|
77 | 80 |
FileInputStream fs = new FileInputStream(file); |
78 | 81 |
// first convert the filename to a byte array |
79 | 82 |
String fn = file.getName(); |
... | ... | |
93 | 96 |
} |
94 | 97 |
dsout.write(0); |
95 | 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 |
|
|
96 | 106 |
// now read response from server |
97 |
InputStreamReader isr = new InputStreamReader(in); |
|
107 |
//InputStreamReader isr = new InputStreamReader(in);
|
|
98 | 108 |
BufferedReader rin = new BufferedReader(isr); |
99 | 109 |
|
100 | 110 |
// now send the file data |
... | ... | |
104 | 114 |
while (cnt!=-1) |
105 | 115 |
{ |
106 | 116 |
cnt = fs.read(buf); |
107 |
System.out.println("i = "+ i +" Bytes read = " + cnt); |
|
117 |
//System.out.println("i = "+ i +" Bytes read = " + cnt);
|
|
108 | 118 |
if (cnt!=-1) |
109 | 119 |
{ |
110 | 120 |
dsout.write(buf, 0, cnt); |
... | ... | |
113 | 123 |
} |
114 | 124 |
fs.close(); |
115 | 125 |
dsout.flush(); |
116 |
dsout.close(); |
|
126 |
//dsout.close();
|
|
117 | 127 |
} |
118 | 128 |
catch (Exception w) |
119 | 129 |
{ |
120 | 130 |
System.out.println("error in DataStreamTest: " + w.getMessage()); |
121 | 131 |
} |
122 |
|
|
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 |
} |
|
123 | 155 |
return res; |
124 | 156 |
} |
125 | 157 |
|
... | ... | |
133 | 165 |
String protocol = "http://"; |
134 | 166 |
String host = "alpha.nceas.ucsb.edu"; |
135 | 167 |
String server = protocol + host + servlet; |
136 |
String filename = "/home/berkley/xmltodb/test.dat"; |
|
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 |
} |
|
137 | 177 |
//login url |
138 | 178 |
String u1str = server + "?action=login&username=higgins&password" + |
139 | 179 |
"=neetar&qformat=xml"; |
140 |
System.out.println("u1: " + u1str); |
|
180 |
System.out.println("url 1: " + u1str);
|
|
141 | 181 |
URL u1 = new URL(u1str); |
142 | 182 |
HttpMessage msg = new HttpMessage(u1); |
143 | 183 |
msg.sendPostMessage(); |
144 | 184 |
String cookie = msg.getCookie(); |
145 | 185 |
//get the port to send the data to |
146 |
System.out.println("u2: " + server + "?action=getdataport"); |
|
186 |
System.out.println("url 2: " + server + "?action=getdataport");
|
|
147 | 187 |
URL u2 = new URL(server + "?action=getdataport"); |
148 | 188 |
HttpMessage msg2 = new HttpMessage(u2); |
149 | 189 |
InputStream in = msg2.sendPostMessage(); |
... | ... | |
176 | 216 |
System.out.println("error in main: " + e.getMessage()); |
177 | 217 |
e.printStackTrace(System.out); |
178 | 218 |
} |
179 |
|
|
180 |
|
|
181 | 219 |
} |
182 | 220 |
} |
Also available in: Unified diff
added support for the server to return the docid of the newly added data file. Also, now the data stream must be prefixed with the following information: filename <0> sessionid <0> filesize <0>. where the <0> symbol is a byte containing zero. The filesize is sent as an int and the filename and sessionid are sent as strings (in byte form).