List of usage examples for org.jfree.chart.servlet ServletUtilities sendTempFile
public static void sendTempFile(File file, HttpServletResponse response) throws IOException
From source file:org.opentaps.common.reporting.ChartViewHandler.java
public void render(String name, String page, String info, String contentType, String encoding, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException { /*/*from w ww . j a v a 2 s. c o m*/ * Looks for parameter "chart" first. Send this temporary image files * to client if it exists and return. */ String chartFileName = UtilCommon.getParameter(request, "chart"); if (UtilValidate.isNotEmpty(chartFileName)) { try { ServletUtilities.sendTempFile(chartFileName, response); if (chartFileName.indexOf(ServletUtilities.getTempOneTimeFilePrefix()) != -1) { // delete temporary file File file = new File(System.getProperty("java.io.tmpdir"), chartFileName); file.delete(); } } catch (IOException ioe) { Debug.logError(ioe.getLocalizedMessage(), module); } return; } /* * Next option eliminate need to store chart in file system. Some event handler * included in request chain prior to this view handler can prepare ready to use * instance of JFreeChart and place it to request attribute chartContext. * Currently chartContext should be Map<String, Object> and we expect to see in it: * "charObject" : JFreeChart * "width" : positive Integer * "height" : positive Integer * "encodeAlpha" : Boolean (optional) * "compressRatio" : Integer in range 0-9 (optional) */ Map<String, Object> chartContext = (Map<String, Object>) request.getAttribute("chartContext"); if (UtilValidate.isNotEmpty(chartContext)) { try { sendChart(chartContext, response); } catch (IOException ioe) { Debug.logError(ioe.getLocalizedMessage(), module); } return; } /* * Prepare context for next options */ Map<String, Object> callContext = FastMap.newInstance(); callContext.put("parameters", UtilHttp.getParameterMap(request)); callContext.put("delegator", request.getAttribute("delegator")); callContext.put("dispatcher", request.getAttribute("dispatcher")); callContext.put("userLogin", request.getSession().getAttribute("userLogin")); callContext.put("locale", UtilHttp.getLocale(request)); /* * view-map attribute "page" may contain BeanShell script in component * URL format that should return chartContext map. */ if (UtilValidate.isNotEmpty(page) && UtilValidate.isUrl(page) && page.endsWith(".bsh")) { try { chartContext = (Map<String, Object>) BshUtil.runBshAtLocation(page, callContext); if (UtilValidate.isNotEmpty(chartContext)) { sendChart(chartContext, response); } } catch (GeneralException ge) { Debug.logError(ge.getLocalizedMessage(), module); } catch (IOException ioe) { Debug.logError(ioe.getLocalizedMessage(), module); } return; } /* * As last resort we can decide that "page" attribute contains class name and "info" * contains method Map<String, Object> getSomeChart(Map<String, Object> context). * There are parameters, delegator, dispatcher, userLogin and locale in the context. * Should return chartContext. */ if (UtilValidate.isNotEmpty(page) && UtilValidate.isNotEmpty(info)) { Class handler = null; synchronized (this) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); try { handler = loader.loadClass(page); if (handler != null) { Method runMethod = handler.getMethod(info, new Class[] { Map.class }); chartContext = (Map<String, Object>) runMethod.invoke(null, callContext); if (UtilValidate.isNotEmpty(chartContext)) { sendChart(chartContext, response); } } } catch (ClassNotFoundException cnfe) { Debug.logError(cnfe.getLocalizedMessage(), module); } catch (SecurityException se) { Debug.logError(se.getLocalizedMessage(), module); } catch (NoSuchMethodException nsme) { Debug.logError(nsme.getLocalizedMessage(), module); } catch (IllegalArgumentException iae) { Debug.logError(iae.getLocalizedMessage(), module); } catch (IllegalAccessException iace) { Debug.logError(iace.getLocalizedMessage(), module); } catch (InvocationTargetException ite) { Debug.logError(ite.getLocalizedMessage(), module); } catch (IOException ioe) { Debug.logError(ioe.getLocalizedMessage(), module); } } } // Why you disturb me? throw new ViewHandlerException( "In order to generate chart you have to provide chart object or file name. There are no such data in request. Please read comments to ChartViewHandler class."); }
From source file:edu.sc.seis.receiverFunction.web.MyDisplayChart.java
/** * Service method.//from www.j a v a2 s .c om * * @param request the request. * @param response the response. * * @throws ServletException ??. * @throws IOException ??. */ public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); String filename = request.getParameter("filename"); if (filename == null) { throw new ServletException("Parameter 'filename' must be supplied"); } logger.debug("service :" + filename); // Replace ".." with "" // This is to prevent access to the rest of the file system filename = ServletUtilities.searchReplace(filename, "..", ""); // Check the file exists File file = new File(System.getProperty("java.io.tmpdir"), filename); if (!file.exists()) { throw new ServletException("File '" + file.getAbsolutePath() + "' does not exist"); } // Check that the graph being served was created by the current user // or that it begins with "public" boolean isChartInUserList = false; ChartDeleter chartDeleter = (ChartDeleter) session.getAttribute("JFreeChart_Deleter"); if (chartDeleter != null) { isChartInUserList = chartDeleter.isChartAvailable(filename); } boolean isChartPublic = false; if (filename.length() >= 6) { if (filename.substring(0, 6).equals("public")) { isChartPublic = true; } } boolean isOneTimeChart = false; if (filename.startsWith(ServletUtilities.getTempOneTimeFilePrefix())) { isOneTimeChart = true; } // // WARNING: HACK AHEAD!!!!! // // this is dumb, but since all ears charts are public and one time, just serve it // I think the upgrade to jetty8 caused the http to no longer have sessions, so // the default DisplayChart servlet had all three of // isChartInUserList || isChartPublic || isOneTimeChart false, and // so no image was served. Progress! :( isOneTimeChart = true; if (isChartInUserList || isChartPublic || isOneTimeChart) { // Serve it up logger.debug("sending " + file); ServletUtilities.sendTempFile(file, response); if (isOneTimeChart) { logger.debug("delelte " + file); file.delete(); } } else { logger.error("chart image not found " + filename); throw new ServletException("Chart image not found"); } return; }
From source file:nl.wur.plantbreeding.jfreechart.DisplayChart.java
/** * Service method./*from w ww . j av a 2 s.com*/ * * @param request the request. * @param response the response. * * @throws ServletException ??. * @throws IOException ??. */ @Override public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); String filename = request.getParameter("filename"); if (filename == null) { throw new ServletException("Parameter 'filename' must be supplied"); } // Replace ".." with "" // This is to prevent access to the rest of the file system filename = ServletUtilities.searchReplace(filename, "..", ""); // Check the file exists File file = new File(System.getProperty("java.io.tmpdir"), filename); if (!file.exists()) { throw new ServletException("File '" + file.getAbsolutePath() + "' does not exist"); } // Check that the graph being served was created by the current user // or that it begins with "public" //Fixme: set tot true to override. Like this, we can use the class to //show also our own images. This imposes likely a security thread. //Fix this for the future boolean isChartInUserList = true; ChartDeleter chartDeleter = (ChartDeleter) session.getAttribute("JFreeChart_Deleter"); if (chartDeleter != null) { isChartInUserList = chartDeleter.isChartAvailable(filename); } boolean isChartPublic = false; if (filename.length() >= 6) { if (filename.substring(0, 6).equals("public")) { isChartPublic = true; } //FIXME: override of security checks as plots are not always shown isChartPublic = true; } boolean isOneTimeChart = false; if (filename.startsWith(ServletUtilities.getTempOneTimeFilePrefix())) { isOneTimeChart = true; } LOG.log(Level.WARNING, "Displaychart: " + "ChartInUserList: {0} " + "ChartPublic: {1} " + "OneTimeChart: {2}", new Object[] { isChartInUserList, isChartPublic, isOneTimeChart }); if (isChartInUserList || isChartPublic || isOneTimeChart) { // Serve it up ServletUtilities.sendTempFile(file, response); if (isOneTimeChart) { file.delete(); } } else { throw new ServletException("Chart image not found"); } return; }