Revision 1949
Added by Duane Costa almost 21 years ago
src/edu/ucsb/nceas/metacat/harvesterClient/MetUpload.java | ||
---|---|---|
1 |
package edu.ucsb.nceas.metacat.harvesterClient; |
|
2 |
|
|
3 |
import java.io.*; |
|
4 |
import java.util.*; |
|
1 | 5 |
import javax.servlet.ServletConfig; |
2 | 6 |
import javax.servlet.ServletContext; |
3 | 7 |
import javax.servlet.ServletException; |
... | ... | |
8 | 12 |
import javax.servlet.http.HttpSession; |
9 | 13 |
import javax.servlet.http.HttpUtils; |
10 | 14 |
import javax.servlet.ServletOutputStream; |
11 |
|
|
12 |
//import org.ecoinformatics.eml.EMLParser; |
|
13 |
|
|
14 |
import java.io.*; |
|
15 |
import java.util.*; |
|
16 |
|
|
17 | 15 |
import com.oreilly.servlet.multipart.FilePart; |
18 | 16 |
import com.oreilly.servlet.multipart.MultipartParser; |
19 | 17 |
import com.oreilly.servlet.multipart.ParamPart; |
20 | 18 |
import com.oreilly.servlet.multipart.Part; |
21 |
|
|
22 | 19 |
import edu.ucsb.nceas.metacat.client.*; |
23 | 20 |
import edu.ucsb.nceas.utilities.IOUtil; |
24 |
//import edu.ucsb.nceas.metacat.*; |
|
25 |
//import edu.ucsb.nceas.metacat.MetaCatUtil; |
|
26 | 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); |
|
27 | 32 |
|
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; |
|
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; |
|
40 | 63 |
|
41 |
out = response.getWriter(); |
|
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 |
} |
|
42 | 70 |
|
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 |
/*****************************************************/ |
|
71 |
try { |
|
72 |
out = res.getWriter(); |
|
73 |
parser = new MultipartParser(req, 1024 * 1024); |
|
80 | 74 |
|
75 |
while ((aPart = parser.readNextPart()) != null) { |
|
76 |
if (aPart.isParam() == true) { |
|
77 |
pPart = (ParamPart) aPart; |
|
78 |
fieldName = pPart.getName(); |
|
79 |
fieldValue = pPart.getStringValue(); |
|
81 | 80 |
|
82 |
/* Change for input to metacat */ |
|
81 |
if (fieldName != null) { |
|
82 |
if (fieldValue == null) { |
|
83 |
fieldValue = ""; |
|
84 |
} |
|
83 | 85 |
|
84 |
HttpSession sess = request.getSession(true); |
|
85 |
String username = null,password=null; |
|
86 |
username = (String)sess.getAttribute("Musername"); |
|
87 |
password = (String)sess.getAttribute("Mpassword"); |
|
86 |
formElements.put(fieldName, fieldValue); |
|
88 | 87 |
|
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 |
} |
|
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 | 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); |
|
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 |
} |
|
109 | 118 |
|
110 |
} catch (Exception e) {
|
|
111 |
out.println("General exception:\n" + e.getMessage());
|
|
112 |
}
|
|
119 |
sess = req.getSession(true);
|
|
120 |
username = (String) sess.getAttribute("Musername");
|
|
121 |
password = (String) sess.getAttribute("Mpassword");
|
|
113 | 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 |
} |
|
114 | 130 |
|
131 |
try { |
|
132 |
m = MetacatFactory.createMetacatConnection(metacatUrl); |
|
133 |
} catch (MetacatInaccessibleException mie) { |
|
134 |
out.println("Metacat connection failed."); |
|
135 |
out.println(mie.getMessage()); |
|
136 |
} |
|
115 | 137 |
|
116 |
/****************************************************/ |
|
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 |
} |
|
117 | 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 |
} |
|
118 | 161 |
|
119 |
} catch (Exception e) { |
|
120 |
out.println("An error occurred."); |
|
121 |
e.printStackTrace(out); |
|
122 |
} |
|
123 |
|
|
124 |
out.flush(); |
|
162 |
} catch (Exception e) { |
|
163 |
out.println("An error occurred while attempting to upload."); |
|
164 |
e.printStackTrace(out); |
|
165 |
} |
|
166 |
|
|
167 |
out.flush(); |
|
168 |
} |
|
125 | 169 |
} |
126 |
} |
src/edu/ucsb/nceas/metacat/harvesterClient/LoginServlet.java | ||
---|---|---|
1 |
package edu.ucsb.nceas.metacat.harvesterClient; |
|
2 |
|
|
1 | 3 |
import java.io.PrintWriter; |
2 |
|
|
3 |
|
|
4 | 4 |
import javax.servlet.ServletConfig; |
5 | 5 |
import javax.servlet.ServletContext; |
6 | 6 |
import javax.servlet.ServletException; |
... | ... | |
11 | 11 |
import javax.servlet.http.HttpSession; |
12 | 12 |
import javax.servlet.http.HttpUtils; |
13 | 13 |
import javax.servlet.ServletOutputStream; |
14 |
|
|
15 | 14 |
import edu.ucsb.nceas.metacat.AuthSession; |
16 | 15 |
|
16 |
/** |
|
17 |
* LoginServlet implements a Harvester servlet to login to Metacat |
|
18 |
*/ |
|
17 | 19 |
public class LoginServlet extends HttpServlet { |
18 | 20 |
|
21 |
public void destroy() { |
|
22 |
// Close all connections |
|
23 |
System.out.println("Destroying LoginServlet"); |
|
24 |
} |
|
19 | 25 |
|
20 |
public void destroy() { |
|
21 |
// Close all connections |
|
22 |
System.out.println("Destroying LoginServlet"); |
|
23 |
} |
|
26 |
/** |
|
27 |
* Handle "GET" method requests from HTTP clients |
|
28 |
* |
|
29 |
* @param request The request |
|
30 |
* @param response The response |
|
31 |
* @throws ServletException, java.io.IOException |
|
32 |
*/ |
|
33 |
public void doGet(HttpServletRequest request, HttpServletResponse response) |
|
34 |
throws ServletException, java.io.IOException { |
|
35 |
// Process the data and send back the response |
|
36 |
handleGetOrPost(request, response); |
|
37 |
} |
|
24 | 38 |
|
25 |
/** Handle "GET" method requests from HTTP clients */ |
|
26 |
public void doGet (HttpServletRequest request, HttpServletResponse response) |
|
27 |
throws ServletException,java.io.IOException{ |
|
39 |
/** |
|
40 |
* Handle "POST" method requests from HTTP clients |
|
41 |
* |
|
42 |
* @param request The request |
|
43 |
* @param response The response |
|
44 |
* @throws ServletException, java.io.IOException |
|
45 |
*/ |
|
46 |
public void doPost(HttpServletRequest request, HttpServletResponse response) |
|
47 |
throws ServletException, java.io.IOException { |
|
48 |
// Process the data and send back the response |
|
49 |
handleGetOrPost(request, response); |
|
50 |
} |
|
28 | 51 |
|
29 |
// Process the data and send back the response |
|
30 |
handleGetOrPost(request, response); |
|
31 |
} |
|
32 |
|
|
52 |
/** |
|
53 |
* Handle "GET" or "POST" method requests from HTTP clients |
|
54 |
* |
|
55 |
* @param request The request |
|
56 |
* @param response The response |
|
57 |
* @throws ServletException, java.io.IOException |
|
58 |
*/ |
|
59 |
private void handleGetOrPost(HttpServletRequest request, |
|
60 |
HttpServletResponse response) |
|
61 |
throws ServletException, java.io.IOException { |
|
62 |
String user = "skr"; |
|
63 |
// String group = "skr"; |
|
64 |
String passwd = "skr"; |
|
65 |
AuthSession sess = null; |
|
66 |
boolean isValid; |
|
67 |
PrintWriter out1; |
|
33 | 68 |
|
34 |
/** Handle "POST" method requests from HTTP clients */
|
|
35 |
public void doPost( HttpServletRequest request, HttpServletResponse response)
|
|
36 |
throws ServletException,java.io.IOException{
|
|
69 |
user = request.getParameter("user");
|
|
70 |
// group = request.getParameter("group");
|
|
71 |
passwd = request.getParameter("passwd");
|
|
37 | 72 |
|
38 |
// Process the data and send back the response |
|
39 |
handleGetOrPost(request, response); |
|
40 |
} |
|
73 |
try { |
|
74 |
sess = new AuthSession(); |
|
75 |
} catch (Exception e) { |
|
76 |
System.out.println( |
|
77 |
"Error in LoginServlet.handleGetOrPost AuthSession" + |
|
78 |
e.getMessage()); |
|
79 |
return; |
|
80 |
} |
|
41 | 81 |
|
42 |
private void handleGetOrPost(HttpServletRequest request,
|
|
43 |
HttpServletResponse response)
|
|
44 |
throws ServletException,java.io.IOException
|
|
45 |
{
|
|
82 |
isValid = sess.authenticate(request, user, passwd);
|
|
83 |
out1 = response.getWriter();
|
|
84 |
System.out.println("Sess.authenticate " + sess.getMessage());
|
|
85 |
out1.println("Sess.authenticate " + sess.getMessage());
|
|
46 | 86 |
|
47 |
String user="skr"; |
|
48 |
// String group="skr"; |
|
49 |
String passwd="skr"; |
|
50 |
AuthSession sess =null; |
|
87 |
/*****************************************************/ |
|
88 |
HttpSession sess1 = request.getSession(true); |
|
51 | 89 |
|
52 |
user = request.getParameter("user"); |
|
53 |
// group = request.getParameter("group"); |
|
54 |
passwd = request.getParameter("passwd"); |
|
55 |
|
|
56 |
try{ |
|
57 |
sess = new AuthSession(); |
|
58 |
} catch (Exception e) { |
|
59 |
System.out.println("Error in LoginServlet.handleGetorPost Authsession" + |
|
60 |
e.getMessage()); |
|
61 |
return; |
|
62 |
} |
|
90 |
// if (sess.isNew()) { |
|
91 |
sess1.putValue("Musername", user); |
|
92 |
sess1.putValue("Mpassword", passwd); |
|
93 |
// sess.putValue("groupnames", group); |
|
94 |
// } |
|
63 | 95 |
|
96 |
// PrintWriter out = response.getWriter(); |
|
97 |
// out.println(sess.getValue("username")); |
|
98 |
/*****************************************************/ |
|
64 | 99 |
|
65 |
boolean isValid = sess.authenticate(request, user,passwd); |
|
66 |
PrintWriter out1 = response.getWriter(); |
|
67 |
System.out.println("Sess.authenticate "+sess.getMessage()); |
|
68 |
out1.println("Sess.authenticate "+sess.getMessage()); |
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
/*****************************************************/ |
|
73 |
HttpSession sess1 = request.getSession(true); |
|
74 |
|
|
75 |
/*if (sess.isNew()) {*/ |
|
76 |
sess1.putValue("Musername",user); |
|
77 |
sess1.putValue("Mpassword",passwd); |
|
78 |
// sess.putValue("groupnames", group); |
|
79 |
|
|
80 |
//} |
|
81 |
|
|
82 |
|
|
83 |
// PrintWriter out = response.getWriter(); |
|
84 |
// out.println(sess.getValue("username")); |
|
85 |
/*****************************************************/ |
|
86 |
response.sendRedirect("../uploademl.html"); |
|
87 |
|
|
88 |
|
|
89 |
|
|
100 |
response.sendRedirect("../harvesterUpload.html"); |
|
101 |
} |
|
90 | 102 |
} |
91 |
} |
Also available in: Unified diff
Improvements to Harvester single file upload servlets