Project

General

Profile

« Previous | Next » 

Revision 1737

Added by skrish almost 21 years ago

Metacat EMl Upload Interface

View differences:

src/edu/ucsb/nceas/metacat/harvesterClient/MetUpload.java
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.*;
18
import edu.ucsb.nceas.metacat.*;
19
import edu.ucsb.nceas.metacat.MetaCatUtil;
20

  
21

  
22
public class MetUpload extends HttpServlet
23
{
24
public void service(HttpServletRequest request, HttpServletResponse response)
25
        throws IOException
26
{
27
	PrintWriter out = null;
28
	try {
29
  		int id=0;
30

  
31
		out = response.getWriter();
32

  
33
  		String file = new String("");
34
		DBConnection dbconn = null;
35
	 	File dataDirectory = null;
36
	 	MetaCatUtil util = null;
37
		String datafilepath = null;
38
		String fileName = null;
39
		FilePart fPart = null;
40
      		MultipartParser parser = new MultipartParser((HttpServletRequest)request, 1024 * 1024);
41
      		//MultipartParser parser = new MultipartParser(request, 1024 * 1024);
42
		DocumentImplWrapper documentWrapper = null;
43
      		Part aPart;
44
		String eml_filename=" ",docid=" ";
45
		StringReader xml = null;
46
		/*String eml_filename=" ",docid=" ";*/
47
	    	Hashtable formElements = new Hashtable();
48
 	    	while( (aPart = parser.readNextPart()) != null ) {
49
	      		if( aPart.isParam() == true ) {
50
          			ParamPart pPart = (ParamPart)aPart;
51
	        		String field = pPart.getName();
52
          			String value = pPart.getStringValue();
53
          			if(field != null) {
54
		        		if(value == null)
55
			        		value = "";
56
            				formElements.put(field, value);
57
					docid=value;
58
		      		}
59
	      		}
60
        		else if( aPart.isFile() == true ) {
61
		      		fPart = (FilePart)aPart;
62
          			String field = fPart.getName();
63
		      		fileName = fPart.getFileName();
64
          			if(fileName != null) {
65
		        		//write the image to a file
66
		        		String dir = "/tmp/Upload_data";  
67
		        		File theFile = new File(dir, fileName);
68
					fPart.writeTo(theFile);
69
					eml_filename = dir+"/"+fileName;
70
		        		formElements.put(field, fileName);
71
           			}
72
        		}
73
      		}
74
	//	out.println( formElements );
75
		out.println( docid + " " + eml_filename );
76
    		out.println("EML File uploaded");
77
/*****************************************************/
78

  
79

  
80
/* Change for input to metacat */
81

  
82
		util = new MetaCatUtil();
83
		datafilepath = util.getOption("datafilepath");
84
		dataDirectory = new File(datafilepath);
85
	HttpSession sess = request.getSession(true);
86
	String username = null;
87
	username = (String)sess.getAttribute("username");
88

  
89
	/*
90
dbconn=DBConnectionPool.getDBConnection("DocumentImpl.main");
91

  
92
		documentWrapper = new DocumentImplWrapper("", false);
93
/*String newdocid = DocumentImpl.write(dbconn, eml_filename, null,
94
                                   "null", "INSERT", docid,
95
                                   null, null);*/
96
	/*xml = new StringReader(eml_filename);
97
String newdocid = documentWrapper.write(dbconn, xml, null,
98
                                   null, "INSERT", docid,
99
                                   null, null);
100
if ((docid != null) && (!docid.equals(newdocid))) {
101
         
102
            System.out.println("New document ID generated!!! ");
103
          }*/
104

  
105
 if (DocumentImpl.getDataFileLockGrant(docid))
106
 {
107
	 DocumentImpl.registerDocument(fileName, "BIN", docid, username);
108

  
109
	 // Save the data file to disk using "docid" as the name
110
	 dataDirectory.mkdirs();
111
	 File newFile = new File(dataDirectory, docid);
112

  
113
	 long size = fPart.writeTo(newFile);
114
	 ForceReplicationHandler frh = new ForceReplicationHandler
115
	 				(docid, "insert", false, null);
116

  
117

  
118
	 // set content type and other response header fields first
119
	 out.println("success");
120
}
121
else 
122
	
123
	 out.println("failure");
124

  
125

  
126

  
127
/****************************************************/
128

  
129

  
130
  	} catch (Exception e) {
131
		out.println("An error occurred.");
132
		e.printStackTrace(out);
133
  	}
134
	
135
	out.flush();
136
}
137
}
0 138

  

Also available in: Unified diff