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.util.Collections;
22 import java.util.Enumeration;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.faces.render.ResponseStateManager;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletRequestWrapper;
29 import net.sf.jasperreports.jsf.Constants;
30
31 import net.sf.jasperreports.jsf.util.Util;
32
33 /**
34 * HTTP implementation of a <tt>ReportRenderRequest</tt>.
35 *
36 * @author A. Alonso Dominguez
37 */
38 final class ReportHttpRenderRequest extends HttpServletRequestWrapper
39 implements ReportRenderRequest {
40
41 /** Current request path info. */
42 private String pathInfo;
43 /** Current request servlet path. */
44 private String servletPath;
45 /** Current request view id. */
46 private String viewId;
47 /** View state to be restored. */
48 private String viewState;
49
50 /**
51 * Instantiates a new HTTP report render request.
52 *
53 * @param request HTTP request to wrap around.
54 * @param viewId view id to be simulated.
55 * @param facesMapping request faces mapping.
56 * @param viewState view state to restore.
57 */
58 public ReportHttpRenderRequest(final HttpServletRequest request,
59 final String viewId, final String facesMapping,
60 final String viewState) {
61 super(request);
62 this.viewId = viewId;
63 this.viewState = viewState;
64 reverseEngineerPaths(facesMapping);
65 }
66
67 /**
68 * Overriden version to allow obtaining the view state
69 * as a list of parameters.
70 *
71 * @param name the parameter name.
72 * @return the parameter value.
73 */
74 @Override
75 public String getParameter(final String name) {
76 String[] values = getParameterValues(name);
77 if (values == null || values.length == 0) {
78 return null;
79 } else {
80 return values[0];
81 }
82 }
83
84 /**
85 * Overriden version of the parameter map containing the view state.
86 *
87 * @return the parameter map.
88 */
89 @Override
90 @SuppressWarnings("unchecked")
91 public Map<String, String[]> getParameterMap() {
92 Map<String, String[]> paramMap = new HashMap<String, String[]>();
93 paramMap.putAll(super.getParameterMap());
94 paramMap.put(ResponseStateManager.VIEW_STATE_PARAM,
95 new String[]{viewState});
96 return Collections.unmodifiableMap(paramMap);
97 }
98
99 /**
100 * Overriden version of the parameter names enumerator.
101 *
102 * @return an enumeration of the parameter names.
103 */
104 @Override
105 public Enumeration<String> getParameterNames() {
106 return Collections.enumeration(getParameterMap().keySet());
107 }
108
109 /**
110 * Overriden version for obtaining an array of parameter values.
111 *
112 * @param name the parameter name.
113 * @return the parameter values.
114 */
115 @Override
116 public String[] getParameterValues(final String name) {
117 return getParameterMap().get(name);
118 }
119
120 /**
121 * Obtains the request path info.
122 *
123 * @return the request path info.
124 */
125 @Override
126 public String getPathInfo() {
127 return pathInfo;
128 }
129
130 /**
131 * Obtains the report component client id.
132 *
133 * @return the report component client id.
134 */
135 public String getReportClientId() {
136 return getRequest().getParameter(Constants.PARAM_CLIENTID);
137 }
138
139 /**
140 * Obtains the request servlet path.
141 *
142 * @return the request servlet path.
143 */
144 @Override
145 public String getServletPath() {
146 return servletPath;
147 }
148
149 /**
150 * Obtains the report view id.
151 *
152 * @return the report view id.
153 */
154 public String getViewId() {
155 return viewId;
156 }
157
158 /**
159 * Releases internal local variables.
160 */
161 public void release() {
162 servletPath = null;
163 pathInfo = null;
164 }
165
166 /**
167 * Method designed to guess the path info & servlet path data
168 * from the view id and the faces mapping.
169 *
170 * @param facesMapping application default faces' mapping.
171 */
172 private void reverseEngineerPaths(final String facesMapping) {
173 if (Util.isPrefixMapped(facesMapping)) {
174 int i = facesMapping.indexOf("/*");
175 if (i != -1) {
176 servletPath = facesMapping.substring(0, i);
177 } else {
178 servletPath = facesMapping;
179 }
180 pathInfo = viewId;
181 } else {
182 servletPath = viewId.substring(0, viewId.lastIndexOf('.'))
183 + facesMapping.substring(facesMapping.indexOf('.'));
184 pathInfo = null;
185 }
186 }
187 }