View Javadoc

1   package ecar.util.jasper.servlet;
2   
3   import java.io.IOException;
4   import java.io.PrintWriter;
5   
6   import javax.servlet.RequestDispatcher;
7   import javax.servlet.ServletException;
8   import javax.servlet.ServletOutputStream;
9   import javax.servlet.http.HttpServlet;
10  import javax.servlet.http.HttpServletRequest;
11  import javax.servlet.http.HttpServletResponse;
12  
13  import org.apache.log4j.Logger;
14  
15  public abstract class RelatorioServlet extends HttpServlet {
16  	
17  	private Logger logger = Logger.getLogger(this.getClass());
18  
19  	public RelatorioServlet(){
20  		super();
21  	}
22  
23  	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
24  		logger.info("request :: GET:: Classe:: " + this.getClass().getName() );
25  		doRelatorioGet(request,response);
26  	}
27  	
28  	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
29  		logger.info("request :: POST:: Classe:: " + this.getClass().getName() );		
30  		doRelatorioPost(request,response);
31  	}
32  
33  	public abstract void doRelatorioGet(HttpServletRequest request, HttpServletResponse response) ;
34  	public abstract void doRelatorioPost(HttpServletRequest request, HttpServletResponse response) ;	
35  	
36  	
37  	protected void responseToPDF ( HttpServletResponse response, byte[] arquivo ) throws IOException{
38  		response.setContentType("application/pdf");
39  		response.setContentLength(arquivo.length);	
40  		ServletOutputStream outputStream = response.getOutputStream();
41  		outputStream.write(arquivo,0,arquivo.length);
42  		outputStream.flush();
43  		outputStream.close();
44  	}
45  	
46  	/*  TODO implementar formato para HTML  
47  	 * */
48  	protected void responseToHTML ( HttpServletResponse response, String conteudo ) throws IOException {
49  		response.setContentType("text/html");
50  		PrintWriter writer = response.getWriter();
51  		writer.append(conteudo);
52  		writer.flush();
53  		writer.close();
54  	}
55  	
56  	/**
57  	 * Redireciona para pagina de erro padrao se o parametro "objeto" for nulo
58  	 * @param request requisicao
59  	 * @param response responsta
60  	 * @param objeto objeto a ser testado
61  	 * @throws ServletException
62  	 * @throws IOException
63  	 */
64  	protected void redirectDefaultErrorIfNull( HttpServletRequest request, HttpServletResponse response, Object objeto  ) throws ServletException, IOException{
65  		if ( objeto == null){
66  			RequestDispatcher dispatcher = request.getRequestDispatcher( "../erro.jsp" );
67  			dispatcher.forward( request, response); 
68  		}
69  	}
70  	
71  }