Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements a socket server for data files that 
4
 *             writes the file to the servlet's local file system
5
 *  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: berkley $'
11
 *     '$Date: 2001-01-18 15:15:21 -0800 (Thu, 18 Jan 2001) $'
12
 * '$Revision: 675 $'
13
 *
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
 */
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
public class DataFileServer extends DataFileUploadInterface
39
{
40
  MetaCatUtil util = new MetaCatUtil();
41
  static String filedir = "";
42

    
43
  protected Socket s;
44
  
45
  public DataFileServer(int port, String user, String sess_id)
46
  {
47
    super(port, user, sess_id);
48
    System.out.println("port: " + port + " user: " + user + " sess: " +
49
                       sess_id);
50
  }
51
  
52
  public void getFile(int port, String user, String sess_id)
53
  {
54
    filedir = util.getOption("datafilepath"); 
55
    ServerSocket server = null;
56
    Socket client = null;
57
    OutputStreamWriter osw = null;
58
    try
59
    {
60
      System.out.println("Starting on port " + port);
61
      System.out.println("Waiting for sess_id: " + sess_id);
62
      server = new ServerSocket((new Integer(port)).intValue());
63
      server.setSoTimeout(30000); //set a 30 second timeout
64
      client = server.accept();
65
      System.out.println("Accepted from " + client.getInetAddress());
66
    
67
      InputStream in = client.getInputStream();      //in from the client
68
      OutputStream sout = client.getOutputStream();  //out to the client
69
      osw = new OutputStreamWriter(sout);
70
      System.out.println("output stream received");
71
      // 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
      while (val != 0) 
77
      {
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
      System.out.println("filename: " + filename);
85
      
86
      val = -1;
87
      i = 0;
88
      str = new byte[1024];
89
      //get the session id
90
      while (val != 0) 
91
      {
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
      //get the length of the file in bytes
99
      val = -1;
100
      i = 0;
101
      str = new byte[1024];
102
      while(val != 0)
103
      {
104
        val = in.read();
105
        str[i] = (byte)val;
106
        i++;
107
      }
108
      String stemp = (new String(str, 0, i-1)).trim();
109
      Long fsizetemp = new Long(stemp);
110
      long fsize = fsizetemp.longValue();
111
      //System.out.println("file size: " + fsize);
112
      
113
      if(!sess_id.equals(session_id))
114
      {//this is an unauthorized connection to this port
115
        System.out.println("unauthorized request");
116
        osw.write("<?xml version=\"1.0\"?><error>unauthorized request</error>");
117
        osw.flush();
118
        osw.close();
119
        return;
120
      }
121
      else
122
      {
123
        System.out.println("User authenticated on port " + port);
124
      }
125
      
126
      String restext=null;
127
      File outfile = new File(filedir + filename);
128
      System.out.println("outfile: " + filedir + filename);
129
      boolean nameInUse = outfile.exists();
130
      int filenametemp = 1;
131
      String fn = filename;
132
      while(nameInUse)
133
      {
134
        filename = fn + filenametemp;
135
        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
        
145
        FileOutputStream out = new FileOutputStream(outfile);
146
        byte[] buf = new byte[1024];
147
        int cnt = 0;
148
        int j = 0;
149
        while (j < fsize) 
150
        {
151
          cnt = in.read(buf, 0, 1024);
152
          if (cnt!=-1) 
153
          {
154
            out.write(buf, 0, cnt);
155
          }
156
          j += cnt;
157
        }
158

    
159
        out.flush();
160
        out.close();
161
        //put the new document info into the DB
162
        updateDB(accnum, filename, user);
163
        //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
      }
169
      catch(Exception e)
170
      {
171
        System.out.println("error in DataFileServer.run(): " + e.getMessage());
172
        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
          System.out.println("error DataFileServer.run(): " + ee.getMessage());
182
        }
183
        e.printStackTrace(System.out);
184
      }
185
    } 
186
    catch(InterruptedIOException iioe)
187
    {
188
      //the accept timeout passed
189
      System.out.println("socket on port " + port + " timed out. " +
190
                         " (DataFileServer.run)");
191
    }
192
    catch (IOException ex) 
193
    {
194
      ex.printStackTrace();
195
      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
        System.out.println("error in DataFileServer.run(): " + ee.getMessage());
205
      }
206
    } 
207
    finally 
208
    {
209
      try
210
      {
211
        server.close();
212
        //System.out.println("server socket closed");
213
        client.close();
214
        //System.out.println("client socket closed");
215
      } 
216
      catch (IOException ex) 
217
      {
218
        ex.printStackTrace();
219
      }
220
    }
221
  }
222
}
(21-21/43)