List of usage examples for org.apache.wicket.request.resource SharedResourceReference getKey
public final Key getKey()
From source file:org.opensingular.form.wicket.mapper.attachment.DownloadSupportedBehavior.java
License:Apache License
/** * Registra um recurso compartilhado do wicket para permitir o download * sem bloquear a fila de ajax do wicket. * O recurso compartilhado removido to logo o download executado * Esse procedimento visa garantir que somente quem tem acesso pgina pode fazer * download dos arquivos./*from w w w . j a v a 2s. c o m*/ * * @param filename * @return */ private String getDownloadURL(String id, String filename) { String url = DOWNLOAD_PATH + "/" + id + "/" + new Date().getTime(); SharedResourceReference ref = new SharedResourceReference(String.valueOf(id)); AbstractResource resource = new AbstractResource() { @Override protected ResourceResponse newResourceResponse(Attributes attributes) { IAttachmentRef fileRef = findAttachmentRef(id); if (fileRef == null) { return new ResourceResponse().setStatusCode(HttpServletResponse.SC_NOT_FOUND); } ResourceResponse resourceResponse = new ResourceResponse(); if (fileRef.getSize() > 0) { resourceResponse.setContentLength(fileRef.getSize()); } resourceResponse.setFileName(filename); try { resourceResponse.setContentDisposition(contentDisposition); resourceResponse.setContentType(fileRef.getContentType()); resourceResponse.setWriteCallback(new WriteCallback() { @Override public void writeData(Attributes attributes) throws IOException { try (InputStream inputStream = fileRef.getContentAsInputStream()) { IOUtils.copy(inputStream, attributes.getResponse().getOutputStream()); /*Desregistrando recurso compartilhado*/ WebApplication.get().unmount(url); WebApplication.get().getSharedResources().remove(ref.getKey()); } catch (Exception e) { getLogger().error("Erro ao recuperar arquivo.", e); ((WebResponse) attributes.getResponse()) .setStatus(HttpServletResponse.SC_NOT_FOUND); resourceResponse.setStatusCode(HttpServletResponse.SC_NOT_FOUND); } } }); } catch (Exception e) { getLogger().error("Erro ao recuperar arquivo.", e); resourceResponse.setStatusCode(HttpServletResponse.SC_NOT_FOUND); } return resourceResponse; } }; /*registrando recurso compartilhado*/ WebApplication.get().getSharedResources().add(String.valueOf(id), resource); WebApplication.get().mountResource(url, ref); String path = WebApplication.get().getServletContext().getContextPath() + "/" + WebApplication.get().getWicketFilter().getFilterPath() + url; path = path.replaceAll("\\*", "").replaceAll("//", "/"); return path; }