org.jc.exercicios.download.DownloadServlet.java Source code

Java tutorial

Introduction

Here is the source code for org.jc.exercicios.download.DownloadServlet.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jc.exercicios.download;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author jean
 */
@WebServlet
public class DownloadServlet extends HttpServlet {

    /**
     * Realiza o download de uma nota fiscal previamente emitida. Para encontrar
     * uma nota fiscal, so necessrios o cpf/cnpj do cliente (cpf_cnpj), a data
     * de emisso (dt_emissao) e o nmero da nota (num_nf). Somente a verso em
     * xml da nota fiscal permanece disponvel para download.
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
            throws ServletException, IOException {
        boolean authenticated = req.authenticate(resp);
        if (authenticated) {
            String identificacao = StringUtils.defaultIfEmpty(req.getParameter("cpf_cnpj"), "");
            String data = StringUtils.defaultIfEmpty(req.getParameter("dt_emissao"), "");
            String numero = StringUtils.defaultIfEmpty(req.getParameter("num_nf"), "");
            File dir = new File(System.getProperty("nfe.dir"));
            if (identificacao.matches("\\d+") && numero.matches("\\d+")
                    && data.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}")) {
                File nfe = new File(new File(dir, identificacao), data + '_' + numero + ".xml");
                if (nfe.exists()) {
                    resp.setContentType("text/xml");
                    try (BufferedReader reader = new BufferedReader(new FileReader(nfe));
                            PrintWriter writer = resp.getWriter();) {
                        String line;
                        while ((line = reader.readLine()) != null) {
                            writer.write(line);
                            //                            writer.println();
                        }
                        writer.flush();
                    }
                    resp.setStatus(HttpServletResponse.SC_OK);
                }
            } else {
                String msg = String.format(
                        "Prezado cliente %s, no encontramos a nota nmero %s," + " na data %s.", identificacao,
                        numero, data);
                resp.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
            }
        } else {
            resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Voc no pode fazer download de notas fiscais!");
        }
    }
}