java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/tags/ChartImgTag.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Feb 2009 21:31:02 +0100
changeset 1 639991d0808a
permissions -rw-r--r--
Rozbalená knihovna verze 1.0
     1 /* ================================================================
     2  * Cewolf : Chart enabling Web Objects Framework
     3  * ================================================================
     4  *
     5  * Project Info:  http://cewolf.sourceforge.net
     6  * Project Lead:  Guido Laures (guido@laures.de);
     7  *
     8  * (C) Copyright 2002, by Guido Laures
     9  *
    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.
    14  *
    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.
    18  *
    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.
    22  */
    23 
    24 package de.laures.cewolf.taglib.tags;
    25 
    26 import java.io.IOException;
    27 import java.io.Writer;
    28 
    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;
    35 
    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;
    47 
    48 /**
    49  * This is the tag implementation of the &lt;img&gt; tag. This tag inputs the
    50  * proper &lt;img&gt; 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.
    53  * 
    54  * @author glaures
    55  * @see de.laures.cewolf.ChartImage
    56  */
    57 public class ChartImgTag extends HTMLImgTag implements CewolfRootTag, Mapped, TaglibConstants, WebConstants
    58 {
    59 
    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;
    63 
    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;
    69   
    70   private ChartImageDefinition chartImageDefinition;
    71 
    72   public int doStartTag() throws JspException
    73   {
    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();
    77     try
    78     {
    79       this.sessionKey = storage.storeChartImage(chartImageDefinition, pageContext);
    80     }
    81     catch (CewolfException cwex)
    82     {
    83       throw new JspException(cwex.getMessage());
    84     }
    85     return EVAL_PAGE;
    86   }
    87 
    88   public int doAfterBody() throws JspException
    89   {
    90     try
    91     {
    92       // double checking for null as Resin had problems with that
    93       final BodyContent body = getBodyContent();
    94       if ( body != null )
    95       {
    96         final JspWriter writer = getPreviousOut();
    97         if ( writer != null )
    98         {
    99           body.writeOut(writer);
   100         }
   101       }
   102 
   103     }
   104     catch (IOException ioex)
   105     {
   106       log.error(ioex);	
   107       throw new JspException(ioex.getMessage());
   108     }
   109     return SKIP_BODY;
   110   }
   111 
   112   public int doEndTag() throws JspException
   113   {
   114     super.doStartTag();
   115     final StringBuffer buffer = new StringBuffer(" src=\"");
   116     buffer.append(getImgURL());
   117     buffer.append("\"");
   118     try
   119     {
   120       pageContext.getOut().write(buffer.toString());
   121     }
   122     catch (IOException ioex)
   123     {
   124       reset();
   125       log.error(ioex);
   126       throw new JspException(ioex.getMessage());
   127     }
   128     return super.doEndTag();
   129   }
   130 
   131   /**
   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
   137    */
   138   public static String fixAbsolutURL(String url, HttpServletRequest request) {
   139     if ( url.startsWith("/") )
   140     {
   141       //final HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
   142       final String context = request.getContextPath();
   143       url = context + url;
   144     }
   145     return url;
   146   }
   147   
   148   /**
   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
   153    */
   154   public static String fixAbsolutURL(String url, PageContext pageContext) {
   155   	return fixAbsolutURL(url, (HttpServletRequest) pageContext.getRequest());
   156   }
   157   
   158   /**
   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 
   167    */
   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 )
   175 	{
   176 		if (forceSessionId)
   177 		{
   178 			  final String sessionId = pageContext.getSession().getId();
   179 			  url.append(";" + SESSIONID_KEY + "=" + sessionId);			
   180 		}
   181 	}
   182 	url.append("?" + IMG_PARAM + "=" + sessionKey);
   183 	url.append(AMPERSAND + WIDTH_PARAM + "=" + width);
   184 	url.append(AMPERSAND + HEIGHT_PARAM + "=" + height);
   185 	if (removeAfterRender)
   186 	{
   187 		url.append(AMPERSAND + REMOVE_AFTER_RENDERING + "=true");		
   188 	}
   189 	url.append(AMPERSAND + "iehack=" + MIMEExtensionHelper.getExtensionForMimeType(mimeType));
   190 	return url.toString();  	
   191   }
   192   
   193   /**
   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
   196    * side.
   197    */
   198   protected String getImgURL()
   199   {
   200   	return buildImgURL(renderer, pageContext, sessionKey, width, height, mimeType, forceSessionId, removeAfterRender);
   201   }
   202 
   203   public Object getRenderingInfo() throws CewolfException
   204   {
   205     return chartImageDefinition.getRenderingInfo();
   206   }
   207 
   208   protected String getMimeType()
   209   {
   210     return mimeType;
   211   }
   212 
   213   protected void reset()
   214   {
   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;
   223     super.reset();
   224     this.height = lHeight;
   225     this.width = lWidth;
   226     this.timeout = lTimeout;
   227   }
   228 
   229   public void enableMapping()
   230   {
   231     setUsemap("#" + chartId);
   232   }
   233 
   234   public String getChartId()
   235   {
   236     return getChartid();
   237   }
   238 
   239   public void setChartid( String id )
   240   {
   241     this.chartId = id;
   242   }
   243 
   244   public String getChartid()
   245   {
   246     return chartId;
   247   }
   248 
   249   public void setRenderer( String renderer )
   250   {
   251     this.renderer = renderer;
   252   }
   253 
   254   protected String getRenderer()
   255   {
   256     return this.renderer;
   257   }
   258 
   259   public int getWidth()
   260   {
   261     return this.width;
   262   }
   263 
   264   public int getHeight()
   265   {
   266     return height;
   267   }
   268 
   269   /**
   270    * Sets the mimeType.
   271    * 
   272    * @param mimeType
   273    *          The mimeType to set
   274    */
   275   public void setMime( String mimeType )
   276   {
   277     this.mimeType = mimeType;
   278   }
   279 
   280   /**
   281    * @see de.laures.cewolf.taglib.html.AbstractHTMLBaseTag#getTagName()
   282    */
   283   protected String getTagName()
   284   {
   285     if ( MIME_SVG.equals(mimeType) )
   286     {
   287       return TAG_NAME_SVG;
   288     }
   289     return super.getTagName();
   290   }
   291 
   292   /**
   293    * @see de.laures.cewolf.taglib.html.AbstractHTMLBaseTag#writeAttributes(Writer)
   294    */
   295   public void writeAttributes( Writer wr )
   296   {
   297     super.writeAttributes(wr);
   298     if ( MIME_SVG.equals(mimeType) )
   299     {
   300       try
   301       {
   302         appendAttributeDeclaration(wr, "http://www.adobe.com/svg/viewer/install/", "PLUGINSPAGE");
   303       }
   304       catch (IOException ioex)
   305       {
   306         ioex.printStackTrace();
   307       }
   308     }
   309   }
   310 
   311   /**
   312    * @return Returns the timeout.
   313    */
   314   public int getTimeout()
   315   {
   316     return timeout;
   317   }
   318   /**
   319    * @param timeout The timeout to set.
   320    */
   321   public void setTimeout( int timeout )
   322   {
   323     this.timeout = timeout;
   324   }
   325 }