9 |
9 |
import javax.servlet.http.HttpUtils;
|
10 |
10 |
import javax.servlet.ServletOutputStream;
|
11 |
11 |
|
12 |
|
import org.ecoinformatics.eml.EMLParser;
|
|
12 |
//import org.ecoinformatics.eml.EMLParser;
|
13 |
13 |
|
14 |
14 |
import java.io.*;
|
15 |
15 |
import java.util.*;
|
... | ... | |
19 |
19 |
import com.oreilly.servlet.multipart.ParamPart;
|
20 |
20 |
import com.oreilly.servlet.multipart.Part;
|
21 |
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;
|
22 |
26 |
|
23 |
|
import edu.ucsb.nceas.metacat.*;
|
24 |
|
import edu.ucsb.nceas.metacat.MetaCatUtil;
|
25 |
27 |
|
26 |
|
|
27 |
28 |
public class MetUpload extends HttpServlet
|
28 |
29 |
{
|
|
30 |
private String insertXML = "";
|
|
31 |
private String metacatUrl =
|
|
32 |
"http://knb.lternet.edu:8098/knb/servlet/metacat";
|
|
33 |
private Metacat m;
|
29 |
34 |
public void service(HttpServletRequest request, HttpServletResponse response)
|
30 |
35 |
throws IOException
|
31 |
36 |
{
|
... | ... | |
36 |
41 |
out = response.getWriter();
|
37 |
42 |
|
38 |
43 |
String file = new String("");
|
39 |
|
DBConnection dbconn = null;
|
40 |
|
DBConnectionPool connPool = null;
|
41 |
|
File dataDirectory = null;
|
42 |
|
MetaCatUtil util = null;
|
43 |
|
String datafilepath = null;
|
44 |
44 |
String fileName = null;
|
45 |
45 |
FilePart fPart = null;
|
46 |
46 |
MultipartParser parser = new MultipartParser((HttpServletRequest)request, 1024 * 1024);
|
47 |
47 |
//MultipartParser parser = new MultipartParser(request, 1024 * 1024);
|
48 |
48 |
Part aPart;
|
49 |
|
DocumentImplWrapper documentWrapper = null;
|
50 |
49 |
String eml_filename=" ",docid=" ";
|
51 |
|
StringReader xml = null;
|
52 |
|
/*String eml_filename=" ",docid=" ";*/
|
53 |
50 |
Hashtable formElements = new Hashtable();
|
54 |
51 |
while( (aPart = parser.readNextPart()) != null ) {
|
55 |
52 |
if( aPart.isParam() == true ) {
|
... | ... | |
72 |
69 |
//write the image to a file
|
73 |
70 |
String dir = "/tmp/Upload_data";
|
74 |
71 |
File theFile = new File(dir, fileName);
|
75 |
|
/*fPart.writeTo(theFile);
|
|
72 |
fPart.writeTo(theFile);
|
76 |
73 |
eml_filename = dir+"/"+fileName;
|
77 |
|
formElements.put(field, fileName);*/
|
|
74 |
formElements.put(field, fileName);
|
78 |
75 |
break;
|
79 |
76 |
}
|
80 |
77 |
}
|
81 |
78 |
}
|
82 |
|
// out.println( formElements );
|
83 |
|
//out.println( docid + " " + eml_filename );
|
84 |
|
// out.println("EML File uploaded");
|
85 |
79 |
/*****************************************************/
|
86 |
80 |
|
87 |
81 |
|
88 |
82 |
/* Change for input to metacat */
|
89 |
83 |
|
90 |
|
util = new MetaCatUtil();
|
91 |
|
datafilepath = util.getOption("datafilepath");
|
92 |
|
dataDirectory = new File(datafilepath);
|
93 |
84 |
HttpSession sess = request.getSession(true);
|
94 |
|
String username = null;
|
95 |
|
username = (String)sess.getAttribute("username");
|
|
85 |
String username = null,password=null;
|
|
86 |
username = (String)sess.getAttribute("Musername");
|
|
87 |
password = (String)sess.getAttribute("Mpassword");
|
96 |
88 |
|
97 |
89 |
//out.println(username);
|
98 |
90 |
out.println("Username is "+username+" <BR>");
|
99 |
|
|
|
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 |
}
|
100 |
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);
|
101 |
109 |
|
102 |
|
//initial DBConnection pool
|
103 |
|
connPool = DBConnectionPool.getInstance();
|
104 |
|
|
105 |
|
/*
|
106 |
|
dbconn=DBConnectionPool.getDBConnection("DocumentImpl.main");
|
|
110 |
} catch (Exception e) {
|
|
111 |
out.println("General exception:\n" + e.getMessage());
|
|
112 |
}
|
107 |
113 |
|
108 |
|
documentWrapper = new DocumentImplWrapper("", false);
|
109 |
|
/*String newdocid = DocumentImpl.write(dbconn, eml_filename, null,
|
110 |
|
"null", "INSERT", docid,
|
111 |
|
null, null);*/
|
112 |
|
/*xml = new StringReader(eml_filename);
|
113 |
|
String newdocid = documentWrapper.write(dbconn, xml, null,
|
114 |
|
null, "INSERT", docid,
|
115 |
|
null, null);
|
116 |
|
if ((docid != null) && (!docid.equals(newdocid))) {
|
117 |
|
|
118 |
|
System.out.println("New document ID generated!!! ");
|
119 |
|
}*/
|
120 |
114 |
|
121 |
|
if (DocumentImpl.getDataFileLockGrant(docid))
|
122 |
|
{
|
123 |
|
//out.println(docid);
|
124 |
|
DocumentImpl.registerDocument(fileName, "BIN", docid, username);
|
125 |
115 |
|
126 |
|
// Save the data file to disk using "docid" as the name
|
127 |
|
dataDirectory.mkdirs();
|
128 |
|
File newFile = new File(dataDirectory, docid);
|
129 |
|
|
130 |
|
long size = fPart.writeTo(newFile);
|
131 |
|
|
132 |
|
//out.println(docid);
|
133 |
|
ForceReplicationHandler frh = new ForceReplicationHandler
|
134 |
|
(docid, "insert", false, null);
|
135 |
|
|
136 |
|
|
137 |
|
// set content type and other response header fields first
|
138 |
|
out.println(" EML file uploaded successfully with docid as "+docid+"<BR>" );
|
139 |
|
}
|
140 |
|
else
|
141 |
|
|
142 |
|
out.println(" EML file upload with docid as "+docid+" failed<br>");
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
116 |
/****************************************************/
|
147 |
117 |
|
148 |
118 |
|