001/*
002 * (C) Copyright 2010 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 *    Mariana Cedica
018 *
019 * $Id$
020 */
021package org.nuxeo.ecm.platform.routing.web.pdf;
022
023import static org.jboss.seam.ScopeType.EVENT;
024
025import java.io.IOException;
026import java.io.Serializable;
027
028import javax.faces.context.FacesContext;
029import javax.servlet.ServletRequest;
030import javax.servlet.ServletResponse;
031import javax.servlet.http.HttpServletRequest;
032import javax.servlet.http.HttpServletResponse;
033
034import org.jboss.seam.annotations.Name;
035import org.jboss.seam.annotations.Scope;
036import org.jboss.seam.core.Conversation;
037import org.nuxeo.ecm.platform.forms.layout.service.WebLayoutManager;
038import org.nuxeo.ecm.platform.ui.web.auth.NXAuthConstants;
039import org.nuxeo.ecm.platform.ui.web.util.BaseURL;
040import org.nuxeo.runtime.api.Framework;
041
042/**
043 * Actions for generating a pdf for the current document route
044 *
045 * @author Mariana Cedica
046 */
047@Deprecated
048@Name("routeToPdfActionBean")
049@Scope(EVENT)
050public class RouteToPdfActionsBean implements Serializable {
051
052    private static final long serialVersionUID = 1L;
053
054    public void doRenderView(String view_name) throws IOException {
055        String base = BaseURL.getBaseURL(getHttpServletRequest());
056        String url = base + view_name + "?conversationId=" + Conversation.instance().getId();
057        /**
058         * hack needed for jboss 4
059         */
060        HttpServletResponse response = getHttpServletResponse();
061        response.resetBuffer();
062        response.sendRedirect(url);
063        response.flushBuffer();
064        getHttpServletRequest().setAttribute(NXAuthConstants.DISABLE_REDIRECT_REQUEST_KEY, Boolean.TRUE);
065        FacesContext.getCurrentInstance().responseComplete();
066    }
067
068    public int getLayoutColumnsCount(String layoutName) {
069        WebLayoutManager wlm = Framework.getService(WebLayoutManager.class);
070        return wlm.getLayoutDefinition(layoutName).getRows().length;
071    }
072
073    private static HttpServletResponse getHttpServletResponse() {
074        ServletResponse response = null;
075        final FacesContext facesContext = FacesContext.getCurrentInstance();
076        if (facesContext != null) {
077            response = (ServletResponse) facesContext.getExternalContext().getResponse();
078        }
079
080        if (response != null && response instanceof HttpServletResponse) {
081            return (HttpServletResponse) response;
082        }
083        return null;
084    }
085
086    private static HttpServletRequest getHttpServletRequest() {
087        ServletRequest request = null;
088        final FacesContext facesContext = FacesContext.getCurrentInstance();
089        if (facesContext != null) {
090            request = (ServletRequest) facesContext.getExternalContext().getRequest();
091        }
092
093        if (request != null && request instanceof HttpServletRequest) {
094            return (HttpServletRequest) request;
095        }
096        return null;
097    }
098}