com.vigglet.oei.vehicle.GetVehiclePdfServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.vigglet.oei.vehicle.GetVehiclePdfServlet.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 com.vigglet.oei.vehicle;

import com.vigglet.oei.user.User;
import com.vigglet.servlet.BasePdfServlet;
import com.vigglet.pdf.ServicereportPdf;
import java.io.File;
import java.io.FileInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author vikn
 */
@WebServlet("/api/vehicle/download-service-report")
public class GetVehiclePdfServlet extends BasePdfServlet {

    @Override
    protected void downloadPdf(HttpServletRequest req, HttpServletResponse resp, User user, final int modelId)
            throws Exception {
        File file = new ServicereportPdf(VehicleUtil.getInstance().findById(modelId), user).createServiceReport();
        resp.setContentLength((int) file.length());

        FileInputStream fis = new FileInputStream(file);
        IOUtils.copy(fis, resp.getOutputStream());
        IOUtils.closeQuietly(fis);
    }
}