View Javadoc

1   //--------------------------------------
2   //
3   // LoadView.java
4   // Since: 2008/02/12
5   //
6   //--------------------------------------
7   package org.utgenome.gwt.utgb.server.app;
8   
9   import java.io.BufferedReader;
10  import java.io.IOException;
11  import java.io.InputStreamReader;
12  import java.io.StringWriter;
13  
14  import javax.servlet.ServletException;
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  
18  import org.apache.commons.fileupload.FileItemIterator;
19  import org.apache.commons.fileupload.FileItemStream;
20  import org.apache.commons.fileupload.FileUploadException;
21  import org.apache.commons.fileupload.servlet.ServletFileUpload;
22  import org.utgenome.gwt.utgb.server.WebTrackBase;
23  import org.xerial.util.StringUtil;
24  import org.xerial.util.log.Logger;
25  
26  /**
27   * Get the view data from the user's file via POST request, and return the data by embedding it in an HTML.
28   * 
29   * 
30   */
31  public class LoadView extends WebTrackBase {
32  	private static final long serialVersionUID = 1L;
33  	private static Logger _logger = Logger.getLogger(LoadView.class);
34  
35  	public LoadView() {
36  	}
37  
38  	public void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
39  		if (!ServletFileUpload.isMultipartContent(request)) {
40  			_logger.error("not the multipart post");
41  			return;
42  		}
43  
44  		response.setContentType("text/html");
45  		// Create a new file upload handler
46  		ServletFileUpload upload = new ServletFileUpload();
47  
48  		// Parse the request
49  		try {
50  			for (FileItemIterator it = upload.getItemIterator(request); it.hasNext();) {
51  				FileItemStream fs = it.next();
52  				if (fs.getFieldName().equals("file")) {
53  					BufferedReader reader = new BufferedReader(new InputStreamReader(fs.openStream()));
54  					StringWriter buf = new StringWriter();
55  					String line;
56  
57  					buf.append("<body><!--");
58  
59  					while ((line = reader.readLine()) != null) {
60  						buf.append(line);
61  						buf.append(StringUtil.newline());
62  					}
63  					buf.append("--></body>");
64  					buf.flush();
65  
66  					response.getWriter().append(buf.toString());
67  					response.getWriter().flush();
68  					return;
69  				}
70  			}
71  		}
72  		catch (FileUploadException e) {
73  			_logger.error(e);
74  		}
75  
76  	}
77  
78  }