PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/jasperreports/src/net/sf/jasperreports/web/servlets/ReportContextCreatorServlet.java

https://gitlab.com/samuel-davis/JasperReports-OSGI
Java | 118 lines | 67 code | 21 blank | 30 comment | 11 complexity | ec9124e9eb04a748ffd64d62a94b3ac5 MD5 | raw file
  1. /*
  2. * JasperReports - Free Java Reporting Library.
  3. * Copyright (C) 2001 - 2016 TIBCO Software Inc. All rights reserved.
  4. * http://www.jaspersoft.com
  5. *
  6. * Unless you have purchased a commercial license agreement from Jaspersoft,
  7. * the following license terms apply:
  8. *
  9. * This program is part of JasperReports.
  10. *
  11. * JasperReports is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * JasperReports is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. package net.sf.jasperreports.web.servlets;
  25. import java.io.IOException;
  26. import java.io.PrintWriter;
  27. import javax.servlet.ServletException;
  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.http.HttpServletResponse;
  30. import net.sf.jasperreports.engine.JRConstants;
  31. import net.sf.jasperreports.engine.JRException;
  32. import net.sf.jasperreports.engine.JRRuntimeException;
  33. import net.sf.jasperreports.engine.ReportContext;
  34. import net.sf.jasperreports.engine.util.JRStringUtil;
  35. import net.sf.jasperreports.web.JRInteractiveException;
  36. import net.sf.jasperreports.web.WebReportContext;
  37. import net.sf.jasperreports.web.util.WebUtil;
  38. import org.apache.commons.logging.Log;
  39. import org.apache.commons.logging.LogFactory;
  40. /**
  41. * @author Narcis Marcu(nmarcu@users.sourceforge.net)
  42. */
  43. public class ReportContextCreatorServlet extends AbstractServlet {
  44. private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
  45. private static final Log log = LogFactory.getLog(ReportContextCreatorServlet.class);
  46. @Override
  47. public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
  48. response.setContentType(JSON_CONTENT_TYPE);
  49. setNoExpire(response);
  50. String reportUri = request.getParameter(WebUtil.REQUEST_PARAMETER_REPORT_URI);
  51. PrintWriter out = response.getWriter();
  52. // get the contextid and run the report
  53. if (reportUri != null && request.getHeader("accept").indexOf(JSON_ACCEPT_HEADER) >= 0) {
  54. WebReportContext webReportContext = WebReportContext.getInstance(request);
  55. // begin: run report
  56. JasperPrintAccessor jasperPrintAccessor = (JasperPrintAccessor) webReportContext.getParameterValue(WebReportContext.REPORT_CONTEXT_PARAMETER_JASPER_PRINT_ACCESSOR);
  57. if (jasperPrintAccessor == null) {
  58. webReportContext.setParameterValue(WebUtil.REQUEST_PARAMETER_REPORT_URI, reportUri);
  59. String async = request.getParameter(WebUtil.REQUEST_PARAMETER_ASYNC_REPORT);
  60. if (async != null) {
  61. webReportContext.setParameterValue(WebUtil.REQUEST_PARAMETER_ASYNC_REPORT, Boolean.valueOf(async));
  62. }
  63. String appDomain = request.getParameter(ReportContext.REQUEST_PARAMETER_APPLICATION_DOMAIN);
  64. if (appDomain != null) {
  65. if (appDomain.endsWith("/")) {
  66. appDomain = appDomain.substring(0, appDomain.length() - 1);
  67. }
  68. webReportContext.setParameterValue(ReportContext.REQUEST_PARAMETER_APPLICATION_DOMAIN, appDomain);
  69. }
  70. Controller controller = new Controller(getJasperReportsContext());
  71. initWebContext(request, webReportContext);
  72. try {
  73. controller.runReport(webReportContext, null);
  74. } catch (JRInteractiveException e) {
  75. // there's no action running at this point, so nothing to do
  76. log.error("Error on report execution", e);
  77. throw new JRRuntimeException(e);
  78. } catch (JRException e) {
  79. log.error("Error on report execution", e);
  80. response.setStatus(404);
  81. out.println("{\"msg\": \"JasperReports encountered an error on context creation!\",");
  82. out.println("\"devmsg\": \"" + JRStringUtil.escapeJavaStringLiteral(e.getMessage()) + "\"}");
  83. return;
  84. } catch (Throwable t) {
  85. t.printStackTrace();
  86. }
  87. }
  88. // end: run report
  89. out.println("{\"contextid\": " + webReportContext.getId() + "}");
  90. } else {
  91. response.setStatus(400);
  92. out.println("{\"msg\": \"Wrong parameters!\"}");
  93. }
  94. }
  95. protected void initWebContext(HttpServletRequest request, WebReportContext webReportContext) {
  96. }
  97. }