java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/tags/ChartImgTag.java
changeset 1 639991d0808a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/tags/ChartImgTag.java	Sat Feb 28 21:31:02 2009 +0100
     1.3 @@ -0,0 +1,325 @@
     1.4 +/* ================================================================
     1.5 + * Cewolf : Chart enabling Web Objects Framework
     1.6 + * ================================================================
     1.7 + *
     1.8 + * Project Info:  http://cewolf.sourceforge.net
     1.9 + * Project Lead:  Guido Laures (guido@laures.de);
    1.10 + *
    1.11 + * (C) Copyright 2002, by Guido Laures
    1.12 + *
    1.13 + * This library is free software; you can redistribute it and/or modify it under the terms
    1.14 + * of the GNU Lesser General Public License as published by the Free Software Foundation;
    1.15 + * either version 2.1 of
    1.16 + * the License, or (at your option) any later version.
    1.17 + *
    1.18 + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    1.19 + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.20 + * See the GNU Lesser General Public License for more details.
    1.21 + *
    1.22 + * You should have received a copy of the GNU Lesser General Public License along with this
    1.23 + * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    1.24 + * Boston, MA 02111-1307, USA.
    1.25 + */
    1.26 +
    1.27 +package de.laures.cewolf.taglib.tags;
    1.28 +
    1.29 +import java.io.IOException;
    1.30 +import java.io.Writer;
    1.31 +
    1.32 +import javax.servlet.http.HttpServletRequest;
    1.33 +import javax.servlet.http.HttpServletResponse;
    1.34 +import javax.servlet.jsp.JspException;
    1.35 +import javax.servlet.jsp.JspWriter;
    1.36 +import javax.servlet.jsp.PageContext;
    1.37 +import javax.servlet.jsp.tagext.BodyContent;
    1.38 +
    1.39 +import de.laures.cewolf.CewolfException;
    1.40 +import de.laures.cewolf.ChartHolder;
    1.41 +import de.laures.cewolf.ChartImage;
    1.42 +import de.laures.cewolf.Configuration;
    1.43 +import de.laures.cewolf.Storage;
    1.44 +import de.laures.cewolf.WebConstants;
    1.45 +import de.laures.cewolf.taglib.ChartImageDefinition;
    1.46 +import de.laures.cewolf.taglib.TaglibConstants;
    1.47 +import de.laures.cewolf.taglib.html.HTMLImgTag;
    1.48 +import de.laures.cewolf.taglib.util.MIMEExtensionHelper;
    1.49 +import de.laures.cewolf.taglib.util.PageUtils;
    1.50 +
    1.51 +/**
    1.52 + * This is the tag implementation of the <img> tag. This tag inputs the
    1.53 + * proper <img> tag into the HTML page delivered to the client. It
    1.54 + * therefor determines the chart ID which will be used by the rendering servlet
    1.55 + * to retrieve the chart.
    1.56 + * 
    1.57 + * @author glaures
    1.58 + * @see de.laures.cewolf.ChartImage
    1.59 + */
    1.60 +public class ChartImgTag extends HTMLImgTag implements CewolfRootTag, Mapped, TaglibConstants, WebConstants
    1.61 +{
    1.62 +
    1.63 +  private static final String  DEFAULT_MIME_TYPE = MIME_PNG;
    1.64 +  private static final String  TAG_NAME_SVG      = "EMBED";
    1.65 +  private static final int     DEFAULT_TIMEOUT   = 300;
    1.66 +
    1.67 +  private String               chartId           = null;
    1.68 +  private String               renderer;
    1.69 +  private String               mimeType          = DEFAULT_MIME_TYPE;
    1.70 +  private int                  timeout           = DEFAULT_TIMEOUT;
    1.71 +  protected String             sessionKey        = null;
    1.72 +  
    1.73 +  private ChartImageDefinition chartImageDefinition;
    1.74 +
    1.75 +  public int doStartTag() throws JspException
    1.76 +  {
    1.77 +    final ChartHolder chartHolder = PageUtils.getChartHolder(chartId, pageContext);
    1.78 +    this.chartImageDefinition = new ChartImageDefinition(chartHolder, width, height, ChartImage.IMG_TYPE_CHART, mimeType,timeout);
    1.79 +    Storage storage = Configuration.getInstance(pageContext.getServletContext()).getStorage();
    1.80 +    try
    1.81 +    {
    1.82 +      this.sessionKey = storage.storeChartImage(chartImageDefinition, pageContext);
    1.83 +    }
    1.84 +    catch (CewolfException cwex)
    1.85 +    {
    1.86 +      throw new JspException(cwex.getMessage());
    1.87 +    }
    1.88 +    return EVAL_PAGE;
    1.89 +  }
    1.90 +
    1.91 +  public int doAfterBody() throws JspException
    1.92 +  {
    1.93 +    try
    1.94 +    {
    1.95 +      // double checking for null as Resin had problems with that
    1.96 +      final BodyContent body = getBodyContent();
    1.97 +      if ( body != null )
    1.98 +      {
    1.99 +        final JspWriter writer = getPreviousOut();
   1.100 +        if ( writer != null )
   1.101 +        {
   1.102 +          body.writeOut(writer);
   1.103 +        }
   1.104 +      }
   1.105 +
   1.106 +    }
   1.107 +    catch (IOException ioex)
   1.108 +    {
   1.109 +      log.error(ioex);	
   1.110 +      throw new JspException(ioex.getMessage());
   1.111 +    }
   1.112 +    return SKIP_BODY;
   1.113 +  }
   1.114 +
   1.115 +  public int doEndTag() throws JspException
   1.116 +  {
   1.117 +    super.doStartTag();
   1.118 +    final StringBuffer buffer = new StringBuffer(" src=\"");
   1.119 +    buffer.append(getImgURL());
   1.120 +    buffer.append("\"");
   1.121 +    try
   1.122 +    {
   1.123 +      pageContext.getOut().write(buffer.toString());
   1.124 +    }
   1.125 +    catch (IOException ioex)
   1.126 +    {
   1.127 +      reset();
   1.128 +      log.error(ioex);
   1.129 +      throw new JspException(ioex.getMessage());
   1.130 +    }
   1.131 +    return super.doEndTag();
   1.132 +  }
   1.133 +
   1.134 +  /**
   1.135 +   * Fix an absolute url given as attribute by adding the full application url path to it.
   1.136 +   * It is considered absolute url (not relative) when it starts with "/"
   1.137 +   * @param url The url to fix
   1.138 +   * @param request The http request
   1.139 +   * @return Fixed url contains the full path
   1.140 +   */
   1.141 +  public static String fixAbsolutURL(String url, HttpServletRequest request) {
   1.142 +    if ( url.startsWith("/") )
   1.143 +    {
   1.144 +      //final HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
   1.145 +      final String context = request.getContextPath();
   1.146 +      url = context + url;
   1.147 +    }
   1.148 +    return url;
   1.149 +  }
   1.150 +  
   1.151 +  /**
   1.152 +   * Same as the other fixAbsolutURL, convinience only.
   1.153 +   * @param url The url to fix
   1.154 +   * @param pageContext The page context
   1.155 +   * @return Fixed url contains the full path
   1.156 +   */
   1.157 +  public static String fixAbsolutURL(String url, PageContext pageContext) {
   1.158 +  	return fixAbsolutURL(url, (HttpServletRequest) pageContext.getRequest());
   1.159 +  }
   1.160 +  
   1.161 +  /**
   1.162 +   * Build the image url
   1.163 +   * @param renderer the url of the renderer
   1.164 +   * @param pageContext Page context
   1.165 +   * @param sessionKey The session key for the image stored.
   1.166 +   * @param width The width 
   1.167 +   * @param height The height
   1.168 +   * @param mimeType the mime-type (for example png) of it
   1.169 +   * @return The full url 
   1.170 +   */
   1.171 +  public static String buildImgURL(
   1.172 +		  String renderer, PageContext pageContext, String sessionKey, int width, int height, String mimeType,
   1.173 +		  boolean forceSessionId, boolean removeAfterRender) {
   1.174 +	renderer = fixAbsolutURL(renderer, pageContext);
   1.175 +	final HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
   1.176 +	StringBuffer url = new StringBuffer(response.encodeURL(renderer));
   1.177 +	if ( url.toString().indexOf(SESSIONID_KEY) == -1 )
   1.178 +	{
   1.179 +		if (forceSessionId)
   1.180 +		{
   1.181 +			  final String sessionId = pageContext.getSession().getId();
   1.182 +			  url.append(";" + SESSIONID_KEY + "=" + sessionId);			
   1.183 +		}
   1.184 +	}
   1.185 +	url.append("?" + IMG_PARAM + "=" + sessionKey);
   1.186 +	url.append(AMPERSAND + WIDTH_PARAM + "=" + width);
   1.187 +	url.append(AMPERSAND + HEIGHT_PARAM + "=" + height);
   1.188 +	if (removeAfterRender)
   1.189 +	{
   1.190 +		url.append(AMPERSAND + REMOVE_AFTER_RENDERING + "=true");		
   1.191 +	}
   1.192 +	url.append(AMPERSAND + "iehack=" + MIMEExtensionHelper.getExtensionForMimeType(mimeType));
   1.193 +	return url.toString();  	
   1.194 +  }
   1.195 +  
   1.196 +  /**
   1.197 +   * To enable further server side scriptings on JSP output the session ID is
   1.198 +   * always encoded into the image URL even if cookies are enabled on the client
   1.199 +   * side.
   1.200 +   */
   1.201 +  protected String getImgURL()
   1.202 +  {
   1.203 +  	return buildImgURL(renderer, pageContext, sessionKey, width, height, mimeType, forceSessionId, removeAfterRender);
   1.204 +  }
   1.205 +
   1.206 +  public Object getRenderingInfo() throws CewolfException
   1.207 +  {
   1.208 +    return chartImageDefinition.getRenderingInfo();
   1.209 +  }
   1.210 +
   1.211 +  protected String getMimeType()
   1.212 +  {
   1.213 +    return mimeType;
   1.214 +  }
   1.215 +
   1.216 +  protected void reset()
   1.217 +  {
   1.218 +    this.mimeType = DEFAULT_MIME_TYPE;
   1.219 +    // as of a weird JSP compiler in resin
   1.220 +    // a reused tag's attribute is only set if
   1.221 +    // it changes. So width an height may not
   1.222 +    // be unset to ensure correct values.
   1.223 +    int lHeight = this.height;
   1.224 +    int lWidth = this.width;
   1.225 +    int lTimeout = this.timeout;
   1.226 +    super.reset();
   1.227 +    this.height = lHeight;
   1.228 +    this.width = lWidth;
   1.229 +    this.timeout = lTimeout;
   1.230 +  }
   1.231 +
   1.232 +  public void enableMapping()
   1.233 +  {
   1.234 +    setUsemap("#" + chartId);
   1.235 +  }
   1.236 +
   1.237 +  public String getChartId()
   1.238 +  {
   1.239 +    return getChartid();
   1.240 +  }
   1.241 +
   1.242 +  public void setChartid( String id )
   1.243 +  {
   1.244 +    this.chartId = id;
   1.245 +  }
   1.246 +
   1.247 +  public String getChartid()
   1.248 +  {
   1.249 +    return chartId;
   1.250 +  }
   1.251 +
   1.252 +  public void setRenderer( String renderer )
   1.253 +  {
   1.254 +    this.renderer = renderer;
   1.255 +  }
   1.256 +
   1.257 +  protected String getRenderer()
   1.258 +  {
   1.259 +    return this.renderer;
   1.260 +  }
   1.261 +
   1.262 +  public int getWidth()
   1.263 +  {
   1.264 +    return this.width;
   1.265 +  }
   1.266 +
   1.267 +  public int getHeight()
   1.268 +  {
   1.269 +    return height;
   1.270 +  }
   1.271 +
   1.272 +  /**
   1.273 +   * Sets the mimeType.
   1.274 +   * 
   1.275 +   * @param mimeType
   1.276 +   *          The mimeType to set
   1.277 +   */
   1.278 +  public void setMime( String mimeType )
   1.279 +  {
   1.280 +    this.mimeType = mimeType;
   1.281 +  }
   1.282 +
   1.283 +  /**
   1.284 +   * @see de.laures.cewolf.taglib.html.AbstractHTMLBaseTag#getTagName()
   1.285 +   */
   1.286 +  protected String getTagName()
   1.287 +  {
   1.288 +    if ( MIME_SVG.equals(mimeType) )
   1.289 +    {
   1.290 +      return TAG_NAME_SVG;
   1.291 +    }
   1.292 +    return super.getTagName();
   1.293 +  }
   1.294 +
   1.295 +  /**
   1.296 +   * @see de.laures.cewolf.taglib.html.AbstractHTMLBaseTag#writeAttributes(Writer)
   1.297 +   */
   1.298 +  public void writeAttributes( Writer wr )
   1.299 +  {
   1.300 +    super.writeAttributes(wr);
   1.301 +    if ( MIME_SVG.equals(mimeType) )
   1.302 +    {
   1.303 +      try
   1.304 +      {
   1.305 +        appendAttributeDeclaration(wr, "http://www.adobe.com/svg/viewer/install/", "PLUGINSPAGE");
   1.306 +      }
   1.307 +      catch (IOException ioex)
   1.308 +      {
   1.309 +        ioex.printStackTrace();
   1.310 +      }
   1.311 +    }
   1.312 +  }
   1.313 +
   1.314 +  /**
   1.315 +   * @return Returns the timeout.
   1.316 +   */
   1.317 +  public int getTimeout()
   1.318 +  {
   1.319 +    return timeout;
   1.320 +  }
   1.321 +  /**
   1.322 +   * @param timeout The timeout to set.
   1.323 +   */
   1.324 +  public void setTimeout( int timeout )
   1.325 +  {
   1.326 +    this.timeout = timeout;
   1.327 +  }
   1.328 +}
   1.329 \ No newline at end of file