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 = { 047 "WebUI.DownloadFile" }) 048public class DownloadFile { 049 050 protected static Log log = LogFactory.getLog(DownloadFile.class); 051 052 public static final String ID = "Seam.DownloadFile"; 053 054 @Context 055 protected OperationContext ctx; 056 057 @OperationMethod 058 public void run(Blob blob) throws OperationException, IOException { 059 if (blob == null) { 060 throw new OperationException("there is no file content available"); 061 } 062 DownloadService downloadService = Framework.getService(DownloadService.class); 063 String key = downloadService.storeBlobs(Collections.singletonList(blob)); 064 String url = BaseURL.getBaseURL() + downloadService.getDownloadUrl(key); 065 066 ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); 067 HttpServletRequest request = (HttpServletRequest) externalContext.getRequest(); 068 HttpServletResponse response = (HttpServletResponse) externalContext.getResponse(); 069 070 try { 071 // Operation was probably triggered by a POST 072 // so we need to de-activate the ResponseWrapper that would 073 // rewrite the URL 074 request.setAttribute(NXAuthConstants.DISABLE_REDIRECT_REQUEST_KEY, new Boolean(true)); 075 // send the redirect 076 externalContext.redirect(url); 077 // mark all JSF processing as completed 078 response.flushBuffer(); 079 FacesContext.getCurrentInstance().responseComplete(); 080 // set Outcome to null (just in case) 081 ctx.getVars().put("Outcome", null); 082 } catch (IOException e) { 083 log.error("Error while redirecting for big blob downloader", e); 084 } 085 } 086 087}