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.UUID;
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.tag.fn.Functions;
041import org.nuxeo.ecm.platform.ui.web.util.BaseURL;
042import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils;
043
044/**
045 * @author Anahide Tchertchian
046 */
047@Operation(id = DownloadFile.ID, category = Constants.CAT_UI, requires = Constants.SEAM_CONTEXT, label = "Download file", description = "Download a file", aliases = { "Seam.DownloadFile" })
048public class DownloadFile {
049
050    protected static Log log = LogFactory.getLog(DownloadFile.class);
051
052    public static final String ID = "WebUI.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
063        String filename = blob.getFilename();
064        if (blob.getLength() > Functions.getBigFileSizeLimit()) {
065
066            ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
067            HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
068            HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
069
070            String sid = UUID.randomUUID().toString();
071            request.getSession(true).setAttribute(sid, blob);
072
073            String bigDownloadURL = BaseURL.getBaseURL(request);
074            bigDownloadURL += DownloadService.NXBIGBLOB + "/" + sid;
075
076            try {
077                // Operation was probably triggered by a POST
078                // so we need to de-activate the ResponseWrapper that would
079                // rewrite the URL
080                request.setAttribute(NXAuthConstants.DISABLE_REDIRECT_REQUEST_KEY, new Boolean(true));
081                // send the redirect
082                response.sendRedirect(bigDownloadURL);
083                // mark all JSF processing as completed
084                response.flushBuffer();
085                FacesContext.getCurrentInstance().responseComplete();
086                // set Outcome to null (just in case)
087                ctx.getVars().put("Outcome", null);
088            } catch (IOException e) {
089                log.error("Error while redirecting for big blob downloader", e);
090            }
091        } else {
092            ComponentUtils.download(null, null, blob, filename, "operation");
093        }
094    }
095
096}