Olá pessoa, boa noite.
Estou enfrentando uns problemas com relação a oferta de downloads ao usuário de um sistema JSF 2.2 + Primefaces 4 + Tomcat 7.0.42.
Existe uma tabela a partir da qual o usuário pode baixar arquivos.
A princípio, tudo funciona direitinho, cadastro, edição, remoção, todo o CRUD básico, o problema acontece quando o usuário faz download de algum registro.
Depois do primeiro download, ou até exibição inline, por algum motivo os botões que antes abriam p:dialogs para cadastro, edição e remoção param de funcionar.
E um outro problema acontece no que se diz respeito ao download de fato, algumas vezes ao invés de começar o download, simplesmente abre uma página em branco, mas se voltar pra página da tabela, atualizar e tentar novamente, o download acontece normalmente, é como se só funcionasse a primeira vez, mas nem sempre buga de primeira, parece ter haver com o tamanho do arquivo, se for pequeno não buga.
código da tabela
<p:panel styleClass="panelDefault">
<f:facet name="header">#{msgs.balancetes}</f:facet>
<h:form id="formTabela">
<p:outputPanel id="panelTabela">
<sec:authorize ifAnyGranted="ROLE_GERENTE, ROLE_ADMINISTRADOR">
<p:commandButton value="#{msgs.novo_balancete}"
oncomplete="novoDialog.show()" update=":formNovo:panelNovo"
icon="ui-icon-plus" />
<p:separator />
</sec:authorize>
<center>
<p:dataTable id="tabela" value="#{balanceteBean.balancetes}"
var="balancete" paginator="true" paginatorAlwaysVisible="false"
rows="20" emptyMessage="#{msgs.sem_registros}"
style="width: 60%">
<p:column style="width: 100px;">
<f:facet name="header">#{msgs.data_documento}</f:facet>
<p:outputLabel value="#{balancete.arquivo.dataDocumento.time}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</p:outputLabel>
</p:column>
<p:column>
<f:facet name="header">#{msgs.ano}</f:facet>
<p:outputLabel value="#{balancete.ano}" />
</p:column>
<p:column>
<f:facet name="header">#{msgs.mes}</f:facet>
<p:outputLabel value="#{balancete.mes}" />
</p:column>
<p:column style="width: 80px;">
<f:facet name="header">#{msgs.acoes}</f:facet>
<center>
<h:commandLink
actionListener="#{downloadBean.download(balancete.arquivo)}"
target="_blank">
<p:graphicImage library="imagens" name="download.png"
title="#{msgs.fazer_download}" style="border: none" />
</h:commandLink>
<p:outputLabel value=" " />
<h:commandLink
actionListener="#{downloadBean.preview(balancete.arquivo)}"
target="_blank">
<p:graphicImage library="imagens" name="printer.png"
title="#{msgs.imprimir}" style="border: none" />
</h:commandLink>
<sec:authorize ifAnyGranted="ROLE_GERENTE, ROLE_ADMINISTRADOR">
<p:outputLabel value=" " />
<p:commandLink oncomplete="editarDialog.show()"
update=":formEditar:panelEditar"
actionListener="#{balanceteBean.atualizar(balancete.id)}">
<p:graphicImage library="imagens" name="pencil.png"
title="#{msgs.editar_balancete}" style="border: none" />
</p:commandLink>
<p:outputLabel value=" " />
<p:commandLink oncomplete="removerDialog.show()"
update=":formRemover:panelRemover">
<p:graphicImage library="imagens" name="delete.png"
title="#{msgs.remover_balancete}" style="border: none" />
<f:setPropertyActionListener
target="#{balanceteBean.balancete}" value="#{balancete}" />
</p:commandLink>
</sec:authorize>
</center>
</p:column>
</p:dataTable>
</center>
</p:outputPanel>
</h:form>
</p:panel>
código do bean de download
@ManagedBean
@RequestScoped
public class DownloadBean {
private void imprimirErro(String mensagem) {
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR, mensagem, null));
}
public void download(Arquivo arquivo) {
HttpServletResponse response = (HttpServletResponse) FacesContext
.getCurrentInstance().getExternalContext().getResponse();
try {
byte[] conteudo = ArquivoUtil.lerArquivo(arquivo.getDiretorio());
response.addHeader("Content-Disposition", "attachment; filename="
+ arquivo.getNome());
response.setContentLength(conteudo.length);
response.setContentType("application/pdf");
response.getOutputStream().write(conteudo);
FacesContext.getCurrentInstance().responseComplete();
} catch (Exception e) {
imprimirErro(e.getMessage());
} finally {
try {
if (response.getOutputStream() != null) {
response.getOutputStream().close();
}
} catch (Exception e) {
imprimirErro(e.getMessage());
}
}
}
public void preview(Arquivo arquivo) {
HttpServletResponse response = (HttpServletResponse) FacesContext
.getCurrentInstance().getExternalContext().getResponse();
try {
byte[] conteudo = ArquivoUtil.lerArquivo(arquivo.getDiretorio());
response.addHeader("Content-Disposition", "inline; filename="
+ arquivo.getNome());
response.setContentLength(conteudo.length);
response.setContentType("application/pdf");
response.getOutputStream().write(conteudo);
FacesContext.getCurrentInstance().responseComplete();
} catch (Exception e) {
imprimirErro(e.getMessage());
} finally {
try {
if (response.getOutputStream() != null) {
response.getOutputStream().close();
}
} catch (Exception e) {
imprimirErro(e.getMessage());
}
}
}
}
Alguém tem alguma sugestão sobre algum dos problemas?
Muito obrigado!