1 |
647
|
berkley
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
656
|
berkley
|
* Purpose: A Class that implements a socket server for data files that
|
4 |
|
|
* writes the file to the servlet's local file system
|
5 |
647
|
berkley
|
* Copyright: 2000 Regents of the University of California and the
|
6 |
|
|
* National Center for Ecological Analysis and Synthesis
|
7 |
|
|
* Authors: Chad Berkley
|
8 |
|
|
* Release: @release@
|
9 |
|
|
*
|
10 |
|
|
* '$Author$'
|
11 |
|
|
* '$Date$'
|
12 |
|
|
* '$Revision$'
|
13 |
669
|
jones
|
*
|
14 |
|
|
* This program is free software; you can redistribute it and/or modify
|
15 |
|
|
* it under the terms of the GNU General Public License as published by
|
16 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
17 |
|
|
* (at your option) any later version.
|
18 |
|
|
*
|
19 |
|
|
* This program is distributed in the hope that it will be useful,
|
20 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22 |
|
|
* GNU General Public License for more details.
|
23 |
|
|
*
|
24 |
|
|
* You should have received a copy of the GNU General Public License
|
25 |
|
|
* along with this program; if not, write to the Free Software
|
26 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
27 |
647
|
berkley
|
*/
|
28 |
|
|
|
29 |
|
|
package edu.ucsb.nceas.metacat;
|
30 |
|
|
|
31 |
|
|
import java.net.*;
|
32 |
|
|
import java.io.*;
|
33 |
|
|
import java.util.Properties;
|
34 |
|
|
import java.util.Date;
|
35 |
|
|
import java.text.SimpleDateFormat;
|
36 |
|
|
import java.sql.*;
|
37 |
|
|
|
38 |
656
|
berkley
|
public class DataFileServer extends DataFileUploadInterface
|
39 |
647
|
berkley
|
{
|
40 |
|
|
MetaCatUtil util = new MetaCatUtil();
|
41 |
|
|
static String filedir = "";
|
42 |
656
|
berkley
|
|
43 |
647
|
berkley
|
protected Socket s;
|
44 |
|
|
|
45 |
656
|
berkley
|
public DataFileServer(int port, String user, String sess_id)
|
46 |
647
|
berkley
|
{
|
47 |
656
|
berkley
|
super(port, user, sess_id);
|
48 |
|
|
System.out.println("port: " + port + " user: " + user + " sess: " +
|
49 |
|
|
sess_id);
|
50 |
647
|
berkley
|
}
|
51 |
|
|
|
52 |
656
|
berkley
|
public void getFile(int port, String user, String sess_id)
|
53 |
651
|
berkley
|
{
|
54 |
656
|
berkley
|
filedir = util.getOption("datafilepath");
|
55 |
647
|
berkley
|
ServerSocket server = null;
|
56 |
|
|
Socket client = null;
|
57 |
653
|
berkley
|
OutputStreamWriter osw = null;
|
58 |
647
|
berkley
|
try
|
59 |
|
|
{
|
60 |
656
|
berkley
|
System.out.println("Starting on port " + port);
|
61 |
|
|
System.out.println("Waiting for sess_id: " + sess_id);
|
62 |
647
|
berkley
|
server = new ServerSocket((new Integer(port)).intValue());
|
63 |
651
|
berkley
|
server.setSoTimeout(30000); //set a 30 second timeout
|
64 |
647
|
berkley
|
client = server.accept();
|
65 |
656
|
berkley
|
System.out.println("Accepted from " + client.getInetAddress());
|
66 |
647
|
berkley
|
|
67 |
653
|
berkley
|
InputStream in = client.getInputStream(); //in from the client
|
68 |
|
|
OutputStream sout = client.getOutputStream(); //out to the client
|
69 |
|
|
osw = new OutputStreamWriter(sout);
|
70 |
656
|
berkley
|
System.out.println("output stream received");
|
71 |
647
|
berkley
|
// first read to get the file name
|
72 |
|
|
byte[] str = new byte[1024];
|
73 |
|
|
int val = -1;
|
74 |
|
|
int i = 0;
|
75 |
|
|
//get the filename
|
76 |
653
|
berkley
|
while (val != 0)
|
77 |
647
|
berkley
|
{
|
78 |
|
|
//System.out.println("i: " + i);
|
79 |
|
|
val = in.read();
|
80 |
|
|
str[i] = (byte)val;
|
81 |
|
|
i++;
|
82 |
|
|
}
|
83 |
|
|
String filename = (new String(str,0, i-1)).trim();
|
84 |
656
|
berkley
|
System.out.println("filename: " + filename);
|
85 |
647
|
berkley
|
|
86 |
|
|
val = -1;
|
87 |
|
|
i = 0;
|
88 |
|
|
str = new byte[1024];
|
89 |
|
|
//get the session id
|
90 |
653
|
berkley
|
while (val != 0)
|
91 |
647
|
berkley
|
{
|
92 |
|
|
val = in.read();
|
93 |
|
|
str[i] = (byte)val;
|
94 |
|
|
i++;
|
95 |
|
|
}
|
96 |
|
|
String session_id = (new String(str,0, i-1)).trim();
|
97 |
|
|
|
98 |
653
|
berkley
|
//get the length of the file in bytes
|
99 |
654
|
berkley
|
val = -1;
|
100 |
|
|
i = 0;
|
101 |
|
|
str = new byte[1024];
|
102 |
|
|
while(val != 0)
|
103 |
653
|
berkley
|
{
|
104 |
654
|
berkley
|
val = in.read();
|
105 |
|
|
str[i] = (byte)val;
|
106 |
|
|
i++;
|
107 |
653
|
berkley
|
}
|
108 |
654
|
berkley
|
String stemp = (new String(str, 0, i-1)).trim();
|
109 |
|
|
Long fsizetemp = new Long(stemp);
|
110 |
|
|
long fsize = fsizetemp.longValue();
|
111 |
653
|
berkley
|
//System.out.println("file size: " + fsize);
|
112 |
|
|
|
113 |
647
|
berkley
|
if(!sess_id.equals(session_id))
|
114 |
|
|
{//this is an unauthorized connection to this port
|
115 |
|
|
System.out.println("unauthorized request");
|
116 |
656
|
berkley
|
osw.write("<?xml version=\"1.0\"?><error>unauthorized request</error>");
|
117 |
|
|
osw.flush();
|
118 |
|
|
osw.close();
|
119 |
647
|
berkley
|
return;
|
120 |
|
|
}
|
121 |
|
|
else
|
122 |
|
|
{
|
123 |
656
|
berkley
|
System.out.println("User authenticated on port " + port);
|
124 |
647
|
berkley
|
}
|
125 |
|
|
|
126 |
|
|
String restext=null;
|
127 |
|
|
File outfile = new File(filedir + filename);
|
128 |
656
|
berkley
|
System.out.println("outfile: " + filedir + filename);
|
129 |
647
|
berkley
|
boolean nameInUse = outfile.exists();
|
130 |
650
|
berkley
|
int filenametemp = 1;
|
131 |
651
|
berkley
|
String fn = filename;
|
132 |
650
|
berkley
|
while(nameInUse)
|
133 |
647
|
berkley
|
{
|
134 |
651
|
berkley
|
filename = fn + filenametemp;
|
135 |
650
|
berkley
|
outfile = new File (filedir + filename);
|
136 |
|
|
nameInUse = outfile.exists();
|
137 |
|
|
filenametemp++;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
try
|
141 |
|
|
{
|
142 |
|
|
AccessionNumber anum = new AccessionNumber();
|
143 |
|
|
String accnum = anum.generate(null, "INSERT");
|
144 |
653
|
berkley
|
|
145 |
650
|
berkley
|
FileOutputStream out = new FileOutputStream(outfile);
|
146 |
|
|
byte[] buf = new byte[1024];
|
147 |
|
|
int cnt = 0;
|
148 |
653
|
berkley
|
int j = 0;
|
149 |
|
|
while (j < fsize)
|
150 |
647
|
berkley
|
{
|
151 |
650
|
berkley
|
cnt = in.read(buf, 0, 1024);
|
152 |
|
|
if (cnt!=-1)
|
153 |
647
|
berkley
|
{
|
154 |
650
|
berkley
|
out.write(buf, 0, cnt);
|
155 |
647
|
berkley
|
}
|
156 |
653
|
berkley
|
j += cnt;
|
157 |
647
|
berkley
|
}
|
158 |
653
|
berkley
|
|
159 |
650
|
berkley
|
out.flush();
|
160 |
|
|
out.close();
|
161 |
|
|
//put the new document info into the DB
|
162 |
|
|
updateDB(accnum, filename, user);
|
163 |
653
|
berkley
|
//System.out.println("sending new docid: " + accnum);
|
164 |
|
|
osw.write("<?xml version=\"1.0\"?><docid>" + accnum + "</docid>");
|
165 |
|
|
osw.flush();
|
166 |
|
|
osw.close();
|
167 |
|
|
//System.out.println("docid sent");
|
168 |
647
|
berkley
|
}
|
169 |
650
|
berkley
|
catch(Exception e)
|
170 |
|
|
{
|
171 |
|
|
System.out.println("error in DataFileServer.run(): " + e.getMessage());
|
172 |
653
|
berkley
|
try
|
173 |
|
|
{
|
174 |
|
|
osw.write("<?xml version=\"1.0\"?><error>" + e.getMessage() +
|
175 |
|
|
"</error>");
|
176 |
|
|
osw.flush();
|
177 |
|
|
osw.close();
|
178 |
|
|
}
|
179 |
|
|
catch(Exception ee)
|
180 |
|
|
{
|
181 |
675
|
berkley
|
System.out.println("error DataFileServer.run(): " + ee.getMessage());
|
182 |
653
|
berkley
|
}
|
183 |
650
|
berkley
|
e.printStackTrace(System.out);
|
184 |
647
|
berkley
|
}
|
185 |
|
|
}
|
186 |
651
|
berkley
|
catch(InterruptedIOException iioe)
|
187 |
|
|
{
|
188 |
|
|
//the accept timeout passed
|
189 |
675
|
berkley
|
System.out.println("socket on port " + port + " timed out. " +
|
190 |
|
|
" (DataFileServer.run)");
|
191 |
651
|
berkley
|
}
|
192 |
647
|
berkley
|
catch (IOException ex)
|
193 |
|
|
{
|
194 |
|
|
ex.printStackTrace();
|
195 |
653
|
berkley
|
try
|
196 |
|
|
{
|
197 |
|
|
osw.write("<?xml version=\"1.0\"?><error>" + ex.getMessage() +
|
198 |
|
|
"</error>");
|
199 |
|
|
osw.flush();
|
200 |
|
|
osw.close();
|
201 |
|
|
}
|
202 |
|
|
catch(Exception ee)
|
203 |
|
|
{
|
204 |
675
|
berkley
|
System.out.println("error in DataFileServer.run(): " + ee.getMessage());
|
205 |
653
|
berkley
|
}
|
206 |
647
|
berkley
|
}
|
207 |
|
|
finally
|
208 |
|
|
{
|
209 |
|
|
try
|
210 |
|
|
{
|
211 |
651
|
berkley
|
server.close();
|
212 |
|
|
//System.out.println("server socket closed");
|
213 |
647
|
berkley
|
client.close();
|
214 |
651
|
berkley
|
//System.out.println("client socket closed");
|
215 |
647
|
berkley
|
}
|
216 |
|
|
catch (IOException ex)
|
217 |
|
|
{
|
218 |
|
|
ex.printStackTrace();
|
219 |
|
|
}
|
220 |
|
|
}
|
221 |
|
|
}
|
222 |
|
|
}
|