1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that implements a socket server for data files
|
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-09 10:57:58 -0800 (Tue, 09 Jan 2001) $'
|
11
|
* '$Revision: 647 $'
|
12
|
*/
|
13
|
|
14
|
package edu.ucsb.nceas.metacat;
|
15
|
|
16
|
import java.net.*;
|
17
|
import java.io.*;
|
18
|
import java.util.Properties;
|
19
|
import java.util.Date;
|
20
|
import java.text.SimpleDateFormat;
|
21
|
import java.sql.*;
|
22
|
|
23
|
public class DataFileServer extends Thread
|
24
|
{
|
25
|
MetaCatUtil util = new MetaCatUtil();
|
26
|
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/";
|
31
|
protected Socket s;
|
32
|
|
33
|
DataFileServer (int port, String user, String sess_id)
|
34
|
{
|
35
|
this.sess_id = sess_id;
|
36
|
this.user = user;
|
37
|
this.port = port;
|
38
|
filedir = util.getOption("datafilepath");
|
39
|
}
|
40
|
|
41
|
/**
|
42
|
* returns true if the port specified is not in use. false otherwise
|
43
|
*/
|
44
|
public static boolean portIsAvailable(int port)
|
45
|
{
|
46
|
Socket s;
|
47
|
String host = "localhost";
|
48
|
try
|
49
|
{
|
50
|
s = new Socket(host, port);
|
51
|
s.close();
|
52
|
//we could create a socket on this port so the port is not available
|
53
|
return false;
|
54
|
}
|
55
|
catch(UnknownHostException u)
|
56
|
{
|
57
|
//it better know localhost!
|
58
|
}
|
59
|
catch(IOException i)
|
60
|
{
|
61
|
//an ioexception is thrown if the port is not in use
|
62
|
//System.out.println("port not in use");
|
63
|
return true;
|
64
|
}
|
65
|
return true;
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Updates xml_documents with the new data document information
|
70
|
* @param accnum the accession number of the new data file
|
71
|
* @param filename the filename of the new data file
|
72
|
* @param userOwner the document's owner's username
|
73
|
*/
|
74
|
private void updateDB(String accnum, String filename, String userOwner)
|
75
|
{
|
76
|
SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss");
|
77
|
java.util.Date localtime = new java.util.Date();
|
78
|
String dateString = formatter.format(localtime);
|
79
|
|
80
|
String sqlDateString = "to_date('" + dateString + "', 'YY-MM-DD HH:MI:SS')";
|
81
|
|
82
|
StringBuffer sql = new StringBuffer();
|
83
|
sql.append("insert into xml_documents (docid, docname, doctype, ");
|
84
|
sql.append("user_owner, user_updated, server_location, rev, date_created");
|
85
|
sql.append(", date_updated, public_access) values ('");
|
86
|
sql.append(accnum).append("','").append(filename).append("','BIN','");
|
87
|
sql.append(userOwner).append("','").append(userOwner).append("','1','");
|
88
|
sql.append("1',").append(sqlDateString).append(",");
|
89
|
sql.append(sqlDateString).append(",'0')");
|
90
|
//System.out.println("sql: " + sql.toString());
|
91
|
try
|
92
|
{
|
93
|
Connection conn = util.openDBConnection();
|
94
|
PreparedStatement pstmt = conn.prepareStatement(sql.toString());
|
95
|
pstmt.execute();
|
96
|
conn.close();
|
97
|
}
|
98
|
catch(Exception e)
|
99
|
{
|
100
|
System.out.println("error with db connection in DataFileServer.updateDB" +
|
101
|
": " + e.getMessage());
|
102
|
}
|
103
|
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* This method is invoked when this class is broken off into a new thread
|
108
|
*/
|
109
|
public void run ()
|
110
|
{
|
111
|
ServerSocket server = null;
|
112
|
Socket client = null;
|
113
|
try
|
114
|
{
|
115
|
//System.out.println("Starting on port " + port);
|
116
|
//System.out.println("Waiting for sess_id: " + sess_id);
|
117
|
server = new ServerSocket((new Integer(port)).intValue());
|
118
|
//System.out.println("Waiting");
|
119
|
client = server.accept();
|
120
|
//System.out.println("Accepted from " + client.getInetAddress());
|
121
|
|
122
|
InputStream in = client.getInputStream();
|
123
|
OutputStreamWriter osw = new OutputStreamWriter(client.getOutputStream());
|
124
|
//System.out.println("output stream received");
|
125
|
// first read to get the file name
|
126
|
byte[] str = new byte[1024];
|
127
|
int val = -1;
|
128
|
int i = 0;
|
129
|
//get the filename
|
130
|
while (val!=0)
|
131
|
{
|
132
|
//System.out.println("i: " + i);
|
133
|
val = in.read();
|
134
|
str[i] = (byte)val;
|
135
|
i++;
|
136
|
}
|
137
|
String filename = (new String(str,0, i-1)).trim();
|
138
|
//System.out.println("filename: " + filename);
|
139
|
|
140
|
val = -1;
|
141
|
i = 0;
|
142
|
str = new byte[1024];
|
143
|
//get the session id
|
144
|
while (val!=0)
|
145
|
{
|
146
|
val = in.read();
|
147
|
str[i] = (byte)val;
|
148
|
i++;
|
149
|
}
|
150
|
String session_id = (new String(str,0, i-1)).trim();
|
151
|
//System.out.println("session_id: " + session_id);
|
152
|
|
153
|
if(!sess_id.equals(session_id))
|
154
|
{//this is an unauthorized connection to this port
|
155
|
System.out.println("unauthorized request");
|
156
|
return;
|
157
|
}
|
158
|
else
|
159
|
{
|
160
|
System.out.println("User authenticated on port " + port);
|
161
|
}
|
162
|
|
163
|
String restext=null;
|
164
|
File outfile = new File(filedir + filename);
|
165
|
//System.out.println("outfile: " + filedir + filename);
|
166
|
boolean nameInUse = outfile.exists();
|
167
|
if(!nameInUse)
|
168
|
{
|
169
|
try
|
170
|
{
|
171
|
AccessionNumber anum = new AccessionNumber();
|
172
|
String accnum = anum.generate(null, "INSERT");
|
173
|
//osw.write(restext,0,restext.length());
|
174
|
//osw.flush();
|
175
|
FileOutputStream out = new FileOutputStream(outfile);
|
176
|
byte[] buf = new byte[1024];
|
177
|
int cnt = 0;
|
178
|
while (cnt!=-1)
|
179
|
{
|
180
|
cnt = in.read(buf, 0, 1024);
|
181
|
if (cnt!=-1)
|
182
|
{
|
183
|
out.write(buf, 0, cnt);
|
184
|
}
|
185
|
}
|
186
|
out.flush();
|
187
|
out.close();
|
188
|
//put the new document info into the DB
|
189
|
updateDB(accnum, filename, user);
|
190
|
}
|
191
|
catch(Exception e)
|
192
|
{
|
193
|
System.out.println("error in DataFileServer.run(): " + e.getMessage());
|
194
|
e.printStackTrace(System.out);
|
195
|
}
|
196
|
}
|
197
|
else
|
198
|
{//error the file name is already in use on the server
|
199
|
//send an error message here!
|
200
|
System.out.println("error: name in use.");
|
201
|
}
|
202
|
}
|
203
|
catch (IOException ex)
|
204
|
{
|
205
|
ex.printStackTrace();
|
206
|
}
|
207
|
finally
|
208
|
{
|
209
|
try
|
210
|
{
|
211
|
client.close();
|
212
|
System.out.println("client socket closed");
|
213
|
server.close();
|
214
|
System.out.println("server socket closed");
|
215
|
}
|
216
|
catch (IOException ex)
|
217
|
{
|
218
|
ex.printStackTrace();
|
219
|
}
|
220
|
System.out.println("done");
|
221
|
}
|
222
|
}
|
223
|
|
224
|
}
|