001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Anahide Tchertchian 018 */ 019package org.nuxeo.ecm.automation.jsf.operations; 020 021import java.io.IOException; 022import java.util.Collections; 023 024import javax.faces.context.ExternalContext; 025import javax.faces.context.FacesContext; 026import javax.servlet.http.HttpServletRequest; 027import javax.servlet.http.HttpServletResponse; 028 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031import org.nuxeo.ecm.automation.OperationContext; 032import org.nuxeo.ecm.automation.OperationException; 033import org.nuxeo.ecm.automation.core.Constants; 034import org.nuxeo.ecm.automation.core.annotations.Context; 035import org.nuxeo.ecm.automation.core.annotations.Operation; 036import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 037import org.nuxeo.ecm.core.api.Blob; 038import org.nuxeo.ecm.core.io.download.DownloadService; 039import org.nuxeo.ecm.platform.ui.web.auth.NXAuthConstants; 040import org.nuxeo.ecm.platform.ui.web.util.BaseURL; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * @author Anahide Tchertchian 045 */ 046@Operation(id = DownloadFile.ID, category = Constants.CAT_UI, requires = Constants.SEAM_CONTEXT, label = "Download file", description = "Download a file", aliases = { "Seam.DownloadFile" }) 047public class DownloadFile { 048 049 protected static Log log = LogFactory.getLog(DownloadFile.class); 050 051 public static final String ID = "WebUI.DownloadFile"; 052 053 @Context 054 protected OperationContext ctx; 055 056 @OperationMethod 057 public void run(Blob blob) throws OperationException, IOException { 058 if (blob == null) { 059 throw new OperationException("there is no file content available"); 060 } 061 DownloadService downloadService = Framework.getService(DownloadService.class); 062 String key = downloadService.storeBlobs(Collections.singletonList(blob)); 063 String url = BaseURL.getBaseURL() + downloadService.getDownloadUrl(key); 064 065 ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); 066 HttpServletRequest request = (HttpServletRequest) externalContext.getRequest(); 067 HttpServletResponse response = (HttpServletResponse) externalContext.getResponse(); 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 externalContext.redirect(url); 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 } 085 086}