franta-hg@1: /* ================================================================ franta-hg@1: * Cewolf : Chart enabling Web Objects Framework franta-hg@1: * ================================================================ franta-hg@1: * franta-hg@1: * Project Info: http://cewolf.sourceforge.net franta-hg@1: * Project Lead: Guido Laures (guido@laures.de); franta-hg@1: * franta-hg@1: * (C) Copyright 2002, by Guido Laures franta-hg@1: * franta-hg@1: * This library is free software; you can redistribute it and/or modify it under the terms franta-hg@1: * of the GNU Lesser General Public License as published by the Free Software Foundation; franta-hg@1: * either version 2.1 of franta-hg@1: * the License, or (at your option) any later version. franta-hg@1: * franta-hg@1: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; franta-hg@1: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. franta-hg@1: * See the GNU Lesser General Public License for more details. franta-hg@1: * franta-hg@1: * You should have received a copy of the GNU Lesser General Public License along with this franta-hg@1: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, franta-hg@1: * Boston, MA 02111-1307, USA. franta-hg@1: */ franta-hg@1: franta-hg@1: package de.laures.cewolf.taglib.tags; franta-hg@1: franta-hg@1: import java.io.IOException; franta-hg@1: import java.io.Writer; franta-hg@1: franta-hg@1: import javax.servlet.http.HttpServletRequest; franta-hg@1: import javax.servlet.http.HttpServletResponse; franta-hg@1: import javax.servlet.jsp.JspException; franta-hg@1: import javax.servlet.jsp.JspWriter; franta-hg@1: import javax.servlet.jsp.PageContext; franta-hg@1: import javax.servlet.jsp.tagext.BodyContent; franta-hg@1: franta-hg@1: import de.laures.cewolf.CewolfException; franta-hg@1: import de.laures.cewolf.ChartHolder; franta-hg@1: import de.laures.cewolf.ChartImage; franta-hg@1: import de.laures.cewolf.Configuration; franta-hg@1: import de.laures.cewolf.Storage; franta-hg@1: import de.laures.cewolf.WebConstants; franta-hg@1: import de.laures.cewolf.taglib.ChartImageDefinition; franta-hg@1: import de.laures.cewolf.taglib.TaglibConstants; franta-hg@1: import de.laures.cewolf.taglib.html.HTMLImgTag; franta-hg@1: import de.laures.cewolf.taglib.util.MIMEExtensionHelper; franta-hg@1: import de.laures.cewolf.taglib.util.PageUtils; franta-hg@1: franta-hg@1: /** franta-hg@1: * This is the tag implementation of the <img> tag. This tag inputs the franta-hg@1: * proper <img> tag into the HTML page delivered to the client. It franta-hg@1: * therefor determines the chart ID which will be used by the rendering servlet franta-hg@1: * to retrieve the chart. franta-hg@1: * franta-hg@1: * @author glaures franta-hg@1: * @see de.laures.cewolf.ChartImage franta-hg@1: */ franta-hg@1: public class ChartImgTag extends HTMLImgTag implements CewolfRootTag, Mapped, TaglibConstants, WebConstants franta-hg@1: { franta-hg@1: franta-hg@1: private static final String DEFAULT_MIME_TYPE = MIME_PNG; franta-hg@1: private static final String TAG_NAME_SVG = "EMBED"; franta-hg@1: private static final int DEFAULT_TIMEOUT = 300; franta-hg@1: franta-hg@1: private String chartId = null; franta-hg@1: private String renderer; franta-hg@1: private String mimeType = DEFAULT_MIME_TYPE; franta-hg@1: private int timeout = DEFAULT_TIMEOUT; franta-hg@1: protected String sessionKey = null; franta-hg@1: franta-hg@1: private ChartImageDefinition chartImageDefinition; franta-hg@1: franta-hg@1: public int doStartTag() throws JspException franta-hg@1: { franta-hg@1: final ChartHolder chartHolder = PageUtils.getChartHolder(chartId, pageContext); franta-hg@1: this.chartImageDefinition = new ChartImageDefinition(chartHolder, width, height, ChartImage.IMG_TYPE_CHART, mimeType,timeout); franta-hg@1: Storage storage = Configuration.getInstance(pageContext.getServletContext()).getStorage(); franta-hg@1: try franta-hg@1: { franta-hg@1: this.sessionKey = storage.storeChartImage(chartImageDefinition, pageContext); franta-hg@1: } franta-hg@1: catch (CewolfException cwex) franta-hg@1: { franta-hg@1: throw new JspException(cwex.getMessage()); franta-hg@1: } franta-hg@1: return EVAL_PAGE; franta-hg@1: } franta-hg@1: franta-hg@1: public int doAfterBody() throws JspException franta-hg@1: { franta-hg@1: try franta-hg@1: { franta-hg@1: // double checking for null as Resin had problems with that franta-hg@1: final BodyContent body = getBodyContent(); franta-hg@1: if ( body != null ) franta-hg@1: { franta-hg@1: final JspWriter writer = getPreviousOut(); franta-hg@1: if ( writer != null ) franta-hg@1: { franta-hg@1: body.writeOut(writer); franta-hg@1: } franta-hg@1: } franta-hg@1: franta-hg@1: } franta-hg@1: catch (IOException ioex) franta-hg@1: { franta-hg@1: log.error(ioex); franta-hg@1: throw new JspException(ioex.getMessage()); franta-hg@1: } franta-hg@1: return SKIP_BODY; franta-hg@1: } franta-hg@1: franta-hg@1: public int doEndTag() throws JspException franta-hg@1: { franta-hg@1: super.doStartTag(); franta-hg@1: final StringBuffer buffer = new StringBuffer(" src=\""); franta-hg@1: buffer.append(getImgURL()); franta-hg@1: buffer.append("\""); franta-hg@1: try franta-hg@1: { franta-hg@1: pageContext.getOut().write(buffer.toString()); franta-hg@1: } franta-hg@1: catch (IOException ioex) franta-hg@1: { franta-hg@1: reset(); franta-hg@1: log.error(ioex); franta-hg@1: throw new JspException(ioex.getMessage()); franta-hg@1: } franta-hg@1: return super.doEndTag(); franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Fix an absolute url given as attribute by adding the full application url path to it. franta-hg@1: * It is considered absolute url (not relative) when it starts with "/" franta-hg@1: * @param url The url to fix franta-hg@1: * @param request The http request franta-hg@1: * @return Fixed url contains the full path franta-hg@1: */ franta-hg@1: public static String fixAbsolutURL(String url, HttpServletRequest request) { franta-hg@1: if ( url.startsWith("/") ) franta-hg@1: { franta-hg@1: //final HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); franta-hg@1: final String context = request.getContextPath(); franta-hg@1: url = context + url; franta-hg@1: } franta-hg@1: return url; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Same as the other fixAbsolutURL, convinience only. franta-hg@1: * @param url The url to fix franta-hg@1: * @param pageContext The page context franta-hg@1: * @return Fixed url contains the full path franta-hg@1: */ franta-hg@1: public static String fixAbsolutURL(String url, PageContext pageContext) { franta-hg@1: return fixAbsolutURL(url, (HttpServletRequest) pageContext.getRequest()); franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Build the image url franta-hg@1: * @param renderer the url of the renderer franta-hg@1: * @param pageContext Page context franta-hg@1: * @param sessionKey The session key for the image stored. franta-hg@1: * @param width The width franta-hg@1: * @param height The height franta-hg@1: * @param mimeType the mime-type (for example png) of it franta-hg@1: * @return The full url franta-hg@1: */ franta-hg@1: public static String buildImgURL( franta-hg@1: String renderer, PageContext pageContext, String sessionKey, int width, int height, String mimeType, franta-hg@1: boolean forceSessionId, boolean removeAfterRender) { franta-hg@1: renderer = fixAbsolutURL(renderer, pageContext); franta-hg@1: final HttpServletResponse response = (HttpServletResponse) pageContext.getResponse(); franta-hg@1: StringBuffer url = new StringBuffer(response.encodeURL(renderer)); franta-hg@1: if ( url.toString().indexOf(SESSIONID_KEY) == -1 ) franta-hg@1: { franta-hg@1: if (forceSessionId) franta-hg@1: { franta-hg@1: final String sessionId = pageContext.getSession().getId(); franta-hg@1: url.append(";" + SESSIONID_KEY + "=" + sessionId); franta-hg@1: } franta-hg@1: } franta-hg@1: url.append("?" + IMG_PARAM + "=" + sessionKey); franta-hg@1: url.append(AMPERSAND + WIDTH_PARAM + "=" + width); franta-hg@1: url.append(AMPERSAND + HEIGHT_PARAM + "=" + height); franta-hg@1: if (removeAfterRender) franta-hg@1: { franta-hg@1: url.append(AMPERSAND + REMOVE_AFTER_RENDERING + "=true"); franta-hg@1: } franta-hg@1: url.append(AMPERSAND + "iehack=" + MIMEExtensionHelper.getExtensionForMimeType(mimeType)); franta-hg@1: return url.toString(); franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * To enable further server side scriptings on JSP output the session ID is franta-hg@1: * always encoded into the image URL even if cookies are enabled on the client franta-hg@1: * side. franta-hg@1: */ franta-hg@1: protected String getImgURL() franta-hg@1: { franta-hg@1: return buildImgURL(renderer, pageContext, sessionKey, width, height, mimeType, forceSessionId, removeAfterRender); franta-hg@1: } franta-hg@1: franta-hg@1: public Object getRenderingInfo() throws CewolfException franta-hg@1: { franta-hg@1: return chartImageDefinition.getRenderingInfo(); franta-hg@1: } franta-hg@1: franta-hg@1: protected String getMimeType() franta-hg@1: { franta-hg@1: return mimeType; franta-hg@1: } franta-hg@1: franta-hg@1: protected void reset() franta-hg@1: { franta-hg@1: this.mimeType = DEFAULT_MIME_TYPE; franta-hg@1: // as of a weird JSP compiler in resin franta-hg@1: // a reused tag's attribute is only set if franta-hg@1: // it changes. So width an height may not franta-hg@1: // be unset to ensure correct values. franta-hg@1: int lHeight = this.height; franta-hg@1: int lWidth = this.width; franta-hg@1: int lTimeout = this.timeout; franta-hg@1: super.reset(); franta-hg@1: this.height = lHeight; franta-hg@1: this.width = lWidth; franta-hg@1: this.timeout = lTimeout; franta-hg@1: } franta-hg@1: franta-hg@1: public void enableMapping() franta-hg@1: { franta-hg@1: setUsemap("#" + chartId); franta-hg@1: } franta-hg@1: franta-hg@1: public String getChartId() franta-hg@1: { franta-hg@1: return getChartid(); franta-hg@1: } franta-hg@1: franta-hg@1: public void setChartid( String id ) franta-hg@1: { franta-hg@1: this.chartId = id; franta-hg@1: } franta-hg@1: franta-hg@1: public String getChartid() franta-hg@1: { franta-hg@1: return chartId; franta-hg@1: } franta-hg@1: franta-hg@1: public void setRenderer( String renderer ) franta-hg@1: { franta-hg@1: this.renderer = renderer; franta-hg@1: } franta-hg@1: franta-hg@1: protected String getRenderer() franta-hg@1: { franta-hg@1: return this.renderer; franta-hg@1: } franta-hg@1: franta-hg@1: public int getWidth() franta-hg@1: { franta-hg@1: return this.width; franta-hg@1: } franta-hg@1: franta-hg@1: public int getHeight() franta-hg@1: { franta-hg@1: return height; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Sets the mimeType. franta-hg@1: * franta-hg@1: * @param mimeType franta-hg@1: * The mimeType to set franta-hg@1: */ franta-hg@1: public void setMime( String mimeType ) franta-hg@1: { franta-hg@1: this.mimeType = mimeType; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * @see de.laures.cewolf.taglib.html.AbstractHTMLBaseTag#getTagName() franta-hg@1: */ franta-hg@1: protected String getTagName() franta-hg@1: { franta-hg@1: if ( MIME_SVG.equals(mimeType) ) franta-hg@1: { franta-hg@1: return TAG_NAME_SVG; franta-hg@1: } franta-hg@1: return super.getTagName(); franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * @see de.laures.cewolf.taglib.html.AbstractHTMLBaseTag#writeAttributes(Writer) franta-hg@1: */ franta-hg@1: public void writeAttributes( Writer wr ) franta-hg@1: { franta-hg@1: super.writeAttributes(wr); franta-hg@1: if ( MIME_SVG.equals(mimeType) ) franta-hg@1: { franta-hg@1: try franta-hg@1: { franta-hg@1: appendAttributeDeclaration(wr, "http://www.adobe.com/svg/viewer/install/", "PLUGINSPAGE"); franta-hg@1: } franta-hg@1: catch (IOException ioex) franta-hg@1: { franta-hg@1: ioex.printStackTrace(); franta-hg@1: } franta-hg@1: } franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * @return Returns the timeout. franta-hg@1: */ franta-hg@1: public int getTimeout() franta-hg@1: { franta-hg@1: return timeout; franta-hg@1: } franta-hg@1: /** franta-hg@1: * @param timeout The timeout to set. franta-hg@1: */ franta-hg@1: public void setTimeout( int timeout ) franta-hg@1: { franta-hg@1: this.timeout = timeout; franta-hg@1: } franta-hg@1: }