Project

General

Profile

1
package edu.ucsb.nceas.metacat.harvesterClient;
2

    
3
import java.io.*;
4
import java.util.*;
5
import javax.servlet.ServletConfig;
6
import javax.servlet.ServletContext;
7
import javax.servlet.ServletException;
8
import javax.servlet.ServletInputStream;
9
import javax.servlet.http.HttpServlet;
10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
12
import javax.servlet.http.HttpSession;
13
import javax.servlet.http.HttpUtils;
14
import javax.servlet.ServletOutputStream;
15
import com.oreilly.servlet.multipart.FilePart;
16
import com.oreilly.servlet.multipart.MultipartParser;
17
import com.oreilly.servlet.multipart.ParamPart;
18
import com.oreilly.servlet.multipart.Part;
19
import edu.ucsb.nceas.metacat.client.*;
20
import edu.ucsb.nceas.utilities.IOUtil;
21

    
22
/**
23
 *  MetUpload implements a Harvester servlet to upload a single file to Metacat
24
 */
25
public class MetUpload extends HttpServlet {
26
    private Metacat m;
27
    private String  insertXML = "";
28
    private String  metacatUrl;
29
    private String  userName = System.getProperty("user.name");
30
    private String  tmpDirPath = "/tmp/" + userName;
31
    private File    tmpDir = new File(tmpDirPath);
32

    
33
    /**
34
     *  Service requests made to the Harvester MetUpload servlet
35
     *
36
     *  @param  req   The request
37
     *  @param  res   The response
38
     *  @throws IOException
39
     */
40
    public void service(HttpServletRequest req,
41
                        HttpServletResponse res) throws IOException {
42
        int             strLen;
43
        File            tmpFile;
44
        FilePart        fPart = null;
45
        FileReader      fr;
46
        Hashtable       formElements = new Hashtable();
47
        HttpSession     sess;
48
        MultipartParser parser;
49
        ParamPart       pPart;
50
        Part            aPart;
51
        PrintWriter     out = null;
52
        String          docid = "";
53
        String          tmpFileName = "";
54
        String          tmpFilePath = "";
55
        String          fieldName;
56
        String          fieldValue;
57
        String          metacatResponse;
58
        String          password = null;
59
        String          revision;
60
        String          updateResult;
61
        String          username = null;
62
        StringReader    sr;
63

    
64
        if (userName.equals("harvest")) {
65
            metacatUrl = "http://knb.lternet.edu:8888/knb/servlet/metacat";
66
        }
67
        else {
68
            metacatUrl = "http://knb.lternet.edu:8088/knb/servlet/metacat";
69
        }
70

    
71
        try {
72
            out = res.getWriter();
73
            parser = new MultipartParser(req, 1024 * 1024);
74

    
75
            while ((aPart = parser.readNextPart()) != null) {
76
                if (aPart.isParam() == true) {
77
                    pPart = (ParamPart) aPart;
78
                    fieldName = pPart.getName();
79
                    fieldValue = pPart.getStringValue();
80

    
81
                    if (fieldName != null) {
82
                        if (fieldValue == null) {
83
                            fieldValue = "";
84
                        }
85

    
86
                        formElements.put(fieldName, fieldValue);
87

    
88
                        if (fieldName.equals("docid")) {
89
                            docid = fieldValue;
90
                        }
91
                    }
92
                }
93
                else if (aPart.isFile() == true) {
94
                    fPart = (FilePart)aPart;
95
                    fieldName = fPart.getName();
96
                    tmpFileName = fPart.getFileName();
97

    
98
                    if (tmpFileName != null) {
99
                        // Create the temporary directory if it doesn't exist
100
                        try {
101
                            if (!tmpDir.exists()) {
102
                                tmpDir.mkdirs();
103
                            }
104
                        } catch (SecurityException se) {
105
                            out.println("Can't create temporary directory: " +
106
                                        tmpDir.getPath());
107
                            out.println(se.getMessage());
108
                        }
109
                        // Write the image to a file
110
                        tmpFile = new File(tmpDirPath, tmpFileName);
111
                        fPart.writeTo(tmpFile);
112
                        tmpFilePath = tmpDirPath + "/" + tmpFileName;
113
                        formElements.put(fieldName, tmpFileName);
114
                        break;
115
                    }
116
                }
117
            }
118

    
119
            sess = req.getSession(true);
120
            username = (String) sess.getAttribute("Musername");
121
            password = (String) sess.getAttribute("Mpassword");
122

    
123
            try {
124
                fr = new FileReader(tmpFilePath);
125
                insertXML = IOUtil.getAsString(fr, true);
126
            } catch (IOException ioe) {
127
                out.println("Error reading file: " + tmpFilePath);
128
                out.println(ioe.getMessage());
129
            }
130

    
131
            try {
132
                m = MetacatFactory.createMetacatConnection(metacatUrl);
133
            } catch (MetacatInaccessibleException mie) {
134
                out.println("Metacat connection failed.");
135
                out.println(mie.getMessage());
136
            }
137

    
138
            try {
139
                m.login(username, password);
140
                strLen = docid.length();
141
                revision = docid.substring((strLen - 2), strLen);
142
                sr = new StringReader(insertXML);
143
                
144
                if (revision.equals(".1")) {
145
                    metacatResponse = m.insert(docid, sr, null);
146
                }
147
                else {
148
                    metacatResponse = m.update(docid, sr, null);
149
                }
150

    
151
                out.println(metacatResponse);
152
            } catch (Exception e) {
153
				out.println("General exception:\n" + e.getMessage());
154
            }
155
            
156
            // Clean up the temporary file
157
            if (!tmpFileName.equals("")) {
158
                tmpFile = new File(tmpDirPath, tmpFileName);
159
                tmpFile.delete();
160
            }
161

    
162
        } catch (Exception e) {
163
            out.println("An error occurred while attempting to upload.");
164
            e.printStackTrace(out);
165
        }
166

    
167
        out.flush();
168
    }
169
}
(7-7/7)