Java tutorial
/* * This file is licensed to the Toobs Framework Group under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The Toobs Framework Group licenses this file to You under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of the * License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.toobsframework.pres.chart.controller; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.jfree.chart.ChartRenderingInfo; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.imagemap.ImageMapUtilities; import org.jfree.chart.imagemap.StandardURLTagFragmentGenerator; import org.jfree.chart.imagemap.URLTagFragmentGenerator; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.util.WebUtils; import org.toobsframework.pres.base.HandlerBase; import org.toobsframework.pres.chart.ChartBuilder; import org.toobsframework.pres.chart.ChartDefinition; import org.toobsframework.pres.chart.ChartException; import org.toobsframework.pres.chart.ChartNotFoundException; import org.toobsframework.pres.chart.manager.IChartManager; import org.toobsframework.pres.url.UrlDispatchInfo; import org.toobsframework.pres.util.ComponentRequestManager; import org.toobsframework.util.Configuration; import org.toobsframework.util.IRequest; public class ChartHandler extends HandlerBase implements IChartHandler, BeanFactoryAware { private IChartManager chartManager = null; private ChartBuilder chartBuilder = null; private ComponentRequestManager componentRequestManager = null; private BeanFactory beanFactory; /** * * Retrieves the URL path to use for lookup and delegates to * <code>getViewNameForUrlPath</code>. * * @throws Exception Exception fetching or rendering component. * @see #getViewNameForUrlPath * */ @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response, UrlDispatchInfo dispatchInfo) throws Exception { String chartId = dispatchInfo.getResourceId(); if (log.isDebugEnabled()) { log.debug("Rendering chart '" + chartId + "' for lookup path: " + dispatchInfo.getOriginalPath()); } IRequest componentRequest = this.setupComponentRequest(dispatchInfo, request, response, true); Date startTime = null; if (log.isDebugEnabled()) { startTime = new Date(); } JFreeChart chart = null; int height = 400; int width = 600; ChartDefinition chartDef = null; if (null != chartId && !chartId.equals("")) { try { request.setAttribute("chartId", chartId); chartDef = this.chartManager.getChartDefinition(chartId); } catch (ChartNotFoundException cnfe) { log.warn("Chart " + chartId + " not found."); throw cnfe; } try { chart = chartBuilder.build(chartDef, componentRequest); width = chartDef.getChartWidth(); height = chartDef.getChartHeight(); } catch (ChartException e) { Throwable t = e.rootCause(); log.info("Root cause " + t.getClass().getName() + " " + t.getMessage()); throw e; } catch (Exception e) { throw e; } finally { this.componentRequestManager.unset(); } } else { throw new Exception("No chartId specified"); } //Write out to the response. response.setHeader("Pragma", "no-cache"); // HTTP 1.0 response.setHeader("Cache-Control", "max-age=0, must-revalidate"); // HTTP 1.1 ChartRenderingInfo chartRenderingInfo = new ChartRenderingInfo(); if (chartDef.doImageWithMap()) { response.setContentType("text/html; charset=UTF-8"); String genFileName = chartDef.getId() + "-" + new Date().getTime() + ".png"; String imageOutputFileName = configuration.getUploadDir() + genFileName; File imageOutputFile = new File(imageOutputFileName); OutputStream os = null; try { os = new FileOutputStream(imageOutputFile); ChartUtilities.writeChartAsPNG(os, chart, width, height, chartRenderingInfo); } finally { if (os != null) { os.close(); } } PrintWriter writer = response.getWriter(); StringBuffer sb = new StringBuffer(); // TODO BUGBUG Chart location needs to fixed sb.append("<img id=\"chart-").append(chartDef.getId()).append("\" src=\"") .append(/*Configuration.getInstance().getMainContext() +*/ "/upload/" + genFileName) .append("\" ismap=\"ismap\" usemap=\"#").append(chartDef.getId()).append("Map\" />"); URLTagFragmentGenerator urlGenerator; if (chartDef.getUrlFragmentBean() != null) { urlGenerator = (URLTagFragmentGenerator) beanFactory.getBean(chartDef.getUrlFragmentBean()); } else { urlGenerator = new StandardURLTagFragmentGenerator(); } sb.append(ImageMapUtilities.getImageMap(chartDef.getId() + "Map", chartRenderingInfo, null, urlGenerator)); writer.print(sb.toString()); writer.flush(); } else { response.setContentType("image/png"); ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, width, height, chartRenderingInfo); } if (log.isDebugEnabled()) { Date endTime = new Date(); log.debug("Time [" + chartId + "] - " + (endTime.getTime() - startTime.getTime())); } return null; } /** * Extract the URL filename from the given request URI. * Delegates to <code>WebUtils.extractViewNameFromUrlPath(String)</code>. * @param uri the request URI (e.g. "/index.html") * @return the extracted URI filename (e.g. "index") * @see org.springframework.web.util.WebUtils#extractFilenameFromUrlPath */ protected String extractViewNameFromUrlPath(String uri) { return WebUtils.extractFilenameFromUrlPath(uri); } public IChartManager getChartManager() { return chartManager; } public void setChartManager(IChartManager chartManager) { this.chartManager = chartManager; } public ComponentRequestManager getComponentRequestManager() { return componentRequestManager; } public void setComponentRequestManager(ComponentRequestManager componentRequestManager) { this.componentRequestManager = componentRequestManager; } public ChartBuilder getChartBuilder() { return chartBuilder; } public void setChartBuilder(ChartBuilder chartBuilder) { this.chartBuilder = chartBuilder; } public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; } }