net.sf.jasperreports.web.WebReportContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jasperreports Show documentation
Show all versions of jasperreports Show documentation
Free Java Reporting Library
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JasperReports is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JasperReports. If not, see .
*/
package net.sf.jasperreports.web;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.ReportContext;
/**
* @author Teodor Danciu ([email protected])
*/
public class WebReportContext implements ReportContext
{
/**
*
*/
private static final String SESSION_ATTRIBUTE_REPORT_CONTEXT_ID_PREFIX = "net.sf.jasperreports.web.report.context_";
public static final String REQUEST_PARAMETER_REPORT_CONTEXT_ID = "jr_ctxid";
public static final String REPORT_CONTEXT_PARAMETER_JASPER_PRINT_ACCESSOR = "net.sf.jasperreports.web.jasper_print.accessor";
//public static final String REPORT_CONTEXT_PARAMETER_JASPER_REPORT = "net.sf.jasperreports.web.jasper_report";
public static final String APPLICATION_CONTEXT_PATH = "net.sf.jasperreports.web.app.context.path";
/**
*
*/
//private ThreadLocal threadLocalRequest = new ThreadLocal();//FIXMEJIVE
private Map requestParameters = new HashMap();
private Map parameterValues;
private String id;
/**
*
*/
public static final WebReportContext getInstance(HttpServletRequest request)
{
return getInstance(request, true);
}
public static final WebReportContext getInstance(HttpServletRequest request, String reportContextId) {
return getInstance(request, reportContextId, true);
}
public static final WebReportContext getInstance(HttpServletRequest request, boolean create) {
return getInstance(request, null, create);
}
/**
*
*/
public static final WebReportContext getInstance(HttpServletRequest request, String reportContextId, boolean create)
{
WebReportContext webReportContext = null;
if (reportContextId == null) {
reportContextId = request.getParameter(REQUEST_PARAMETER_REPORT_CONTEXT_ID);
}
if (reportContextId != null)
{
webReportContext = (WebReportContext)request.getSession().getAttribute(getSessionAttributeName(reportContextId));
}
if (webReportContext == null && create)
{
webReportContext = new WebReportContext();
request.getSession().setAttribute(webReportContext.getSessionAttributeName(), webReportContext); //FIXMEJIVE use FIFO accessor
}
if (webReportContext != null)
{
//webReportContext.setRequest(request);
webReportContext.setParameterValue(APPLICATION_CONTEXT_PATH, request.getContextPath());
webReportContext.setParameterValue(JRParameter.REPORT_CONTEXT, webReportContext);
}
return webReportContext;
}
/**
*
*/
private WebReportContext()
{
parameterValues = new HashMap();
id = UUID.randomUUID().toString();
// parameterValues.put(JRParameter.REPORT_CONTEXT, this);
}
@Override
public String getId()
{
return id;
}
/**
*
*
public void setRequest(HttpServletRequest request)
{
//threadLocalRequest.set(request);
requestParameters.clear();
for(@SuppressWarnings("unchecked") Enumeration params = request.getParameterNames();
params.hasMoreElements(); )
{
String param = params.nextElement();
String value = request.getParameter(param);// do getValues here?
requestParameters.put(param, value);
}
}
/**
*
*/
public String getSessionAttributeName()
{
return getSessionAttributeName(getId());
}
@Override
public Object getParameterValue(String parameterName)
{
String requestParameterValue = requestParameters.get(parameterName);
if (requestParameterValue != null)
{
return requestParameterValue;
}
return parameterValues.get(parameterName);
}
@Override
public boolean containsParameter(String parameterName)
{
String requestParameterValue = requestParameters.get(parameterName);
if (requestParameterValue != null)
{
return true;
}
return parameterValues.containsKey(parameterName);
}
@Override
public void setParameterValue(String parameterName, Object value)
{
parameterValues.put(parameterName, value);
}
/**
*
*/
public void setParameterValues(Map newValues)
{
parameterValues.putAll(newValues);
}
@Override
public Map getParameterValues()
{
return parameterValues;
}
/**
*
*/
private static final String getSessionAttributeName(String id)
{
return SESSION_ATTRIBUTE_REPORT_CONTEXT_ID_PREFIX + id;
}
@Override
public Object removeParameterValue(String parameterName)
{
return parameterValues.remove(parameterName);
}
@Override
public void clearParameterValues()
{
parameterValues.clear();
}
}