View Javadoc

1   /*
2    * JaspertReports JSF Plugin Copyright (C) 2011 A. Alonso Dominguez
3    *
4    * This library is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as published by
6    * the Free Software Foundation; either version 2.1 of the License, or (at
7    * your option) any later version. This library is distributed in the hope
8    * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9    * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   *
11   * See the GNU Lesser General Public License for more details. You should have
12   * received a copy of the GNU Lesser General Public License along with this
13   * library; if not, write to the Free Software Foundation, Inc., 59 Temple
14   * Place, Suite 330, Boston, MA 02111-1307 USA A.
15   *
16   * Alonso Dominguez
17   * alonsoft@users.sf.net
18   */
19  package net.sf.jasperreports.jsf.context;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.Enumeration;
27  import java.util.List;
28  
29  import javax.faces.context.ExternalContext;
30  import javax.servlet.ServletContext;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  import net.sf.jasperreports.jsf.Constants;
35  import net.sf.jasperreports.jsf.component.UIReport;
36  import net.sf.jasperreports.jsf.config.Configuration;
37  import net.sf.jasperreports.jsf.renderkit.ReportRenderer;
38  
39  /**
40   * Servlet implementation of the external context helper.
41   *
42   * @author A. Alonso Dominguez
43   */
44  final class ServletContextHelper extends ExternalContextHelper {
45  	
46      /**
47       * Private constructor to prevent instantiation.
48       */
49      protected ServletContextHelper() { }
50  
51      /**
52       * Creates a <code>ReportRenderRequest</code> based on the data code in the
53       * current ExternalContext.
54       *
55       * @param context the current ExternalContext
56       * @return A representation of the request render request
57       */
58      @Override
59      public ReportRenderRequest restoreReportRequest(
60              final ExternalContext context) {
61          final Configuration config = Configuration.getInstance(context);
62          final String viewId = context.getRequestParameterMap()
63                  .get(Constants.PARAM_VIEWID);
64          final String viewState = getViewCacheMap(context).get(viewId);
65  
66          HttpServletRequest request = (HttpServletRequest) context.getRequest();
67          request = new ReportHttpRenderRequest(request, viewId,
68                  config.getDefaultMapping(), viewState);
69          context.setRequest(request);
70          return (ReportRenderRequest) request;
71      }
72  
73      @Override
74      @SuppressWarnings("unchecked")
75      public Collection<ContentType> getAcceptedContentTypes(
76          final ExternalContext context) {
77      	final HttpServletRequest request = (HttpServletRequest)
78              context.getRequest();
79      	
80      	Enumeration<String> values = request.getHeaders("Accept");
81      	List<ContentType> list = new ArrayList<ContentType>();
82      	while (values.hasMoreElements()) {
83              ContentType type = new ContentType(values.nextElement());
84              list.add(type);
85      	}
86      	Collections.sort(list);
87      	
88      	return Collections.unmodifiableList(list);
89      }
90      
91      /**
92       * Obtains the server name of the current request.
93       *
94       * @param context the current ExternalContext
95       * @return the server name
96       */
97      @Override
98      public String getRequestServerName(final ExternalContext context) {
99          final HttpServletRequest request = (HttpServletRequest)
100                 context.getRequest();
101         return request.getServerName();
102     }
103 
104     /**
105      * Gets the request uri.
106      *
107      * @param context the context
108      * @return the request uri
109      */
110     @Override
111     public String getRequestURI(final ExternalContext context) {
112         final HttpServletRequest request = (HttpServletRequest)
113                 context.getRequest();
114         return request.getRequestURI();
115     }
116 
117     /**
118      * Obtains the real path name of the resource local to the current context.
119      *
120      * @param context the current ExternalContext
121      * @param name the resource name
122      * @return the resource real path name
123      */
124     @Override
125     public String getResourceRealPath(final ExternalContext context,
126             final String name) {
127         final ServletContext ctx = (ServletContext) context.getContext();
128         return ctx.getRealPath(name);
129     }
130 
131     /**
132      * Writes the report headers into the current response using the report
133      * renderer.
134      *
135      * @param context the current ExternalContext
136      * @param renderer the report renderer instance
137      * @param report the report component instance
138      * @throws IOException if any input or output error happens when writing
139      *         the report headers
140      */
141     @Override
142     public void writeHeaders(final ExternalContext context,
143             final ReportRenderer renderer, final UIReport report)
144             throws IOException {
145         final HttpServletResponse response = (HttpServletResponse)
146                 context.getResponse();
147         response.setHeader("Cache-Type", "no-cache");
148         response.setHeader("Expires", "0");
149 
150         if (report.getName() != null) {
151             response.setHeader("Content-Disposition", 
152                     renderer.encodeContentDisposition(report,
153                     response.getCharacterEncoding()));
154         }
155     }
156 
157     /**
158      * Writes the report contents into the context response.
159      *
160      * @param context the context
161      * @param contentType the content type
162      * @param data the data
163      * @throws IOException Signals that an I/O exception has occurred.
164      */
165     @Override
166     public void writeResponse(final ExternalContext context,
167             final ContentType contentType, final InputStream stream) 
168     throws IOException {
169         final HttpServletResponse response = (HttpServletResponse)
170                 context.getResponse();        
171         response.setContentType(contentType.toString());
172         
173         int contentLength = 0;
174         byte[] data = new byte[BUFFER_SIZE];
175         int bytesRead;
176         while (-1 != (bytesRead = stream.read(data))) {
177         	response.getOutputStream().write(data, 0, bytesRead);
178         	contentLength += bytesRead;
179         }
180         
181         response.setContentLength(contentLength);
182     }
183 }