Revision 3995
Added by berkley about 16 years ago
src/edu/ucsb/nceas/metacat/MetaCatServlet.java | ||
---|---|---|
86 | 86 |
* action=read -- read any metadata/data file from Metacat and from Internet |
87 | 87 |
* <br> |
88 | 88 |
* action=insert -- insert an XML document into the database store <br> |
89 |
* action=insertmultipart -- insert an xml document into the database using multipart encoding<br> |
|
89 | 90 |
* action=update -- update an XML document that is in the database store <br> |
90 | 91 |
* action=delete -- delete an XML document from the database store <br> |
91 | 92 |
* action=validate -- vallidate the xml contained in valtext <br> |
... | ... | |
1901 | 1902 |
Logger logMetacat = Logger.getLogger(MetaCatServlet.class); |
1902 | 1903 |
DBConnection dbConn = null; |
1903 | 1904 |
int serialNumber = -1; |
1905 |
String output = ""; |
|
1906 |
String qformat = null; |
|
1907 |
if(params.containsKey("qformat")) |
|
1908 |
{ |
|
1909 |
qformat = ((String[])params.get("qformat"))[0]; |
|
1910 |
} |
|
1904 | 1911 |
|
1905 | 1912 |
if(params.get("docid") == null){ |
1906 | 1913 |
out.println("<?xml version=\"1.0\"?>"); |
... | ... | |
1922 | 1929 |
|
1923 | 1930 |
try { |
1924 | 1931 |
// Get the document indicated |
1932 |
System.out.println("params: " + params.toString()); |
|
1933 |
|
|
1925 | 1934 |
String[] doctext = (String[]) params.get("doctext"); |
1926 | 1935 |
String pub = null; |
1927 | 1936 |
if (params.containsKey("public")) { |
... | ... | |
1995 | 2004 |
String newdocid = null; |
1996 | 2005 |
|
1997 | 2006 |
String doAction = null; |
1998 |
if (action[0].equals("insert")) { |
|
2007 |
if (action[0].equals("insert") || action[0].equals("insertmultipart")) {
|
|
1999 | 2008 |
doAction = "INSERT"; |
2000 | 2009 |
} else if (action[0].equals("update")) { |
2001 | 2010 |
doAction = "UPDATE"; |
... | ... | |
2015 | 2024 |
if (accNumber.equals("")) { |
2016 | 2025 |
accNumber = null; |
2017 | 2026 |
} |
2027 |
|
|
2018 | 2028 |
newdocid = documentWrapper.write(dbConn, xml, pub, dtd, |
2019 | 2029 |
doAction, accNumber, user, groups); |
2020 | 2030 |
EventLog.getInstance().log(request.getRemoteAddr(), |
2021 | 2031 |
user, accNumber, action[0]); |
2022 | 2032 |
} catch (NullPointerException npe) { |
2033 |
System.out.println("writing with null acnumber"); |
|
2023 | 2034 |
newdocid = documentWrapper.write(dbConn, xml, pub, dtd, |
2024 | 2035 |
doAction, null, user, groups); |
2025 | 2036 |
EventLog.getInstance().log(request.getRemoteAddr(), |
... | ... | |
2032 | 2043 |
|
2033 | 2044 |
// set content type and other response header fields first |
2034 | 2045 |
//response.setContentType("text/xml"); |
2035 |
out.println("<?xml version=\"1.0\"?>");
|
|
2036 |
out.println("<success>");
|
|
2037 |
out.println("<docid>" + newdocid + "</docid>");
|
|
2038 |
out.println("</success>");
|
|
2046 |
output += "<?xml version=\"1.0\"?>";
|
|
2047 |
output += "<success>";
|
|
2048 |
output += "<docid>" + newdocid + "</docid>";
|
|
2049 |
output += "</success>";
|
|
2039 | 2050 |
|
2040 | 2051 |
} catch (NullPointerException npe) { |
2041 | 2052 |
//response.setContentType("text/xml"); |
2042 |
out.println("<?xml version=\"1.0\"?>");
|
|
2043 |
out.println("<error>");
|
|
2044 |
out.println(npe.getMessage());
|
|
2045 |
out.println("</error>");
|
|
2053 |
output += "<?xml version=\"1.0\"?>";
|
|
2054 |
output += "<error>";
|
|
2055 |
output += npe.getMessage();
|
|
2056 |
output += "</error>";
|
|
2046 | 2057 |
logMetacat.warn("Error in writing eml document to the database" + npe.getMessage()); |
2047 | 2058 |
npe.printStackTrace(); |
2048 | 2059 |
} |
2049 | 2060 |
} catch (Exception e) { |
2050 | 2061 |
//response.setContentType("text/xml"); |
2051 |
out.println("<?xml version=\"1.0\"?>");
|
|
2052 |
out.println("<error>");
|
|
2053 |
out.println(e.getMessage());
|
|
2054 |
out.println("</error>");
|
|
2062 |
output += "<?xml version=\"1.0\"?>";
|
|
2063 |
output += "<error>";
|
|
2064 |
output += e.getMessage();
|
|
2065 |
output += "</error>";
|
|
2055 | 2066 |
logMetacat.warn("Error in writing eml document to the database" + e.getMessage()); |
2056 | 2067 |
e.printStackTrace(); |
2057 | 2068 |
} |
2069 |
|
|
2070 |
if (qformat == null || qformat.equals("xml")) { |
|
2071 |
response.setContentType("text/xml"); |
|
2072 |
out.println(output); |
|
2073 |
} else { |
|
2074 |
try { |
|
2075 |
DBTransform trans = new DBTransform(); |
|
2076 |
response.setContentType("text/html"); |
|
2077 |
trans.transformXMLDocument(output, |
|
2078 |
"message", "-//W3C//HTML//EN", qformat, |
|
2079 |
out, null); |
|
2080 |
} catch (Exception e) { |
|
2081 |
|
|
2082 |
logMetacat.error( |
|
2083 |
"Error in MetaCatServlet.handleLoginAction: " |
|
2084 |
+ e.getMessage()); |
|
2085 |
} |
|
2086 |
} |
|
2058 | 2087 |
} |
2059 | 2088 |
|
2060 | 2089 |
/** |
... | ... | |
2908 | 2937 |
out.println("Permission denied for " + action); |
2909 | 2938 |
out.println("</error>"); |
2910 | 2939 |
} |
2940 |
} else if(action.equals("insertmultipart")) { |
|
2941 |
if (username != null && !username.equals("public")) { |
|
2942 |
System.out.println("!!!!!!!!!!handling multipart insert"); |
|
2943 |
handleInsertMultipartAction(request, response, |
|
2944 |
out, params, fileList, username, groupnames); |
|
2945 |
} else { |
|
2946 |
out.println("<?xml version=\"1.0\"?>"); |
|
2947 |
out.println("<error>"); |
|
2948 |
out.println("Permission denied for " + action); |
|
2949 |
out.println("</error>"); |
|
2950 |
} |
|
2911 | 2951 |
} else { |
2912 | 2952 |
/* |
2913 | 2953 |
* try { out = response.getWriter(); } catch (IOException ioe2) { |
... | ... | |
2924 | 2964 |
} |
2925 | 2965 |
|
2926 | 2966 |
/** |
2967 |
* this action allows users to upload an xml document with multipart encoding. |
|
2968 |
*/ |
|
2969 |
private void handleInsertMultipartAction(HttpServletRequest request, |
|
2970 |
HttpServletResponse response, |
|
2971 |
PrintWriter out, Hashtable params, Hashtable fileList, |
|
2972 |
String username, String[] groupnames) |
|
2973 |
{ |
|
2974 |
Logger logMetacat = Logger.getLogger(MetaCatServlet.class); |
|
2975 |
String action = null; |
|
2976 |
String docid = null; |
|
2977 |
String qformat = null; |
|
2978 |
String output = ""; |
|
2979 |
|
|
2980 |
/* |
|
2981 |
* response.setContentType("text/xml"); try { out = |
|
2982 |
* response.getWriter(); } catch (IOException ioe2) { |
|
2983 |
* System.err.println("Fatal Error: couldn't get response output |
|
2984 |
* stream."); |
|
2985 |
*/ |
|
2986 |
|
|
2987 |
if (params.containsKey("docid")) |
|
2988 |
{ |
|
2989 |
docid = (String) params.get("docid"); |
|
2990 |
} |
|
2991 |
|
|
2992 |
if(params.containsKey("qformat")) |
|
2993 |
{ |
|
2994 |
qformat = (String)params.get("qformat"); |
|
2995 |
} |
|
2996 |
|
|
2997 |
// Make sure we have a docid and datafile |
|
2998 |
if (docid != null && fileList.containsKey("datafile")) |
|
2999 |
{ |
|
3000 |
logMetacat.info("Uploading data docid: " + docid); |
|
3001 |
// Get a reference to the file part of the form |
|
3002 |
//FilePart filePart = (FilePart) fileList.get("datafile"); |
|
3003 |
String fileName = (String) fileList.get("filename"); |
|
3004 |
logMetacat.info("Uploading filename: " + fileName); |
|
3005 |
// Check if the right file existed in the uploaded data |
|
3006 |
if (fileName != null) |
|
3007 |
{ |
|
3008 |
|
|
3009 |
try |
|
3010 |
{ |
|
3011 |
//logMetacat.info("Upload datafile " + docid |
|
3012 |
// +"...", 10); |
|
3013 |
//If document get lock data file grant |
|
3014 |
if (DocumentImpl.getDataFileLockGrant(docid)) |
|
3015 |
{ |
|
3016 |
// Save the data file to disk using "docid" as the name |
|
3017 |
String datafilepath = MetaCatUtil.getOption("datafilepath"); |
|
3018 |
File dataDirectory = new File(datafilepath); |
|
3019 |
dataDirectory.mkdirs(); |
|
3020 |
File newFile = null; |
|
3021 |
File tempFile = null; |
|
3022 |
String tempFileName = (String) fileList.get("name"); |
|
3023 |
String newFileName = dataDirectory + File.separator + docid; |
|
3024 |
long size = 0; |
|
3025 |
boolean fileExists = false; |
|
3026 |
|
|
3027 |
try |
|
3028 |
{ |
|
3029 |
newFile = new File(newFileName); |
|
3030 |
fileExists = newFile.exists(); |
|
3031 |
logMetacat.error("new file status is: " + fileExists); |
|
3032 |
if ( fileExists == false ) |
|
3033 |
{ |
|
3034 |
// copy file to desired output location |
|
3035 |
try |
|
3036 |
{ |
|
3037 |
MetaCatUtil.copyFile(tempFileName, newFileName); |
|
3038 |
} |
|
3039 |
catch (IOException ioe) |
|
3040 |
{ |
|
3041 |
logMetacat.error("IO Exception copying file: " + |
|
3042 |
ioe.getMessage()); |
|
3043 |
} |
|
3044 |
size = newFile.length(); |
|
3045 |
if (size == 0) |
|
3046 |
{ |
|
3047 |
throw new IOException("Uploaded file is 0 bytes!"); |
|
3048 |
} |
|
3049 |
} |
|
3050 |
logMetacat.info("Uploading the following to Metacat:" + |
|
3051 |
fileName + ", " + docid + ", " + |
|
3052 |
username + ", " + groupnames); |
|
3053 |
FileReader fr = new FileReader(newFile); |
|
3054 |
|
|
3055 |
char[] c = new char[1024]; |
|
3056 |
int numread = fr.read(c, 0, 1024); |
|
3057 |
StringBuffer sb = new StringBuffer(); |
|
3058 |
while(numread != -1) |
|
3059 |
{ |
|
3060 |
sb.append(c, 0, numread); |
|
3061 |
numread = fr.read(c, 0, 1024); |
|
3062 |
} |
|
3063 |
|
|
3064 |
Enumeration keys = params.keys(); |
|
3065 |
while(keys.hasMoreElements()) |
|
3066 |
{ //convert the params to arrays |
|
3067 |
String key = (String)keys.nextElement(); |
|
3068 |
String param = (String)params.get(key); |
|
3069 |
String[] s = new String[1]; |
|
3070 |
s[0] = param; |
|
3071 |
params.put(key, s); |
|
3072 |
} |
|
3073 |
//add the doctext to the params |
|
3074 |
String doctext = sb.toString(); |
|
3075 |
String[] doctextArr = new String[1]; |
|
3076 |
doctextArr[0] = doctext; |
|
3077 |
params.put("doctext", doctextArr); |
|
3078 |
//call the insert routine |
|
3079 |
handleInsertOrUpdateAction(request, response, out, |
|
3080 |
params, username, groupnames); |
|
3081 |
} |
|
3082 |
catch(Exception e) |
|
3083 |
{ |
|
3084 |
throw e; |
|
3085 |
} |
|
3086 |
} |
|
3087 |
} |
|
3088 |
catch(Exception e) |
|
3089 |
{ |
|
3090 |
System.out.println("error uploading text file via multipart: " + e.getMessage()); |
|
3091 |
e.printStackTrace(); |
|
3092 |
} |
|
3093 |
} |
|
3094 |
} |
|
3095 |
} |
|
3096 |
|
|
3097 |
/** |
|
2927 | 3098 |
* Handle the upload action by saving the attached file to disk and |
2928 | 3099 |
* registering it in the Metacat db |
2929 | 3100 |
*/ |
... | ... | |
3069 | 3240 |
DBTransform trans = new DBTransform(); |
3070 | 3241 |
response.setContentType("text/html"); |
3071 | 3242 |
trans.transformXMLDocument(output, |
3072 |
"success", "-//W3C//HTML//EN", qformat,
|
|
3243 |
"message", "-//W3C//HTML//EN", qformat,
|
|
3073 | 3244 |
out, null); |
3074 | 3245 |
} catch (Exception e) { |
3075 | 3246 |
|
Also available in: Unified diff
added support to upload an xml document using multipart encoding. the enables better support for web interfaces that wish to use multipart to upload xml documents. the action is 'insertmultipart'