Revision 656
Added by berkley over 23 years ago
DataFileServer.java | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* '$RCSfile$' |
3 |
* Purpose: A Class that implements a socket server for data files |
|
3 |
* Purpose: A Class that implements a socket server for data files that |
|
4 |
* writes the file to the servlet's local file system |
|
4 | 5 |
* Copyright: 2000 Regents of the University of California and the |
5 | 6 |
* National Center for Ecological Analysis and Synthesis |
6 | 7 |
* Authors: Chad Berkley |
... | ... | |
20 | 21 |
import java.text.SimpleDateFormat; |
21 | 22 |
import java.sql.*; |
22 | 23 |
|
23 |
public class DataFileServer extends Thread
|
|
24 |
public class DataFileServer extends DataFileUploadInterface
|
|
24 | 25 |
{ |
25 | 26 |
MetaCatUtil util = new MetaCatUtil(); |
26 | 27 |
static String filedir = ""; |
27 |
String user = null; |
|
28 |
String sess_id = null; |
|
29 |
int port; |
|
30 |
static String httpstring = "http://dev.nceas.ucsb.edu:8090/metacat/upload/"; |
|
28 |
|
|
31 | 29 |
protected Socket s; |
32 | 30 |
|
33 |
DataFileServer (int port, String user, String sess_id)
|
|
31 |
public DataFileServer(int port, String user, String sess_id)
|
|
34 | 32 |
{ |
35 |
this.sess_id = sess_id; |
|
36 |
this.user = user; |
|
37 |
this.port = port; |
|
38 |
filedir = util.getOption("datafilepath"); |
|
33 |
super(port, user, sess_id); |
|
34 |
System.out.println("port: " + port + " user: " + user + " sess: " + |
|
35 |
sess_id); |
|
39 | 36 |
} |
40 | 37 |
|
41 |
/** |
|
42 |
* attempts to connect a socket, returns null if it is not successful |
|
43 |
* returns the connected socket if it is successful. |
|
44 |
*/ |
|
45 |
public static Socket getSocket(String host, int port) |
|
38 |
public void getFile(int port, String user, String sess_id) |
|
46 | 39 |
{ |
47 |
Socket s = null; |
|
48 |
try |
|
49 |
{ |
|
50 |
s = new Socket(host, port); |
|
51 |
//we could create a socket on this port so the port is not available |
|
52 |
//System.out.println("socket connnected"); |
|
53 |
return s; |
|
54 |
} |
|
55 |
catch(UnknownHostException u) |
|
56 |
{ |
|
57 |
} |
|
58 |
catch(IOException i) |
|
59 |
{ |
|
60 |
//an ioexception is thrown if the port is not in use |
|
61 |
//System.out.println("socket not connected"); |
|
62 |
return s; |
|
63 |
} |
|
64 |
return s; |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* returns true if the port specified is not in use. false otherwise |
|
69 |
*/ |
|
70 |
public static boolean portIsAvailable(int port) |
|
71 |
{ |
|
72 |
Socket s; |
|
73 |
String host = "localhost"; |
|
74 |
try |
|
75 |
{ |
|
76 |
s = new Socket(host, port); |
|
77 |
//s.close(); |
|
78 |
//we could create a socket on this port so the port is not available |
|
79 |
//System.out.println("socket not available"); |
|
80 |
return false; |
|
81 |
} |
|
82 |
catch(UnknownHostException u) |
|
83 |
{ |
|
84 |
//it better know localhost! |
|
85 |
} |
|
86 |
catch(IOException i) |
|
87 |
{ |
|
88 |
//an ioexception is thrown if the port is not in use |
|
89 |
//System.out.println("socket available"); |
|
90 |
return true; |
|
91 |
} |
|
92 |
return true; |
|
93 |
} |
|
94 |
|
|
95 |
/** |
|
96 |
* Updates xml_documents with the new data document information |
|
97 |
* @param accnum the accession number of the new data file |
|
98 |
* @param filename the filename of the new data file |
|
99 |
* @param userOwner the document's owner's username |
|
100 |
*/ |
|
101 |
private void updateDB(String accnum, String filename, String userOwner) |
|
102 |
{ |
|
103 |
SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss"); |
|
104 |
java.util.Date localtime = new java.util.Date(); |
|
105 |
String dateString = formatter.format(localtime); |
|
106 |
|
|
107 |
String sqlDateString = "to_date('" + dateString + "', 'YY-MM-DD HH24:MI:SS')"; |
|
108 |
|
|
109 |
StringBuffer sql = new StringBuffer(); |
|
110 |
sql.append("insert into xml_documents (docid, docname, doctype, "); |
|
111 |
sql.append("user_owner, user_updated, server_location, rev, date_created"); |
|
112 |
sql.append(", date_updated, public_access) values ('"); |
|
113 |
sql.append(accnum).append("','").append(filename).append("','BIN','"); |
|
114 |
sql.append(userOwner).append("','").append(userOwner).append("','1','"); |
|
115 |
sql.append("1',").append(sqlDateString).append(","); |
|
116 |
sql.append(sqlDateString).append(",'0')"); |
|
117 |
//System.out.println("sql: " + sql.toString()); |
|
118 |
try |
|
119 |
{ |
|
120 |
Connection conn = util.openDBConnection(); |
|
121 |
PreparedStatement pstmt = conn.prepareStatement(sql.toString()); |
|
122 |
pstmt.execute(); |
|
123 |
conn.close(); |
|
124 |
} |
|
125 |
catch(Exception e) |
|
126 |
{ |
|
127 |
System.out.println("error with db connection in DataFileServer.updateDB" + |
|
128 |
": " + e.getMessage()); |
|
129 |
} |
|
130 |
|
|
131 |
} |
|
132 |
|
|
133 |
/** |
|
134 |
* This method is invoked when this class is broken off into a new thread |
|
135 |
*/ |
|
136 |
public void run () |
|
137 |
{ |
|
40 |
filedir = util.getOption("datafilepath"); |
|
138 | 41 |
ServerSocket server = null; |
139 | 42 |
Socket client = null; |
140 | 43 |
OutputStreamWriter osw = null; |
141 | 44 |
try |
142 | 45 |
{ |
143 |
//System.out.println("Starting on port " + port);
|
|
144 |
//System.out.println("Waiting for sess_id: " + sess_id);
|
|
46 |
System.out.println("Starting on port " + port); |
|
47 |
System.out.println("Waiting for sess_id: " + sess_id); |
|
145 | 48 |
server = new ServerSocket((new Integer(port)).intValue()); |
146 |
//System.out.println("Waiting"); |
|
147 | 49 |
server.setSoTimeout(30000); //set a 30 second timeout |
148 | 50 |
client = server.accept(); |
149 |
//System.out.println("Accepted from " + client.getInetAddress());
|
|
51 |
System.out.println("Accepted from " + client.getInetAddress()); |
|
150 | 52 |
|
151 | 53 |
InputStream in = client.getInputStream(); //in from the client |
152 | 54 |
OutputStream sout = client.getOutputStream(); //out to the client |
153 | 55 |
osw = new OutputStreamWriter(sout); |
154 |
//System.out.println("output stream received");
|
|
56 |
System.out.println("output stream received"); |
|
155 | 57 |
// first read to get the file name |
156 | 58 |
byte[] str = new byte[1024]; |
157 | 59 |
int val = -1; |
... | ... | |
165 | 67 |
i++; |
166 | 68 |
} |
167 | 69 |
String filename = (new String(str,0, i-1)).trim(); |
168 |
//System.out.println("filename: " + filename);
|
|
70 |
System.out.println("filename: " + filename); |
|
169 | 71 |
|
170 | 72 |
val = -1; |
171 | 73 |
i = 0; |
... | ... | |
197 | 99 |
if(!sess_id.equals(session_id)) |
198 | 100 |
{//this is an unauthorized connection to this port |
199 | 101 |
System.out.println("unauthorized request"); |
102 |
osw.write("<?xml version=\"1.0\"?><error>unauthorized request</error>"); |
|
103 |
osw.flush(); |
|
104 |
osw.close(); |
|
200 | 105 |
return; |
201 | 106 |
} |
202 | 107 |
else |
203 | 108 |
{ |
204 |
//System.out.println("User authenticated on port " + port);
|
|
109 |
System.out.println("User authenticated on port " + port); |
|
205 | 110 |
} |
206 | 111 |
|
207 | 112 |
String restext=null; |
208 | 113 |
File outfile = new File(filedir + filename); |
209 |
//System.out.println("outfile: " + filedir + filename);
|
|
114 |
System.out.println("outfile: " + filedir + filename); |
|
210 | 115 |
boolean nameInUse = outfile.exists(); |
211 | 116 |
int filenametemp = 1; |
212 | 117 |
String fn = filename; |
... | ... | |
222 | 127 |
{ |
223 | 128 |
AccessionNumber anum = new AccessionNumber(); |
224 | 129 |
String accnum = anum.generate(null, "INSERT"); |
225 |
//osw.write(restext,0,restext.length()); |
|
226 |
//osw.flush(); |
|
227 | 130 |
|
228 | 131 |
FileOutputStream out = new FileOutputStream(outfile); |
229 | 132 |
byte[] buf = new byte[1024]; |
... | ... | |
231 | 134 |
int j = 0; |
232 | 135 |
while (j < fsize) |
233 | 136 |
{ |
234 |
//System.out.println("1 j:" + j); |
|
235 | 137 |
cnt = in.read(buf, 0, 1024); |
236 | 138 |
if (cnt!=-1) |
237 | 139 |
{ |
... | ... | |
300 | 202 |
{ |
301 | 203 |
ex.printStackTrace(); |
302 | 204 |
} |
303 |
//System.out.println("done"); |
|
304 | 205 |
} |
305 | 206 |
} |
306 |
|
|
307 | 207 |
} |
Also available in: Unified diff
added support for the new abstract class) to handle data file uploading through metnewninterface (actuall