001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Anahide Tchertchian 011 */ 012package org.nuxeo.ecm.automation.jsf.operations; 013 014import java.io.IOException; 015import java.util.UUID; 016 017import javax.faces.context.ExternalContext; 018import javax.faces.context.FacesContext; 019import javax.servlet.http.HttpServletRequest; 020import javax.servlet.http.HttpServletResponse; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.nuxeo.ecm.automation.OperationContext; 025import org.nuxeo.ecm.automation.OperationException; 026import org.nuxeo.ecm.automation.core.Constants; 027import org.nuxeo.ecm.automation.core.annotations.Context; 028import org.nuxeo.ecm.automation.core.annotations.Operation; 029import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 030import org.nuxeo.ecm.core.api.Blob; 031import org.nuxeo.ecm.core.io.download.DownloadService; 032import org.nuxeo.ecm.platform.ui.web.auth.NXAuthConstants; 033import org.nuxeo.ecm.platform.ui.web.tag.fn.Functions; 034import org.nuxeo.ecm.platform.ui.web.util.BaseURL; 035import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils; 036 037/** 038 * @author Anahide Tchertchian 039 */ 040@Operation(id = DownloadFile.ID, category = Constants.CAT_UI, requires = Constants.SEAM_CONTEXT, label = "Download file", description = "Download a file", aliases = { "Seam.DownloadFile" }) 041public class DownloadFile { 042 043 protected static Log log = LogFactory.getLog(DownloadFile.class); 044 045 public static final String ID = "WebUI.DownloadFile"; 046 047 @Context 048 protected OperationContext ctx; 049 050 @OperationMethod 051 public void run(Blob blob) throws OperationException, IOException { 052 if (blob == null) { 053 throw new OperationException("there is no file content available"); 054 } 055 056 String filename = blob.getFilename(); 057 if (blob.getLength() > Functions.getBigFileSizeLimit()) { 058 059 ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); 060 HttpServletRequest request = (HttpServletRequest) externalContext.getRequest(); 061 HttpServletResponse response = (HttpServletResponse) externalContext.getResponse(); 062 063 String sid = UUID.randomUUID().toString(); 064 request.getSession(true).setAttribute(sid, blob); 065 066 String bigDownloadURL = BaseURL.getBaseURL(request); 067 bigDownloadURL += DownloadService.NXBIGBLOB + "/" + sid; 068 069 try { 070 // Operation was probably triggered by a POST 071 // so we need to de-activate the ResponseWrapper that would 072 // rewrite the URL 073 request.setAttribute(NXAuthConstants.DISABLE_REDIRECT_REQUEST_KEY, new Boolean(true)); 074 // send the redirect 075 response.sendRedirect(bigDownloadURL); 076 // mark all JSF processing as completed 077 response.flushBuffer(); 078 FacesContext.getCurrentInstance().responseComplete(); 079 // set Outcome to null (just in case) 080 ctx.getVars().put("Outcome", null); 081 } catch (IOException e) { 082 log.error("Error while redirecting for big blob downloader", e); 083 } 084 } else { 085 ComponentUtils.download(null, null, blob, filename, "operation"); 086 } 087 } 088 089}