View Javadoc

1   /*
2    * Criado em 13/04/2005
3    *
4    */
5   package ecar.servlet.file;
6   
7   import java.io.File;
8   import java.io.FileInputStream;
9   import java.io.IOException;
10  import java.io.OutputStream;
11  import java.util.HashMap;
12  import java.util.Iterator;
13  
14  import javax.servlet.ServletException;
15  import javax.servlet.http.HttpServlet;
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
18  
19  import org.apache.log4j.Logger;
20  
21  import comum.util.Util;
22  import ecar.util.ConfiguracaoResponseBean;
23  
24  /**
25   * @author felipev
26   *
27   */ 
28  public class DownloadFile extends HttpServlet{
29  
30  	/**
31  	 * 
32  	 */
33  	private static final long serialVersionUID = 5548532554690785199L;
34  	private Logger logger = Logger.getLogger(this.getClass());
35  
36  	/**
37  	 * @author N/C
38  	 * @since N/C
39  	 * @version N/C
40  	 */
41  	public DownloadFile(){
42  				
43  	}
44  	
45  	/**
46  	 * Controla download de arquivos de acordo com permissao de acesso publico.<br>
47  	 * 
48  	 * @author N/C
49  	 * @since N/C
50  	 * @version N/C
51  	 * @param HttpServletRequest request
52  	 * @param HttpServletResponse response
53  	 * @throws ServletException
54  	 * @throws IOException
55  	 */
56  	public final void service(HttpServletRequest request,
57  			HttpServletResponse response) throws ServletException, IOException {
58  		String strDownloadDeslogado = request.getParameter("acessoPublico");
59  		if (strDownloadDeslogado != null) {
60  			request.getSession().setAttribute("downloadDeslogado",
61  					new Boolean(true));
62  		}
63  
64  //		if (request.getSession().getAttribute("seguranca") == null) {
65  //			Boolean downloadDeslogado = ((Boolean) request.getSession()
66  //					.getAttribute("downloadDeslogado"));
67  //			boolean eMail = false;
68  //			if (request.getParameter("downloadEmail") != null)
69  //				eMail = "S".equals(request.getParameter("downloadEmail"));
70  //			if ((downloadDeslogado == null || !downloadDeslogado.booleanValue())
71  //					&& !eMail) {
72  //				throw new ServletException(
73  //						"Download nao permitido: Usuario nao autenticado");
74  //			}
75  //			request.getSession().removeAttribute("downloadDeslogado");
76  //		}
77  
78  		String tipo = request.getParameter("tipo");
79  		boolean download = true;
80  		boolean multimidia = false;
81  		if (tipo != null && "open".equals(tipo))
82  			download = false;
83  		if (tipo != null && "multimidia".equals(tipo)) {
84  			multimidia = true;
85  		}
86  
87  		String file = request.getParameter("RemoteFile");
88  		file = Util.trocar(file, "/", File.separator);
89  		String fileName = file.substring(file.lastIndexOf(File.separator) + 1);
90  		String porta = String.valueOf(request.getLocalPort());
91  
92  		if (multimidia) {
93  			response.setContentType("video/mpeg");
94  			response.setHeader("Cache-Control", "cache; filename=\"" + fileName
95  					+ "\"");
96  		} else if (download) {
97  			/**
98  			 * @author 05228805419
99  			 */
100 			// recupera o objeto de configuracao do response do contexto da aplicação
101 			ConfiguracaoResponseBean configuracaoResponseBean = (ConfiguracaoResponseBean) request
102 					.getSession().getServletContext().getAttribute(
103 							ConfiguracaoResponseBean.class.getName());
104 
105 			// verifica se o objeto de configuracao do response deve ser utilizado
106 			if (configuracaoResponseBean != null
107 					&& configuracaoResponseBean
108 							.getInUtilizarConfiguracaoResponse()) {
109 
110 				// verifica se deve setar o content length
111 				if (configuracaoResponseBean.getInSetarContentLength()) {
112 					File f = new File(file);
113 					response.setContentLength((int) f.length());
114 				}
115 				
116 				if (configuracaoResponseBean.getContentType() != null) {
117 					response.setContentType(configuracaoResponseBean
118 							.getContentType());
119 				}
120 
121 				HashMap<String, String> hmHeaders = configuracaoResponseBean
122 						.getHeaders();
123 				Iterator iterator = hmHeaders.keySet().iterator();
124 
125 				while (iterator.hasNext()) {
126 					String chave = (String) iterator.next();
127 					String valor = (String) hmHeaders.get(chave);
128 
129 					response.setHeader(chave, valor);
130 				}
131 			} else {
132 				// Garantir que o IE fará o download sob https
133 				File f = new File(file);
134 				response.setContentLength((int) f.length());
135 				response.setContentType("application/octetstream");
136 				response.setHeader("Pragma", "Public");
137 				response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
138 			}
139 		} else {
140 			response.setContentType("image/jpeg");
141 			response.setHeader("Cache-Control", "no-cache; filename=\""
142 					+ fileName + "\"");
143 		}
144 		java.io.InputStream is = null;
145 		OutputStream out2 = null;
146 		try {
147 			out2 = response.getOutputStream();
148 			is = new FileInputStream(file);
149 			int i;
150 			while ((i = is.read()) != -1) {
151 				out2.write(i);
152 			}
153 		} catch (Exception e) {
154 			this.logger.error(e);
155 			logger.equals(e);
156 		} finally {
157 			if (out2 != null) {
158 				out2.flush();
159 				out2.close();
160 			}
161 			if (is != null) {
162 				is.close();
163 			}
164 		}
165 	}
166 	
167 	private void configurarResponse(HttpServletResponse response) {
168 		
169 	}
170 }