1
|
import javax.servlet.ServletConfig;
|
2
|
import javax.servlet.ServletContext;
|
3
|
import javax.servlet.ServletException;
|
4
|
import javax.servlet.ServletInputStream;
|
5
|
import javax.servlet.http.HttpServlet;
|
6
|
import javax.servlet.http.HttpServletRequest;
|
7
|
import javax.servlet.http.HttpServletResponse;
|
8
|
import javax.servlet.http.HttpSession;
|
9
|
import javax.servlet.http.HttpUtils;
|
10
|
import javax.servlet.ServletOutputStream;
|
11
|
|
12
|
//import org.ecoinformatics.eml.EMLParser;
|
13
|
|
14
|
import java.io.*;
|
15
|
import java.util.*;
|
16
|
|
17
|
import com.oreilly.servlet.multipart.FilePart;
|
18
|
import com.oreilly.servlet.multipart.MultipartParser;
|
19
|
import com.oreilly.servlet.multipart.ParamPart;
|
20
|
import com.oreilly.servlet.multipart.Part;
|
21
|
|
22
|
import edu.ucsb.nceas.metacat.client.*;
|
23
|
import edu.ucsb.nceas.utilities.IOUtil;
|
24
|
//import edu.ucsb.nceas.metacat.*;
|
25
|
//import edu.ucsb.nceas.metacat.MetaCatUtil;
|
26
|
|
27
|
|
28
|
public class MetUpload extends HttpServlet
|
29
|
{
|
30
|
private String insertXML = "";
|
31
|
private String metacatUrl =
|
32
|
"http://knb.lternet.edu:8098/knb/servlet/metacat";
|
33
|
private Metacat m;
|
34
|
public void service(HttpServletRequest request, HttpServletResponse response)
|
35
|
throws IOException
|
36
|
{
|
37
|
PrintWriter out = null;
|
38
|
try {
|
39
|
int id=0;
|
40
|
|
41
|
out = response.getWriter();
|
42
|
|
43
|
String file = new String("");
|
44
|
String fileName = null;
|
45
|
FilePart fPart = null;
|
46
|
MultipartParser parser = new MultipartParser((HttpServletRequest)request, 1024 * 1024);
|
47
|
//MultipartParser parser = new MultipartParser(request, 1024 * 1024);
|
48
|
Part aPart;
|
49
|
String eml_filename=" ",docid=" ";
|
50
|
Hashtable formElements = new Hashtable();
|
51
|
while( (aPart = parser.readNextPart()) != null ) {
|
52
|
if( aPart.isParam() == true ) {
|
53
|
ParamPart pPart = (ParamPart)aPart;
|
54
|
String field = pPart.getName();
|
55
|
String value = pPart.getStringValue();
|
56
|
if(field != null) {
|
57
|
if(value == null)
|
58
|
value = "";
|
59
|
formElements.put(field, value);
|
60
|
if (field.equals("docid"))
|
61
|
docid=value;
|
62
|
}
|
63
|
}
|
64
|
else if( aPart.isFile() == true ) {
|
65
|
fPart = (FilePart)aPart;
|
66
|
String field = fPart.getName();
|
67
|
fileName = fPart.getFileName();
|
68
|
if(fileName != null) {
|
69
|
//write the image to a file
|
70
|
String dir = "/tmp/Upload_data";
|
71
|
File theFile = new File(dir, fileName);
|
72
|
fPart.writeTo(theFile);
|
73
|
eml_filename = dir+"/"+fileName;
|
74
|
formElements.put(field, fileName);
|
75
|
break;
|
76
|
}
|
77
|
}
|
78
|
}
|
79
|
/*****************************************************/
|
80
|
|
81
|
|
82
|
/* Change for input to metacat */
|
83
|
|
84
|
HttpSession sess = request.getSession(true);
|
85
|
String username = null,password=null;
|
86
|
username = (String)sess.getAttribute("Musername");
|
87
|
password = (String)sess.getAttribute("Mpassword");
|
88
|
|
89
|
//out.println(username);
|
90
|
out.println("Username is "+username+" <BR>");
|
91
|
try {
|
92
|
FileReader fr = new FileReader(eml_filename);
|
93
|
insertXML = IOUtil.getAsString(fr, true);
|
94
|
} catch (IOException ioe) {
|
95
|
out.println("Can't read test data to run the test: " + eml_filename);
|
96
|
}
|
97
|
|
98
|
try {
|
99
|
m = MetacatFactory.createMetacatConnection(metacatUrl);
|
100
|
} catch (MetacatInaccessibleException mie) {
|
101
|
out.println("Metacat connection failed." + mie.getMessage());
|
102
|
}
|
103
|
try {
|
104
|
String identifier = docid + ".1";
|
105
|
m.login(username, password);
|
106
|
String response1 = m.insert(identifier,
|
107
|
new StringReader(insertXML), null);
|
108
|
out.println(response1);
|
109
|
|
110
|
} catch (Exception e) {
|
111
|
out.println("General exception:\n" + e.getMessage());
|
112
|
}
|
113
|
|
114
|
|
115
|
|
116
|
/****************************************************/
|
117
|
|
118
|
|
119
|
} catch (Exception e) {
|
120
|
out.println("An error occurred.");
|
121
|
e.printStackTrace(out);
|
122
|
}
|
123
|
|
124
|
out.flush();
|
125
|
}
|
126
|
}
|