1 /* ================================================================
2 * Cewolf : Chart enabling Web Objects Framework
3 * ================================================================
5 * Project Info: http://cewolf.sourceforge.net
6 * Project Lead: Guido Laures (guido@laures.de);
8 * (C) Copyright 2002, by Guido Laures
10 * This library is free software; you can redistribute it and/or modify it under the terms
11 * of the GNU Lesser General Public License as published by the Free Software Foundation;
12 * either version 2.1 of
13 * the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
16 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 * See the GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License along with this
20 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21 * Boston, MA 02111-1307, USA.
24 package de.laures.cewolf.taglib.tags;
26 import java.io.IOException;
27 import java.io.Writer;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import javax.servlet.jsp.JspException;
32 import javax.servlet.jsp.JspWriter;
33 import javax.servlet.jsp.PageContext;
34 import javax.servlet.jsp.tagext.BodyContent;
36 import de.laures.cewolf.CewolfException;
37 import de.laures.cewolf.ChartHolder;
38 import de.laures.cewolf.ChartImage;
39 import de.laures.cewolf.Configuration;
40 import de.laures.cewolf.Storage;
41 import de.laures.cewolf.WebConstants;
42 import de.laures.cewolf.taglib.ChartImageDefinition;
43 import de.laures.cewolf.taglib.TaglibConstants;
44 import de.laures.cewolf.taglib.html.HTMLImgTag;
45 import de.laures.cewolf.taglib.util.MIMEExtensionHelper;
46 import de.laures.cewolf.taglib.util.PageUtils;
49 * This is the tag implementation of the <img> tag. This tag inputs the
50 * proper <img> tag into the HTML page delivered to the client. It
51 * therefor determines the chart ID which will be used by the rendering servlet
52 * to retrieve the chart.
55 * @see de.laures.cewolf.ChartImage
57 public class ChartImgTag extends HTMLImgTag implements CewolfRootTag, Mapped, TaglibConstants, WebConstants
60 private static final String DEFAULT_MIME_TYPE = MIME_PNG;
61 private static final String TAG_NAME_SVG = "EMBED";
62 private static final int DEFAULT_TIMEOUT = 300;
64 private String chartId = null;
65 private String renderer;
66 private String mimeType = DEFAULT_MIME_TYPE;
67 private int timeout = DEFAULT_TIMEOUT;
68 protected String sessionKey = null;
70 private ChartImageDefinition chartImageDefinition;
72 public int doStartTag() throws JspException
74 final ChartHolder chartHolder = PageUtils.getChartHolder(chartId, pageContext);
75 this.chartImageDefinition = new ChartImageDefinition(chartHolder, width, height, ChartImage.IMG_TYPE_CHART, mimeType,timeout);
76 Storage storage = Configuration.getInstance(pageContext.getServletContext()).getStorage();
79 this.sessionKey = storage.storeChartImage(chartImageDefinition, pageContext);
81 catch (CewolfException cwex)
83 throw new JspException(cwex.getMessage());
88 public int doAfterBody() throws JspException
92 // double checking for null as Resin had problems with that
93 final BodyContent body = getBodyContent();
96 final JspWriter writer = getPreviousOut();
99 body.writeOut(writer);
104 catch (IOException ioex)
107 throw new JspException(ioex.getMessage());
112 public int doEndTag() throws JspException
115 final StringBuffer buffer = new StringBuffer(" src=\"");
116 buffer.append(getImgURL());
120 pageContext.getOut().write(buffer.toString());
122 catch (IOException ioex)
126 throw new JspException(ioex.getMessage());
128 return super.doEndTag();
132 * Fix an absolute url given as attribute by adding the full application url path to it.
133 * It is considered absolute url (not relative) when it starts with "/"
134 * @param url The url to fix
135 * @param request The http request
136 * @return Fixed url contains the full path
138 public static String fixAbsolutURL(String url, HttpServletRequest request) {
139 if ( url.startsWith("/") )
141 //final HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
142 final String context = request.getContextPath();
149 * Same as the other fixAbsolutURL, convinience only.
150 * @param url The url to fix
151 * @param pageContext The page context
152 * @return Fixed url contains the full path
154 public static String fixAbsolutURL(String url, PageContext pageContext) {
155 return fixAbsolutURL(url, (HttpServletRequest) pageContext.getRequest());
159 * Build the image url
160 * @param renderer the url of the renderer
161 * @param pageContext Page context
162 * @param sessionKey The session key for the image stored.
163 * @param width The width
164 * @param height The height
165 * @param mimeType the mime-type (for example png) of it
166 * @return The full url
168 public static String buildImgURL(
169 String renderer, PageContext pageContext, String sessionKey, int width, int height, String mimeType,
170 boolean forceSessionId, boolean removeAfterRender) {
171 renderer = fixAbsolutURL(renderer, pageContext);
172 final HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
173 StringBuffer url = new StringBuffer(response.encodeURL(renderer));
174 if ( url.toString().indexOf(SESSIONID_KEY) == -1 )
178 final String sessionId = pageContext.getSession().getId();
179 url.append(";" + SESSIONID_KEY + "=" + sessionId);
182 url.append("?" + IMG_PARAM + "=" + sessionKey);
183 url.append(AMPERSAND + WIDTH_PARAM + "=" + width);
184 url.append(AMPERSAND + HEIGHT_PARAM + "=" + height);
185 if (removeAfterRender)
187 url.append(AMPERSAND + REMOVE_AFTER_RENDERING + "=true");
189 url.append(AMPERSAND + "iehack=" + MIMEExtensionHelper.getExtensionForMimeType(mimeType));
190 return url.toString();
194 * To enable further server side scriptings on JSP output the session ID is
195 * always encoded into the image URL even if cookies are enabled on the client
198 protected String getImgURL()
200 return buildImgURL(renderer, pageContext, sessionKey, width, height, mimeType, forceSessionId, removeAfterRender);
203 public Object getRenderingInfo() throws CewolfException
205 return chartImageDefinition.getRenderingInfo();
208 protected String getMimeType()
213 protected void reset()
215 this.mimeType = DEFAULT_MIME_TYPE;
216 // as of a weird JSP compiler in resin
217 // a reused tag's attribute is only set if
218 // it changes. So width an height may not
219 // be unset to ensure correct values.
220 int lHeight = this.height;
221 int lWidth = this.width;
222 int lTimeout = this.timeout;
224 this.height = lHeight;
226 this.timeout = lTimeout;
229 public void enableMapping()
231 setUsemap("#" + chartId);
234 public String getChartId()
239 public void setChartid( String id )
244 public String getChartid()
249 public void setRenderer( String renderer )
251 this.renderer = renderer;
254 protected String getRenderer()
256 return this.renderer;
259 public int getWidth()
264 public int getHeight()
273 * The mimeType to set
275 public void setMime( String mimeType )
277 this.mimeType = mimeType;
281 * @see de.laures.cewolf.taglib.html.AbstractHTMLBaseTag#getTagName()
283 protected String getTagName()
285 if ( MIME_SVG.equals(mimeType) )
289 return super.getTagName();
293 * @see de.laures.cewolf.taglib.html.AbstractHTMLBaseTag#writeAttributes(Writer)
295 public void writeAttributes( Writer wr )
297 super.writeAttributes(wr);
298 if ( MIME_SVG.equals(mimeType) )
302 appendAttributeDeclaration(wr, "http://www.adobe.com/svg/viewer/install/", "PLUGINSPAGE");
304 catch (IOException ioex)
306 ioex.printStackTrace();
312 * @return Returns the timeout.
314 public int getTimeout()
319 * @param timeout The timeout to set.
321 public void setTimeout( int timeout )
323 this.timeout = timeout;