001/*
002 * (C) Copyright 2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *    Mariana Cedica
016 *
017 * $Id$
018 */
019package org.nuxeo.ecm.platform.routing.web.pdf;
020
021import static org.jboss.seam.ScopeType.EVENT;
022
023import java.io.IOException;
024import java.io.Serializable;
025
026import javax.faces.context.FacesContext;
027import javax.servlet.ServletRequest;
028import javax.servlet.ServletResponse;
029import javax.servlet.http.HttpServletRequest;
030import javax.servlet.http.HttpServletResponse;
031
032import org.jboss.seam.annotations.Name;
033import org.jboss.seam.annotations.Scope;
034import org.jboss.seam.core.Conversation;
035import org.nuxeo.ecm.platform.forms.layout.service.WebLayoutManager;
036import org.nuxeo.ecm.platform.ui.web.auth.NXAuthConstants;
037import org.nuxeo.ecm.platform.ui.web.util.BaseURL;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * Actions for generating a pdf for the current document route
042 *
043 * @author Mariana Cedica
044 */
045@Deprecated
046@Name("routeToPdfActionBean")
047@Scope(EVENT)
048public class RouteToPdfActionsBean implements Serializable {
049
050    private static final long serialVersionUID = 1L;
051
052    public void doRenderView(String view_name) throws IOException {
053        String base = BaseURL.getBaseURL(getHttpServletRequest());
054        String url = base + view_name + "?conversationId=" + Conversation.instance().getId();
055        /**
056         * hack needed for jboss 4
057         */
058        HttpServletResponse response = getHttpServletResponse();
059        response.resetBuffer();
060        response.sendRedirect(url);
061        response.flushBuffer();
062        getHttpServletRequest().setAttribute(NXAuthConstants.DISABLE_REDIRECT_REQUEST_KEY, Boolean.TRUE);
063        FacesContext.getCurrentInstance().responseComplete();
064    }
065
066    public int getLayoutColumnsCount(String layoutName) {
067        WebLayoutManager wlm = Framework.getService(WebLayoutManager.class);
068        return wlm.getLayoutDefinition(layoutName).getRows().length;
069    }
070
071    private static HttpServletResponse getHttpServletResponse() {
072        ServletResponse response = null;
073        final FacesContext facesContext = FacesContext.getCurrentInstance();
074        if (facesContext != null) {
075            response = (ServletResponse) facesContext.getExternalContext().getResponse();
076        }
077
078        if (response != null && response instanceof HttpServletResponse) {
079            return (HttpServletResponse) response;
080        }
081        return null;
082    }
083
084    private static HttpServletRequest getHttpServletRequest() {
085        ServletRequest request = null;
086        final FacesContext facesContext = FacesContext.getCurrentInstance();
087        if (facesContext != null) {
088            request = (ServletRequest) facesContext.getExternalContext().getRequest();
089        }
090
091        if (request != null && request instanceof HttpServletRequest) {
092            return (HttpServletRequest) request;
093        }
094        return null;
095    }
096}