View Javadoc

1   package br.com.ibnetwork.guara.modules.screens;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.OutputStream;
6   
7   import javax.servlet.http.HttpServletResponse;
8   
9   import org.apache.avalon.framework.service.ServiceManager;
10  import org.apache.commons.logging.Log;
11  
12  import br.com.ibnetwork.guara.message.SystemMessage;
13  import br.com.ibnetwork.guara.message.SystemMessageBroker;
14  import br.com.ibnetwork.guara.modules.Module;
15  import br.com.ibnetwork.guara.rundata.Outcome;
16  import br.com.ibnetwork.guara.rundata.RunData;
17  import br.com.ibnetwork.guara.view.Context;
18  
19  /***
20   * @author <a href="mailto:leandro@ibnetwork.com.br">leandro</a>
21   */
22  public abstract class DownloadSupport
23  	implements Module
24  {
25  	protected ServiceManager manager;
26  
27      protected SystemMessageBroker sysMsg;
28      
29      protected String name;
30  	
31  	protected Log logger;
32  
33      
34      public String getName()
35      {
36          return name;
37      }
38  
39      public void setName(String name)
40      {
41          this.name = name;
42      }
43  
44      public Outcome doPerform(RunData data, Context context)
45      	throws Exception
46      {
47  		File file = getFile(data);
48  		
49  		if(file == null || !file.exists())
50  		{
51  			SystemMessage msg;
52  			Object[] params = null;
53  			if(file != null)
54  			{
55  			    params = new Object[]{file.getName()};
56  				msg = sysMsg.getSystemMessage("downloadUnavailable");
57  			}
58  			else
59  			{
60  				msg = sysMsg.getSystemMessage("downloadUnavailable",null);
61  			}
62  			data.setMessage(msg,params);
63  			return Outcome.UNKNOWN;
64  		}
65  
66  		HttpServletResponse response = data.getResponse();
67  		response.setContentType("application/octet-stream");
68  		response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
69  		response.setHeader("Content-Length",Long.toString(file.length()));
70  		FileInputStream fis = null;
71  		try
72  		{
73  		    fis = new FileInputStream(file.getPath());	
74  	    	OutputStream out = response.getOutputStream();
75  	    	byte[] buf = new byte[4 * 1024];  // 4K buffer
76  		    int bytesRead;
77  		    int bytesTotal = 0;
78  		    while ((bytesRead = fis.read(buf)) != -1) 
79  		    {
80  				bytesTotal +=bytesRead;
81  				out.write(buf, 0, bytesRead);
82  	    	}
83  			logger.info("file ["+file.getPath()+"] downloaded");
84  			doClean(file);
85  		}
86  		catch(Exception e)
87  		{
88  			logger.error("file ["+file.getPath()+"] download error",e);
89  		}
90  		finally 
91  		{
92  	    	try
93  	    	{
94  				if (fis != null) fis.close();
95  	    	}
96  	    	catch(Exception ignored)
97  	    	{}
98  		}
99      	
100         return Outcome.UNKNOWN;
101     }
102 
103     protected abstract void doClean(File file)
104     	throws Exception;
105 
106     protected abstract File getFile(RunData data)
107     	throws Exception;
108 
109 }