6. Aplicacion java (III). Servlets, Observaciones

1. Servlet

La clase es src/main/java/ximodante/rest/servlets/DownloadServletCSV.java 

Solo hay un servlet, su código es

package ximodante.rest.servlets;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.logging.Level;

import org.apache.commons.io.IOUtils;

import jakarta.servlet.ServletException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import ximodante.rest.services.CSVService;
import ximodante.rest.services.LoggingService;

@WebServlet(urlPatterns ="/downloadServletCSV", asyncSupported = true)
public class DownloadServletCSV extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DownloadServletCSV() {
		super();
		System.out.println("Creating Servlet DownloadServletCSV ....");
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session=request.getSession();
		//X509Certificate[] certs = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");
		X509Certificate clientCert=null;
		String csv= (String) request.getParameter("csv");
		System.out.println("csv="+csv);
		try {
    		X509Certificate[] certs= (X509Certificate[]) request.getAttribute("jakarta.servlet.request.X509Certificate");
    		clientCert= certs!=null ? certs[0]: null;
    	} catch (Exception e) {e.printStackTrace();}
		
		if (clientCert!=null) {
			//String csv= (String) request.getParameter("csv");
			String dateInfo=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
			LoggingService.logger.log(Level.INFO, "DATA:"+ dateInfo +" SOL.LICITANT:" + clientCert.getSubjectX500Principal() + " CSV:" + csv + " ISSUER:"+ clientCert.getIssuerX500Principal());
			String myFileName="Aj_" + dateInfo + ".pdf"; 
		
			InputStream inputStream=null;
			try {
			
				inputStream=CSVService.getdownloadStream(csv, clientCert);
				if (inputStream!=null) {
					response.setContentType("application/pdf");	
					response.setHeader("Content-disposition", "attachment; filename=" + myFileName);
					ServletOutputStream outStrm=response.getOutputStream();
					IOUtils.copy(inputStream, outStrm);
					outStrm.close();
				} else {
					response.setContentType("text/html");	
					PrintWriter writer = response.getWriter();
					writer.println("<h1> Error: File not existing.. </h1>");
					writer.println("<h1> Error: Fichero inexistente.. </h1>");
					writer.println("<h1> Error: Fitxer inexistent.. </h1>");
					//writer.println("<h2> Could not download the file " + fileName +"</h1>");
					writer.close();
				}
			
			} catch(Exception e) {
				e.printStackTrace();
				response.setContentType("text/html");	
				PrintWriter writer = response.getWriter();
				writer.println("<h1> Error downloading file </h1>");
				//writer.println("<h2> Could not download the file " + fileName +"</h1>");
				writer.close();
			}
		} else {
			response.setContentType("text/html");	
			PrintWriter writer = response.getWriter();
			writer.println("<h1> Error: Sin certificado.. </h1>");
			writer.println("<h1> Error: No certificate.. </h1>");
			writer.println("<h1> Error: Sense certificat.. </h1>");
			//writer.println("<h2> Could not download the file " + fileName +"</h1>");
			writer.close();
		}
	}

}


2. Observaciones

Hay que tener en cuenta que se han sustituido las librerias "javax" por "jakarta", y eso requiere el uso de Tomcat 10.
Por tanto cuando se genere el fichero "war", no va a funcionar en versiones de tomcat 9 ni en las anteriores.



Comentarios

Entradas populares de este blog

1. Instalación del servidor Tomcat y VSFTP , certificados y librerias

7. Comprobaciones

8.Eclipse. Cambio de configuración de los servidores