Boa tarde!
Tenho uma API Restful Java (JAX-RS) e preciso retornar um PDF.
A ideia é a seguinte:
tenho um método GET que vai me passar uns parâmetros
e esse método vai chamar o gerarJasper que precisa retornar um .jasper e imprimir um PDF no navegador
segue abaixo o código
@Path("/Integracao")
public class Integracao{
@GET
@Path("/teste")
public void teste(){
TrCboServiceImpl cS = new TrCboServiceImpl();
List datasource = null;
try {
datasource = cS.listAll();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AbstractApplicationBean aab = new AbstractApplicationBean() {
@Override
public boolean useMultiempresaService() {
// TODO Auto-generated method stub
return false;
}
};
try {
aab.gerarJasper("RelTrCbo", TipoRelatorioEnum.PDF.getType(), datasource, new HashMap());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
e o método do jasper
public void gerarJasper(String name, String type, List data, Map params) throws IllegalArgumentException, RuntimeException, Exception {
boolean found = false;
for (int i = 0; i < VALID_TYPES.length; i++) {
if (VALID_TYPES[i].equals(type)) {
found = true;
break;
}
}
if (!found) {
throw new IllegalArgumentException("Tipo solicitado '" + type + "' inválido");
}
// Procurar recurso de design de relatório compilado
// dá NullPointerException na linha abaixo
ExternalContext econtext = FacesContext.getCurrentInstance().getExternalContext();
InputStream stream = econtext.getResourceAsStream(PREFIX + name + SUFFIX);
if (stream == null) {
throw new IllegalArgumentException("O relatório '" + name + "' não existe");
}
FacesContext fc = FacesContext.getCurrentInstance();
ServletContext context = (ServletContext)fc.getExternalContext().getContext();
String path = context.getRealPath(File.separator) + "resources/jasper" + File.separator;
String logo = context.getRealPath(File.separator) + "resources/imagens" + File.separator;
params.put("SUBREPORT_DIR", path);
params.put("LOGO_DIR", logo);
JRDataSource ds = new JRBeanArrayDataSource(data.toArray());
JasperPrint jasperPrint = null;
try {
jasperPrint = JasperFillManager.fillReport(stream, params, ds);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new FacesException(e);
} finally {
try {
stream.close();
} catch (IOException e) {
}
}
JRExporter exporter = null;
HttpServletResponse response = (HttpServletResponse) econtext.getResponse();
FacesContext fcontext = FacesContext.getCurrentInstance();
try {
response.setContentType(type);
if ("application/pdf".equals(type)) {
exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
} else if ("text/html".equals(type)) {
exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, response.getWriter());
// Tornar imagens disponÃveis para a saÃda HTML
HttpServletRequest request = (HttpServletRequest) fcontext.getExternalContext().getRequest();
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, new HashMap());
// A seguinte instrução requer mapeamento / imagem
// para o imageServlet no web.xml.
//
// Este servlet serve imagens, incluindo imagens px
// para espaçamento.
//
// Sirva as imagens diretamente para não
// incorrermos em tempo extra associado a
// a uma solicitação JSF para uma entidade não-JSF.
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + "/image?image=");
}else if("application/xlsx".equals(type)){
exporter = new JRXlsxExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
}else if("application/docx".equals(type)){
exporter = new JRDocxExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
} else if("application/rtf".equals(type)){
exporter = new JRRtfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new FacesException(e);
}
try {
exporter.exportReport();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new FacesException(e);
}
fcontext.responseComplete();
}
alguém poderia me ajudar?