View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.application;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.apache.myfaces.tobago.internal.application.ViewHandlerImpl;
25  import org.apache.myfaces.tobago.internal.webapp.TobagoResponse;
26  import org.apache.myfaces.tobago.util.DebugUtils;
27  import org.apache.myfaces.tobago.util.VariableResolverUtils;
28  
29  import javax.faces.FacesException;
30  import javax.faces.application.ViewHandler;
31  import javax.faces.component.UIViewRoot;
32  import javax.faces.context.FacesContext;
33  import javax.servlet.RequestDispatcher;
34  import javax.servlet.ServletException;
35  import javax.servlet.ServletResponse;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  import java.io.IOException;
39  
40  /*
41   * Date: Nov 7, 2006
42   * Time: 9:09:52 PM
43   */
44  public class FoViewHandlerImpl extends ViewHandlerImpl {
45  
46    private static final Logger LOG = LoggerFactory.getLogger(FoViewHandlerImpl.class);
47  
48    public FoViewHandlerImpl(ViewHandler base) {
49      super(base);
50    }
51  
52    public void renderView(FacesContext facesContext, UIViewRoot viewRoot)
53        throws IOException, FacesException {
54      String requestUri = viewRoot.getViewId();
55  
56      String contentType = VariableResolverUtils.resolveClientProperties(facesContext).getContentType();
57      if (LOG.isDebugEnabled()) {
58        LOG.debug("contentType = '" + contentType + "'");
59      }
60      if (contentType.indexOf("fo") == -1) {
61        // standard
62        super.renderView(facesContext, viewRoot);
63      } else {
64        try {
65          // TODO PortletResponse ??
66          if (facesContext.getExternalContext().getResponse() instanceof TobagoResponse) {
67            ((TobagoResponse) facesContext.getExternalContext().getResponse()).setBuffering();
68            // own dispatch
69            HttpServletRequest request = (HttpServletRequest)
70                facesContext.getExternalContext().getRequest();
71            HttpServletResponse response = (HttpServletResponse)
72                facesContext.getExternalContext().getResponse();
73            RequestDispatcher requestDispatcher
74                = request.getRequestDispatcher(requestUri);
75            requestDispatcher.include(request, response);
76            response.setContentType("application/pdf");
77            String buffer =
78                ((TobagoResponse) facesContext.getExternalContext().getResponse()).getBufferedString();
79            ServletResponse servletResponse = (ServletResponse)
80                facesContext.getExternalContext().getResponse();
81            if (LOG.isDebugEnabled()) {
82              LOG.debug("fo buffer: " + buffer);
83            }
84            FopConverter.fo2Pdf(servletResponse, buffer);
85          }
86        } catch (ServletException e) {
87          IOException ex = new IOException();
88          ex.initCause(e);
89          throw ex;
90        }
91      }
92      if (LOG.isDebugEnabled()) {
93        LOG.debug("VIEW");
94        LOG.debug(DebugUtils.toString(facesContext.getViewRoot(), 0));
95      }
96    }
97  
98  }