Project

General

Profile

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

    
23
import edu.ucsb.nceas.metacat.*;
24
import edu.ucsb.nceas.metacat.MetaCatUtil;
25

    
26

    
27
public class MetUpload extends HttpServlet
28
{
29
public void service(HttpServletRequest request, HttpServletResponse response)
30
        throws IOException
31
{
32
	PrintWriter out = null;
33
	try {
34
  		int id=0;
35

    
36
		out = response.getWriter();
37

    
38
  		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
		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
		DocumentImplWrapper documentWrapper = null;
50
		String eml_filename=" ",docid=" ";
51
		StringReader xml = null;
52
		/*String eml_filename=" ",docid=" ";*/
53
	    	Hashtable formElements = new Hashtable();
54
 	    	while( (aPart = parser.readNextPart()) != null ) {
55
	      		if( aPart.isParam() == true ) {
56
          			ParamPart pPart = (ParamPart)aPart;
57
	        		String field = pPart.getName();
58
          			String value = pPart.getStringValue();
59
          			if(field != null) {
60
		        		if(value == null)
61
			        		value = "";
62
            				formElements.put(field, value);
63
					if (field.equals("docid"))
64
						docid=value;
65
		      		}
66
	      		}
67
        		else if( aPart.isFile() == true ) {
68
		      		fPart = (FilePart)aPart;
69
          			String field = fPart.getName();
70
		      		fileName = fPart.getFileName();
71
          			if(fileName != null) {
72
		        		//write the image to a file
73
		        		String dir = "/tmp/Upload_data";  
74
		        		File theFile = new File(dir, fileName);
75
					/*fPart.writeTo(theFile);
76
					eml_filename = dir+"/"+fileName;
77
		        		formElements.put(field, fileName);*/
78
					break;
79
           			}
80
        		}
81
      		}
82
	//	out.println( formElements );
83
		//out.println( docid + " " + eml_filename );
84
    	//	out.println("EML File uploaded");
85
/*****************************************************/
86

    
87

    
88
/* Change for input to metacat */
89

    
90
		util = new MetaCatUtil();
91
		datafilepath = util.getOption("datafilepath");
92
		dataDirectory = new File(datafilepath);
93
	HttpSession sess = request.getSession(true);
94
	String username = null;
95
	username = (String)sess.getAttribute("username");
96

    
97
	 //out.println(username);
98
	 out.println("Username is "+username+" <BR>");
99
	 
100

    
101

    
102
	 //initial DBConnection pool
103
	      connPool = DBConnectionPool.getInstance();
104
	 
105
	/*
106
dbconn=DBConnectionPool.getDBConnection("DocumentImpl.main");
107

    
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

    
121
 if (DocumentImpl.getDataFileLockGrant(docid))
122
 {
123
	 //out.println(docid);
124
	 DocumentImpl.registerDocument(fileName, "BIN", docid, username);
125

    
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
/****************************************************/
147

    
148

    
149
  	} catch (Exception e) {
150
		out.println("An error occurred.");
151
		e.printStackTrace(out);
152
  	}
153
	
154
	out.flush();
155
}
156
}
(2-2/3)